bin/tonodes

filter: Statementstmt 48.2% 113 / 234 Branchbran 36.5% 60 / 164 Conditioncond 19.0% 4 / 21 MC/DCmcdc 14.2% 2 / 14 Subroutinesub 61.5% 8 / 13 Podpod n/a totaltotal 41.9% 187 / 446 CC 2 SCAR 3.6
CC
2
Cov
42%
__ANON__
2.0
__ANON__
2.0
__ANON__
2.0
CRAP 1.4
« blib/lib/SSH/Batch.pm ↑ Summary bin/fornodes »
1 #!/usr/bin/env perl
2  
3 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 #use Smart::Comments::JSON '##';
7 15 ▶use lib 'lib';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use Net::OpenSSH;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use Term::ReadKey;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use SSH::Batch::ForNodes;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 15 ▶use File::Temp qw/ :POSIX /;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 sub help ($);
14  
15 15 ▶if (!@ARGV) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@ARGV) 1 14 15
16 1 warn "No argument specified.\n\n";
17 1 help(1);
18 }
19  
20 14 my $list_hosts_only = 0;
21 14 my ($user, $port, $timeout, $verbose, $ask_for_pass, $ask_for_passphrase, $recursive, $use_glob);
22 14 my ($bwlimit, $use_rsync);
23 14 my $concurrency = 20;
24 14 my (@files, @exprs);
25 14 my $fetch_value;
26 14 my $found_sep;
27 14 my $last_option;
28 14 my $line_mode = $ENV{SSH_BATCH_LINE_MODE};
29  
30 14 my $rsync_archive;
31 my $rsync_update;
32 14 my $rsync_compress;
33  
34 14 for (@ARGV) {
35 70 ▶ if (defined $fetch_value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fetch_value) 0 70 70
36 0 $fetch_value->($_);
37 0 undef $fetch_value;
38 0 next;
39 }
40 70 ▶ if ($_ eq '--') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq "--") 4 66 70
41 4 @files = @exprs;
42 4 @exprs = ();
43 4 $found_sep = 1;
44 4 next;
45 }
46 66 ▶ if (/^-(.+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^-(.+)/) 15 51 66
47 15 my $group = $1;
48 15 ▶ if ($group eq 'l') {
Branch: 17 branches
BranchTrueFalseTotal
if ($group eq 'l') { } 6 9 15
elsif ($group eq 'u') { } 1 8 9
elsif ($group eq 't') { } 0 8 8
elsif ($group eq 'h') { } 1 7 8
elsif ($group eq 'p') { } 0 7 7
elsif ($group eq 'v') { } 6 1 7
elsif ($group eq 'w') { } 0 1 1
elsif ($group eq 'P') { } 0 1 1
elsif ($group eq 'r') { } 0 1 1
elsif ($group eq 'g') { } 0 1 1
elsif ($group eq 'b') { } 0 1 1
elsif ($group eq 'rsync') { } 1 0 1
elsif ($group eq 'archive') { } 0 0 0
elsif ($group eq 'update') { } 0 0 0
elsif ($group eq 'compress') { } 0 0 0
elsif ($group eq 'c') { } 0 0 0
elsif ($group eq 'L') { } 0 0 0
49 6 $list_hosts_only = 1;
50 } elsif ($group eq 'u') {
51 1 ▶ $fetch_value = sub { $user = shift };
Subroutine __ANON__: not called CC=1 SCAR=1.2
52 } elsif ($group eq 't') {
53 0 ▶ $fetch_value = sub { $timeout = shift };
Subroutine __ANON__: not called CC=1 SCAR=6.9
54 } elsif ($group eq 'h') {
55 1 help(0);
56 } elsif ($group eq 'p') {
57 0 ▶ $fetch_value = sub { $port = shift };
Subroutine __ANON__: not called CC=1 SCAR=6.9
58 } elsif ($group eq 'v') {
59 6 $verbose = 1;
60 } elsif ($group eq 'w') {
61 0 $ask_for_pass = 1;
62 } elsif ($group eq 'P') {
63 0 $ask_for_passphrase = 1;
64 } elsif ($group eq 'r') {
65 0 $recursive = 1;
66 } elsif ($group eq 'g') {
67 0 $use_glob = 1;
68 } elsif ($group eq 'b') {
69 0 ▶ $fetch_value = sub { $bwlimit = shift };
Subroutine __ANON__: not called CC=1 SCAR=6.9
70 } elsif ($group eq 'rsync') {
71 1 $use_rsync = 1;
72 } elsif ($group eq 'archive') {
73 0 $rsync_archive = 1;
74 } elsif ($group eq 'update') {
75 0 $rsync_update = 1;
76 } elsif ($group eq 'compress') {
77 0 $rsync_compress = 1;
78 } elsif ($group eq 'c') {
79 0 ▶ $fetch_value = sub { $concurrency = shift };
Subroutine __ANON__: not called CC=1 SCAR=6.9
80 } elsif ($group eq 'L') {
81 0 $line_mode = 1;
82 } else {
83 0 die "Unknown option: $_\n";
84 }
85 14 $last_option = $_;
86 14 next;
87 }
88 51 push @exprs, $_;
89 }
90 13 ▶if (defined $fetch_value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fetch_value) 1 12 13
91 1 die "ERROR: Option $last_option takes a value.\n";
92 }
93  
94 12 ▶if (!$found_sep && !@files) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found_sep or @files) 8 4 12
Condition66%2/3
$found_sep or @files
l!l&&r!l&&!r
408
MC/DC50%1/2
$found_sep or @files
$found_sep @files
Truth table66%2/3
A or B
A= $found_sepB= @files
ABresult
1X1
011
000
95 8 push @files, shift @exprs;
96 }
97 12 ▶if (!@files) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@files) 1 11 12
98 1 die "No local files/directories specified.\n";
99 }
100  
101 11 ▶if ($use_glob) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_glob) 0 11 11
102 0 @files = map glob, @files;
103 }
104  
105 11 my $changed = 0;
106 11 for my $file (@files) {
107 11 ▶ if (!-e $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $file) 1 10 11
108 1 die "Local file/directory $file not found.\n";
109 }
110 10 ▶ if (!$recursive && -d $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $recursive and -d $file) 1 9 10
Condition66%2/3
not $recursive and -d $file
!ll&&!rl&&r
091
MC/DC50%1/2
not $recursive and -d $file
not $recursive -d $file
Truth table66%2/3
not(A) and B
A= not $recursiveB= -d $file
ABresult
0X0
100
111
111 1 warn "Warning: Skipped directory $file.\n";
112 1 undef $file;
113 1 $changed = 1;
114 }
115 }
116  
117 10 ▶if ($changed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($changed) 1 9 10
118 1 @files = grep { defined $_ } @files;
119 1 ▶ if (!@files) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@files) 1 0 1
120 1 die "ERROR: No files to be transferred.\n";
121 }
122 }
123  
124 9 ▶if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 6 3 9
125 6 ▶ warn "Using ", $use_rsync ? 'Rsync' : 'Scp', " method.\n";
Branch: 1 branch
BranchTrueFalseTotal
$use_rsync ? : 1 5 6
126 6 warn "Local files: ", (map { "[$_]" } @files), "\n";
127 }
128  
129 9 my $expr = join ' ', @exprs;
130 9 my $target_path;
131 9 ▶if ($expr =~ s/\s*:(\D\S*)\s*$//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ s/\s*:(\D\S*)\s*$//) { } 8 1 9
132 8 $target_path = $1;
133 } else {
134 1 die "No remote target path specified.\n",
135 " (You forgot to specify \":/path/to/target\" at the end of the command line?)\n";
136 }
137 8 my $expanded_path = 0;
138 8 ▶if (defined $ENV{USER}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ENV{"USER"}) 8 0 8
139 8 my $old_target = $target_path;
140 # XXX FIXME Maybe we should expand ~ on the remote server?
141 8 ▶ if ($target_path =~ s{^~(/|$)}{/home/$ENV{USER}$1}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($target_path =~ s[^~(/|$)][/home/$ENV{"USER"}$1]) 2 6 8
142 2 warn "WARNING: Expanding target path $old_target to $target_path\n";
143 2 $expanded_path = 1;
144 }
145 }
146 8 ▶if (!$expanded_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($expanded_path) 6 2 8
147 6 my $old_target = $target_path;
148 6 ▶ if ($target_path =~ s{^~(\w+)}{/home/$1}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($target_path =~ s[^~(\w+)][/home/$1]) 3 3 6
149 3 warn "WARNING: Expanding target path $old_target to $target_path\n";
150 3 $expanded_path = 1;
151 }
152 }
153  
154 8 ▶if ($expr =~ /^\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^\s*$/) 1 7 8
155 1 die "No cluster expression specified.\n";
156 }
157  
158 7 ▶if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 6 1 7
159 6 warn "Cluster expression: $expr\n";
160 6 ▶ warn "Target path: ", defined $target_path ? $target_path : '', "\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $target_path ? : 6 0 6
161 }
162  
163 7 SSH::Batch::ForNodes::init_rc();
164 6 my $set = SSH::Batch::ForNodes::parse_expr($expr);
165  
166 6 ▶if ($set->is_empty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($set->is_empty) 0 6 6
167 0 die "No machine to be operated.\n";
168 }
169 6 my @hosts = sort $set->elements;
170  
171 6 ▶if ($verbose) {
Branch: 2 branches
BranchTrueFalseTotal
if ($verbose) { } 6 0 6
elsif ($list_hosts_only) { } 0 0 0
172 6 warn "Cluster set: @hosts\n";
173 } elsif ($list_hosts_only) {
174 0 print "Cluster set: @hosts\n";
175 }
176  
177 6 ▶if ($list_hosts_only) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list_hosts_only) 6 0 6
178 6 exit(0);
179 }
180 0 my ($passphrase, $password);
181 0 ▶if ($ask_for_passphrase) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ask_for_passphrase) { } 0 0 0
elsif ($ask_for_pass) { } 0 0 0
182 0 $passphrase = $ENV{SSH_BATCH_PASSPHRASE};
183 0 ▶ if (!$passphrase) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($passphrase) 0 0 0
184 0 print STDERR "Passphrase:";
185 0 ReadMode(2);
186 0 ▶ while (not defined ($passphrase = ReadLine(0))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($passphrase = ReadLine(0))) 0 0 0
187 }
188 0 ReadMode(0);
189 0 print "\n";
190 0 chomp $passphrase;
191 }
192 0 ▶ if (!$passphrase) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($passphrase) 0 0 0
193 0 die "No passphrase specified.\n";
194 }
195 } elsif ($ask_for_pass) {
196 0 $password = $ENV{SSH_BATCH_PASSWORD};
197 0 ▶ if (!$password) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($password) 0 0 0
198 0 print STDERR "Password:";
199 0 ReadMode(2);
200 0 ▶ while (not defined ($password = ReadLine(0))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($password = ReadLine(0))) 0 0 0
201 }
202 0 ReadMode(0);
203 0 print "\n";
204 0 chomp $password;
205 }
206 0 ▶ if (!$password) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($password) 0 0 0
207 0 die "No password specified.\n";
208 }
209 }
210  
211 0 my (%conns, @pids, @outs);
212 0 my %pid2host;
213 0 my $active_count = 0;
214 0 while (1) {
215 0 ▶ last if !@hosts && !@pids;
Branch: 1 branch
BranchTrueFalseTotal
unless @hosts or @pids 0 0 0
Condition0%0/3
@hosts or @pids
l!l&&r!l&&!r
000
MC/DC0%0/2
@hosts or @pids
@hosts @pids
Truth table0%0/3
A or B
A= @hostsB= @pids
ABresult
1X1
011
000
216 0 my @active_hosts;
217 0 ▶ while ($active_count < $concurrency) {
Branch: 1 branch
BranchTrueFalseTotal
if ($active_count < $concurrency) 0 0 0
218 0 ▶ last if !@hosts;
Branch: 1 branch
BranchTrueFalseTotal
unless @hosts 0 0 0
219 0 my $host = shift @hosts;
220 ## connecting to host: $host
221 0 ▶ my $ssh = Net::OpenSSH->new(
Branch: 5 branches
BranchTrueFalseTotal
defined $timeout ? : 0 0 0
defined $user ? : 0 0 0
defined $port ? : 0 0 0
defined $passphrase ? : 0 0 0
defined $password ? : 0 0 0
222 $host,
223 async => 1,
224 defined $timeout ? (timeout => $timeout) : (),
225 defined $user ? (user => $user) : (),
226 defined $port ? (port => $port) : (),
227 defined $passphrase ? (passphrase => $passphrase) : (),
228 defined $password ? (password => $password) : (),
229 );
230 0 ▶ if ($ssh->error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ssh->error) 0 0 0
231 0 ▶ if ($line_mode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line_mode) { } 0 0 0
232 0 print STDERR "$host: ";
233 } else {
234 0 print "===" x 7, " $host ", "===" x 7, "\n";
235 }
236 0 warn "ERROR: Failed to establish SSH connection: ",
237 $ssh->error, "\n";
238 0 next;
239 }
240 0 $conns{$host} = $ssh;
241 0 $active_count++;
242 0 push @active_hosts, $host;
243 }
244 0 for my $host (@active_hosts) {
245 0 my ($out, $outfile) = tmpnam();
246 0 ▶ my $meth = $use_rsync ? 'rsync_put' : 'scp_put';
Branch: 1 branch
BranchTrueFalseTotal
$use_rsync ? : 0 0 0
247  
248 ## starting transfer to host: $host
249 0 my $ssh = $conns{$host};
250 0 ▶ my $pid = $ssh->$meth({
Branch: 5 branches
BranchTrueFalseTotal
defined $recursive ? : 0 0 0
defined $bwlimit ? : 0 0 0
defined $use_rsync && defined $rsync_archive ? : 0 0 0
defined $use_rsync && defined $rsync_update ? : 0 0 0
defined $use_rsync && defined $rsync_compress ? : 0 0 0
Condition0%0/9
defined $use_rsync and defined $rsync_archive
!ll&&!rl&&r
000
defined $use_rsync and defined $rsync_update
!ll&&!rl&&r
000
defined $use_rsync and defined $rsync_compress
!ll&&!rl&&r
000
MC/DC0%0/2
defined $use_rsync and defined $rsync_archive
defined $use_rsync defined $rsync_archive
MC/DC0%0/2
defined $use_rsync and defined $rsync_update
defined $use_rsync defined $rsync_update
MC/DC0%0/2
defined $use_rsync and defined $rsync_compress
defined $use_rsync defined $rsync_compress
Truth table0%0/3
A and B
A= defined $use_rsyncB= defined $rsync_archive
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $use_rsyncB= defined $rsync_update
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $use_rsyncB= defined $rsync_compress
ABresult
0X0
100
111
251 stdout_fh => $out,
252 stderr_to_stdout => 1,
253 async => 1,
254 defined $recursive ? (recursive => $recursive) : (),
255 defined $bwlimit ? (bwlimit => $bwlimit) : (),
256  
257 defined $use_rsync && defined $rsync_archive ? (archive => 1) : (),
258 defined $use_rsync && defined $rsync_update ? (update => 1) : (),
259 defined $use_rsync && defined $rsync_compress ? (compress => 1) : (),
260 # XXX recursive
261 }, @files, $target_path);
262 0 ▶ if (!defined $pid or $pid == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $pid or $pid == -1) 0 0 0
Condition0%0/3
not defined $pid or $pid == -1
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $pid or $pid == -1
not defined $pid $pid == -1
Truth table0%0/3
not(A) or B
A= not defined $pidB= $pid == -1
ABresult
1X1
011
000
263 0 $active_count--;
264 0 ▶ if ($line_mode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line_mode) { } 0 0 0
265 0 print STDERR "$host: ";
266 } else {
267 0 print "===" x 7, " $host ", "===" x 7, "\n";
268 }
269 0 ▶ if ($ssh->error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ssh->error) { } 0 0 0
270 0 warn "ERROR: ", $ssh->error, "\n";
271 } else {
272 0 warn "ERROR: Failed to transfer files.\n";
273 }
274 0 close $out;
275 0 unlink $outfile;
276 0 delete $conns{$host};
277 0 next;
278 }
279 0 push @outs, $outfile;
280 0 push @pids, $pid;
281 0 $pid2host{$pid} = $host;
282 }
283 0 ▶ if (@pids) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pids) 0 0 0
284 0 my $pid = shift @pids;
285 0 my $host = delete $pid2host{$pid};
286 0 $active_count--;
287 ## waiting transfer to host: $host
288 0 ▶ if (!$line_mode) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line_mode) 0 0 0
289 0 print "===" x 7, " $host ", "===" x 7, "\n";
290 }
291 0 ▶ if (!defined $pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pid) 0 0 0
292 0 warn "Failed to connect to host $host.\n";
293 0 delete $conns{$host};
294 0 next;
295 }
296 0 my $ret = waitpid($pid, 0);
297 0 my $exit = ($? >> 8);
298  
299 0 delete $conns{$host};
300  
301 0 ▶ if ($ret > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret > 0) { } 0 0 0
302 0 ▶ if ($exit > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit > 0) 0 0 0
303 0 warn "$host: Transfer of files failed (status code: $exit)\n";
304 0 next;
305 }
306 } else {
307 #redo if ($! == EINTR);
308 0 ▶ if ($line_mode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line_mode) 0 0 0
309 0 print STDERR "$host: ";
310 }
311 0 warn "$host: waitpid($pid) failed: $!\n";
312 0 next;
313 }
314 0 my $outfile = shift @outs;
315 0 my $in;
316 0 ▶ if (!open $in, $outfile) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $in, $outfile) 0 0 0
317 0 ▶ if ($line_mode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line_mode) 0 0 0
318 0 print STDERR "$host: ";
319 }
320 0 warn "Can't open $outfile for reading: $!\n";
321 0 next;
322 }
323 0 ▶ while (<$in>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $in)) 0 0 0
324 0 chomp;
325 0 ▶ if ($line_mode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line_mode) 0 0 0
326 0 print "$host: ";
327 }
328 0 print "$_\n";
329 }
330 0 ▶ if (!$line_mode) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line_mode) 0 0 0
331 0 print "\n";
332 }
333 0 close $in;
334 }
335 }
336  
337 sub help ($) {
338 2 ▶ my $exit_code = shift;
Subroutine help: called CC=2 SCAR=6.9
339 2 my $msg = <<'_EOC_';
340 USAGE:
341  
342 tonodes [OPTIONS] FILE... -- HOST_PATTERN... [OPTIONS]
343 tonodes [OPTIONS] FILE HOST_PATTERN... [OPTIONS]
344  
345 OPTIONS:
346 -c <num> Set SSH concurrency limit. (default: 20)
347 -b <num> bandwidth limit in Kbits/sec.
348 -g Use glob to process the input files/directories.
349 -h Print this help.
350 -l List the hosts and do nothing else.
351 -L Use the line-mode output format, i.e., prefixing
352 every output line with the machine name.
353 (could be controlled by the env SSH_BATCH_LINE_MODE)
354 -p <port> Port for the remote SSH service.
355 -r Recurse into directories too.
356 -rsync Use "rsync" to transfer files.
357 -archive Enable rsync archive mode
358 -update Enable rsync update
359 -compress Enable rsync compress
360 -t <timeout> Specify timeout for net traffic.
361 -u <user> User account for SSH login.
362 -v Be verbose.
363 -w Prompt for password (used mostly for login and sudo,
364 could be privided by SSH_BATCH_PASSWORD).
365 -P Prompt for passphrase (used mostly for login,
366 could be privided by SSH_BATCH_PASSPHRASE).
367 _EOC_
368 2 ▶ if ($exit_code == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit_code == 0) { } 1 1 2
369 1 print $msg;
370 1 exit(0);
371 } else {
372 1 warn $msg;
373 1 exit($exit_code);
374 }
375 }
376 __END__
« blib/lib/SSH/Batch.pm ↑ Summary bin/fornodes »