blib/lib/Illumos/SMF.pm untested

filter: Statementstmt 0.0% 0 / 282 Branchbran 0.0% 0 / 80 Conditioncond 0.0% 0 / 82 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 25 Podpod 0.0% 0 / 22 totaltotal 0.0% 0 / 491 SCAR 72.5
CC
37
Cov
0%
getFMRIProperties
42.0
listFMRI
30.0
addInstance
20.0
CRAP 1406.0
1 package Illumos::SMF;
2  
3 use strict;
4 use warnings;
5  
6 # version
7 our $VERSION = '0.1.6';
8  
9 # commands
10 my $SVCS = '/usr/bin/svcs';
11 my $SVCCFG = '/usr/sbin/svccfg';
12 my $SVCADM = '/usr/sbin/svcadm';
13 my $ZLOGIN = '/usr/sbin/zlogin';
14  
15 # constructor
16 sub new {
17 my $class = shift;
18 my $self = { @_ };
19  
20 # add Illumos::Zone instance if zone support is required
21 $self->{zonesupport} && do {
22 eval {
23 require Illumos::Zones;
24 };
25 if ($@) {
26 die "ERROR: Unable to load package Illumos::Zones.";
27 }
28  
29 $self->{zone} = Illumos::Zones->new(debug => $self->{debug});
30 };
31
32 return bless $self, $class
33 }
34 # private methods
35 my $svcAdm = sub {
36 my $self = shift;
37 my $cmd = shift;
38 my $fmri = shift;
39  
40 my @cmd = ($SVCADM, $cmd, $fmri);
41  
42 print STDERR '# ' . join(' ', @cmd) . "\n" if $self->{debug};
43 system(@cmd) and die "ERROR: cannot $cmd '$fmri'\n";
44 };
45  
46 my $zoneCmd = sub {
47 my $self = shift;
48 my $zoneName = shift;
49  
50 print STDERR "WARNING: zonename specified but 'zonesupport' not enabled for Illumos::SMF\n"
51 . "use 'Illumos::SMF(zonesupport => 1)' to enable zone support\n" if $zoneName && !$self->{zone};
52  
53 return { cmd => [], shellquote => q{"} } if !$zoneName || !$self->{zone};
54  
55 my $zone = $self->{zone}->listZone($zoneName);
56 if ($zone && $zone->{state} eq 'running') {
57 return { cmd => [ $ZLOGIN, $zoneName ], shellquote => q{'"'} };
58 }
59 else {
60 return { cmd => [], zpath => $zone->{zonepath}, shellquote => q{"} };
61 }
62  
63 # just in case, should never reach here...
64 return { cmd => [], shellquote => q{"} };
65 };
66  
67 # public methods
68 sub refreshFMRI {
69 my $self = shift;
70 my $fmri = shift;
71 my $opts = $_[0] // {};
72
73 my $zcmd = $self->$zoneCmd($opts->{zonename});
74 my @cmd = @{$zcmd->{cmd}};
75 local $ENV{SVCCFG_REPOSITORY} = $zcmd->{zpath}
76 . '/root/etc/svc/repository.db' if $zcmd->{zpath};
77  
78 push @cmd, ($SVCCFG, '-s', $fmri, 'refresh');
79  
80 print STDERR '# ' . join(' ', @cmd) . "\n" if $self->{debug};
81 system(@cmd) and die "ERROR: cannot refresh FMRI '$fmri'\n";
82
83 return 1;
84 }
85  
86 sub listFMRI {
87 my $self = shift;
88 my $fmri = shift;
89 my $opts = $_[0] // {};
90 my @fmris;
91
92 my $zcmd = $self->$zoneCmd($opts->{zonename});
93 my @cmd = @{$zcmd->{cmd}};
94 local $ENV{SVCCFG_REPOSITORY} = $zcmd->{zpath}
95 . '/root/etc/svc/repository.db' if $zcmd->{zpath};
96
97 $fmri ||= '*';
98
99 # remove leading 'svc:/'
100 $fmri =~ s/^svc:\///;
101  
102 my @cmd1 = (@cmd, $SVCCFG, 'list', $fmri);
103  
104 print STDERR '# ' . join(' ', @cmd1) . "\n" if $self->{debug};
105 open my $fmris, '-|', @cmd1
106 or die "ERROR: cannot get list of FMRI\n";
107  
108 while (my $elem = <$fmris>) {
109 chomp $elem;
110 push @fmris, "svc:/$elem" if !$opts->{instancesonly};
111
112 my @cmd2 = (@cmd, $SVCCFG, '-s', $elem, 'list');
113  
114 open my $instances, '-|', @cmd2
115 or die "ERROR: cannot get instances of '$elem'\n";
116  
117 while (<$instances>) {
118 chomp;
119 next if /:properties/;
120 push @fmris, "svc:/$elem:$_";
121 }
122 close $instances;
123 }
124  
125 return [ @fmris ];
126 }
127  
128 sub fmriExists {
129 my $self = shift;
130 my $fmri = shift;
131 my $opts = shift;
132  
133 # remove instance name
134 my ($baseFmri) = $fmri =~ /^((?:svc:)?[^:]+)/;
135  
136 return grep { $fmri eq $_ } @{$self->listFMRI($baseFmri, $opts)};
137 }
138  
139 sub fmriState {
140 my $self = shift;
141 my $fmri = shift;
142 my $opts = shift;
143  
144 my @cmd = ($SVCS, $opts->{zonename} ? ('-z', $opts->{zonename}) : (), qw(-H -o state), $fmri);
145  
146 print STDERR '# ' . join(' ', @cmd) . "\n" if $self->{debug};
147 open my $fmris, '-|', @cmd
148 or die "ERROR: cannot get list of FMRI\n";
149  
150 chomp(my $state = <$fmris>);
151 return $state;
152 }
153  
154 sub fmriOnline {
155 my $self = shift;
156
157 return $self->fmriState(shift, shift) eq 'online';
158 }
159  
160 sub enable {
161 my $self = shift;
162 my $fmri = shift;
163  
164 $self->$svcAdm('enable', $fmri);
165 }
166  
167 sub disable {
168 my $self = shift;
169 my $fmri = shift;
170  
171 $self->$svcAdm('disable', $fmri);
172 }
173  
174 sub restart {
175 my $self = shift;
176 my $fmri = shift;
177  
178 $self->$svcAdm('restart', $fmri);
179 }
180  
181 sub addFMRI {
182 my $self = shift;
183 my $fmri = shift;
184 my $opts = $_[0] // {};
185  
186 my $zcmd = $self->$zoneCmd($opts->{zonename});
187 my @cmd = @{$zcmd->{cmd}};
188 local $ENV{SVCCFG_REPOSITORY} = $zcmd->{zpath}
189 . '/root/etc/svc/repository.db' if $zcmd->{zpath};
190  
191 # remove leading 'svc:/'
192 $fmri =~ s/^svc:\///;
193  
194 push @cmd, ($SVCCFG, 'add', $fmri);
195  
196 print STDERR '# ' . join(' ', @cmd) . "\n" if $self->{debug};
197 system(@cmd) and die "ERROR: cannot add '$fmri'\n";
198 }
199  
200 sub deleteFMRI {
201 my $self = shift;
202 my $fmri = shift;
203 my $opts = $_[0] // {};
204  
205 my $zcmd = $self->$zoneCmd($opts->{zonename});
206 my @cmd = @{$zcmd->{cmd}};
207 local $ENV{SVCCFG_REPOSITORY} = $zcmd->{zpath}
208 . '/root/etc/svc/repository.db' if $zcmd->{zpath};
209  
210 push @cmd, ($SVCCFG, 'delete', $fmri);
211 print STDERR '# ' . join(' ', @cmd) . "\n" if $self->{debug};
212 system(@cmd) and die "ERROR: cannot delete $fmri\n";
213 }
214  
215 sub addInstance {
216 my $self = shift;
217 my $fmri = shift;
218 my $instance = shift;
219 my $opts = $_[0] // {};
220  
221 my $zcmd = $self->$zoneCmd($opts->{zonename});
222 my @cmd = @{$zcmd->{cmd}};
223 local $ENV{SVCCFG_REPOSITORY} = $zcmd->{zpath}
224 . '/root/etc/svc/repository.db' if $zcmd->{zpath};
225  
226 push @cmd, ($SVCCFG, '-s', $fmri, 'add', $instance);
227 print STDERR '# ' . join(' ', @cmd) . "\n" if $self->{debug};
228 system(@cmd) and die "ERROR: cannot add instance '$instance' to $fmri\n";
229  
230 $self->addPropertyGroup("$fmri:$instance", 'general', 'framework', $opts);
231 $self->setProperty("$fmri:$instance", 'general/complete', $instance, undef, $opts);
232 $self->setProperty("$fmri:$instance", 'general/enabled',
233 $opts->{enabled} ? 'true' : 'false', undef, $opts);
234 }
235  
236 sub getPropertyGroups {
237 my $self = shift;
238 my $fmri = shift;
239 my $opts = $_[0] // {};
240  
241 my $zcmd = $self->$zoneCmd($opts->{zonename});
242 my @cmd = @{$zcmd->{cmd}};
243 local $ENV{SVCCFG_REPOSITORY} = $zcmd->{zpath}
244 . '/root/etc/svc/repository.db' if $zcmd->{zpath};
245  
246 my $pg = [];
247 push @cmd, ($SVCCFG, '-s', $fmri, 'listpg');
248  
249 print STDERR '# ' . join(' ', @cmd) . "\n" if $self->{debug};
250 open my $props, '-|', @cmd
251 or die "ERROR: cannot get property group of FMRI '$fmri'\n";
252  
253 while (my $prop = <$props>){
254 chomp $prop;
255 my ($name, $type) = split /\s+/, $prop, 2;
256 push @$pg, $name;
257 }
258
259 return $pg;
260 }
261  
262 sub propertyExists {
263 my $self = shift;
264 my $fmri = shift;
265 my $property = shift;
266 my $opts = shift;
267
268 # extract property group
269 my ($pg) = $property =~ /^([^\/]+)/;
270  
271 return grep { $property eq $_ } keys %{$self->getProperties($fmri, $pg, $opts)};
272 }
273  
274 sub propertyGroupExists {
275 my $self = shift;
276 my $fmri = shift;
277 my $pg = shift;
278 my $opts = shift;
279  
280 return grep { $pg eq $_ } @{$self->getPropertyGroups($fmri, $opts)};
281 }
282  
283 sub addPropertyGroup {
284 my $self = shift;
285 my $fmri = shift;
286 my $pg = shift;
287 my $type = shift;
288 my $opts = $_[0] // {};
289
290 my $zcmd = $self->$zoneCmd($opts->{zonename});
291 my @cmd = @{$zcmd->{cmd}};
292 local $ENV{SVCCFG_REPOSITORY} = $zcmd->{zpath}
293 . '/root/etc/svc/repository.db' if $zcmd->{zpath};
294
295 # set type to application if not specified
296 $type //= 'application';
297  
298 return if $self->propertyGroupExists($fmri, $pg, $opts);
299  
300 push @cmd, ($SVCCFG, '-s', $fmri, 'addpg', $pg, $type);
301 print STDERR '# ' . join(' ', @cmd) . "\n" if $self->{debug};
302 system(@cmd) and die "ERROR: cannot add property group to $fmri\n";
303 }
304  
305 sub deletePropertyGroup {
306 my $self = shift;
307 my $fmri = shift;
308 my $pg = shift;
309 my $opts = $_[0] // {};
310
311 my $zcmd = $self->$zoneCmd($opts->{zonename});
312 my @cmd = @{$zcmd->{cmd}};
313 local $ENV{SVCCFG_REPOSITORY} = $zcmd->{zpath}
314 . '/root/etc/svc/repository.db' if $zcmd->{zpath};
315
316 push @cmd, ($SVCCFG, '-s', $fmri, 'delpg', $pg);
317 print STDERR '# ' . join(' ', @cmd) . "\n" if $self->{debug};
318 system(@cmd) and die "ERROR: cannot delete property group from $fmri\n";
319 }
320  
321 sub setProperty {
322 my $self = shift;
323 my $fmri = shift;
324 my $property = shift;
325 my $value = shift;
326 my $type = shift;
327 my $opts = $_[0] // {};
328
329 my $zcmd = $self->$zoneCmd($opts->{zonename});
330 my @cmd = @{$zcmd->{cmd}};
331 local $ENV{SVCCFG_REPOSITORY} = $zcmd->{zpath}
332 . '/root/etc/svc/repository.db' if $zcmd->{zpath};
333  
334 # guess property type if not provided
335 $type || do {
336 $type = 'astring';
337  
338 for ($value){
339 /^\d+$/ && do {
340 $type = 'count';
341 last;
342 };
343  
344 /^(?:true|false)$/i && do {
345 $type = 'boolean';
346 last;
347 };
348 }
349 };
350  
351 push @cmd, $self->propertyExists($fmri, $property, $opts) ?
352 ($SVCCFG, '-s', $fmri, 'setprop', $property, '=',
353 $zcmd->{shellquote} . $value . $zcmd->{shellquote})
354 : ($SVCCFG, '-s', $fmri, 'addpropvalue', $property, "$type:",
355 $zcmd->{shellquote} . $value . $zcmd->{shellquote});
356 print STDERR '# ' . join(' ', @cmd) . "\n" if $self->{debug};
357 system(@cmd) and die "ERROR: cannot set property $property of $fmri\n";
358 }
359  
360 sub setProperties {
361 my $self = shift;
362 my $fmri = shift;
363 my $properties = shift;
364 my $opts = shift;
365  
366 for my $key (keys %$properties){
367 $self->setProperty($fmri, $key, $properties->{$key}, undef, $opts)
368 }
369 }
370  
371 sub getProperties {
372 my $self = shift;
373 my $fmri = shift;
374 my $pg = shift;
375 my $opts = $_[0] // {};
376
377 my $zcmd = $self->$zoneCmd($opts->{zonename});
378 my @cmd = @{$zcmd->{cmd}};
379 local $ENV{SVCCFG_REPOSITORY} = $zcmd->{zpath}
380 . '/root/etc/svc/repository.db' if $zcmd->{zpath};
381  
382 my $properties = {};
383  
384 push @cmd, ($SVCCFG, '-s', $fmri, 'listprop', $pg);
385  
386 print STDERR '# ' . join(' ', @cmd) . "\n" if $self->{debug};
387 open my $props, '-|', @cmd
388 or die "ERROR: cannot get properties of FMRI '$fmri'\n";
389  
390 while (<$props>){
391 chomp;
392 my ($name, $type, $value) = split /\s+/, $_, 3;
393 next if $name eq $pg;
394 #remove quotes
395 $value =~ s/^"|"$//g;
396 $properties->{$name} = $value;
397  
398 }
399
400 return $properties;
401 }
402  
403 sub setFMRIProperties {
404 my $self = shift;
405 my $fmri = shift;
406 my $properties = shift;
407 my $opts = $_[0] // {};
408
409 $self->addFMRI($fmri, $opts) if !$self->fmriExists($fmri, $opts);
410 # extract property groups
411 my @pg = map { $properties->{$_}->{members} ? $_ : () } keys %$properties;
412  
413 for my $pg (@pg) {
414 $self->addPropertyGroup($fmri, $pg, $properties->{$pg}->{type}, $opts);
415 for my $prop (keys %{$properties->{$pg}->{members}}) {
416 $self->setProperty($fmri, "$pg/$prop",
417 $properties->{$pg}->{members}->{$prop}->{value},
418 $properties->{$pg}->{members}->{$prop}->{type},
419 $opts);
420 }
421 delete $properties->{$pg};
422 }
423  
424 for my $prop (keys %$properties) {
425 $self->setProperty($fmri, $prop,
426 $properties->{$prop}->{value},
427 $properties->{$prop}->{type},
428 $opts);
429 }
430 }
431  
432 sub getFMRIProperties {
433 my $self = shift;
434 my $fmri = shift;
435 my $opts = $_[0] // {};
436
437 my $zcmd = $self->$zoneCmd($opts->{zonename});
438 my @cmd = @{$zcmd->{cmd}};
439 local $ENV{SVCCFG_REPOSITORY} = $zcmd->{zpath}
440 . '/root/etc/svc/repository.db' if $zcmd->{zpath};
441  
442 my $properties = {};
443  
444 push @cmd, ($SVCCFG, '-s', $fmri, 'listprop');
445  
446 print STDERR '# ' . join(' ', @cmd) . "\n" if $self->{debug};
447 open my $props, '-|', @cmd
448 or die "ERROR: cannot get properties of FMRI\n";
449  
450 while (<$props>) {
451 chomp;
452 my ($pg, $prop, $type, $value) = /^(?:([-\w]+)\/)?([-\w]+)\s+([-\w]+)(?:\s+(.+))?$/;
453 next if !$prop || !$type;
454 # remove quotes from $value
455 $value =~ s/^"|"$//g if $value;
456 if ($pg) {
457 $properties->{$pg}->{members}->{$prop}->{type} = $type;
458 $properties->{$pg}->{members}->{$prop}->{value} = $value;
459 }
460 else {
461 $properties->{$prop}->{type} = $type;
462 $properties->{$prop}->{value} = $value // '';
463 }
464 }
465  
466 return $properties;
467 }
468  
469 1;
470  
471 __END__