File Coverage

lib/Rex/Service/Base.pm
Criterion Covered Total %
statement 11 93 11.8
branch 0 26 0.0
condition 0 9 0.0
subroutine 4 17 23.5
pod 0 10 0.0
total 15 155 9.6


line stmt bran cond sub pod time code
1             #
2             # (c) Jan Gehring
3             #
4              
5             package Rex::Service::Base;
6              
7 1     1   13 use v5.12.5;
  1         5  
8 1     1   5 use warnings;
  1         2  
  1         40  
9              
10             our $VERSION = '1.14.2.3'; # TRIAL VERSION
11              
12 1     1   7 use Rex::Helper::Run;
  1         2  
  1         67  
13 1     1   6 use Rex::Logger;
  1         2  
  1         6  
14              
15             my $known_services = {};
16              
17             sub new {
18 0     0 0   my $that = shift;
19 0   0       my $proto = ref($that) || $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     0 0   sub get_output { shift->{__cmd_output__}; }
30              
31             sub _prepare_service_name {
32 0     0     my ( $self, $service_name ) = @_;
33              
34 0 0 0       if ( !$self->service_exists($service_name)
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             sub _filter_options {
44 0     0     my ( $self, $service, $options ) = @_;
45              
46 0           for my $key (qw/start stop status restart reload ensure_stop ensure_start/) {
47 0 0         if ( exists $options->{$key} ) {
48 0           $known_services->{$service}->{$key} = $options->{$key};
49             }
50             }
51             }
52              
53             sub _execute {
54 0     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 0         } or do {
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             sub start {
69 0     0 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 0         if ( exists $known_services->{$service}->{start} ) {
76 0           $cmd = $known_services->{$service}->{start};
77             }
78              
79 0           return $self->_execute($cmd);
80             }
81              
82             sub restart {
83 0     0 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 0         if ( exists $known_services->{$service}->{restart} ) {
90 0           $cmd = $known_services->{$service}->{restart};
91             }
92              
93 0           return $self->_execute($cmd);
94             }
95              
96             sub stop {
97 0     0 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 0         if ( exists $known_services->{$service}->{stop} ) {
104 0           $cmd = $known_services->{$service}->{stop};
105             }
106              
107 0           return $self->_execute($cmd);
108             }
109              
110             sub reload {
111 0     0 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 0         if ( exists $known_services->{$service}->{reload} ) {
118 0           $cmd = $known_services->{$service}->{reload};
119             }
120              
121 0           return $self->_execute($cmd);
122             }
123              
124             sub status {
125 0     0 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 0         if ( exists $known_services->{$service}->{status} ) {
132 0           $cmd = $known_services->{$service}->{status};
133             }
134              
135 0           return $self->_execute($cmd);
136             }
137              
138             sub ensure {
139 0     0 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 0 0       if ( $what =~ /^stop/ ) {
    0          
146 0           $self->stop( $service, $options );
147 0           my $cmd = sprintf $self->{commands}->{ensure_stop}, $service;
148              
149 0 0         if ( exists $known_services->{$service}->{ensure_stop} ) {
150 0           $cmd = $known_services->{$service}->{ensure_stop};
151             }
152              
153 0           return $self->_execute($cmd);
154             }
155             elsif ( $what =~ /^start/ || $what =~ m/^run/ ) {
156 0           $self->start( $service, $options );
157 0           my $cmd = sprintf $self->{commands}->{ensure_start}, $service;
158              
159 0 0         if ( exists $known_services->{$service}->{ensure_start} ) {
160 0           $cmd = $known_services->{$service}->{ensure_start};
161             }
162              
163 0           return $self->_execute($cmd);
164             }
165             }
166              
167             sub action {
168 0     0 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             sub service_exists {
176 0     0 0   my ( $self, $service ) = @_;
177              
178             # always return true if we can't verify if a service exists
179 0 0         if ( !exists $self->{commands}->{service_exists} ) {
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;