File Coverage

blib/lib/SysV/Init/Service.pm
Criterion Covered Total %
statement 54 60 90.0
branch 12 14 85.7
condition n/a
subroutine 14 16 87.5
pod 6 6 100.0
total 86 96 89.5


line stmt bran cond sub pod time code
1             package SysV::Init::Service;
2              
3 6     6   181227 use strict;
  6         10  
  6         174  
4 6     6   20 use warnings;
  6         11  
  6         357  
5              
6 6     6   2692 use Capture::Tiny qw(capture);
  6         166434  
  6         441  
7 6     6   1096 use Class::Utils qw(set_params);
  6         25213  
  6         208  
8 6     6   211 use English qw(-no_match_vars);
  6         15  
  6         38  
9 6     6   2076 use Error::Pure qw(err);
  6         11  
  6         252  
10 6     6   1065 use File::Spec::Functions qw(catfile);
  6         1682  
  6         5369  
11              
12             our $VERSION = 0.07;
13              
14             # Construct.
15             sub new {
16 12     12 1 1344024 my ($class, @params) = @_;
17              
18             # Create object.
19 12         68 my $self = bless {}, $class;
20              
21             # Service.
22 12         64 $self->{'service'} = undef;
23              
24             # Service directory.
25 12         40 $self->{'service_dir'} = '/etc/init.d';
26              
27             # Process parameters.
28 12         3418 set_params($self, @params);
29              
30             # Check for service.
31 10 100       231 if (! defined $self->{'service'}) {
32 1         5 err "Parameter 'service' is required.";
33             }
34 9 100       57 if ($self->{'service'} =~ m/\.sh$/ms) {
35 1         5 err "Service with .sh suffix doesn't possible.";
36             }
37             $self->{'_service_path'} = catfile($self->{'service_dir'},
38 8         76 $self->{'service'});
39 8 100       349 if (! -x $self->{'_service_path'}) {
40 1         8 err "Service '$self->{'service'}' doesn't present.";
41             }
42              
43             # Object.
44 7         105 return $self;
45             }
46              
47             # Get service commands.
48             sub commands {
49 3     3 1 30 my $self = shift;
50             my ($stdout, $stderr, $exit_code) = capture {
51 3     3   1887757 system $self->{'_service_path'};
52 3         222 };
53 3 100       5206 if ($stderr) {
54 1         17 $stdout .= $stderr;
55             }
56 3         13 my @commands;
57 3 100       107 if ($stdout =~ m/{([\w\|\-]+)}/ms) {
    50          
58 2         32 @commands = split m/\|/ms, $1;
59             } elsif ($stdout =~ m/([\w\-]+)\s*$/ms) {
60 1         21 @commands = $1;
61             }
62 3         56 return sort @commands;
63             }
64              
65             # Get service name.
66             sub name {
67 1     1 1 5 my $self = shift;
68 1         3 return $self->{'service'};
69             }
70              
71             # Start service.
72             sub start {
73 0     0 1 0 my $self = shift;
74 0         0 return $self->_service_command('start');
75             }
76              
77             # Get status.
78             sub status {
79 2     2 1 12 my $self = shift;
80 2         8 return $self->_service_command('status');
81             }
82              
83             # Stop service.
84             sub stop {
85 0     0 1 0 my $self = shift;
86 0         0 return $self->_service_command('stop');
87             }
88              
89             # Common command.
90             sub _service_command {
91 2     2   5 my ($self, $command) = @_;
92             my ($stdout, $stderr, $exit_code) = capture {
93 2     2   1002582 system $self->{'_service_path'}.' '.$command;
94 2         79 };
95 2         3053 my $ret_code = $exit_code >> 8;
96 2 50       31 if ($stderr) {
97 0         0 chomp $stderr;
98 0         0 err "Problem with service '$self->{'service'}' $command.",
99             'STDERR', $stderr,
100             'Exit code', $ret_code;
101             }
102 2         30 return $ret_code;
103             }
104              
105             1;
106              
107             __END__