blib/lib/Clustericious/Admin/Server.pm

filter: Statementstmt 83.1% 99 / 119 Branchbran 78.2% 36 / 46 Conditioncond 85.7% 12 / 14 MC/DCmcdc 77.7% 7 / 9 Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 82.6% 162 / 196 CC 32 SCAR 30.0
CC
32
Cov
82%
_server
26.5
_decode
13.1
BEGIN
1.0
CRAP 20.1
« blib/lib/Clustericious/Admin/ConfigData.pm ↑ Summary blib/lib/App/clad.pm »
1 package Clustericious::Admin::Server;
2  
3 33 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 33 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 33 ▶use Sys::Hostname qw( hostname );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 33 ▶use File::Temp qw( tempdir );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 33 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 33 ▶use File::Path qw( mkpath );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # ABSTRACT: Parallel SSH client server side code
11 33 our $VERSION = '1.11'; # VERSION
12  
13  
14 # This is the implementation of the clad server.
15 #
16 # - requires Perl 5.10
17 # - it is pure perl capable
18 # - no non-core requirements as of 5.14
19 # - single file implementation
20 # - optionally uses YAML::XS IF available
21 #
22 # The idea is that if App::clad is properly installed
23 # on the remote end, "clad --server" can be used to
24 # invoke, and you get YAML encoded payload. The YAML
25 # payload is preferred because it is easier to read
26 # when things go wrong. If App::clad is NOT installed
27 # on the remote end, then you can take this pm file,
28 # append the payload as Perl Dump after the __DATA__
29 # section below and send the server and payload and
30 # feed it into perl on the remote end.
31  
32 sub _decode
33 47 ▶{
Subroutine _decode: called CC=7 SCAR=25.7
34 47 my(undef, $fh) = @_;
35 47 my $raw = do { local $/; <$fh> };
36  
37 47 my $payload;
38  
39 47 ▶ if($raw =~ /^---/)
Branch: 2 branches
BranchTrueFalseTotal
if ($raw =~ /^---/) { } 46 1 47
elsif ($raw =~ /^#perl/) { } 0 1 1
40 {
41 46 eval {
42 46 require YAML::XS;
43 46 $payload = YAML::XS::Load($raw);
44 };
45 46 ▶ if(my $yaml_error = $@)
Branch: 1 branch
BranchTrueFalseTotal
if (my $yaml_error = $@) 0 46 46
46 {
47 0 print STDERR "Clad Server: side YAML Error:\n";
48 0 print STDERR $yaml_error, "\n";
49 0 print STDERR "payload:\n";
50 0 print STDERR $raw, "\n";
51 0 return;
52 }
53 46 ▶ print STDERR YAML::XS::Dump($payload) if $payload->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $payload->{"verbose"} 1 45 46
54 }
55 elsif($raw =~ /^#perl/)
56 {
57 0 $payload = eval $raw;
58 0 ▶ if(my $perl_error = $@)
Branch: 1 branch
BranchTrueFalseTotal
if (my $perl_error = $@) 0 0 0
59 {
60 0 print STDERR "Clad Server: side Perl Error:\n";
61 0 print STDERR $perl_error, "\n";
62 0 print STDERR "payload:\n";
63 0 print STDERR $raw, "\n";
64 0 return;
65 }
66 0 eval {
67 0 require Data::Dumper;
68 0 ▶ print Dumper($payload) if $payload->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $payload->{"verbose"} 0 0 0
69 };
70 }
71 else
72 {
73 1 print STDERR "Clad Server: unable to detect encoding.\n";
74 1 print STDERR "payload:\n";
75 1 print STDERR $raw;
76 }
77
78 47 $payload;
79 }
80  
81 sub _server
82 47 ▶{
Subroutine _server: called CC=26 SCAR=32.8
83 47 ▶ my $payload = _decode(@_) || return 2;
Condition100%2/2
_decode(@_) || (return 2)
l!l
461
MC/DC100%1/1
_decode(@_) || (return 2)
_decode(@_)
Truth table100%2/2
A
A= _decode(@_)
Aresult
11
00
84
85 # Payload:
86 #
87 # command: required, must be a array with at least one element
88 # the command to execute
89 #
90 # env: optional, must be a hash reference
91 # any environmental overrides
92 #
93 # verbose: optional true/false
94 # print out extra diagnostics
95 #
96 # version: required number or 'dev'
97 # the client version
98 #
99 # require: optional, number or 'dev'
100 # specifies the minimum required server
101 # server should die if requirement isn't met
102 # ignored if set to 'dev'
103 #
104 # files: optional list of hashref [ 1.01 ]
105 # each hashref has:
106 # name: the file basename (no directory)
107 # content: the content of the file
108 # mode: (optional) octal unix permission mode as a string (ie "0755" or "0644")
109 # env: (optional) environment variable to use instead of FILEx
110 #
111 # dir: optional hash of hash [ 1.02 ]
112 # each key is a path
113 # each value is a hash
114 # is_dir
115 # content
116 # mode
117 #
118 # stdin: optional scalar [ 1.04 ]
119  
120 46 ▶ if(ref $payload->{command} ne 'ARRAY' || @{ $payload->{command} } == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (ref $payload->{"command"} ne "ARRAY" or @{$payload->{"command"};} == 0) 2 44 46
Condition100%3/3
ref $payload->{"command"} ne "ARRAY" or @{$payload->{"command"};} == 0
l!l&&r!l&&!r
1144
MC/DC100%2/2
ref $payload->{"command"} ne "ARRAY" or @{$payload->{"command"};} == 0
ref $payload->{"command"} ne "ARRAY" @{$payload->{"command"};} == 0
Truth table100%3/3
A or B
A= ref $payload->{"command"} ne "ARRAY"B= @{$payload->{"command"};} == 0
ABresult
1X1
011
000
121 {
122 2 print STDERR "Clad Server: Unable to find command\n";
123 2 return 2;
124 }
125
126 44 ▶ if(defined $payload->{env} && ref $payload->{env} ne 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
if (defined $payload->{"env"} and ref $payload->{"env"} ne "HASH") 1 43 44
Condition100%3/3
defined $payload->{"env"} and ref $payload->{"env"} ne "HASH"
!ll&&!rl&&r
6371
MC/DC100%2/2
defined $payload->{"env"} and ref $payload->{"env"} ne "HASH"
defined $payload->{"env"} ref $payload->{"env"} ne "HASH"
Truth table100%3/3
A and B
A= defined $payload->{"env"}B= ref $payload->{"env"} ne "HASH"
ABresult
0X0
100
111
127 {
128 1 print STDERR "Clad Server: env is not hash\n";
129 1 return 2;
130 }
131
132 43 ▶ unless($payload->{version})
Branch: 1 branch
BranchTrueFalseTotal
unless ($payload->{"version"}) 1 42 43
133 {
134 1 print STDERR "Clad Server: no client version\n";
135 1 return 2;
136 }
137
138 42 ▶ if($payload->{require} && defined $Clustericious::Admin::Server::VERSION)
Branch: 1 branch
BranchTrueFalseTotal
if ($payload->{"require"} and defined $Clustericious::Admin::Server::VERSION) 10 32 42
Condition66%2/3
$payload->{"require"} and defined $Clustericious::Admin::Server::VERSION
!ll&&!rl&&r
32010
MC/DC50%1/2
$payload->{"require"} and defined $Clustericious::Admin::Server::VERSION
$payload->{"require"} defined $Clustericious::Admin::Server::VERSION
Truth table66%2/3
A and B
A= $payload->{"require"}B= defined $Clustericious::Admin::Server::VERSION
ABresult
0X0
100
111
139 {
140 10 ▶ if($payload->{require} ne 'dev' && $payload->{require} > $Clustericious::Admin::Server::VERSION)
Branch: 1 branch
BranchTrueFalseTotal
if ($payload->{"require"} ne "dev" and $payload->{"require"} > $Clustericious::Admin::Server::VERSION) 1 9 10
Condition66%2/3
$payload->{"require"} ne "dev" and $payload->{"require"} > $Clustericious::Admin::Server::VERSION
!ll&&!rl&&r
091
MC/DC50%1/2
$payload->{"require"} ne "dev" and $payload->{"require"} > $Clustericious::Admin::Server::VERSION
$payload->{"require"} ne "dev" $payload->{"require"} > $Clustericious::Admin::Server::VERSION
Truth table66%2/3
A and B
A= $payload->{"require"} ne "dev"B= $payload->{"require"} > $Clustericious::Admin::Server::VERSION
ABresult
0X0
100
111
141 {
142 1 print STDERR "Clad Server: client requested version @{[ $payload->{require} ]} but this is only $Clustericious::Admin::Server::VERSION\n";
143 1 return 2;
144 }
145 }
146  
147 41 ▶ if($payload->{files})
Branch: 1 branch
BranchTrueFalseTotal
if ($payload->{"files"}) 6 35 41
148 {
149 6 my $count = 1;
150 6 foreach my $file (@{ $payload->{files} })
151 {
152 9 my $path = File::Spec->catfile( tempdir( CLEANUP => 1 ), $file->{name} );
153 9 open my $fh, '>', $path;
154 9 ▶ chmod oct($file->{mode}), $path if defined $file->{mode};
Branch: 1 branch
BranchTrueFalseTotal
if defined $file->{"mode"} 9 0 9
155 9 binmode $fh;
156 9 print $fh $file->{content};
157 9 close $fh;
158 9 my $env = $file->{env};
159 9 ▶ $env = "FILE@{[ $count++ ]}" unless defined $env;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $env 4 5 9
160 9 $ENV{$env} = $path;
161 }
162 }
163
164 41 ▶ if($payload->{dir})
Branch: 1 branch
BranchTrueFalseTotal
if ($payload->{"dir"}) 2 39 41
165 {
166 2 my $root = $ENV{DIR} = tempdir( CLEANUP => 1 );
167
168 2 foreach my $name (sort keys %{ $payload->{dir} })
169 {
170 14 my $dir = $payload->{dir}->{$name};
171 14 ▶ next unless $dir->{is_dir};
Branch: 1 branch
BranchTrueFalseTotal
unless $dir->{"is_dir"} 6 8 14
172 8 my $path = File::Spec->catdir($root, $name);
173 8 mkdir $path;
174 8 ▶ chmod oct($dir->{mode}), $path if defined $dir->{mode};
Branch: 1 branch
BranchTrueFalseTotal
if defined $dir->{"mode"} 5 3 8
175 }
176
177 2 foreach my $name (sort keys %{ $payload->{dir} })
178 {
179 14 my $file = $payload->{dir}->{$name};
180 14 ▶ next if $file->{is_dir};
Branch: 1 branch
BranchTrueFalseTotal
if $file->{"is_dir"} 8 6 14
181 6 my $path = File::Spec->catfile($root, $name);
182 6 open my $fh, '>', $path;
183 6 ▶ chmod oct($file->{mode}), $fh if defined $file->{mode};
Branch: 1 branch
BranchTrueFalseTotal
if defined $file->{"mode"} 6 0 6
184 6 binmode $fh;
185 6 print $fh $file->{content};
186 6 close $fh;
187 }
188 }
189  
190 41 $ENV{$_} = $payload->{env}->{$_} for keys %{ $payload->{env} };
191
192 41 ▶ if(defined $payload->{stdin})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $payload->{"stdin"}) 0 41 41
193 {
194 0 my $filename = File::Spec->catfile(tempdir(CLEANUP => 1), 'stdin.txt');
195 0 open OUT, ">$filename";
196 0 print OUT $payload->{stdin};
197 0 close OUT;
198 0 open STDIN, "<$filename";
199 }
200
201 41 system @{ $payload->{command} };
202
203 41 ▶ if($? == -1)
Branch: 2 branches
BranchTrueFalseTotal
if ($? == -1) { } 1 40 41
elsif ($? & 127) { } 1 39 40
204 {
205 1 print STDERR "Clad Server: failed to execute on @{[ hostname ]}\n";
206 1 return 2;
207 }
208 elsif($? & 127)
209 {
210 1 print STDERR "Clad Server: died with signal @{[ $? & 127 ]} on @{[ hostname ]}\n";
211 1 return 2;
212 }
213
214 39 return $? >> 8;
215 }
216  
217 33 ▶exit __PACKAGE__->_server(*DATA) unless caller;
Branch: 1 branch
BranchTrueFalseTotal
unless caller 0 33 33
218  
219 33 1;
220  
221 =pod
222  
223 =encoding UTF-8
224  
225 =head1 NAME
226  
227 Clustericious::Admin::Server - Parallel SSH client server side code
228  
229 =head1 VERSION
230  
231 version 1.11
232  
233 =head1 SYNOPSIS
234  
235 % perldoc clad
236  
237 =head1 DESCRIPTION
238  
239 This module provides part of the implementation for the
240 L<clad> command. See the L<clad> command for the public
241 interface.
242  
243 =head1 SEE ALSO
244  
245 =over 4
246  
247 =item L<clad>
248  
249 =back
250  
251 =head1 AUTHOR
252  
253 Graham Ollis <plicease@cpan.org>
254  
255 =head1 COPYRIGHT AND LICENSE
256  
257 This software is copyright (c) 2015 by Graham Ollis.
258  
259 This is free software; you can redistribute it and/or modify it under
260 the same terms as the Perl 5 programming language system itself.
261  
262 =cut
263  
264 __DATA__
« blib/lib/Clustericious/Admin/ConfigData.pm ↑ Summary blib/lib/App/clad.pm »