From 842c705778853021da330244f0dff5a1a6e420e4 Mon Sep 17 00:00:00 2001 From: Doug Bell Date: Fri, 5 Jan 2018 14:38:42 -0600 Subject: [PATCH] add service task to restart services on hosts --- Rexfile | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/Rexfile b/Rexfile index b14a085..bddb2ab 100644 --- a/Rexfile +++ b/Rexfile @@ -1010,6 +1010,46 @@ task update_legacy_config => }; }; +=head2 service + + rex service [--command=] [--services=,...] [--force=1] + +Restart services on the machine(s). C<--command> is the command to run +and defaults to C (use C to get service status). +C<--services> is a comma-separated list of services to command (defaults +to C<*>, all services). Use C<--force=1> to force-kill any service that +is not restarted/stopped. + +=cut + +task service => + group => [qw( all )], + sub { + my ( $opt ) = @_; + my $command = $opt->{command} || 'restart'; + my @services = split( /,/, $opt->{services} ) || '*'; + + my $services = join " ", map { '~cpantesters/service/' . $_ } @services; + my $line = sprintf( 'sudo -u cpantesters sv %s %s', $command, $services ); + Rex::Logger::info( 'Running: ' . $line ); + my @out = run $line; + Rex::Logger::info( $_ ) for @out; + + if ( $command =~ /restart|stop/ && $opt->{force} ) { + for my $out ( grep { /^timeout:/ } @out ) { + my ( $status, $state, $path, $pid ) = $out =~ /^([^:]+):\s+([^:]+)\s+([^:]+):\s+\(pid (\d+)\)/; + my $service = basename $path; + if ( $state eq 'run' && $pid ) { + Rex::Logger::info( 'Force killing ' . $pid ); + run "kill $pid"; + } + else { + Rex::Logger::info( sprintf 'Cannot force kill %s: Service is %s', $service, $state ); + } + } + } + }; + ####################################################################### =head1 Subroutines