blib/lib/App/MultiSsh.pm

filter: Statementstmt 54.7% 93 / 170 Branchbran 29.4% 20 / 68 Conditioncond 11.1% 2 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 75.0% 15 / 20 Podpod 100% 8 / 8 totaltotal 46.6% 138 / 296 CC 46 SCAR 47.9
CC
46
Cov
45%
multi_run
380.0
_read_label_line
42.0
get_groups
30.0
CRAP 120.0
↑ Summary
1 package App::MultiSsh;
2  
3 # Created on: 2014-09-04 17:12:36
4 # Create by: Ivan Wills
5 # $Id$
6 # $Revision$, $HeadURL$, $Date$
7 # $Revision$, $Source$, $Date$
8  
9 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use feature qw/:5.10/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use POSIX qw/ceil/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use Data::Dumper qw/Dumper/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use English qw/ -no_match_vars /;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use base qw/Exporter/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 ▶use POSIX qw/:errno_h/;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 5 our $VERSION = '0.26';
22 5 our @EXPORT_OK = qw/hosts_from_map is_host multi_run shell_quote tmux/;
23 5 our %EXPORT_TAGS = ();
24  
25 12 ▶sub hosts_from_map {
Subroutine hosts_from_map: called CC=5 SCAR=16.1
Pod documented
26 12 my ($map) = @_;
27 12 my @hosts;
28  
29 12 my $int_re = qr/ [0-9a-zA-Z] /xms;
30 12 my $range_re = qr/ ($int_re) (?:[.][.]|-) ($int_re)/xms;
31 12 my $group_re = qr/ (?: $int_re | $range_re ) /xms;
32 12 my $seperated_re = qr/ $group_re (?: , $group_re ) * /xms;
33 12 my $num_range_re = qr/ [[{] ( $seperated_re ) [\]}] /xms;
34  
35 12 ▶ while ( my $host_range = shift @{$map} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $host_range = shift @{$map;}) 14 12 26
36 14 my ($num_range) = $host_range =~ /$num_range_re/;
37  
38 14 ▶ if ( !$num_range ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($num_range) 1 13 14
39 1 push @hosts, $host_range;
40 1 next;
41  
42 #if ( is_host($host_range) ) {
43 # push @hosts, $host_range;
44 # next;
45 #}
46 #else {
47 # unshift @{$hosts}, $host_range;
48 # last;
49 #}
50 }
51  
52 13 ▶ my @numbs = map { /$range_re/ ? ( $1 .. $2 ) : ($_) } split /,/,
Branch: 1 branch
BranchTrueFalseTotal
/$range_re/ ? : 11 8 19
53 $num_range;
54 my @hostmaps =
55 13 map { $a = $host_range; $a =~ s/$num_range_re/$_/e; $a } @numbs;
56  
57 13 ▶ if ( $hostmaps[0] =~ /$num_range_re/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hostmaps[0] =~ /$num_range_re/) { } 1 12 13
58 1 push @{$map}, @hostmaps;
59 }
60 else {
61 12 push @hosts, @hostmaps;
62 }
63 }
64  
65 12 return @hosts;
66 }
67  
68 0 ▶sub is_host {
Subroutine is_host: not called CC=1 SCAR=6.9
Pod documented
69 0 my $full_name = `host $_[0]`;
70 0 return $full_name !~ /not found/;
71 }
72  
73 38 ▶sub shell_quote {
Subroutine shell_quote: called CC=2 SCAR=6.9
Pod documented
74 38 my ($text) = @_;
75  
76 38 ▶ if ( $text =~ /[\s$|><;&*?#]/xms ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /[\s$|><;&*?#]/msx) 34 4 38
77 34 $text =~ s/'/'\\''/gxms;
78 34 $text = "'$text'";
79 }
80  
81 38 return $text;
82 }
83  
84 0 ▶sub multi_run {
Subroutine multi_run: not called CC=19 SCAR=59.4
Pod documented
85 0 my ( $hosts, $remote_cmd, $option ) = @_;
86  
87 0 ▶ if ( $option->{tmux} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option->{"tmux"}) 0 0 0
88 0 my @cmds = map { "ssh $_ " . shell_quote($remote_cmd) } @$hosts;
89 0 ▶ exec tmux( $option, @cmds ) if !$option->{test};
Branch: 1 branch
BranchTrueFalseTotal
unless $option->{"test"} 0 0 0
90 0 print tmux( $option, @cmds ) . "\n";
91 0 return;
92 }
93  
94 # store child processes if forking
95 0 my @children;
96  
97 # loop over each host and run the remote command
98 0 for my $host (@$hosts) {
99 0 my $cmd = "ssh $host " . shell_quote($remote_cmd);
100 0 ▶ print "$cmd\n" if $option->{verbose} > 1 || $option->{test};
Branch: 1 branch
BranchTrueFalseTotal
if $option->{"verbose"} > 1 or $option->{"test"} 0 0 0
Condition0%0/3
$option->{"verbose"} > 1 or $option->{"test"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$option->{"verbose"} > 1 or $option->{"test"}
$option->{"verbose"} > 1 $option->{"test"}
Truth table0%0/3
A or B
A= $option->{"verbose"} > 1B= $option->{"test"}
ABresult
1X1
011
000
101 0 ▶ next if $option->{test};
Branch: 1 branch
BranchTrueFalseTotal
if $option->{"test"} 0 0 0
102  
103 0 ▶ if ( $option->{parallel} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option->{'parallel'}) { } 0 0 0
104 0 my $child = fork;
105  
106 0 ▶ if ($child) {
Branch: 2 branches
BranchTrueFalseTotal
if ($child) { } 0 0 0
elsif (defined $child) { } 0 0 0
107  
108 # parent stuff
109 0 push @children, $child;
110  
111 0 ▶ if ( @children == $option->{parallel} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@children == $option->{"parallel"}) 0 0 0
112 warn "Waiting for children to finish\n"
113 0 ▶ if $option->{verbose} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $option->{"verbose"} > 1 0 0 0
114  
115 # reap children if reached max fork count
116 0 ▶ while ( my $pid = shift @children ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pid = shift @children) 0 0 0
117 0 waitpid $pid, 0;
118 }
119 }
120 }
121 elsif ( defined $child ) {
122  
123 # child code
124 0 ▶ if ( $option->{interleave} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option->{'interleave'}) { } 0 0 0
125 0 ▶ print "$host -\n" if $option->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $option->{"verbose"} 0 0 0
126  
127 0 require IPC::Open3::Callback;
128 0 my ( $pid, $in, $out, $err ) =
129 IPC::Open3::Callback::safe_open3($cmd);
130  
131 0 close $in;
132 0 $out->blocking(0);
133 0 $err->blocking(0);
134 0 ▶ while ( $out && $err ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($out and $err) 0 0 0
Condition0%0/3
$out and $err
!ll&&!rl&&r
000
MC/DC0%0/2
$out and $err
$out $err
Truth table0%0/3
A and B
A= $outB= $err
ABresult
0X0
100
111
135 0 $out = _read_label_line( $out, \*STDOUT, $host );
136 0 $err = _read_label_line( $err, \*STDERR, $host );
137 }
138 0 waitpid $pid, 0;
139 0 exit 0;
140 }
141 else {
142 0 my $out = `$cmd 2>&1`;
143  
144 0 ▶ print "$host -\n" if $option->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $option->{"verbose"} 0 0 0
145 0 print $out;
146 }
147 0 exit;
148 }
149 else {
150 0 die "Error: $!\n";
151 }
152 }
153 else {
154 0 ▶ print "$host -\n" if $option->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $option->{"verbose"} 0 0 0
155 0 system $cmd;
156 }
157 }
158  
159 # reap any outstanding children
160 0 wait;
161 }
162  
163 0 ▶sub _read_label_line {
Subroutine _read_label_line: not called CC=6 SCAR=37.4
164 0 my ( $in_fh, $out_fh, $host ) = @_;
165 0 state %hosts;
166 0 my @colours = (
167 qw/
168 red on_red bright_red
169 green on_green bright_green
170 blue on_blue bright_blue
171 magenta on_magenta bright_magenta
172 cyan on_cyan
173 yellow on_yellow
174 /
175 );
176 0 ▶ return if !$in_fh;
Branch: 1 branch
BranchTrueFalseTotal
unless $in_fh 0 0 0
177  
178 0 my $line = <$in_fh>;
179  
180 0 ▶ if ( !defined $line && $! != EAGAIN ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $line and $! != 11) 0 0 0
Condition0%0/3
not defined $line and $! != 11
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $line and $! != 11
not defined $line $! != 11
Truth table0%0/3
not(A) and B
A= not defined $lineB= $! != 11
ABresult
0X0
100
111
181 0 close $in_fh;
182 0 return;
183 }
184  
185 0 ▶ if ( defined $line ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line) 0 0 0
186 0 ▶ $hosts{$host} ||= $colours[ rand @colours ];
Condition0%0/3
$hosts{$host} ||= $colours[rand @colours]
l!l&&r!l&&!r
000
MC/DC0%0/2
$hosts{$host} ||= $colours[rand @colours]
$hosts{$host} $colours[rand @colours]
Truth table0%0/3
A ||= B
A= $hosts{$host}B= $colours[rand @colours]
ABresult
1X1
011
000
187 0 require Term::ANSIColor;
188 0 print {$out_fh} '[', Term::ANSIColor::colored( $host, $hosts{$host} ),
189 '] ', $line;
190 }
191  
192 0 return $in_fh;
193 }
194  
195 5 ▶sub tmux {
Subroutine tmux: called CC=8 SCAR=22.0
Pod documented
196 5 my ( $option, @commands ) = @_;
197  
198 5 ▶ confess "No commands for tmux to run!\n" if !@commands;
Branch: 1 branch
BranchTrueFalseTotal
unless @commands 0 5 5
199  
200 5 my $layout = layout(@commands);
201 5 my $tmux = '';
202 5 my $final = '';
203 5 my $pct = int( 100 / scalar @commands );
204  
205 5 for my $ssh (@commands) {
206 15 ▶ if ( !$tmux && $option->{tmux_nested} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $tmux and $option->{'tmux_nested'}) { } 0 15 15
Condition66%2/3
not $tmux and $option->{"tmux_nested"}
!ll&&!rl&&r
1050
MC/DC0%0/2
not $tmux and $option->{"tmux_nested"}
not $tmux $option->{"tmux_nested"}
Truth table66%2/3
not(A) and B
A= not $tmuxB= $option->{"tmux_nested"}
ABresult
0X0
100
111
207 0 $tmux = ' rename-window mssh';
208 0 $final =
209 '; bash -c '
210 . shell_quote(
211 "echo $ssh; echo 'set-window-option synchronize-panes on|off'")
212 . '\\;'
213 . shell_quote($ssh);
214 }
215 else {
216 15 ▶ my $cmd = !$tmux ? 'new-session' : '\\; split-window -d -p ' . $pct;
Branch: 1 branch
BranchTrueFalseTotal
!$tmux ? : 5 10 15
217  
218 15 $tmux .=
219 " $cmd " . shell_quote("echo $ssh") . '\\;' . shell_quote($ssh);
220 }
221 }
222  
223 5 ▶ my $sync = $option->{tmux_sync} ? 'on' : 'off';
Branch: 1 branch
BranchTrueFalseTotal
$option->{'tmux_sync'} ? : 0 5 5
224 5 ▶ $tmux .= " \\; set-window-option synchronize-panes $sync"
Branch: 1 branch
BranchTrueFalseTotal
unless $commands[0] =~ /\s$/msx 0 5 5
225 if $commands[0] !~ /\s$/xms;
226  
227 return
228 5 "tmux$tmux \\; select-layout tiled \\; setw synchronize-panes $sync$final";
229 }
230  
231 16 ▶sub layout {
Subroutine layout: called CC=5 SCAR=16.1
Pod documented
232 16 my (@commands) = @_;
233 16 my $rows = int sqrt @commands + 1;
234 16 my $cols = ceil @commands / $rows;
235 16 my $out = [];
236 16 ▶ if ( $cols > $rows + 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cols > $rows + 1) 1 15 16
237 1 my $tmp = $rows;
238 1 $rows++;
239 1 $cols--;
240 }
241 ROW:
242 16 for my $row ( 0 .. $rows - 1 ) {
243 33 for my $col ( 0 .. $cols - 1 ) {
244 89 ▶ last ROW if !@commands;
Branch: 1 branch
BranchTrueFalseTotal
unless @commands 8 81 89
245 81 $out->[$row][$col] = shift @commands;
246 }
247 }
248  
249 16 return $out;
250 }
251  
252 0 ▶sub config {
Subroutine config: not called CC=3 SCAR=24.8
Pod documented
253 0 state $config;
254 0 ▶ return $config if $config;
Branch: 1 branch
BranchTrueFalseTotal
if $config 0 0 0
255  
256 0 my $config_file = path( $ENV{HOME}, '.mssh' );
257 0 ▶ if ( !-f $config_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $config_file) 0 0 0
258 0 $config = {};
259  
260 # create a default config file
261 0 $config_file->spew("---\ngroups:\n eg:\n - host1\n - host2\n");
262  
263 0 return $config;
264 }
265  
266 0 require YAML;
267 0 $config = YAML::LoadFile($config_file);
268  
269 0 return $config;
270 }
271  
272 0 ▶sub get_groups {
Subroutine get_groups: not called CC=5 SCAR=34.0
Pod documented
273 0 my ($groups) = @_;
274 0 my $config = config();
275 0 my @hosts;
276  
277 0 for my $group (@$groups) {
278 0 ▶ if ( $config->{groups} && $config->{groups}{$group} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{'groups'} and $config->{'groups'}{$group}) { } 0 0 0
Condition0%0/3
$config->{"groups"} and $config->{"groups"}{$group}
!ll&&!rl&&r
000
MC/DC0%0/2
$config->{"groups"} and $config->{"groups"}{$group}
$config->{"groups"} $config->{"groups"}{$group}
Truth table0%0/3
A and B
A= $config->{"groups"}B= $config->{"groups"}{$group}
ABresult
0X0
100
111
279 push @hosts,
280 ref $config->{groups}{$group}
281 0 ? @{ $config->{groups}{$group} }
282 0 ▶ : $config->{groups}{$group};
Branch: 1 branch
BranchTrueFalseTotal
ref $config->{'groups'}{$group} ? : 0 0 0
283 }
284 else {
285 0 warn "No host group '$group' defined in the config!\n";
286 }
287 }
288  
289 0 return @hosts;
290 }
291  
292 5 1;
293  
294 __END__
↑ Summary