File Coverage

lib/Rex/Service/Redhat/systemd.pm
Criterion Covered Total %
statement 11 30 36.6
branch 0 4 0.0
condition 0 3 0.0
subroutine 4 7 57.1
pod 0 2 0.0
total 15 46 32.6


line stmt bran cond sub pod time code
1             #
2             # (c) Jan Gehring
3             #
4              
5             package Rex::Service::Redhat::systemd;
6              
7 1     1   14 use v5.12.5;
  1         4  
8 1     1   16 use warnings;
  1         3  
  1         53  
9              
10             our $VERSION = '1.14.2.2'; # TRIAL VERSION
11              
12 1     1   12 use Rex::Helper::Run;
  1         1  
  1         71  
13              
14 1     1   7 use base qw(Rex::Service::Base);
  1         5  
  1         374  
15              
16             sub new {
17 0     0 0   my $that = shift;
18 0   0       my $proto = ref($that) || $that;
19 0           my $self = $proto->SUPER::new(@_);
20              
21 0           bless( $self, $proto );
22              
23             $self->{commands} = {
24 0           start => 'systemctl --no-pager start %s',
25             restart => 'systemctl --no-pager restart %s',
26             stop => 'systemctl --no-pager stop %s',
27             reload => 'systemctl --no-pager reload %s',
28             status => 'systemctl --no-pager is-active %s',
29             ensure_stop => 'systemctl --no-pager disable %s',
30             ensure_start => 'systemctl --no-pager enable %s',
31             service_exists => 'systemctl --no-pager show %s | grep LoadState=loaded',
32             };
33              
34 0           return $self;
35             }
36              
37             # all systemd services must end with .service
38             # so it will be appended if there is no "." in the name.
39             sub _prepare_service_name {
40 0     0     my ( $self, $service ) = @_;
41              
42 0 0         unless ( $service =~ m/\./ ) {
43 0           $service .= ".service";
44             }
45              
46 0           $self->SUPER::_prepare_service_name($service);
47              
48 0           return $service;
49             }
50              
51             sub action {
52 0     0 0   my ( $self, $service, $action ) = @_;
53              
54 0           my $ret_val;
55             eval {
56 0           i_run "systemctl --no-pager $action $service >/dev/null", nohup => 1;
57 0           $ret_val = 1;
58 0           1;
59 0 0         } or do {
60 0           $ret_val = 0;
61             };
62              
63 0           return $ret_val;
64             }
65              
66             1;