Svsh.pm

filter: Statementstmt 22.5% 14 / 62 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 11.1% 1 / 9 Podpod 100% 2 / 2 totaltotal 15.3% 17 / 111 SCAR 50.2
CC
15
Cov
15%
run_cmd
42.0
find_logfile
42.0
_expand_wildcards
20.0
CRAP 151.3
↑ Summary Svsh/Perp.pm »
1 package Svsh;
2  
3 # ABSTRACT: Process supervision shell for daemontools/perp/s6/runit
4  
5 1 our $VERSION = "1.002000";
6 1 $VERSION = eval $VERSION;
7  
8 1 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Svsh - Process supervision shell for daemontools/perp/s6/runit (base class)
13  
14 =head1 SYNOPSIS
15  
16 package Svsh::SomeSupervisor;
17  
18 use Moo;
19 use namespace::clean;
20  
21 with 'Svsh';
22  
23 # implement required methods
24  
25 =head1 DESCRIPTION
26  
27 C<svsh> is a shell for process supervision suites of the C<daemontools> family,
28 including C<perp>, C<s6> and C<runit>. Refer to L<svsh> for documentation of
29 the shell itself. This file documents the base class for Svsh adapter classes.
30  
31 =head1 ATTRIBUTES
32  
33 =head2 basedir
34  
35 I<Required, Read-Only>.
36  
37 The base directory from which the process supervisor is managing services.
38  
39 =cut
40  
41 1 has 'basedir' => (
42 is => 'ro',
43 required => 1
44 );
45  
46 =head2 bindir
47  
48 I<Read-Only>.
49  
50 The directory where the process supervisor's tools are located. Any call to
51 the supervisor's tools will be prefixed with this path if provided. For usage
52 in case the tools are not in the running user's C<PATH> environment variable.
53  
54 =cut
55  
56 1 has 'bindir' => (
57 is => 'ro'
58 );
59  
60 =head2 collapse
61  
62 I<Read-Write>.
63  
64 A boolean indicating whether the L<collapse|svsh/"COLLAPSE"> option should be
65 enabled.
66  
67 =cut
68  
69 has 'collapse' => (
70 is => 'rw',
71 0 ▶ default => sub { 0 }
Subroutine __ANON__: not called CC=1 SCAR=6.9
72 1 );
73  
74 =head2 statuses
75  
76 I<Read-Only>.
77  
78 A hash-ref of services and their statuses (this is automatically populated by
79 the respective C<status()> method in the adapter classes. For every service,
80 a hash-ref with C<status>, C<duration> and C<pid> keys should exist.
81  
82 =cut
83  
84 1 has 'statuses' => (
85 is => 'ro',
86 writer => '_set_statuses'
87 );
88  
89 =head1 REQUIRED METHODS
90  
91 =head2 status()
92  
93 Finds all services managed by the supervisor, and populates
94 the L<statuses> attribute.
95  
96 =head2 start( @services )
97  
98 Starts a list of services if they are down.
99  
100 =head2 stop( @services )
101  
102 Stops a list of services (should not restart them).
103  
104 =head2 restart( @services )
105  
106 Stops and starts a list of services. Generally, this is implemented
107 with a C<QUIT> signal to the services, but check with the specific
108 adapter class.
109  
110 =head2 signal( $signal, @services )
111  
112 Sends UNIX signal to a list of services.
113  
114 =head2 fg( $service )
115  
116 Finds the log file to which a service is writing, and displays it
117 on screen with the C<tail -f> command.
118  
119 =head1 WANTED METHODS
120  
121 These methods are not required by adapter classes. If they are not
122 implemented, they will be unavailable in the shell.
123  
124 =head2 rescan()
125  
126 Causes the supervisor to rescan the service directory to find
127 new or removed services.
128  
129 =head2 terminate()
130  
131 Terminates the supervisor. Should also terminate all running services.
132  
133 =cut
134  
135 1 requires qw/status start stop restart signal fg/;
136  
137 before [qw/start stop restart/] => sub {
138 0 ▶ $_[2]->{args} = [$_[0]->_expand_wildcards(@{$_[2]->{args}})];
Subroutine __ANON__: not called CC=1 SCAR=6.9
139 1 };
140  
141 before signal => sub {
142 0 ▶ my ($signal, @svcs) = @{$_[2]->{args}};
Subroutine __ANON__: not called CC=1 SCAR=6.9
143 0 $_[2]->{args} = [$signal, $_[0]->_expand_wildcards(@svcs)];
144 1 };
145  
146 around 'status' => sub {
147 0 ▶ my ($orig, $self) = (shift, shift);
Subroutine __ANON__: not called CC=1 SCAR=6.9
148 0 $self->_set_statuses($orig->($self, @_));
149 0 return $self->statuses;
150 1 };
151  
152 =head1 METHODS
153  
154 =head2 run_cmd( $cmd, [ @args ] )
155  
156 Runs a shell command with zero or more arguments and returns its
157 output. If the C<bindir> attribute is set, and the C<$cmd> is one
158 of the supervision suite's library of tools, C<$cmd> will be prefixed
159 with C<bindir>.
160  
161 =cut
162  
163 sub run_cmd {
164 0 ▶ my ($self, $cmd, @args) = @_;
Subroutine run_cmd: not called CC=6 SCAR=37.4
Pod documented
165  
166 0 my $options = {};
167  
168 0 ▶ $cmd = $self->bindir . '/' . $cmd
Branch: 1 branch
BranchTrueFalseTotal
if $self->bindir and $cmd =~ /^(perp|s6|sv)/ 0 0 0
Condition0%0/3
$self->bindir and $cmd =~ /^(perp|s6|sv)/
!ll&&!rl&&r
000
MC/DC0%0/2
$self->bindir and $cmd =~ /^(perp|s6|sv)/
$self->bindir $cmd =~ /^(perp|s6|sv)/
Truth table0%0/3
A and B
A= $self->bindirB= $cmd =~ /^(perp|s6|sv)/
ABresult
0X0
100
111
169 if $self->bindir && $cmd =~ m/^(perp|s6|sv)/;
170  
171 0 ▶ if (scalar @args && ref $args[-1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @args and ref $args[-1]) 0 0 0
Condition0%0/3
scalar @args and ref $args[-1]
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @args and ref $args[-1]
scalar @args ref $args[-1]
Truth table0%0/3
A and B
A= scalar @argsB= ref $args[-1]
ABresult
0X0
100
111
172 0 $options = pop @args;
173 }
174  
175 0 ▶ if ($options->{as_system}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{'as_system'}) { } 0 0 0
176 0 system($cmd, @args);
177 } else {
178 0 $cmd = join(' ', $cmd, @args);
179 0 return qx/$cmd 2>&1/;
180 }
181 }
182  
183 =head2 find_logfile( $pid )
184  
185 Finds the log file into which a logging program is currently
186 writing to. C<$pid> is the process ID of the logging program.
187 Currently, C<tinylog>, C<s6-log>, C<svlogd> and C<multilog>
188 are supported.
189  
190 Returns C<undef> if the file is not found.
191  
192 =cut
193  
194 sub find_logfile {
195 0 ▶ my ($self, $pid) = @_;
Subroutine find_logfile: not called CC=6 SCAR=37.4
Pod documented
196  
197 0 ▶ my $exe = readlink("/proc/$pid/exe")
Condition0%0/2
readlink "/proc/$pid/exe" || (return)
l!l
00
MC/DC0%0/1
readlink "/proc/$pid/exe" || (return)
readlink "/proc/$pid/exe"
Truth table0%0/2
A
A= readlink "/proc/$pid/exe"
Aresult
11
00
198 || return;
199  
200 0 my $file;
201  
202 0 ▶ if ($exe =~ m/tinylog/ || $exe =~ m/s6-log/ || $exe =~ m/svlogd/ || $exe =~ m/multilog/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exe =~ /tinylog/ or $exe =~ /s6-log/ or $exe =~ /svlogd/ or $exe =~ /multilog/) 0 0 0
Condition0%0/9
$exe =~ /tinylog/ or $exe =~ /s6-log/ or $exe =~ /svlogd/ or $exe =~ /multilog/
l!l&&r!l&&!r
000
$exe =~ /tinylog/ or $exe =~ /s6-log/ or $exe =~ /svlogd/
l!l&&r!l&&!r
000
$exe =~ /tinylog/ or $exe =~ /s6-log/
l!l&&r!l&&!r
000
MC/DC0%0/4
$exe =~ /tinylog/ or $exe =~ /s6-log/ or $exe =~ /svlogd/ or $exe =~ /multilog/
$exe =~ /tinylog/ $exe =~ /s6-log/ $exe =~ /svlogd/ $exe =~ /multilog/
Truth table0%0/5
A or B or C or D
A= $exe =~ /tinylog/B= $exe =~ /s6-log/C= $exe =~ /svlogd/D= $exe =~ /multilog/
ABCDresult
1XXX1
01XX1
001X1
00011
00000
203 # look for a link to a /current file under /proc/$pid/fd
204 0 opendir my $dir, "/proc/$pid/fd";
205 0 ($file) = grep { m!/current$! } map { readlink("/proc/$pid/fd/$_") } grep { !/^\.\.?$/ } readdir $dir;
206 0 closedir $dir;
207 }
208  
209 0 return $file;
210 }
211  
212 ######################################################################
213 # _expand_wildcards( @services )
214 # goes over a list of services, possibly (but not necessarily)
215 # with wildcards, and returns a new list with all services
216 # that match. For example, if @services = ('sv1', 'sv2', 'worker*'),
217 # and the services worker-1 and worker-2 exist, then the
218 # method will return ('sv1', 'sv2', 'worker-1', 'worker-2')
219 ######################################################################
220  
221 sub _expand_wildcards {
222 0 ▶ my $self = shift;
Subroutine _expand_wildcards: not called CC=4 SCAR=30.0
223  
224 0 my %services;
225 0 foreach (@_) {
226 0 ▶ if (m/\*/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\*/) { } 0 0 0
227 # this is a wildcard, find all services that match it
228 0 my $regex = $_; $regex =~ s/\*/.*/; $regex = qr/^$regex$/;
229 0 foreach my $sv (grep { m/$regex/ } keys %{$self->statuses}) {
230 0 $services{$sv} = 1;
231 }
232 } else {
233 0 $services{$_} = 1;
234 }
235 }
236  
237 0 return keys %services;
238 }
239  
240 #########################################################
241 # _service_dirs()
242 # returns a list of all service directories inside the
243 # base directory
244 #########################################################
245  
246 sub _service_dirs {
247 0 ▶ my $basedir = shift->basedir;
Subroutine _service_dirs: not called CC=2 SCAR=17.9
248  
249 0 opendir(my $dh, $basedir);
250 0 ▶ my @dirs = grep { !/^\./ && -d "$basedir/$_" } readdir $dh;
Branch: 1 branch
BranchTrueFalseTotal
not /^\./ and -d "$basedir/$_" 0 0 0
251 0 closedir $dh;
252  
253 0 return sort @dirs;
254 }
255  
256 =head1 BUGS AND LIMITATIONS
257  
258 No bugs have been reported.
259  
260 Please report any bugs or feature requests to
261 C<bug-Svsh@rt.cpan.org>, or through the web interface at
262 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Svsh>.
263  
264 =head1 SUPPORT
265  
266 You can find documentation for this module with the perldoc command.
267  
268 perldoc Svsh
269  
270 You can also look for information at:
271  
272 =over 4
273
274 =item * RT: CPAN's request tracker
275
276 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Svsh>
277
278 =item * AnnoCPAN: Annotated CPAN documentation
279
280 L<http://annocpan.org/dist/Svsh>
281
282 =item * CPAN Ratings
283
284 L<http://cpanratings.perl.org/d/Svsh>
285
286 =item * Search CPAN
287
288 L<http://search.cpan.org/dist/Svsh/>
289
290 =back
291  
292 =head1 AUTHOR
293  
294 Ido Perlmuter <ido at ido50 dot net>
295  
296 =head1 LICENSE AND COPYRIGHT
297  
298 Copyright (c) 2015, Ido Perlmuter C<< ido at ido50 dot net >>.
299  
300 This module is free software; you can redistribute it and/or
301 modify it under the same terms as Perl itself, either version
302 5.8.1 or any later version. See L<perlartistic|perlartistic>
303 and L<perlgpl|perlgpl>.
304  
305 The full text of the license can be found in the
306 LICENSE file included with this module.
307  
308 =head1 DISCLAIMER OF WARRANTY
309  
310 BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
311 FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
312 OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
313 PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
314 EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
315 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
316 ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
317 YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
318 NECESSARY SERVICING, REPAIR, OR CORRECTION.
319  
320 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
321 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
322 REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
323 LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
324 OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
325 THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
326 RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
327 FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
328 SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
329 SUCH DAMAGES.
330  
331 =cut
332  
333 1 1;
334 __END__
↑ Summary Svsh/Perp.pm »