| 1 | package Ubic::Cmd; | ||
| 2 | $Ubic::Cmd::VERSION = '1.59'; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | # ABSTRACT: ubic methods with pretty printing. | ||
| 7 | |||
| 8 | |||
| 9 | use Params::Validate qw(:all); | ||
| 10 | use Scalar::Util qw(blessed); | ||
| 11 | use List::MoreUtils qw(any); | ||
| 12 | use List::Util qw(max); | ||
| 13 | use Try::Tiny; | ||
| 14 | use Ubic; | ||
| 15 | use Ubic::Result qw(result); | ||
| 16 | use Ubic::Cmd::Results; | ||
| 17 | |||
| 18 | sub new { | ||
| 19 | my $class = shift; | ||
| 20 | my $self = validate(@_, {}); | ||
| 21 | return bless $self => $class; | ||
| 22 | } | ||
| 23 | |||
| 24 | |||
| 25 | our $SINGLETON; | ||
| 26 | sub _obj { | ||
| 27 | my ($param) = validate_pos(@_, 1); | ||
| 28 | if (blessed($param)) { | ||
| 29 | return $param; | ||
| 30 | } | ||
| 31 | if ($param eq 'Ubic::Cmd') { | ||
| 32 | # method called as a class method => singleton | ||
| 33 | $SINGLETON ||= Ubic::Cmd->new(); | ||
| 34 | return $SINGLETON; | ||
| 35 | } | ||
| 36 | die "Unknown argument '$param'"; | ||
| 37 | } | ||
| 38 | |||
| 39 | |||
| 40 | sub _any_method { | ||
| 41 | my $self = shift; | ||
| 42 | my $params = validate(@_, { | ||
| 43 | service => 1, | ||
| 44 | results => 0, | ||
| 45 | action => 1, # Starting/Stopping/... | ||
| 46 | method => 1, | ||
| 47 | enabled_only => 0, | ||
| 48 | }); | ||
| 49 | my ($service, $results, $action, $method, $enabled_only) = @$params{qw/ service results action method enabled_only /}; | ||
| 50 | $results ||= Ubic::Cmd::Results->new; | ||
| 51 | |||
| 52 | $self->traverse($service, sub { | ||
| 53 | my $service = shift; | ||
| 54 | my $name = $service->full_name; | ||
| 55 | if ($enabled_only and not Ubic->is_enabled($name)) { | ||
| 56 | print "$name is down\n"; | ||
| 57 | $results->add(result('down')); | ||
| 58 | return; | ||
| 59 | } | ||
| 60 | print "$action $name... "; | ||
| 61 | my $result = eval { Ubic->$method($name) }; | ||
| 62 | $result ||= result($@); | ||
| 63 | $results->print($result, $@ ? 'bad' : ()); | ||
| 64 | }); | ||
| 65 | return $results; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub start { | ||
| 69 | my $self = _obj(shift); | ||
| 70 | return $self->_any_method({ | ||
| 71 | service => shift, | ||
| 72 | results => shift, | ||
| 73 | action => 'Starting', | ||
| 74 | method => 'start', | ||
| 75 | }); | ||
| 76 | } | ||
| 77 | |||
| 78 | |||
| 79 | sub stop { | ||
| 80 | my $self = _obj(shift); | ||
| 81 | return $self->_any_method({ | ||
| 82 | service => shift, | ||
| 83 | results => shift, | ||
| 84 | action => 'Stopping', | ||
| 85 | method => 'stop', | ||
| 86 | }); | ||
| 87 | } | ||
| 88 | |||
| 89 | sub restart { | ||
| 90 | my $self = _obj(shift); | ||
| 91 | return $self->_any_method({ | ||
| 92 | service => shift, | ||
| 93 | results => shift, | ||
| 94 | action => 'Restarting', | ||
| 95 | method => 'restart', | ||
| 96 | }); | ||
| 97 | } | ||
| 98 | |||
| 99 | sub try_restart { | ||
| 100 | my $self = _obj(shift); | ||
| 101 | 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 | my $self = _obj(shift); | ||
| 112 | 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 | my $self = _obj(shift); | ||
| 123 | 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 | my $self = _obj(shift); | ||
| 134 | my $service = shift; | ||
| 135 | my $command = shift; | ||
| 136 | my $results = shift || Ubic::Cmd::Results->new; | ||
| 137 | |||
| 138 | my $count = 0; | ||
| 139 | my $error = 0; | ||
| 140 | $self->traverse($service, sub { | ||
| 141 | my $service = shift; | ||
| 142 | 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 | if (grep { $_ eq $command } $service->custom_commands) { | ||
| 148 | print "Running $command for $name... "; | ||
| 149 | try { | ||
| 150 | Ubic->do_custom_command($name, $command); | ||
| 151 | $results->print_good("ok\n"); | ||
| 152 | } catch { | ||
| 153 | $results->print(result('unknown', "failed: $_"), 'bad'); | ||
| 154 | }; | ||
| 155 | $count++; | ||
| 156 | } | ||
| 157 | }); | ||
| 158 | unless ($count) { | ||
| 159 | # But if none of X subservices support our custom command, something is obviously wrong. | ||
| 160 | if ($service->isa('Ubic::Multiservice')) { | ||
| 161 | 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 | 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 | return; | ||
| 173 | } | ||
| 174 | |||
| 175 | sub usage { | ||
| 176 | my $self = _obj(shift); | ||
| 177 | my $command = shift; | ||
| 178 | print STDERR "Unknown command '$command'. See 'ubic help'.\n"; | ||
| 179 | exit(2); # or exit(3)? see LSB for details | ||
| 180 | } | ||
| 181 | |||
| 182 | |||
| 183 | sub traverse($$$) { | ||
| 184 | my $self = _obj(shift); | ||
| 185 | my ($service, $callback, $indent) = @_; | ||
| 186 | $indent ||= 0; | ||
| 187 | |||
| 188 | if (not defined $service) { | ||
| 189 | $service = Ubic->root_service; | ||
| 190 | } | ||
| 191 | elsif (not blessed($service)) { | ||
| 192 | $service = Ubic->service($service); | ||
| 193 | } | ||
| 194 | my $name = $service->full_name; | ||
| 195 | |||
| 196 | if ($service->isa('Ubic::Multiservice')) { | ||
| 197 | if ($service->full_name) { | ||
| 198 | print ' ' x $indent, $service->full_name, "\n"; | ||
| 199 | $indent = $indent + 4; | ||
| 200 | } | ||
| 201 | for my $subservice ($service->services) { | ||
| 202 | $self->traverse($subservice, $callback, $indent); # FIXME - remember result | ||
| 203 | } | ||
| 204 | } | ||
| 205 | else { | ||
| 206 | print(' ' x $indent); | ||
| 207 | return $callback->($service, $indent); | ||
| 208 | } | ||
| 209 | } | ||
| 210 | |||
| 211 | sub print_status($$;$$) { | ||
| 212 | my $self = _obj(shift); | ||
| 213 | my $service = shift; | ||
| 214 | my $force_cached = shift; | ||
| 215 | my $results = shift || Ubic::Cmd::Results->new; | ||
| 216 | |||
| 217 | # TODO - use Credentials instead | ||
| 218 | my $user = getpwuid($>); | ||
| 219 | unless (defined $user) { | ||
| 220 | die "Can't detect user by uid $>"; | ||
| 221 | } | ||
| 222 | |||
| 223 | my $max_offset = 0; | ||
| 224 | $self->traverse($service, sub { | ||
| 225 | my ($service, $indent) = @_; | ||
| 226 | my $name = $service->full_name; | ||
| 227 | 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 | my $offset = length($name) + $indent; | ||
| 243 | if ($offset < $max_offset) { | ||
| 244 | print "\t" x (int($max_offset) / 8 - int($offset / 8)); | ||
| 245 | } | ||
| 246 | else { | ||
| 247 | $max_offset = $offset; | ||
| 248 | } | ||
| 249 | print "\t"; | ||
| 250 | |||
| 251 | my $enabled = Ubic->is_enabled($name); | ||
| 252 | unless ($enabled) { | ||
| 253 | print "off\n"; | ||
| 254 | $results->add(result('down')); | ||
| 255 | return; | ||
| 256 | } | ||
| 257 | |||
| 258 | my $status; | ||
| 259 | my $cached; | ||
| 260 | if ($force_cached or ($> and $user ne Ubic->service($name)->user)) { | ||
| 261 | $status = Ubic->cached_status($name); | ||
| 262 | $cached = 1; | ||
| 263 | } | ||
| 264 | else { | ||
| 265 | $status = eval { Ubic->status($name) }; | ||
| 266 | if ($@) { | ||
| 267 | $status = result($@); | ||
| 268 | } | ||
| 269 | } | ||
| 270 | if ($status->status eq 'running') { | ||
| 271 | $results->print($status); | ||
| 272 | } | ||
| 273 | else { | ||
| 274 | $results->print($status, 'bad'); # up and not running is always bad | ||
| 275 | } | ||
| 276 | }); | ||
| 277 | |||
| 278 | # TODO - print actual uplevel service's status, it can be service-specific | ||
| 279 | if (any { $_->status ne 'running' and $_->status ne 'down' } $results->results) { | ||
| 280 | $results->exit_code(3); # some services are not running when they should be | ||
| 281 | } | ||
| 282 | return $results; | ||
| 283 | } | ||
| 284 | |||
| 285 | sub run { | ||
| 286 | my $self = _obj(shift); | ||
| 287 | my $params = validate(@_, { | ||
| 288 | name => 1, | ||
| 289 | command => { type => SCALAR }, | ||
| 290 | force => 0, | ||
| 291 | }); | ||
| 292 | my @names; | ||
| 293 | if (ref $params->{name} eq 'ARRAY') { | ||
| 294 | @names = @{$params->{name}}; | ||
| 295 | } | ||
| 296 | else { | ||
| 297 | @names = ($params->{name}); | ||
| 298 | } | ||
| 299 | |||
| 300 | my $command = $params->{command}; | ||
| 301 | |||
| 302 | my $results = Ubic::Cmd::Results->new; | ||
| 303 | for my $name (@names) { | ||
| 304 | $self->_run_impl({ name => $name, command => $command, force => $params->{force}, results => $results }); | ||
| 305 | } | ||
| 306 | exit $results->exit_code; | ||
| 307 | } | ||
| 308 | |||
| 309 | sub _check_multiop { | ||
| 310 | my $self = _obj(shift); | ||
| 311 | my ($service, $command, $force) = validate_pos(@_, 1, 1, 1); | ||
| 312 | |||
| 313 | return unless $service->isa('Ubic::Multiservice'); | ||
| 314 | |||
| 315 | my $screen_name = $service->name || 'root'; | ||
| 316 | my $multiop = $service->multiop; | ||
| 317 | if ($multiop eq 'forbidden') { | ||
| 318 | die "$screen_name multiservice forbids $command\n"; | ||
| 319 | } | ||
| 320 | elsif ($multiop eq 'protected') { | ||
| 321 | unless ($force) { | ||
| 322 | die "Use --force option if you want to $command all " | ||
| 323 | .($service->name ? $service->name.' ' : '')."services\n"; | ||
| 324 | } | ||
| 325 | } | ||
| 326 | elsif ($multiop ne 'allowed') { | ||
| 327 | die "$screen_name has invalid multiop value '$multiop'\n"; | ||
| 328 | } | ||
| 329 | } | ||
| 330 | |||
| 331 | # run and modify results object | ||
| 332 | sub _run_impl { | ||
| 333 | my $self = _obj(shift); | ||
| 334 | my $params = validate(@_, { | ||
| 335 | name => { type => SCALAR | UNDEF }, | ||
| 336 | command => { type => SCALAR }, | ||
| 337 | results => { isa => 'Ubic::Cmd::Results' }, | ||
| 338 | force => 0, | ||
| 339 | }); | ||
| 340 | my $command = $params->{command}; | ||
| 341 | my $name = $params->{name}; | ||
| 342 | my $results = $params->{results}; | ||
| 343 | |||
| 344 | if ($command eq 'status' or $command eq 'cached-status') { | ||
| 345 | my $force_cached; | ||
| 346 | if ($command eq 'cached-status') { | ||
| 347 | $force_cached = 1; | ||
| 348 | } | ||
| 349 | try { | ||
| 350 | $self->print_status($name, $force_cached, $results); | ||
| 351 | } | ||
| 352 | catch { | ||
| 353 | print STDERR $_; | ||
| 354 | $results->exit_code(4); # internal error | ||
| 355 | }; | ||
| 356 | return; | ||
| 357 | } | ||
| 358 | |||
| 359 | if ($name and not Ubic->root_service->has_service($name)) { | ||
| 360 | print STDERR "Service '$name' not found\n"; | ||
| 361 | $results->exit_code(5); | ||
| 362 | 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 | my $service = $name ? Ubic->service($name) : Ubic->root_service; | ||
| 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 | if (grep { $_ eq $command } $service->custom_commands) { | ||
| 372 | $self->_check_multiop($service, $command, $params->{force}); | ||
| 373 | try { | ||
| 374 | $self->do_custom_command($service, $command, $results); | ||
| 375 | } | ||
| 376 | catch { | ||
| 377 | print STDERR "'$name $command' error: $_\n"; | ||
| 378 | $results->exit_code(1); # generic error, TODO - more lsb-specific errors? | ||
| 379 | }; | ||
| 380 | return; | ||
| 381 | } | ||
| 382 | |||
| 383 | $command = "force_reload" if $command eq "logrotate"; #FIXME: non LSB command! | ||
| 384 | |||
| 385 | my $method = $command; | ||
| 386 | $method =~ s/-/_/g; | ||
| 387 | unless (grep { $_ eq $method } qw/ start stop restart try_restart reload force_reload /) { | ||
| 388 | $self->usage($command); | ||
| 389 | } | ||
| 390 | |||
| 391 | $self->_check_multiop($service, $command, $params->{force}); | ||
| 392 | try { | ||
| 393 | $self->$method($service, $results); | ||
| 394 | } | ||
| 395 | catch { | ||
| 396 | if ($name) { | ||
| 397 | print STDERR "'$name $method' error: $_\n"; | ||
| 398 | } | ||
| 399 | else { | ||
| 400 | print STDERR "'$method' error: $_\n"; | ||
| 401 | } | ||
| 402 | $results->exit_code(1); # generic error, TODO - more lsb-specific errors? | ||
| 403 | }; | ||
| 404 | return; | ||
| 405 | } | ||
| 406 | |||
| 407 | |||
| 408 | 1; | ||
| 409 | |||
| 410 | __END__ |