bin/atnodes

filter: Statementstmt 44.0% 92 / 209 Branchbran 30.6% 41 / 134 Conditioncond 29.4% 5 / 17 MC/DCmcdc 18.1% 2 / 11 Subroutinesub 73.3% 11 / 15 Podpod n/a totaltotal 39.1% 151 / 386 CC 6 SCAR 14.6
CC
6
Cov
38%
check_openssh_version
13.1
help
2.1
__ANON__
2.0
CRAP 4.3
« lib/SSH/Batch/ForNodes.pm ↑ Summary blib/lib/SSH/Batch.pm »
1 #!/usr/bin/env perl
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 #use Smart::Comments::JSON '##';
7 7 ▶use lib 'lib';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Net::OpenSSH;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Term::ReadKey;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use SSH::Batch::ForNodes;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use File::Temp qw/ :POSIX /;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use Time::HiRes qw/sleep/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 sub help ($);
15 sub check_openssh_version ($);
16  
17 7 ▶if (!@ARGV) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@ARGV) 1 6 7
18 1 warn "No argument specified.\n\n";
19 1 help(1);
20 }
21  
22 6 my $list_hosts_only = 0;
23 6 my ($user, $port, $timeout, $verbose, $ask_for_pass, $ask_for_passphrase);
24 6 my $concurrency = 20;
25 6 my (@cmd, @exprs, $ssh_cmd);
26 6 $ssh_cmd = $ENV{SSH_BATCH_SSH_CMD};
27  
28 6 my $fetch_value;
29 my $found_sep;
30 6 my $last_option;
31 6 my $use_tty;
32 6 my $use_quiet_mode;
33 6 my $line_mode = $ENV{SSH_BATCH_LINE_MODE};
34 6 for (@ARGV) {
35 36 ▶ if (defined $fetch_value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fetch_value) 1 35 36
36 1 $fetch_value->($_);
37 1 undef $fetch_value;
38 1 next;
39 }
40 35 ▶ if ($_ eq '--') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq "--") 2 33 35
41 2 @cmd = @exprs;
42 2 @exprs = ();
43 2 $found_sep = 1;
44 2 next;
45 }
46 33 ▶ if (/^-(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^-(.*)/) 6 27 33
47 6 my $group = $1;
48 6 ▶ if ($group eq 'l') {
Branch: 13 branches
BranchTrueFalseTotal
if ($group eq 'l') { } 2 4 6
elsif ($group eq 'u') { } 1 3 4
elsif ($group eq 't') { } 0 3 3
elsif ($group eq 'h') { } 0 3 3
elsif ($group eq 'p') { } 0 3 3
elsif ($group eq 'v') { } 2 1 3
elsif ($group eq 'w') { } 0 1 1
elsif ($group eq 'P') { } 0 1 1
elsif ($group eq 'c') { } 0 1 1
elsif ($group eq 'ssh') { } 1 0 1
elsif ($group eq 'L') { } 0 0 0
elsif ($group eq 'tty') { } 0 0 0
elsif ($group eq 'q') { } 0 0 0
49 2 $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 0 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 2 $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 'c') {
65 0 ▶ $fetch_value = sub { $concurrency = shift };
Subroutine __ANON__: not called CC=1 SCAR=6.9
66 } elsif ($group eq 'ssh') {
67 1 ▶ $fetch_value = sub { $ssh_cmd = shift };
Subroutine __ANON__: called CC=1 SCAR=0.0
68 } elsif ($group eq 'L') {
69 0 $line_mode = 1;
70 } elsif ($group eq 'tty') {
71 0 $use_tty = 1;
72 } elsif ($group eq 'q') {
73 0 $use_quiet_mode = 1;
74 } else {
75 0 die "Unknown option: $_\n";
76 }
77 6 $last_option = $_;
78 6 next;
79 }
80 27 push @exprs, $_;
81 }
82 6 ▶if (defined $fetch_value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fetch_value) 1 5 6
83 1 die "ERROR: Option $last_option takes a value.\n";
84 }
85  
86 5 ▶if (!$found_sep && !@cmd) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found_sep or @cmd) 3 2 5
Condition66%2/3
$found_sep or @cmd
l!l&&r!l&&!r
203
MC/DC50%1/2
$found_sep or @cmd
$found_sep @cmd
Truth table66%2/3
A or B
A= $found_sepB= @cmd
ABresult
1X1
011
000
87 3 push @cmd, shift @exprs;
88 }
89 5 ▶if (!@cmd) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@cmd) 1 4 5
90 1 die "No command specified.\n";
91 }
92  
93 4 ▶if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 2 2 4
94 2 warn "Command: ", (map { "[$_]" } @cmd), "\n";
95 2 ▶ if (defined $ssh_cmd) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ssh_cmd) 1 1 2
96 1 warn "Using SSH program [$ssh_cmd].\n";
97 }
98 }
99  
100 4 ▶check_openssh_version($ssh_cmd || 'ssh');
Condition100%2/2
$ssh_cmd || 'ssh'
l!l
13
MC/DC100%1/1
$ssh_cmd || 'ssh'
$ssh_cmd
Truth table100%2/2
A
A= $ssh_cmd
Aresult
11
00
101  
102 4 ▶if ($use_tty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_tty) 0 4 4
103 0 $concurrency = 1;
104 }
105  
106 4 ▶if (!@exprs) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@exprs) 1 3 4
107 1 die "No cluster expression specified.\n";
108 }
109 3 my $expr = join ' ', @exprs;
110  
111 3 ▶if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 2 1 3
112 2 warn "Cluster expression: $expr\n";
113 }
114  
115 3 SSH::Batch::ForNodes::init_rc();
116 2 my $set = SSH::Batch::ForNodes::parse_expr($expr);
117  
118 2 ▶if ($set->is_empty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($set->is_empty) 0 2 2
119 0 die "No machine to be operated.\n";
120 }
121 2 my @hosts = sort $set->elements;
122  
123 2 ▶if ($verbose) {
Branch: 2 branches
BranchTrueFalseTotal
if ($verbose) { } 2 0 2
elsif ($list_hosts_only) { } 0 0 0
124 2 warn "Cluster set: @hosts\n";
125 } elsif ($list_hosts_only) {
126 0 print "Cluster set: @hosts\n";
127 }
128  
129 2 ▶if ($list_hosts_only) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list_hosts_only) 2 0 2
130 2 exit(0);
131 }
132  
133 0 my ($passphrase, $password);
134 0 ▶if ($ask_for_passphrase) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ask_for_passphrase) { } 0 0 0
elsif ($ask_for_pass) { } 0 0 0
135 0 $passphrase = $ENV{SSH_BATCH_PASSPHRASE};
136 0 ▶ if (!$passphrase) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($passphrase) 0 0 0
137 0 print STDERR "Passphrase:";
138 0 ReadMode(2);
139 0 ▶ while (not defined ($passphrase = ReadLine(0))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($passphrase = ReadLine(0))) 0 0 0
140 }
141 0 ReadMode(0);
142 0 print "\n";
143 0 chomp $passphrase;
144 }
145 0 ▶ if (!$passphrase) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($passphrase) 0 0 0
146 0 die "No passphrase specified.\n";
147 }
148 } elsif ($ask_for_pass) {
149 0 $password = $ENV{SSH_BATCH_PASSWORD};
150 0 ▶ if (!$password) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($password) 0 0 0
151 0 print STDERR "Password:";
152 0 ReadMode(2);
153 0 ▶ while (not defined ($password = ReadLine(0))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($password = ReadLine(0))) 0 0 0
154 }
155 0 ReadMode(0);
156 0 print "\n";
157 0 chomp $password;
158 }
159 0 ▶ if (!$password) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($password) 0 0 0
160 0 die "No password specified.\n";
161 }
162 }
163  
164 0 my (%conns, @pids, @outs);
165 0 my %pid2host;
166 0 my $active_count = 0;
167 0 while (1) {
168 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
169 0 my @active_hosts;
170 0 ▶ while ($active_count < $concurrency) {
Branch: 1 branch
BranchTrueFalseTotal
if ($active_count < $concurrency) 0 0 0
171 0 ▶ last if !@hosts;
Branch: 1 branch
BranchTrueFalseTotal
unless @hosts 0 0 0
172 0 my $host = shift @hosts;
173 0 ▶ my $ssh = Net::OpenSSH->new(
Branch: 7 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
defined $ssh_cmd ? : 0 0 0
$use_quiet_mode ? : 0 0 0
174 $host,
175 async => 1,
176 defined $timeout ? (timeout => $timeout) : (),
177 defined $user ? (user => $user) : (),
178 defined $port ? (port => $port) : (),
179 defined $passphrase ? (passphrase => $passphrase) : (),
180 defined $password ? (password => $password) : (),
181 defined $ssh_cmd ? (ssh_cmd => $ssh_cmd) : (),
182 $use_quiet_mode
183 ? (master_opts => ["-q",], default_ssh_opts => ["-q",],)
184 : (),
185 );
186 0 ▶ if ($ssh->error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ssh->error) 0 0 0
187 0 ▶ if ($line_mode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line_mode) { } 0 0 0
188 0 print STDERR "$host: ";
189 } else {
190 0 print "===" x 7, " $host ", "===" x 7, "\n";
191 }
192 0 warn "ERROR: Failed to establish SSH connection: ",
193 $ssh->error, "\n";
194 0 next;
195 }
196 0 $conns{$host} = $ssh;
197 0 $active_count++;
198 0 push @active_hosts, $host;
199 }
200 0 for my $host (@active_hosts) {
201 0 my ($out, $outfile) = tmpnam();
202 0 my $ssh = $conns{$host};
203 0 ▶ my $pid = $ssh->system({
Branch: 2 branches
BranchTrueFalseTotal
defined $password ? : 0 0 0
defined $use_tty ? : 0 0 0
204 (defined $password?
205 (stdin_data => "$password\n") : ()),
206 stdout_fh => $out,
207 stderr_to_stdout => 1,
208 async => 1,
209 defined $use_tty ? (tty => 1) : (),
210 }, @cmd);
211 #warn "PID: $pid\n";
212 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
213 0 $active_count--;
214 0 ▶ if ($line_mode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line_mode) { } 0 0 0
215 0 print STDERR "$host: ";
216 } else {
217 0 print "===" x 7, " $host ", "===" x 7, "\n";
218 }
219 0 ▶ if ($ssh->error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ssh->error) { } 0 0 0
220 0 warn "ERROR: ", $ssh->error, "\n";
221 } else {
222 0 warn "ERROR: Failed to spawn command.\n";
223 }
224 0 close $out;
225 0 unlink $outfile;
226 0 delete $conns{$host};
227 0 next;
228 }
229 0 push @outs, $outfile;
230 0 push @pids, $pid;
231 0 $pid2host{$pid} = $host;
232 }
233 0 ▶ if (@pids) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pids) 0 0 0
234 0 my $pid = shift @pids;
235 0 my $host = delete $pid2host{$pid};
236 0 $active_count--;
237 0 ▶ if (!$line_mode) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line_mode) 0 0 0
238 0 print "===" x 7, " $host ", "===" x 7, "\n";
239 }
240 0 ▶ if (!defined $pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pid) 0 0 0
241 0 warn "ERROR: Failed to connect to host $host.\n";
242 0 delete $conns{$host};
243 0 next;
244 }
245 0 my $exit = 0;
246 0 my $ret = waitpid($pid, 0);
247 0 $exit = ($? >> 8);
248  
249 0 delete $conns{$host};
250  
251 0 ▶ unless ($ret > 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret > 0) 0 0 0
252 #redo if ($! == EINTR);
253 0 warn "$host: ERROR: waitpid($pid) failed: $!\n";
254 0 next;
255 }
256  
257 0 my $outfile = shift @outs;
258  
259 0 my $in;
260 0 ▶ if (!open $in, $outfile) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $in, $outfile) 0 0 0
261 0 warn "ERROR: Can't open $outfile for reading: $!\n";
262 0 next;
263 }
264 0 ▶ while (<$in>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $in)) 0 0 0
265 0 chomp;
266 0 ▶ if ($line_mode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line_mode) 0 0 0
267 0 print "$host: ";
268 }
269 0 print "$_\n";
270 }
271 0 ▶ if ($exit > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit > 0) 0 0 0
272 0 ▶ if ($line_mode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line_mode) 0 0 0
273 0 print STDERR "$host: ";
274 }
275 0 warn "Remote command returns status code $exit.\n";
276 }
277 0 ▶ if (!$line_mode) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line_mode) 0 0 0
278 0 print "\n";
279 }
280 0 close $in;
281 0 unlink $outfile;
282 }
283 }
284  
285 ## HERE...
286  
287 sub help ($) {
288 1 ▶ my $exit_code = shift;
Subroutine help: called CC=2 SCAR=7.6
289 1 my $msg = <<'_EOC_';
290 USAGE:
291  
292 atnodes [OPTIONS] COMMAND... -- HOST_PATTERN... [OPTIONS]
293 atnodes [OPTIONS] COMMAND HOST_PATTERN... [OPTIONS]
294  
295 OPTIONS:
296 -c <num> Set SSH concurrency limit. (default: 20,
297 when -tty is on, this setting will no use)
298 -h Print this help.
299 -l List the hosts and do nothing else.
300 -L Use the line-mode output format, i.e., prefixing
301 every output line with the machine name.
302 (could be controlled by the env SSH_BATCH_LINE_MODE)
303 -p <port> Port for the remote SSH service.
304 -ssh <path> Specify an alternate ssh program.
305 (This overrides the SSH_BATCH_SSH_CMD environment.)
306 -t <timeout> Specify timeout for net traffic.
307 -u <user> User account for SSH login.
308 -v Be verbose.
309 -w Prompt for password (used for login and sudo,
310 could be privided by SSH_BATCH_PASSWORD).
311 -P Prompt for passphrase (used for login,
312 could be privided by SSH_BATCH_PASSPHRASE).
313 -tty Pseudo-tty.
314 -q Run SSH in quiet mode
315 _EOC_
316 1 ▶ if ($exit_code == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit_code == 0) { } 0 1 1
317 0 print $msg;
318 0 exit(0);
319 } else {
320 1 warn $msg;
321 1 exit($exit_code);
322 }
323 }
324  
325 sub check_openssh_version ($) {
326 4 ▶ my $ssh_cmd = shift;
Subroutine check_openssh_version: called CC=5 SCAR=25.7
327  
328 4 my $version_info = `$ssh_cmd -V 2>&1`;
329 4 ▶ if ($version_info && $version_info =~ /^OpenSSH_(\d+\.\d+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version_info and $version_info =~ /^OpenSSH_(\d+\.\d+)/) 0 4 4
Condition33%1/3
$version_info and $version_info =~ /^OpenSSH_(\d+\.\d+)/
!ll&&!rl&&r
400
MC/DC0%0/2
$version_info and $version_info =~ /^OpenSSH_(\d+\.\d+)/
$version_info $version_info =~ /^OpenSSH_(\d+\.\d+)/
Truth table33%1/3
A and B
A= $version_infoB= $version_info =~ /^OpenSSH_(\d+\.\d+)/
ABresult
0X0
100
111
330 0 my $v = $1;
331 0 ▶ if ($v && $v < 4.1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v and $v < "4.1") 0 0 0
Condition0%0/3
$v and $v < "4.1"
!ll&&!rl&&r
000
MC/DC0%0/2
$v and $v < "4.1"
$v $v < "4.1"
Truth table0%0/3
A and B
A= $vB= $v < "4.1"
ABresult
0X0
100
111
332 0 die "OpenSSH version $v, should be >= 4.1!\n";
333 }
334 }
335 }
336  
337 __END__
« lib/SSH/Batch/ForNodes.pm ↑ Summary blib/lib/SSH/Batch.pm »