line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# $Id$ |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# system::service Brik |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
package Metabrik::System::Service; |
7
|
2
|
|
|
2
|
|
841
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
68
|
|
8
|
2
|
|
|
2
|
|
12
|
use warnings; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
58
|
|
9
|
|
|
|
|
|
|
|
10
|
2
|
|
|
2
|
|
10
|
use base qw(Metabrik::Shell::Command); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
1805
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub brik_properties { |
13
|
|
|
|
|
|
|
return { |
14
|
0
|
|
|
0
|
1
|
|
revision => '$Revision$', |
15
|
|
|
|
|
|
|
tags => [ qw(unstable daemon) ], |
16
|
|
|
|
|
|
|
author => 'GomoR ', |
17
|
|
|
|
|
|
|
license => 'http://opensource.org/licenses/BSD-3-Clause', |
18
|
|
|
|
|
|
|
commands => { |
19
|
|
|
|
|
|
|
get_system_service => [ ], |
20
|
|
|
|
|
|
|
status => [ qw(service_name) ], |
21
|
|
|
|
|
|
|
start => [ qw(service_name) ], |
22
|
|
|
|
|
|
|
stop => [ qw(service_name) ], |
23
|
|
|
|
|
|
|
restart => [ qw(service_name) ], |
24
|
|
|
|
|
|
|
my_os => [ ], |
25
|
|
|
|
|
|
|
enable => [ qw(service_name) ], |
26
|
|
|
|
|
|
|
disable => [ qw(service_name) ], |
27
|
|
|
|
|
|
|
}, |
28
|
|
|
|
|
|
|
require_modules => { |
29
|
|
|
|
|
|
|
'Metabrik::System::Os' => [ ], |
30
|
|
|
|
|
|
|
'Metabrik::System::Ubuntu::Service' => [ ], |
31
|
|
|
|
|
|
|
'Metabrik::System::Debian::Service' => [ ], |
32
|
|
|
|
|
|
|
'Metabrik::System::Kali::Service' => [ ], |
33
|
|
|
|
|
|
|
'Metabrik::System::Centos::Service' => [ ], |
34
|
|
|
|
|
|
|
}, |
35
|
|
|
|
|
|
|
require_binaries => { |
36
|
|
|
|
|
|
|
service => [ ], |
37
|
|
|
|
|
|
|
}, |
38
|
|
|
|
|
|
|
}; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub get_system_service { |
42
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
43
|
|
|
|
|
|
|
|
44
|
0
|
|
|
|
|
|
my $os = $self->my_os; |
45
|
|
|
|
|
|
|
|
46
|
0
|
|
|
|
|
|
my $ss; |
47
|
0
|
0
|
|
|
|
|
if ($os eq 'ubuntu') { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
48
|
0
|
0
|
|
|
|
|
$ss = Metabrik::System::Ubuntu::Service->new_from_brik_init($self) or return; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
elsif ($os eq 'debian') { |
51
|
0
|
0
|
|
|
|
|
$ss = Metabrik::System::Debian::Service->new_from_brik_init($self) or return; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
elsif ($os eq 'kali') { |
54
|
0
|
0
|
|
|
|
|
$ss = Metabrik::System::Kali::Service->new_from_brik_init($self) or return; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
elsif ($os eq 'centos') { |
57
|
0
|
0
|
|
|
|
|
$ss = Metabrik::System::Centos::Service->new_from_brik_init($self) or return; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
else { |
60
|
0
|
|
|
|
|
|
$ss = $self; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
0
|
|
|
|
|
|
return $ss; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub status { |
67
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
68
|
0
|
|
|
|
|
|
my ($name) = @_; |
69
|
|
|
|
|
|
|
|
70
|
0
|
0
|
|
|
|
|
if (defined($name)) { |
|
|
0
|
|
|
|
|
|
71
|
0
|
|
|
|
|
|
return $self->execute("service \"$name\" status"); |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
elsif (! exists($self->brik_properties->{need_services})) { |
74
|
0
|
|
|
|
|
|
return $self->log->error($self->brik_help_run('status')); |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
else { |
77
|
0
|
|
|
|
|
|
my $os = $self->my_os; |
78
|
0
|
0
|
|
|
|
|
if (exists($self->brik_properties->{need_services}{$os})) { |
79
|
0
|
|
|
|
|
|
my $need_services = $self->brik_properties->{need_services}{$os}; |
80
|
0
|
|
|
|
|
|
for my $service (@$need_services) { |
81
|
0
|
|
|
|
|
|
$self->execute("service \"$service\" status"); |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
else { |
85
|
0
|
|
|
|
|
|
return $self->log->error("status: don't know how to do that for OS [$os]"); |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
0
|
|
|
|
|
|
return 1; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub start { |
93
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
94
|
0
|
|
|
|
|
|
my ($name) = @_; |
95
|
|
|
|
|
|
|
|
96
|
0
|
0
|
|
|
|
|
if (defined($name)) { |
|
|
0
|
|
|
|
|
|
97
|
0
|
|
|
|
|
|
return $self->sudo_execute("service \"$name\" start"); |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
elsif (! exists($self->brik_properties->{need_services})) { |
100
|
0
|
|
|
|
|
|
return $self->log->error($self->brik_help_run('start')); |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
else { |
103
|
0
|
|
|
|
|
|
my $os = $self->my_os; |
104
|
0
|
0
|
|
|
|
|
if (exists($self->brik_properties->{need_services}{$os})) { |
105
|
0
|
|
|
|
|
|
my $need_services = $self->brik_properties->{need_services}{$os}; |
106
|
0
|
|
|
|
|
|
for my $service (@$need_services) { |
107
|
0
|
|
|
|
|
|
$self->sudo_execute("service \"$service\" start"); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
else { |
111
|
0
|
|
|
|
|
|
return $self->log->error("start: don't know how to do that for OS [$os]"); |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
0
|
|
|
|
|
|
return 1; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub stop { |
119
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
120
|
0
|
|
|
|
|
|
my ($name) = @_; |
121
|
|
|
|
|
|
|
|
122
|
0
|
0
|
|
|
|
|
if (defined($name)) { |
|
|
0
|
|
|
|
|
|
123
|
0
|
|
|
|
|
|
return $self->sudo_execute("service \"$name\" stop"); |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
elsif (! exists($self->brik_properties->{need_services})) { |
126
|
0
|
|
|
|
|
|
return $self->log->error($self->brik_help_run('stop')); |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
else { |
129
|
0
|
|
|
|
|
|
my $os = $self->my_os; |
130
|
0
|
0
|
|
|
|
|
if (exists($self->brik_properties->{need_services}{$os})) { |
131
|
0
|
|
|
|
|
|
my $need_services = $self->brik_properties->{need_services}{$os}; |
132
|
0
|
|
|
|
|
|
for my $service (@$need_services) { |
133
|
0
|
|
|
|
|
|
$self->sudo_execute("service \"$service\" stop"); |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
else { |
137
|
0
|
|
|
|
|
|
return $self->log->error("stop: don't know how to do that for OS [$os]"); |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
0
|
|
|
|
|
|
return 1; |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
sub restart { |
145
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
146
|
0
|
|
|
|
|
|
my ($name) = @_; |
147
|
|
|
|
|
|
|
|
148
|
0
|
0
|
|
|
|
|
if (defined($name)) { |
|
|
0
|
|
|
|
|
|
149
|
0
|
0
|
|
|
|
|
$self->stop($name) or return; |
150
|
0
|
|
|
|
|
|
sleep(1); |
151
|
0
|
|
|
|
|
|
return $self->start($name); |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
elsif (! exists($self->brik_properties->{need_services})) { |
154
|
0
|
|
|
|
|
|
return $self->log->error($self->brik_help_run('restart')); |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
else { |
157
|
0
|
|
|
|
|
|
my $os = $self->my_os; |
158
|
0
|
0
|
|
|
|
|
if (exists($self->brik_properties->{need_services}{$os})) { |
159
|
0
|
|
|
|
|
|
my $need_services = $self->brik_properties->{need_services}{$os}; |
160
|
0
|
|
|
|
|
|
for my $service (@$need_services) { |
161
|
0
|
0
|
|
|
|
|
$self->stop($name) or next; |
162
|
0
|
|
|
|
|
|
sleep(1); |
163
|
0
|
|
|
|
|
|
$self->start($name); |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
} |
166
|
|
|
|
|
|
|
else { |
167
|
0
|
|
|
|
|
|
return $self->log->error("restart: don't know how to do that for OS [$os]"); |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
|
171
|
0
|
|
|
|
|
|
return 1; |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
sub my_os { |
175
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
176
|
|
|
|
|
|
|
|
177
|
0
|
0
|
|
|
|
|
my $so = Metabrik::System::Os->new_from_brik_init($self) or return; |
178
|
0
|
|
|
|
|
|
return $so->my; |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
sub disable { |
182
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
183
|
0
|
|
|
|
|
|
my ($service_name) = @_; |
184
|
|
|
|
|
|
|
|
185
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('disable', $service_name) or return; |
186
|
|
|
|
|
|
|
|
187
|
0
|
0
|
|
|
|
|
my $ss = $self->get_system_service or return; |
188
|
0
|
|
|
|
|
|
return $ss->disable($service_name); |
189
|
|
|
|
|
|
|
} |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
sub enable { |
192
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
193
|
0
|
|
|
|
|
|
my ($service_name) = @_; |
194
|
|
|
|
|
|
|
|
195
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('enable', $service_name) or return; |
196
|
|
|
|
|
|
|
|
197
|
0
|
0
|
|
|
|
|
my $ss = $self->get_system_service or return; |
198
|
0
|
|
|
|
|
|
return $ss->enable($service_name); |
199
|
|
|
|
|
|
|
} |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
1; |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
__END__ |