Helios/Config.pm

filter: Statementstmt 26.2% 75 / 286 Branchbran 0.0% 0 / 100 Conditioncond 0.0% 0 / 51 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 44.9% 22 / 49 Podpod 25.9% 7 / 27 totaltotal 19.3% 104 / 538 CC 74 SCAR 48.0
CC
74
Cov
17%
getParam
462.0
setParam
156.0
parseConfFile
110.0
CRAP 122.0
« Helios/Service.pm ↑ Summary Helios/TS.pm »
1 package Helios::Config;
2  
3 1 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Sys::Hostname;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use Config::IniFiles;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Helios::ObjectDriver::DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Helios::ConfigParam;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Helios::Error::ConfigError;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 our $VERSION = '2.82';
16  
17 =head1 NAME
18  
19 Helios::Config - base class for Helios configuration system
20  
21 =head1 SYNOPSIS
22  
23 # to determine a service's config based on
24 # a particular conf file and host
25 use Helios::Config;
26 Helios::Config->init(
27 conf_file => '/path/to/helios.ini',
28 hostname => 'host',
29 service => 'ServiceName'
30 );
31 my $config = Helios::Config->parseConfig();
32
33 # if $HELIOS_INI env var is set and current host is used
34 use Helios::Config;
35 Helios::Config->init(service => 'ServiceName');
36 my $config = Helios::Config->parseConfig();
37
38 # same as above; parseConfig() will automatically call init()
39 use Helios::Config;
40 my $config = Helios::Config->parseConfig(service => 'ServiceName');
41
42 # you can also use accessor methods; for example, with Helios::TestService:
43 use Helios::Config;
44 Helios::Config->setService('Helios::TestService');
45 Helios::Config->init();
46 my $config = Helios::Config->parseConfig();
47  
48 # catch config errors with eval {}
49 # Try::Tiny works too
50 use Helios::Config;
51 my $config;
52 eval {
53 Helios::Config->init(service => 'Helios::TestService');
54 $config = Helios::Config->parseConfig();
55 } or do {
56 my $E = $@;
57 if ( $E->isa('Helios::Error::ConfigError') ) {
58 print "Helios configuration error: $E\n";
59 } else {
60 print "I do not know what happened, but it was bad.\n";
61 }
62 };
63
64  
65 =head1 DESCRIPTION
66  
67 Helios::Config is the standard class for determining configuration information
68 in the Helios framework. It handles parsing configuration information from the
69 Helios configuration file, and determining the configuration for services from
70 information in the Helios collective database. Helios::Config also acts as a
71 base class for the Helios configuration API; services can define specialized
72 Helios::Config subclasses to extend configuration subsystem functionality.
73  
74 Normally, the developer of Helios services does not need to interact with
75 Helios::Config directly. Helios normally handles all configuration
76 transparently during service setup before a service's run() method is called.
77 A Helios service need only call its getConfig() method to retrieve a hashref of
78 its configuration parameters. Only those wanting to retrieve a Helios service
79 configuration outside of the service (e.g. to write an external utility as
80 an adjunct to a Helios service) or those with advanced configuration needs
81 will need to work with Helios::Config directly.
82  
83 It should be noted that, like Helios::Logger subclasses, Helios::Config methods
84 are actually class methods, not instance (object) methods. If you need to
85 implement other methods outside of the methods defined
86 below, make sure you implement them as class methods.
87  
88 =head1 ACCESSOR METHODS
89  
90 Helios::Config provides 7 set/get accessor pairs to provide access to
91 configuration data. There are 3 categories of accessors: ones that need to
92 be set before configuration parsing is initialized, those that are used during
93 configuration parsing, and those that hold the end results of the parsing
94 procedure (i.e. the actual configuration helios.pl and the Helios service will
95 need).
96  
97 =head2 INITIALIZATION ACCESSORS
98  
99 These need to be set before configuration parsing is initialized with the
100 init() method. If they are not set, the init() method will try to set them
101 from data in the environment.
102  
103 set/getConfFile() path to the Helios conf file
104 -defaults to $HELIOS_INI env variable
105 set/getHostname() hostname the Helios service is running on
106 -defaults to results of Sys::Hostname::hostname()
107 set/getService() name of the running Helios service
108 -defaults to undefined, which will cause the
109 resulting config to contain the contents of the
110 helios.ini [global] section only
111  
112 =head2 CONFIG PARSING ACCESSORS
113  
114 These methods will be set during the configuration parsing process. Most
115 Helios service developers will not need to be aware of these, but if you are
116 developing a specialized Helios::Config subclass, they may be useful.
117  
118 set/getConfFileConfig() the config info parsed from helios.ini
119 set/getDbConfig() the config info parsed from the collective database
120 set/getDriver() Data::ObjectDriver object connected to collective db
121  
122 =head2 PARSING RESULTS ACCESSORS
123  
124 This method contains the results of the configuration parsing process. In
125 other words, the actual configuration information for the given Helios service.
126  
127 set/getConfig() the complete config info from both conf file & db
128  
129 =cut
130  
131 1 my $Debug = 0;
132 1 my $Errstr;
133 0 ▶sub debug { my $self = shift; @_ ? $Debug = shift : $Debug; }
Subroutine debug: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
134 0 ▶sub errstr { my $self = shift; @_ ? $Errstr = shift : $Errstr; }
Subroutine errstr: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
135  
136 1 my $ConfFile = $ENV{HELIOS_INI};
137 0 ▶sub setConfFile {
Subroutine setConfFile: not called CC=1 SCAR=0.6
Pod undocumented
138 0 my $var = $_[0]."::ConfFile";
139 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
140 0 $$var = $_[1];
141 }
142 0 ▶sub getConfFile {
Subroutine getConfFile: not called CC=1 SCAR=0.6
Pod undocumented
143 0 my $var = $_[0]."::ConfFile";
144 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
145 0 return $$var;
146 }
147  
148 1 my $Hostname = hostname();
149 0 ▶sub setHostname {
Subroutine setHostname: not called CC=1 SCAR=0.6
Pod undocumented
150 0 my $var = $_[0]."::Hostname";
151 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
152 0 $$var = $_[1];
153 }
154 0 ▶sub getHostname {
Subroutine getHostname: not called CC=1 SCAR=0.6
Pod undocumented
155 0 my $var = $_[0]."::Hostname";
156 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
157 0 return $$var;
158 }
159  
160  
161 1 my $ServiceName;
162 0 ▶sub setServiceName {
Subroutine setServiceName: not called CC=1 SCAR=0.6
Pod undocumented
163 0 my $var = $_[0]."::ServiceName";
164 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
165 0 $$var = $_[1];
166 }
167 0 ▶sub getServiceName {
Subroutine getServiceName: not called CC=1 SCAR=0.6
Pod undocumented
168 0 my $var = $_[0]."::ServiceName";
169 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
170 0 return $$var;
171 }
172 0 ▶sub setService { setServiceName(@_); }
Subroutine setService: not called CC=1 SCAR=6.9
Pod undocumented
173 0 ▶sub getService { getServiceName(@_); }
Subroutine getService: not called CC=1 SCAR=6.9
Pod undocumented
174  
175 my $Driver;
176 0 ▶sub setDriver {
Subroutine setDriver: not called CC=1 SCAR=0.6
Pod undocumented
177 0 my $var = $_[0]."::Driver";
178 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
179 0 $$var = $_[1];
180 }
181 0 ▶sub getDriver {
Subroutine getDriver: not called CC=1 SCAR=6.9
Pod undocumented
182 0 initDriver(@_);
183 }
184 0 ▶sub initDriver {
Subroutine initDriver: not called CC=3 SCAR=24.8
Pod undocumented
185 0 my $self = shift;
186 0 my $config = $self->getConfFileConfig();
187 0 ▶ if ($self->debug) { print __PACKAGE__.'->initDriver('.$config->{dsn}.','.$config->{user}.")\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) 0 0 0
188 my $driver = Helios::ObjectDriver::DBI->new(
189 dsn => $config->{dsn},
190 username => $config->{user},
191 password => $config->{password}
192 0 );
193 0 ▶ if ($self->debug) { print __PACKAGE__.'->initDriver() DRIVER: ',$driver,"\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) 0 0 0
194 0 $self->setDriver($driver);
195 0 return $driver;
196 }
197  
198  
199 1 my $Config;
200 0 ▶sub setConfig {
Subroutine setConfig: not called CC=1 SCAR=0.6
Pod undocumented
201 0 my $var = $_[0]."::Config";
202 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
203 0 $$var = $_[1];
204 }
205 0 ▶sub getConfig {
Subroutine getConfig: not called CC=1 SCAR=0.6
Pod undocumented
206 0 my $var = $_[0]."::Config";
207 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
208 0 return $$var;
209 }
210  
211 1 my $ConfFileConfig;
212 0 ▶sub setConfFileConfig {
Subroutine setConfFileConfig: not called CC=1 SCAR=0.6
Pod undocumented
213 0 my $var = $_[0]."::ConfFileConfig";
214 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
215 0 $$var = $_[1];
216 }
217 0 ▶sub getConfFileConfig {
Subroutine getConfFileConfig: not called CC=1 SCAR=0.6
Pod undocumented
218 0 my $var = $_[0]."::ConfFileConfig";
219 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
220 0 return $$var;
221 }
222  
223 1 my $DbConfig;
224 0 ▶sub setDbConfig {
Subroutine setDbConfig: not called CC=1 SCAR=0.6
Pod undocumented
225 0 my $var = $_[0]."::DbConfig";
226 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
227 0 $$var = $_[1];
228 }
229 0 ▶sub getDbConfig {
Subroutine getDbConfig: not called CC=1 SCAR=0.6
Pod undocumented
230 0 my $var = $_[0]."::DbConfig";
231 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
232 0 return $$var;
233 }
234  
235  
236  
237 =head1 CONFIGURATION INITIALIZATION METHODS
238  
239 =head2 init([%params])
240  
241 Prepares Helios::Config to parse the configuration for a particular Helios
242 service. Accepts initialization information as a hash of parameters; if a
243 parameter is not given, init() will attempt to default to values based on
244 information from the environment.
245  
246 The init() method accepts 4 arguments:
247  
248 CONF_FILE path to the helios.ini file (default: $HELIOS_INI env var)
249 HOSTNAME hostname (default: current hostname from Sys::Hostname::hostname())
250 DEBUG enable/disable debug mode (default: disabled)
251 SERVICE name of the Helios service to determine configuration for
252 (default: none)
253  
254 For example, to initialize Helios::Config to parse the configuration
255 information from /etc/helios/helios.ini for the Helios::TestService service
256 on the host named host1.hosting.com, one would call init() as:
257  
258 Helios::Config->init(
259 CONF_FILE => '/etc/helios/helios.ini',
260 HOSTNAME => 'host1.hosting.com',
261 SERVICE => 'Helios::TestService'
262 );
263  
264 Normally the host and config file are specified by the operating system and the
265 $HELIOS_INI environment variable, so a more typical init() call in a properly
266 set up Helios collective would only specify the service:
267  
268 Helios::Config->init(SERVICE => 'Helios::TestService');
269  
270 =cut
271  
272 0 ▶sub init {
Subroutine init: not called CC=9 SCAR=45.0
Pod documented
273 0 my $self = shift;
274 0 my %params = @_;
275 0 foreach (keys %params) {
276 0 $params{lc($_)} = $params{$_};
277 }
278 0 ▶ if ( defined($params{conf_file}) ) { $self->setConfFile($params{conf_file}); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"conf_file"}) 0 0 0
279 0 ▶ if ( defined($params{service}) ) { $self->setServiceName($params{service}); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"service"}) 0 0 0
280 0 ▶ if ( defined($params{hostname}) ) { $self->setHostname($params{hostname}); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"hostname"}) 0 0 0
281 0 ▶ if ( defined($params{debug}) ) { $self->debug($params{debug}); }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"debug"}) 0 0 0
282
283 # pull hostname from the environment if not already set
284 0 ▶ unless ( $self->getHostname() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->getHostname) 0 0 0
285 0 $self->setHostname( hostname() );
286 }
287 # again, pull conf file from environment if not already set
288 0 ▶ if ( !defined($self->getConfFile()) && defined($ENV{HELIOS_INI}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->getConfFile and defined $ENV{"HELIOS_INI"}) 0 0 0
Condition0%0/3
not defined $self->getConfFile and defined $ENV{"HELIOS_INI"}
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $self->getConfFile and defined $ENV{"HELIOS_INI"}
not defined $self->getConfFile defined $ENV{"HELIOS_INI"}
Truth table0%0/3
not(A) and B
A= not defined $self->getConfFileB= defined $ENV{"HELIOS_INI"}
ABresult
0X0
100
111
289 0 $self->setConfFile( $ENV{HELIOS_INI} );
290 }
291
292 # init() clears previous config
293 0 $self->setConfFileConfig(undef);
294 0 $self->setDbConfig(undef);
295 0 $self->setConfig(undef);
296
297 0 return $self;
298 }
299  
300  
301 =head1 CONFIGURATION PARSING METHODS
302  
303 =head2 parseConfig([%params])
304  
305 Given a set of optional initialization parameters, parseConfig() will parse
306 the helios.ini config file and query the Helios collective database for
307 configuration information for a particular Helios service, combining the
308 information into a single set of configuration information, which is returned
309 to the calling routine as a hash reference.
310  
311 The parseConfig() method controls the actual parsing and derivation of a
312 service's configuration. This process has 4 steps:
313  
314 =over 4
315  
316 =item * Initialization (optional)
317  
318 If parseConfig() was given options, it will call the init() method
319 to (re-)initialize the configuration parsing process. If no options were
320 specified, parseConfig() assumes all the necessary options have already been
321 set.
322  
323 =item * Conf file parsing
324  
325 If the configuration file has not yet been parsed, parseConfig() calls
326 parseConfFile() to parse it. If the conf file information has already been
327 parsed, parseConfig() skips this step. This is to ensure the helios.pl daemon
328 and Helios worker processes do not become unstable if the filesystem with the
329 config file becomes unmounted.
330  
331 See the parseConfFile() method entry for more information about this phase of
332 configuration parsing.
333  
334 =item * Conf database parsing
335  
336 Given the information obtained in the previous step, parseConfig() calls the
337 parseConfDb() method to query the Helios collective database for configuration
338 information for the specified Helios service. Unlike the previous step,
339 parseConfig() B<always> calls parseConfDb(). This is so the helios.pl daemon
340 and Helios worker processes can dynamically update their configuration from the
341 database.
342  
343 See the parseConfDb() method entry for more information about this phase of
344 configuration parsing.
345  
346 =item * Merging configurations
347  
348 Once the configurations from the conf file and the database have been
349 acquired, parseConfig() merges the config hashes together into a single hash of
350 configuration parameters for the specified service. This single config hashref
351 is returned to the calling routine. A cached copy is also made available
352 via the getConfig() method.
353  
354 Configuration parameters for a service specified in the collective
355 database override parameters specified in the conf file.
356  
357 NOTE: Prior to Helios::Config, Helios assembled configuration parameter hashes
358 differently. Originally, both helios.ini and database config parameters were
359 reparsed each time a config refresh was requested, and the new parameters were
360 merged with the old configuration values. This caused config values to "stick"
361 even if they were completely deleted from the database or conf file. For
362 example, deleting a HOLD parameter was not enough to take a service out of hold
363 mode; the Helios administrator had to set HOLD to 0.
364  
365 Helios::Config merges configurations differently. Though the conf file config
366 is only parsed once, each refresh of the database config starts with a new
367 hash, and the config merge process starts with a brand new hash as well. That
368 way the config hash returned by parseConfig() contains only the B<current>
369 config parameters, leading to a more predictable configuration subsystem.
370  
371 =back
372  
373 =cut
374  
375 0 ▶sub parseConfig {
Subroutine parseConfig: not called CC=6 SCAR=37.4
Pod documented
376 0 my $self = shift;
377 0 my $conf_file_config;
378 my $conf_db_config;
379  
380 # if we were passed options,
381 # OR we haven't been initialized,
382 # go ahead and call init() (with the given options)
383 0 ▶ if (@_ || !( $self->getConfFile() && $self->getHostname() ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ or not $self->getConfFile && $self->getHostname) 0 0 0
Condition0%0/6
@_ or not $self->getConfFile && $self->getHostname
l!l&&r!l&&!r
000
$self->getConfFile && $self->getHostname
!ll&&!rl&&r
000
MC/DC0%0/3
@_ or not $self->getConfFile && $self->getHostname
@_ $self->getConfFile $self->getHostname
Truth table0%0/4
A or not(B && C)
A= @_B= $self->getConfFileC= $self->getHostname
ABCresult
1XX1
00X1
0101
0110
384 0 $self = $self->init(@_);
385 }
386
387 # only parse conf file once
388 0 ▶ if ( $self->getConfFileConfig() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->getConfFileConfig) { } 0 0 0
389 0 $conf_file_config = $self->getConfFileConfig();
390 } else {
391 0 $conf_file_config = $self->parseConfFile();
392 }
393
394 # conf db always gets reparsed
395 0 $conf_db_config = $self->parseConfDb();
396  
397 # merge configs
398 # deref conf file hashref so db conf
399 # doesn't leak into file conf when merged
400 0 my %conf = %{$self->getConfFileConfig()};
401 0 ▶ while ( my ($key, $value) = each %$conf_db_config ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$conf_db_config) 0 0 0
402 0 $conf{$key} = $value;
403 }
404 0 $self->setConfig(\%conf);
405 0 return \%conf;
406 }
407  
408  
409 =head2 parseConfFile([$conf_file, $service_name])
410  
411 Given an optional conf file and an optional service name, parseConfFile()
412 parses the conf file and returns the resulting hashref to the calling routine.
413 It also makes the hashref available via the getConfFileConfig() accessor.
414 If either the conf file or the service is not specified, the values from the
415 getConfFile() and/or getService() accessor(s) are used. The conf file
416 location is set by init() to the value of the $HELIOS_INI environment variable
417 unless otherwise specified.
418  
419 The default Helios configuration file is the common .ini file format, where
420 section headings are denoted by brackets ([]). Lines not starting with [ are
421 considered parameters belonging to the last declared section. Lines starting
422 with # or ; are considered comments and are ignored. See L<Config::IniFiles>
423 (the default underlying file parser) for more format details.
424  
425 Helios requires at least one section, [global], in the conf file, which should
426 contain at least 3 parameters:
427  
428 dsn DBI datasource name of the Helios collective database
429 user the user to use to access the Helios collective db
430 password the password to use to access the Helios collective db
431  
432 Without these, the helios.pl daemon will be unable to connect to the collective
433 database and will fail to start.
434  
435 You may also specify other configuration parameters in the [global] section.
436 Options set in the [global] section will appear in the configuration parameters
437 of all services using that conf file. This can be useful if you need multiple
438 services on a host to share a configuration (e.g. you want to configure all
439 services on a host to log messages to a syslogd facility using
440 HeliosX::Logger::Syslog).
441  
442 In addition to [global], you can create other sections as well. If a section
443 name matches the service name specified, the configuration parameters in that
444 section will be included in the config hash returned to the calling routine.
445 You can use this feature to set defaults for a service, or to set sensitive
446 parameters (e.g. passwords) that you do not want to be changable from the
447 Helios::Panoptes web admin console.
448  
449 For example, a Helios conf file that configures the Helios collective db and
450 sets some config parameters for the Helios::TestService service would look
451 something like:
452  
453 [global]
454 dsn=dbi:mysql:host=dbhost;db=helios_db
455 user=helios_user
456 password=xyz123
457
458 [Helios::TestService]
459 MAX_WORKERS=1
460 loggers=HeliosX::Logger::Syslog
461 syslog_facility=user
462 syslog_options=pid
463  
464 =cut
465  
466 0 ▶sub parseConfFile {
Subroutine parseConfFile: not called CC=10 SCAR=47.0
Pod documented
467 0 my $self = shift;
468 0 ▶ my $conf_file = @_ ? shift : $self->getConfFile();
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
469 0 ▶ my $service_name = @_ ? shift : $self->getServiceName();
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
470 0 my $conf;
471
472 0 ▶ unless ($conf_file) { Helios::Error::ConfigError->throw("No conf file specified"); }
Branch: 1 branch
BranchTrueFalseTotal
unless ($conf_file) 0 0 0
473 0 ▶ unless (-r $conf_file) { Helios::Error::ConfigError->throw("Cannot read conf file $conf_file"); }
Branch: 1 branch
BranchTrueFalseTotal
unless (-r $conf_file) 0 0 0
474
475 0 my $cif = Config::IniFiles->new( -file => $conf_file );
476 0 ▶ unless ( defined($cif) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cif) 0 0 0
477 # @Config::IniFiles::errors contains the parse error(s);
478 0 my $E = join(" ", @Config::IniFiles::errors);
479 0 Helios::Error::ConfigError->throw("parseConfFile(): Invalid config file: $E");
480 }
481  
482 # global must exist
483 0 ▶ if ($cif->SectionExists("global") ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cif->SectionExists("global")) 0 0 0
484 0 foreach ( $cif->Parameters("global") ) {
485 0 $conf->{$_} = $cif->val("global", $_);
486 }
487 }
488  
489 # if there's a section specifically for this service class, read it too
490 # (it will effectively override the global section, BTW)
491 0 ▶ if ( $cif->SectionExists( $service_name ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cif->SectionExists($service_name)) 0 0 0
492 0 foreach ( $cif->Parameters($service_name) ) {
493 0 $conf->{$_} = $cif->val($service_name, $_);
494 }
495 }
496  
497 0 $self->setConfFileConfig($conf);
498  
499 0 return $conf;
500 }
501  
502  
503 =head2 parseConfDb([$service_name, $hostname])
504  
505 The parseConfDb() method queries the Helios collective database for
506 configuration parameters matching the specified service name and hostname and
507 returns a hashref with those parameters to the calling routine. If the service
508 name and hostname are not specified, the values returned from the
509 getService() and getHostname() accessors are used. The getHostname() value
510 is normally set by init() to the value returned by Sys::Hostname::hostname()
511 unless otherwise specified.
512  
513 The default parseConfDb() queries the HELIOS_PARAMS_TB table in the Helios
514 collective database. Two separate queries are done:
515  
516 =over 4
517  
518 =item *
519  
520 Config params matching the service name and a host of '*'. Config params
521 with a '*' host apply to all instances of the service in the entire
522 collective.
523  
524 =item *
525  
526 Config params matching the service name and the current hostname. Config
527 params with a specific hostname apply only to instances of that service on
528 that particular host. These are useful for HOLDing or HALTing services only on
529 one host, or working with differences between hosts (e.g. a host with 4 cores
530 and 16GB of RAM can support a higher MAX_WORKERS value than a dual core system
531 with 2GB of memory).
532  
533 =back
534  
535 The results of these two queries are merged, and the resulting hashref returned
536 to the calling routine. Config parameters for a specific host override config
537 params for all ('*') hosts.
538  
539 Configuration parameters in the Helios collective database can be set using
540 the Helios::Panoptes web admin console or using your database's standard SQL
541 commands.
542  
543 =cut
544  
545 0 ▶sub parseConfDb {
Subroutine parseConfDb: not called CC=7 SCAR=40.3
Pod documented
546 0 my $self = shift;
547 0 ▶ my $service_name = @_ ? shift : $self->getServiceName();
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
548 0 ▶ my $hostname = @_ ? shift : $self->getHostname();
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
549 0 my $conf_all_hosts = {};
550 0 my $conf_this_host = {};
551 0 my $conf = {};
552 0 my @dbparams;
553  
554 0 my $driver = $self->getDriver();
555
556 0 @dbparams = $driver->search( 'Helios::ConfigParam' => {
557 worker_class => $service_name,
558 host => ['*', $self->getHostname() ],
559 }
560 );
561 0 foreach(@dbparams) {
562 0 ▶ if ($self->debug) { print $_->param(),'=>',$_->value(),"\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) 0 0 0
563 0 ▶ if ( $_->host eq '*') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->host eq '*') { } 0 0 0
564 0 $conf_all_hosts->{$_->param()} = $_->value();
565 } else {
566 0 $conf_this_host->{ $_->param() } = $_->value();
567 }
568 }
569
570 0 $conf = $conf_all_hosts;
571 0 ▶ while ( my ($key, $value) = each %$conf_this_host) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$conf_this_host) 0 0 0
572 0 $conf->{$key} = $value;
573 }
574 0 $self->setDbConfig($conf);
575 0 return $conf;
576 }
577  
578  
579  
580  
581  
582 =head2 getParam(param => $param_name [, service => $service_name] [, hostname => $hostname])
583  
584 Given a service name, parameter name, and (optionally) a hostname, getParam()
585 returns the parameter name's value to the calling routine.
586  
587 If hostname is not specified, the current host is assumed.
588  
589 =cut
590  
591 0 ▶sub getParam {
Subroutine getParam: not called CC=21 SCAR=61.4
Pod documented
592 0 my $self = shift;
593 0 my %params;
594 0 ▶ if (scalar @_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1) { } 0 0 0
595 0 $params{param} = $_[0];
596 } else {
597 0 %params = @_;
598 }
599 0 ▶ my $service_name = defined($params{service}) ? $params{service} : $self->getServiceName;
Branch: 1 branch
BranchTrueFalseTotal
defined $params{'service'} ? : 0 0 0
600 0 my $param_name = $params{param};
601 0 ▶ my $host = defined($params{hostname}) ? $params{hostname} : $self->getHostname();
Branch: 1 branch
BranchTrueFalseTotal
defined $params{'hostname'} ? : 0 0 0
602 0 my $conf = {};
603  
604 # shortcut: if the current config hash has already been retrieved
605 # and the requested param is set, return *that* param
606 0 ▶ if ( defined($self->getConfig()) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->getConfig and defined $self->getService and $self->getService eq $service_name and defined $self->getHostname and $self->getHostname eq $host and defined $self->getConfig->{$param_name}) 0 0 0
Condition0%0/15
defined $self->getConfig and defined $self->getService and $self->getService eq $service_name and defined $self->getHostname and $self->getHostname eq $host and defined $self->getConfig->{$param_name}
!ll&&!rl&&r
000
defined $self->getConfig and defined $self->getService and $self->getService eq $service_name and defined $self->getHostname and $self->getHostname eq $host
!ll&&!rl&&r
000
defined $self->getConfig and defined $self->getService and $self->getService eq $service_name and defined $self->getHostname
!ll&&!rl&&r
000
defined $self->getConfig and defined $self->getService and $self->getService eq $service_name
!ll&&!rl&&r
000
defined $self->getConfig and defined $self->getService
!ll&&!rl&&r
000
MC/DC0%0/6
defined $self->getConfig and defined $self->getService and $self->getService eq $service_name and defined $self->getHostname and $self->getHostname eq $host and defined $self->getConfig->{$param_name}
defined $self->getConfig defined $self->getService $self->getService eq $service_name defined $self->getHostname $self->getHostname eq $host defined $self->getConfig->{$param_name}
Truth table0%0/7
A and B and C and D and E and F
A= defined $self->getConfigB= defined $self->getServiceC= $self->getService eq $service_nameD= defined $self->getHostnameE= $self->getHostname eq $hostF= defined $self->getConfig->{$param_name}
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
607 defined($self->getService) && ($self->getService eq $service_name) &&
608 defined($self->getHostname) && ($self->getHostname eq $host) &&
609 defined($self->getConfig()->{$param_name})
610 ) {
611 0 return $self->getConfig()->{$param_name};
612 }
613  
614 # if we don't have everything, stop before we try
615 0 ▶ unless ($service_name && $host && $param_name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($service_name and $host and $param_name) 0 0 0
Condition0%0/6
$service_name and $host and $param_name
!ll&&!rl&&r
000
$service_name and $host
!ll&&!rl&&r
000
MC/DC0%0/3
$service_name and $host and $param_name
$service_name $host $param_name
Truth table0%0/4
A and B and C
A= $service_nameB= $hostC= $param_name
ABCresult
0XX0
10X0
1100
1111
616 0 Helios::Error::ConfigError->throw('getParam(): service and param are required.');
617 }
618  
619  
620 eval {
621 0 my $driver = $self->getDriver();
622 0 my @dbparams = $driver->search( 'Helios::ConfigParam' => {
623 worker_class => $service_name,
624 param => $param_name,
625 host => ['*', $host ],
626 }
627 );
628 0 my %conf_all_hosts;
629 my %conf_this_host;
630 0 foreach(@dbparams) {
631 0 ▶ if ($self->debug) { print $_->worker_class(),'|', $_->host(),'|', $_->param(),'=>',$_->value(),"\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) 0 0 0
632 0 ▶ if ( $_->host() eq '*') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->host eq '*') { } 0 0 0
633 0 $conf_all_hosts{$_->param()} = $_->value();
634 } else {
635 0 $conf_this_host{ $_->param() } = $_->value();
636 }
637 }
638 0 $conf = \%conf_all_hosts;
639
640 # if host=*, we're done
641 # otherwise, use the given host, if the param is available
642 0 ▶ if ( $host ne '*' && defined($conf_this_host{$param_name}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host ne "*" and defined $conf_this_host{$param_name}) 0 0 0
Condition0%0/3
$host ne "*" and defined $conf_this_host{$param_name}
!ll&&!rl&&r
000
MC/DC0%0/2
$host ne "*" and defined $conf_this_host{$param_name}
$host ne "*" defined $conf_this_host{$param_name}
Truth table0%0/3
A and B
A= $host ne "*"B= defined $conf_this_host{$param_name}
ABresult
0X0
100
111
643 0 $conf->{$param_name} = $conf_this_host{$param_name};
644 }
645  
646 0 1;
647 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { my $driver = $self->getDriver; my(@dbparams) = $driver->search("Helios::ConfigParam", {"worker_class", $service_name, "param", $param_name, "host", ["*", $host]}); my(%conf_all_hosts, %conf_this_host); foreach $_ (@dbparams) { if ($self->debug) { print $_->worker_class, "|", $_->host, "|", $_->param, "=>", $_->value, "\n"; } ; if ($_->host eq "*") { $conf_all_hosts{$_->param} = $_->value; } else { $conf_this_host{$_->param} = $_->value; } ; } ; $conf = \%conf_all_hosts; if ($host ne "*" and defined $conf_this_host{$param_name}) { $conf->{$param_name} = $conf_this_host{$param_name}; } ; 1 } } or do { my $E = $@; "Helios::Error::ConfigError"->throw("getParam(): $E") } 0 0 0
648 0 my $E = $@;
649 0 Helios::Error::ConfigError->throw("getParam(): $E");
650 };
651  
652 0 ▶ if ($self->debug && !defined($conf->{$param_name})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug and not defined $conf->{$param_name}) 0 0 0
Condition0%0/3
$self->debug and not defined $conf->{$param_name}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->debug and not defined $conf->{$param_name}
$self->debug not defined $conf->{$param_name}
Truth table0%0/3
A and not(B)
A= $self->debugB= not defined $conf->{$param_name}
ABresult
0X0
100
111
653 0 print "$service_name|$host|$param_name not found.\n";
654 }
655  
656 0 return $conf->{$param_name};
657 }
658  
659 # this is officially *undocumented*
660 0 ▶sub getAllParams {
Subroutine getAllParams: not called CC=4 SCAR=30.0
Pod undocumented
661 0 my $self = shift;
662 0 my $conf_all_hosts = {};
663 0 my $conf_this_host = {};
664 0 my $conf = {};
665  
666 eval {
667 0 my $driver = $self->getDriver();
668 0 my @dbparams = $driver->search( 'Helios::ConfigParam' );
669 0 foreach(@dbparams) {
670 0 ▶ if ($self->debug) { print $_->param(),'=>',$_->value(),"\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) 0 0 0
671 0 $conf->{ $_->worker_class() }->{ $_->host() }->{ $_->param() } = $_->value();
672 }
673
674 0 1;
675 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { my $driver = $self->getDriver; my(@dbparams) = $driver->search("Helios::ConfigParam"); foreach $_ (@dbparams) { if ($self->debug) { print $_->param, "=>", $_->value, "\n"; } ; $$conf{$_->worker_class}{$_->host}{$_->param} = $_->value; } ; 1 } } or do { my $E = $@; "Helios::Error::ConfigError"->throw("getAllParams(): $E") } 0 0 0
676 0 my $E = $@;
677 0 Helios::Error::ConfigError->throw("getAllParams(): $E");
678 };
679  
680 0 return $conf;
681 }
682  
683  
684 =head2 setParam(param => $param_name [, service => $service_name] [, hostname => $hostname], value => $value)
685  
686 Given a service name, parameter name, parameter value, and (optionally)
687 a hostname, setParam() sets the value for that parameter for that service
688 (and host) in the Helios collective database. If hostname is not specified,
689 the current host is assumed. To set a parameter for all instances of a
690 service in a collective, set the hostname to '*'.
691  
692 =cut
693  
694 0 ▶sub setParam {
Subroutine setParam: not called CC=12 SCAR=50.5
Pod documented
695 0 my $self = shift;
696 0 my %params = @_;
697 0 ▶ my $service_name = defined($params{service}) ? $params{service} : $self->getServiceName;
Branch: 1 branch
BranchTrueFalseTotal
defined $params{'service'} ? : 0 0 0
698 0 my $param_name = $params{param};
699 0 ▶ my $host = defined($params{hostname}) ? $params{hostname} : $self->getHostname;
Branch: 1 branch
BranchTrueFalseTotal
defined $params{'hostname'} ? : 0 0 0
700 0 my $value = $params{value};
701 0 my $cp;
702  
703 # if we don't have everything, stop before we try
704 0 ▶ unless ($service_name && $host && $param_name && defined($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($service_name and $host and $param_name and defined $value) 0 0 0
Condition0%0/9
$service_name and $host and $param_name and defined $value
!ll&&!rl&&r
000
$service_name and $host and $param_name
!ll&&!rl&&r
000
$service_name and $host
!ll&&!rl&&r
000
MC/DC0%0/4
$service_name and $host and $param_name and defined $value
$service_name $host $param_name defined $value
Truth table0%0/5
A and B and C and D
A= $service_nameB= $hostC= $param_nameD= defined $value
ABCDresult
0XXX0
10XX0
110X0
11100
11111
705 0 Helios::Error::ConfigError->throw('setParam(): Service, param name, and value are required.');
706 }
707  
708 eval {
709 # HELIOS_PARAMS_TB does not have a Primary Key.
710 # Because of that, we cannot use D::OD in the normal way (search() or
711 # lookup(), change the value, then save()).
712 # If we find an existing param matching service|host|param, we have to
713 # delete ALL of the matching service|host|param in the table, then
714 # create a new one.
715 # In SQL terms, we'll always do SELECT>DELETE>INSERT instead of
716 # SELECT>UPDATE|INSERT.
717  
718 # query for existing service/host/param
719 0 my $driver = $self->getDriver();
720 0 my @cps = $driver->search( 'Helios::ConfigParam' => {
721 worker_class => $service_name,
722 param => $param_name,
723 host => $host,
724 }
725 );
726 0 my $cp = shift @cps;
727
728 # ok, if there is a service/host/param exists,
729 # we have to clear it out first, then create a new one from scratch
730 0 ▶ if (defined($cp)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cp) { } 0 0 0
731 0 ▶ if ($self->debug) { print "$service_name|$host|$param_name already set to ",$cp->value,". Clearing.\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) 0 0 0
732 0 $driver->remove('Helios::ConfigParam' =>
733 {
734 worker_class => $service_name,
735 host => $host,
736 param => $param_name
737 },
738 { nofetch => 1 }
739 );
740 } else {
741 0 ▶ if ($self->debug) { print "$service_name|$host|$param_name not found. Creating.\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) 0 0 0
742 }
743  
744 # now, create a new Helios::ConfigParam and insert into the database
745 0 ▶ if ($self->debug) { print "$service_name|$host|$param_name setting to $value\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) 0 0 0
746 0 $cp = Helios::ConfigParam->new();
747 0 $cp->worker_class($service_name);
748 0 $cp->host($host);
749 0 $cp->param($param_name);
750 0 $cp->value($value);
751 0 $driver->insert($cp);
752
753 0 1;
754 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { my $driver = $self->getDriver; my(@cps) = $driver->search("Helios::ConfigParam", {"worker_class", $service_name, "param", $param_name, "host", $host}); my $cp = shift @cps; if (defined $cp) { if ($self->debug) { print "$service_name|$host|$param_name already set to ", $cp->value, ". Clearing.\n"; } ; $driver->remove("Helios::ConfigParam", {"worker_class", $service_name, "host", $host, "param", $param_name}, {"nofetch", 1}); } else { if ($self->debug) { print "$service_name|$host|$param_name not found. Creating.\n"; } ; } ; if ($self->debug) { print "$service_name|$host|$param_name setting to $value\n"; } ; $cp = "Helios::ConfigParam"->new; $cp->worker_class($service_name); $cp->host($host); $cp->param($param_name); $cp->value($value); $driver->insert($cp); 1 } } or do { my $E = $@; "Helios::Error::ConfigError"->throw("setParam(): $E") } 0 0 0
755 0 my $E = $@;
756 # rethrow the error as a ConfigError
757 0 Helios::Error::ConfigError->throw("setParam(): $E");
758 };
759  
760 0 return 1;
761 }
762  
763  
764 =head2 unsetParam(param => $param_name [, service => $service_name] [, hostname => $hostname,])
765  
766 Given a service name, parameter name, and (optionally) a hostname, unsetParam()
767 deletes that parameter's entry in the Helios collective database.
768  
769 If hostname is not specified, the current host is assumed. To unset a
770 parameter that is in effect for all instances of a service, you must set the
771 hostname to '*'.
772  
773 =cut
774  
775 0 ▶sub unsetParam {
Subroutine unsetParam: not called CC=8 SCAR=42.8
Pod documented
776 0 my $self = shift;
777 0 my %params = @_;
778 0 ▶ my $service_name = defined($params{service}) ? $params{service} : $self->getServiceName;
Branch: 1 branch
BranchTrueFalseTotal
defined $params{'service'} ? : 0 0 0
779 0 my $param_name = $params{param};
780 0 ▶ my $host = defined($params{hostname}) ? $params{hostname} : $self->getHostname;
Branch: 1 branch
BranchTrueFalseTotal
defined $params{'hostname'} ? : 0 0 0
781 0 my $cp;
782  
783 # if we don't have everything, stop before we try
784 0 ▶ unless ($service_name && $host && $param_name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($service_name and $host and $param_name) 0 0 0
Condition0%0/6
$service_name and $host and $param_name
!ll&&!rl&&r
000
$service_name and $host
!ll&&!rl&&r
000
MC/DC0%0/3
$service_name and $host and $param_name
$service_name $host $param_name
Truth table0%0/4
A and B and C
A= $service_nameB= $hostC= $param_name
ABCresult
0XX0
10X0
1100
1111
785 0 Helios::Error::ConfigError->throw('setParam(): Service and param name are required.');
786 }
787  
788 eval {
789 # delete ALL of the matching service|host|param in the table
790 0 my $driver = $self->getDriver();
791 0 ▶ if ($self->debug) { print "Clearing $service_name|$host|$param_name from param table.\n"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) 0 0 0
792 0 $driver->remove('Helios::ConfigParam' =>
793 {
794 worker_class => $service_name,
795 host => $host,
796 param => $param_name
797 },
798 { nofetch => 1 }
799 );
800
801 0 1;
802 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { my $driver = $self->getDriver; if ($self->debug) { print "Clearing $service_name|$host|$param_name from param table.\n"; } ; $driver->remove("Helios::ConfigParam", {"worker_class", $service_name, "host", $host, "param", $param_name}, {"nofetch", 1}); 1 } } or do { my $E = $@; "Helios::Error::ConfigError"->throw("unsetParam(): $E") } 0 0 0
803 0 my $E = $@;
804 # rethrow the error as a ConfigError
805 0 Helios::Error::ConfigError->throw("unsetParam(): $E");
806 };
807  
808 0 return 1;
809 }
810  
811  
812  
813 1 1;
814 __END__
« Helios/Service.pm ↑ Summary Helios/TS.pm »