lib/Ubic/Cmd.pm

filter: Statementstmt 81.2% 173 / 213 Branchbran 67.6% 46 / 68 Conditioncond 58.8% 20 / 34 MC/DCmcdc 14.2% 3 / 21 Subroutinesub 78.9% 30 / 38 Podpod 100% 12 / 12 totaltotal 73.5% 284 / 386 SCAR 44.2
CC
49
Cov
76%
_run_impl
14.9
__ANON__
10.2
_check_multiop
9.0
CRAP 82.7
« lib/Ubic.pm ↑ Summary blib/lib/Ubic/Cmd/Results.pm »
1 package Ubic::Cmd;
2 40 $Ubic::Cmd::VERSION = '1.59';
3 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # ABSTRACT: ubic methods with pretty printing.
7  
8  
9 40 ▶use Params::Validate qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 40 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 40 ▶use List::MoreUtils qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 40 ▶use List::Util qw(max);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 40 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 40 ▶use Ubic;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 40 ▶use Ubic::Result qw(result);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 40 ▶use Ubic::Cmd::Results;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 sub new {
19 38 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
20 38 my $self = validate(@_, {});
21 38 return bless $self => $class;
22 }
23  
24  
25 40 our $SINGLETON;
26 sub _obj {
27 213 ▶ my ($param) = validate_pos(@_, 1);
Subroutine _obj: called CC=4 SCAR=14.2
28 213 ▶ if (blessed($param)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($param)) 172 41 213
29 172 return $param;
30 }
31 41 ▶ if ($param eq 'Ubic::Cmd') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "Ubic::Cmd") 41 0 41
32 # method called as a class method => singleton
33 41 ▶ $SINGLETON ||= Ubic::Cmd->new();
Condition66%2/3
$Ubic::Cmd::SINGLETON ||= "Ubic::Cmd"->new
l!l&&r!l&&!r
3380
MC/DC0%0/2
$Ubic::Cmd::SINGLETON ||= "Ubic::Cmd"->new
$Ubic::Cmd::SINGLETON "Ubic::Cmd"->new
Truth table66%2/3
A ||= B
A= $Ubic::Cmd::SINGLETONB= "Ubic::Cmd"->new
ABresult
1X1
011
000
34 41 return $SINGLETON;
35 }
36 0 die "Unknown argument '$param'";
37 }
38  
39  
40 sub _any_method {
41 28 ▶ my $self = shift;
Subroutine _any_method: called CC=2 SCAR=7.2
42 28 my $params = validate(@_, {
43 service => 1,
44 results => 0,
45 action => 1, # Starting/Stopping/...
46 method => 1,
47 enabled_only => 0,
48 });
49 28 my ($service, $results, $action, $method, $enabled_only) = @$params{qw/ service results action method enabled_only /};
50 28 ▶ $results ||= Ubic::Cmd::Results->new;
Condition66%2/3
$results ||= "Ubic::Cmd::Results"->new
l!l&&r!l&&!r
2530
MC/DC0%0/2
$results ||= "Ubic::Cmd::Results"->new
$results "Ubic::Cmd::Results"->new
Truth table66%2/3
A ||= B
A= $resultsB= "Ubic::Cmd::Results"->new
ABresult
1X1
011
000
51  
52 $self->traverse($service, sub {
53 52 ▶ my $service = shift;
Subroutine __ANON__: called CC=5 SCAR=17.2
54 52 my $name = $service->full_name;
55 52 ▶ if ($enabled_only and not Ubic->is_enabled($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($enabled_only and not "Ubic"->is_enabled($name)) 0 52 52
Condition66%2/3
$enabled_only and not "Ubic"->is_enabled($name)
!ll&&!rl&&r
4660
MC/DC0%0/2
$enabled_only and not "Ubic"->is_enabled($name)
$enabled_only not "Ubic"->is_enabled($name)
Truth table66%2/3
A and not(B)
A= $enabled_onlyB= not "Ubic"->is_enabled($name)
ABresult
0X0
100
111
56 0 print "$name is down\n";
57 0 $results->add(result('down'));
58 0 return;
59 }
60 52 print "$action $name... ";
61 52 my $result = eval { Ubic->$method($name) };
62 43 ▶ $result ||= result($@);
Condition66%2/3
$result ||= result($@)
l!l&&r!l&&!r
3670
MC/DC0%0/2
$result ||= result($@)
$result result($@)
Truth table66%2/3
A ||= B
A= $resultB= result($@)
ABresult
1X1
011
000
63 43 ▶ $results->print($result, $@ ? 'bad' : ());
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 7 36 43
64 28 });
65 19 return $results;
66 }
67  
68 sub start {
69 15 ▶ my $self = _obj(shift);
Subroutine start: called CC=1 SCAR=0.0
Pod documented
70 15 return $self->_any_method({
71 service => shift,
72 results => shift,
73 action => 'Starting',
74 method => 'start',
75 });
76 }
77  
78  
79 sub stop {
80 7 ▶ my $self = _obj(shift);
Subroutine stop: called CC=1 SCAR=0.0
Pod documented
81 7 return $self->_any_method({
82 service => shift,
83 results => shift,
84 action => 'Stopping',
85 method => 'stop',
86 });
87 }
88  
89 sub restart {
90 0 ▶ my $self = _obj(shift);
Subroutine restart: not called CC=1 SCAR=6.9
Pod documented
91 0 return $self->_any_method({
92 service => shift,
93 results => shift,
94 action => 'Restarting',
95 method => 'restart',
96 });
97 }
98  
99 sub try_restart {
100 0 ▶ my $self = _obj(shift);
Subroutine try_restart: not called CC=1 SCAR=6.9
Pod documented
101 0 return $self->_any_method({
102 service => shift,
103 results => shift,
104 action => 'Restarting',
105 method => 'try_restart',
106 enabled_only => 1,
107 });
108 }
109  
110 sub reload {
111 0 ▶ my $self = _obj(shift);
Subroutine reload: not called CC=1 SCAR=6.9
Pod documented
112 0 return $self->_any_method({
113 service => shift,
114 results => shift,
115 action => 'Reloading',
116 method => 'reload',
117 enabled_only => 1,
118 });
119 }
120  
121 sub force_reload {
122 6 ▶ my $self = _obj(shift);
Subroutine force_reload: called CC=1 SCAR=0.0
Pod documented
123 6 return $self->_any_method({
124 service => shift,
125 results => shift,
126 action => 'Reloading',
127 method => 'force_reload',
128 enabled_only => 1,
129 });
130 }
131  
132 sub do_custom_command {
133 1 ▶ my $self = _obj(shift);
Subroutine do_custom_command: called CC=4 SCAR=15.1
Pod documented
134 1 my $service = shift;
135 1 my $command = shift;
136 1 ▶ my $results = shift || Ubic::Cmd::Results->new;
Condition33%1/3
shift() || 'Ubic::Cmd::Results'->new
l!l&&r!l&&!r
010
MC/DC0%0/2
shift() || 'Ubic::Cmd::Results'->new
shift() 'Ubic::Cmd::Results'->new
Truth table33%1/3
A || B
A= shift()B= 'Ubic::Cmd::Results'->new
ABresult
1X1
011
000
137  
138 1 my $count = 0;
139 1 my $error = 0;
140 $self->traverse($service, sub {
141 1 ▶ my $service = shift;
Subroutine __ANON__: called CC=2 SCAR=7.0
142 1 my $name = $service->full_name;
143  
144 # Imagine we have multiservice X with subservices X.A, X.B and X.C.
145 # X may want to support custom command CC by implementing it in X.A and X.B but not in X.C.
146 # In this case X.A->CC and X.B->CC will be called, and X.C will be skipped.
147 1 ▶ if (grep { $_ eq $command } $service->custom_commands) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq $command;} $service->custom_commands) 1 0 1
148 1 print "Running $command for $name... ";
149 try {
150 1 ▶ Ubic->do_custom_command($name, $command);
Subroutine __ANON__: called CC=1 SCAR=0.0
151 1 $results->print_good("ok\n");
152 } catch {
153 0 ▶ $results->print(result('unknown', "failed: $_"), 'bad');
Subroutine __ANON__: not called CC=1 SCAR=6.9
154 1 };
155 1 $count++;
156 }
157 1 });
158 1 ▶ unless ($count) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($count) 0 1 1
159 # But if none of X subservices support our custom command, something is obviously wrong.
160 0 ▶ if ($service->isa('Ubic::Multiservice')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($service->isa('Ubic::Multiservice')) { } 0 0 0
161 0 die "None of ".$service->full_name." subservices support $command";
162 }
163 else {
164 # it is unlikely that this error will happen, because we already checked that $service supports $command
165 0 die "$command unsupported";
166 }
167 }
168  
169 # TODO - what if X want to implement custom command itself?
170 # should custom commands have different types, "try to call me in each subservice" and "call me for multiservice itself"?
171  
172 1 return;
173 }
174  
175 sub usage {
176 4 ▶ my $self = _obj(shift);
Subroutine usage: called CC=1 SCAR=0.0
Pod documented
177 4 my $command = shift;
178 4 print STDERR "Unknown command '$command'. See 'ubic help'.\n";
179 4 exit(2); # or exit(3)? see LSB for details
180 }
181  
182  
183 sub traverse($$$) {
184 69 ▶ my $self = _obj(shift);
Subroutine traverse: called CC=7 SCAR=19.5
Pod documented
185 69 my ($service, $callback, $indent) = @_;
186 69 ▶ $indent ||= 0;
Condition100%2/2
$indent ||= 0
l!l
1455
MC/DC100%1/1
$indent ||= 0
$indent
Truth table100%2/2
A
A= $indent
Aresult
11
00
187  
188 69 ▶ if (not defined $service) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $service) { } 0 69 69
elsif (not builtin::blessed($service)) { } 12 57 69
189 0 $service = Ubic->root_service;
190 }
191 elsif (not blessed($service)) {
192 12 $service = Ubic->service($service);
193 }
194 69 my $name = $service->full_name;
195  
196 69 ▶ if ($service->isa('Ubic::Multiservice')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($service->isa('Ubic::Multiservice')) { } 8 61 69
197 8 ▶ if ($service->full_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($service->full_name) 6 2 8
198 6 print ' ' x $indent, $service->full_name, "\n";
199 6 $indent = $indent + 4;
200 }
201 8 for my $subservice ($service->services) {
202 32 $self->traverse($subservice, $callback, $indent); # FIXME - remember result
203 }
204 }
205 else {
206 61 print(' ' x $indent);
207 61 return $callback->($service, $indent);
208 }
209 }
210  
211 sub print_status($$;$$) {
212 8 ▶ my $self = _obj(shift);
Subroutine print_status: called CC=4 SCAR=15.0
Pod documented
213 8 my $service = shift;
214 8 my $force_cached = shift;
215 8 ▶ my $results = shift || Ubic::Cmd::Results->new;
Condition33%1/3
shift() || 'Ubic::Cmd::Results'->new
l!l&&r!l&&!r
800
MC/DC0%0/2
shift() || 'Ubic::Cmd::Results'->new
shift() 'Ubic::Cmd::Results'->new
Truth table33%1/3
A || B
A= shift()B= 'Ubic::Cmd::Results'->new
ABresult
1X1
011
000
216  
217 # TODO - use Credentials instead
218 8 my $user = getpwuid($>);
219 8 ▶ unless (defined $user) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $user) 0 8 8
220 0 die "Can't detect user by uid $>";
221 }
222  
223 8 my $max_offset = 0;
224 $self->traverse($service, sub {
225 8 ▶ my ($service, $indent) = @_;
Subroutine __ANON__: called CC=8 SCAR=23.2
226 8 my $name = $service->full_name;
227 8 print $name;
228  
229 # calculating the number of tabs to separate service name from status
230 # status will be aligned whenever possible without sacrificing the real-time properties
231 # i.e., we add several tabs to align status with previous lines, but following lines can increase the number of tabs if necessary
232 # TODO - there are two possibilities to improve this:
233 # 1) look at the further *simple* services and add tabs:
234 # blah
235 # blah.a off
236 # blah.blahblahblah off
237 # blah.c off
238 # (current implementation wouldn't align "blah.a" line correctly)
239 # this would require the change to traverse() method api, though
240 # 2) pre-compile whole service tree before printing anything
241 # but output speed would suffer
242 8 my $offset = length($name) + $indent;
243 8 ▶ if ($offset < $max_offset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < $max_offset) { } 0 8 8
244 0 print "\t" x (int($max_offset) / 8 - int($offset / 8));
245 }
246 else {
247 8 $max_offset = $offset;
248 }
249 8 print "\t";
250  
251 8 my $enabled = Ubic->is_enabled($name);
252 8 ▶ unless ($enabled) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($enabled) 5 3 8
253 5 print "off\n";
254 5 $results->add(result('down'));
255 5 return;
256 }
257  
258 3 my $status;
259 my $cached;
260 3 ▶ if ($force_cached or ($> and $user ne Ubic->service($name)->user)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($force_cached or $> and $user ne 'Ubic'->service($name)->user) { } 0 3 3
Condition33%2/6
$force_cached or $> and $user ne "Ubic"->service($name)->user
l!l&&r!l&&!r
003
$> and $user ne "Ubic"->service($name)->user
!ll&&!rl&&r
030
MC/DC0%0/3
$force_cached or $> and $user ne "Ubic"->service($name)->user
$force_cached $> $user ne "Ubic"->service($name)->user
Truth table25%1/4
A or B and C
A= $force_cachedB= $>C= $user ne "Ubic"->service($name)->user
ABCresult
1XX1
0111
00X0
0100
261 0 $status = Ubic->cached_status($name);
262 0 $cached = 1;
263 }
264 else {
265 3 $status = eval { Ubic->status($name) };
266 3 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 3 3
267 0 $status = result($@);
268 }
269 }
270 3 ▶ if ($status->status eq 'running') {
Branch: 1 branch
BranchTrueFalseTotal
if ($status->status eq 'running') { } 3 0 3
271 3 $results->print($status);
272 }
273 else {
274 0 $results->print($status, 'bad'); # up and not running is always bad
275 }
276 8 });
277  
278 # TODO - print actual uplevel service's status, it can be service-specific
279 8 ▶ if (any { $_->status ne 'running' and $_->status ne 'down' } $results->results) {
Subroutine __ANON__: called CC=2 SCAR=7.0
Branch: 2 branches
BranchTrueFalseTotal
if (&any(sub { $_->status ne "running" and $_->status ne "down"; } , $results->results)) 0 8 8
$_->status ne "running" and $_->status ne "down" 5 3 8
280 0 $results->exit_code(3); # some services are not running when they should be
281 }
282 8 return $results;
283 }
284  
285 sub run {
286 37 ▶ my $self = _obj(shift);
Subroutine run: called CC=3 SCAR=11.0
Pod documented
287 37 my $params = validate(@_, {
288 name => 1,
289 command => { type => SCALAR },
290 force => 0,
291 });
292 37 my @names;
293 37 ▶ if (ref $params->{name} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params->{'name'} eq 'ARRAY') { } 33 4 37
294 33 @names = @{$params->{name}};
295 }
296 else {
297 4 @names = ($params->{name});
298 }
299  
300 37 my $command = $params->{command};
301  
302 37 my $results = Ubic::Cmd::Results->new;
303 37 for my $name (@names) {
304 39 $self->_run_impl({ name => $name, command => $command, force => $params->{force}, results => $results });
305 }
306 22 exit $results->exit_code;
307 }
308  
309 sub _check_multiop {
310 27 ▶ my $self = _obj(shift);
Subroutine _check_multiop: called CC=8 SCAR=22.0
311 27 my ($service, $command, $force) = validate_pos(@_, 1, 1, 1);
312  
313 27 ▶ return unless $service->isa('Ubic::Multiservice');
Branch: 1 branch
BranchTrueFalseTotal
unless $service->isa("Ubic::Multiservice") 23 4 27
314  
315 4 ▶ my $screen_name = $service->name || 'root';
Condition100%2/2
$service->name || 'root'
l!l
13
MC/DC100%1/1
$service->name || 'root'
$service->name
Truth table100%2/2
A
A= $service->name
Aresult
11
00
316 4 my $multiop = $service->multiop;
317 4 ▶ if ($multiop eq 'forbidden') {
Branch: 3 branches
BranchTrueFalseTotal
if ($multiop eq 'forbidden') { } 0 4 4
elsif ($multiop eq 'protected') { } 4 0 4
elsif ($multiop ne 'allowed') { } 0 0 0
318 0 die "$screen_name multiservice forbids $command\n";
319 }
320 elsif ($multiop eq 'protected') {
321 4 ▶ unless ($force) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($force) 2 2 4
322 2 ▶ die "Use --force option if you want to $command all "
Branch: 1 branch
BranchTrueFalseTotal
$service->name ? : 1 1 2
323 .($service->name ? $service->name.' ' : '')."services\n";
324 }
325 }
326 elsif ($multiop ne 'allowed') {
327 0 die "$screen_name has invalid multiop value '$multiop'\n";
328 }
329 }
330  
331 # run and modify results object
332 sub _run_impl {
333 39 ▶ my $self = _obj(shift);
Subroutine _run_impl: called CC=10 SCAR=27.0
334 39 my $params = validate(@_, {
335 name => { type => SCALAR | UNDEF },
336 command => { type => SCALAR },
337 results => { isa => 'Ubic::Cmd::Results' },
338 force => 0,
339 });
340 39 my $command = $params->{command};
341 39 my $name = $params->{name};
342 39 my $results = $params->{results};
343  
344 39 ▶ if ($command eq 'status' or $command eq 'cached-status') {
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq "status" or $command eq "cached-status") 8 31 39
Condition66%2/3
$command eq "status" or $command eq "cached-status"
l!l&&r!l&&!r
8031
MC/DC50%1/2
$command eq "status" or $command eq "cached-status"
$command eq "status" $command eq "cached-status"
Truth table66%2/3
A or B
A= $command eq "status"B= $command eq "cached-status"
ABresult
1X1
011
000
345 8 my $force_cached;
346 8 ▶ if ($command eq 'cached-status') {
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq "cached-status") 0 8 8
347 0 $force_cached = 1;
348 }
349 try {
350 8 ▶ $self->print_status($name, $force_cached, $results);
Subroutine __ANON__: called CC=1 SCAR=0.0
351 }
352 catch {
353 0 ▶ print STDERR $_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
354 0 $results->exit_code(4); # internal error
355 8 };
356 8 return;
357 }
358  
359 31 ▶ if ($name and not Ubic->root_service->has_service($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name and not "Ubic"->root_service->has_service($name)) 0 31 31
Condition66%2/3
$name and not "Ubic"->root_service->has_service($name)
!ll&&!rl&&r
4270
MC/DC0%0/2
$name and not "Ubic"->root_service->has_service($name)
$name not "Ubic"->root_service->has_service($name)
Truth table66%2/3
A and not(B)
A= $nameB= not "Ubic"->root_service->has_service($name)
ABresult
0X0
100
111
360 0 print STDERR "Service '$name' not found\n";
361 0 $results->exit_code(5);
362 0 return;
363 }
364  
365 # FIXME - we're constructing service and drop it to reconstruct later
366 # but we need to construct service to check it's custom commands
367 31 ▶ my $service = $name ? Ubic->service($name) : Ubic->root_service;
Branch: 1 branch
BranchTrueFalseTotal
$name ? : 27 4 31
368  
369 # yes, custom "start" command will override default "start" command, although it's not very useful :)
370 # but we need this because of current "logrotate" hack
371 31 ▶ if (grep { $_ eq $command } $service->custom_commands) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq $command;} $service->custom_commands) 0 31 31
372 0 $self->_check_multiop($service, $command, $params->{force});
373 try {
374 0 ▶ $self->do_custom_command($service, $command, $results);
Subroutine __ANON__: not called CC=1 SCAR=6.9
375 }
376 catch {
377 0 ▶ print STDERR "'$name $command' error: $_\n";
Subroutine __ANON__: not called CC=1 SCAR=6.9
378 0 $results->exit_code(1); # generic error, TODO - more lsb-specific errors?
379 0 };
380 0 return;
381 }
382  
383 31 ▶ $command = "force_reload" if $command eq "logrotate"; #FIXME: non LSB command!
Branch: 1 branch
BranchTrueFalseTotal
if $command eq "logrotate" 6 25 31
384  
385 31 my $method = $command;
386 31 $method =~ s/-/_/g;
387 31 ▶ unless (grep { $_ eq $method } qw/ start stop restart try_restart reload force_reload /) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $method;} "start", "stop", "restart", "try_restart", "reload", "force_reload") 4 27 31
388 4 $self->usage($command);
389 }
390  
391 27 $self->_check_multiop($service, $command, $params->{force});
392 try {
393 25 ▶ $self->$method($service, $results);
Subroutine __ANON__: called CC=1 SCAR=0.0
394 }
395 catch {
396 0 ▶ if ($name) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if ($name) { } 0 0 0
397 0 print STDERR "'$name $method' error: $_\n";
398 }
399 else {
400 0 print STDERR "'$method' error: $_\n";
401 }
402 0 $results->exit_code(1); # generic error, TODO - more lsb-specific errors?
403 25 };
404 16 return;
405 }
406  
407  
408 40 1;
409  
410 __END__
« lib/Ubic.pm ↑ Summary blib/lib/Ubic/Cmd/Results.pm »