| 1 | # | ||||||||||||||||||||||||||||
| 2 | # (c) Jan Gehring <jan.gehring@gmail.com> | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | package Rex::Service::Base; | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use v5.14.4; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | our $VERSION = '1.16.1'; # VERSION | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Rex::Helper::Run; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Rex::Logger; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | my $known_services = {}; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 18 | 0 | my $that = shift; | |||||||||||||||||||||||||||
| 19 | 0 | ▶ | my $proto = ref($that) || $that; | ||||||||||||||||||||||||||
|
Condition0%0/3
ref $that || $that
MC/DC0%0/2
ref $that || $that
ref $that
$that
Truth table0%0/3
A || B
A= ref $thatB= $that
| |||||||||||||||||||||||||||||
| 20 | 0 | my $self = {@_}; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 0 | bless( $self, $proto ); | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 0 | $self->{__cmd_output__} = ''; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 0 | return $self; | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 0 | ▶ | sub get_output { shift->{__cmd_output__}; } | ||||||||||||||||||||||||||
|
Subroutine
get_output: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | sub _prepare_service_name { | ||||||||||||||||||||||||||
|
Subroutine
_prepare_service_name: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 32 | 0 | my ( $self, $service_name ) = @_; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 0 | ▶ | if ( !$self->service_exists($service_name) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $self->service_exists($service_name) and "Rex::Config"->get_check_service_exists
MC/DC0%0/2
not $self->service_exists($service_name) and "Rex::Config"->get_check_service_exists
not $self->service_exists($service_name)
"Rex::Config"->get_check_service_exists
Truth table0%0/3
not(A) and B
A= not $self->service_exists($service_name)B= "Rex::Config"->get_check_service_exists
| |||||||||||||||||||||||||||||
| 35 | && Rex::Config->get_check_service_exists ) | ||||||||||||||||||||||||||||
| 36 | { | ||||||||||||||||||||||||||||
| 37 | 0 | die "Service $service_name not found."; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 0 | return $service_name; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 0 | ▶ | sub _filter_options { | ||||||||||||||||||||||||||
|
Subroutine
_filter_options: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 44 | 0 | my ( $self, $service, $options ) = @_; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 0 | for my $key (qw/start stop status restart reload ensure_stop ensure_start/) { | |||||||||||||||||||||||||||
| 47 | 0 | ▶ | if ( exists $options->{$key} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 0 | $known_services->{$service}->{$key} = $options->{$key}; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | ▶ | sub _execute { | ||||||||||||||||||||||||||
|
Subroutine
_execute: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 54 | 0 | my ( $self, $cmd ) = @_; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | my $ret_val; | |||||||||||||||||||||||||||
| 57 | eval { | ||||||||||||||||||||||||||||
| 58 | 0 | $self->{__cmd_output__} = i_run $cmd, nohup => 1; | |||||||||||||||||||||||||||
| 59 | 0 | $ret_val = 1; | |||||||||||||||||||||||||||
| 60 | 0 | ▶ | } or do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 0 | $self->{__cmd_output__} = Rex::Commands::last_command_output(); | |||||||||||||||||||||||||||
| 62 | 0 | $ret_val = 0; | |||||||||||||||||||||||||||
| 63 | }; | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | return $ret_val; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | sub start { | ||||||||||||||||||||||||||
|
Subroutine
start: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 69 | 0 | my ( $self, $service, $options ) = @_; | |||||||||||||||||||||||||||
| 70 | 0 | $service = $self->_prepare_service_name($service); | |||||||||||||||||||||||||||
| 71 | 0 | $self->_filter_options( $service, $options ); | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | my $cmd = sprintf $self->{commands}->{start}, $service; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | if ( exists $known_services->{$service}->{start} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 0 | $cmd = $known_services->{$service}->{start}; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 0 | return $self->_execute($cmd); | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 0 | ▶ | sub restart { | ||||||||||||||||||||||||||
|
Subroutine
restart: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 83 | 0 | my ( $self, $service, $options ) = @_; | |||||||||||||||||||||||||||
| 84 | 0 | $service = $self->_prepare_service_name($service); | |||||||||||||||||||||||||||
| 85 | 0 | $self->_filter_options( $service, $options ); | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | my $cmd = sprintf $self->{commands}->{restart}, $service; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | ▶ | if ( exists $known_services->{$service}->{restart} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 0 | $cmd = $known_services->{$service}->{restart}; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 0 | return $self->_execute($cmd); | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | ▶ | sub stop { | ||||||||||||||||||||||||||
|
Subroutine
stop: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 97 | 0 | my ( $self, $service, $options ) = @_; | |||||||||||||||||||||||||||
| 98 | 0 | $service = $self->_prepare_service_name($service); | |||||||||||||||||||||||||||
| 99 | 0 | $self->_filter_options( $service, $options ); | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | my $cmd = sprintf $self->{commands}->{stop}, $service; | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 0 | ▶ | if ( exists $known_services->{$service}->{stop} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 0 | $cmd = $known_services->{$service}->{stop}; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | return $self->_execute($cmd); | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 0 | ▶ | sub reload { | ||||||||||||||||||||||||||
|
Subroutine
reload: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 111 | 0 | my ( $self, $service, $options ) = @_; | |||||||||||||||||||||||||||
| 112 | 0 | $service = $self->_prepare_service_name($service); | |||||||||||||||||||||||||||
| 113 | 0 | $self->_filter_options( $service, $options ); | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | my $cmd = sprintf $self->{commands}->{reload}, $service; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | ▶ | if ( exists $known_services->{$service}->{reload} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 0 | $cmd = $known_services->{$service}->{reload}; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 0 | return $self->_execute($cmd); | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | ▶ | sub status { | ||||||||||||||||||||||||||
|
Subroutine
status: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 125 | 0 | my ( $self, $service, $options ) = @_; | |||||||||||||||||||||||||||
| 126 | 0 | $service = $self->_prepare_service_name($service); | |||||||||||||||||||||||||||
| 127 | 0 | $self->_filter_options( $service, $options ); | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | my $cmd = sprintf $self->{commands}->{status}, $service; | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 0 | ▶ | if ( exists $known_services->{$service}->{status} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 0 | $cmd = $known_services->{$service}->{status}; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 0 | return $self->_execute($cmd); | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 0 | ▶ | sub ensure { | ||||||||||||||||||||||||||
|
Subroutine
ensure: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 139 | 0 | my ( $self, $service, $options ) = @_; | |||||||||||||||||||||||||||
| 140 | 0 | $service = $self->_prepare_service_name($service); | |||||||||||||||||||||||||||
| 141 | 0 | $self->_filter_options( $service, $options ); | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 0 | my $what = $options->{ensure}; | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 0 | ▶ | if ( $what =~ /^stop/ ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 146 | 0 | $self->stop( $service, $options ); | |||||||||||||||||||||||||||
| 147 | 0 | my $cmd = sprintf $self->{commands}->{ensure_stop}, $service; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 0 | ▶ | if ( exists $known_services->{$service}->{ensure_stop} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 0 | $cmd = $known_services->{$service}->{ensure_stop}; | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 0 | return $self->_execute($cmd); | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | ▶ | elsif ( $what =~ /^start/ || $what =~ m/^run/ ) { | |||||||||||||||||||||||||||
|
Condition0%0/3
$what =~ /^start/u or $what =~ /^run/u
MC/DC0%0/2
$what =~ /^start/u or $what =~ /^run/u
$what =~ /^start/u
$what =~ /^run/u
Truth table0%0/3
A or B
A= $what =~ /^start/uB= $what =~ /^run/u
| |||||||||||||||||||||||||||||
| 156 | 0 | $self->start( $service, $options ); | |||||||||||||||||||||||||||
| 157 | 0 | my $cmd = sprintf $self->{commands}->{ensure_start}, $service; | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 0 | ▶ | if ( exists $known_services->{$service}->{ensure_start} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | 0 | $cmd = $known_services->{$service}->{ensure_start}; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 0 | return $self->_execute($cmd); | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 0 | ▶ | sub action { | ||||||||||||||||||||||||||
|
Subroutine
action: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 168 | 0 | my ( $self, $service, $action ) = @_; | |||||||||||||||||||||||||||
| 169 | 0 | $service = $self->_prepare_service_name($service); | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 0 | my $cmd = sprintf $self->{commands}->{action}, $service, $action; | |||||||||||||||||||||||||||
| 172 | 0 | return $self->_execute($cmd); | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 0 | ▶ | sub service_exists { | ||||||||||||||||||||||||||
|
Subroutine
service_exists: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 176 | 0 | my ( $self, $service ) = @_; | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | # always return true if we can't verify if a service exists | ||||||||||||||||||||||||||||
| 179 | 0 | ▶ | if ( !exists $self->{commands}->{service_exists} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | 0 | return 1; | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 0 | my $cmd = sprintf $self->{commands}->{service_exists}, $service; | |||||||||||||||||||||||||||
| 184 | 0 | return $self->_execute($cmd); | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 1 | 1; | |||||||||||||||||||||||||||