-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathDshPerlHostLoop.pm
734 lines (580 loc) · 19 KB
/
DshPerlHostLoop.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
package DshPerlHostLoop;
###########################################################################
# #
# Cluster Tools: DshPerlHostLoop.pm #
# Copyright 2007-2011, Albert P. Tobey <[email protected]> #
# #
###########################################################################
use strict;
use warnings;
use Carp;
use File::Basename;
use Data::Dumper;
use IPC::Open3;
use IO::Select;
use IO::Handle;
use Sys::Hostname ();
use Fcntl ':flock';
use File::Temp qw(tempfile);
use Tie::IxHash;
eval { use Net::SSH2; }; # optional
use base 'Exporter';
# globals!
our @opt_filter_excl;
our @opt_filter_incl;
our $opt_batch;
our $ssh_options .= " -o 'BatchMode yes' -o 'StrictHostKeyChecking no' -o 'ConnectTimeout 10'";
our $tag_output = 1;
our $debug = undef;
our $verbose;
our $tempdir = '/var/tmp';
our $mainpid = $$;
our $machines_list ||= "$ENV{HOME}/.dsh/machines.list"; # can be overridden with --list $name
our @tempfiles;
our $remote_user ||= $ENV{USER};
our $sshkey ||= "$ENV{HOME}/.ssh/id_rsa";
our $retry_wait = 30;
our $lock_fh = tempfile();
our $hostname_pad = 8;
# Most shops have a noisy /etc/issue.net. This reads the local issue.net
# and removes any lines matching it from the output from ssh.
my @issue = read_issue();
my $issue_len = length(join("\n", @issue));
use constant BLACK => "\x1b[30m";
use constant RED => "\x1b[31m";
use constant GREEN => "\x1b[32m";
use constant YELLOW => "\x1b[33m";
use constant BLUE => "\x1b[34m";
use constant MAGENTA => "\x1b[35m";
use constant CYAN => "\x1b[36m";
use constant WHITE => "\x1b[37m";
use constant DKGRAY => "\x1b[1;30m";
use constant DKRED => "\x1b[1;31m";
use constant RESET => "\x1b[0m";
# please, for the love of FSM, do not copy the style of this module
# @EXPORT is the kind of thing that makes sense when you quickly turn
# a utility (in this case, cl-run.pl's predecessor) into a module so
# you can whip up a bunch of look-alike utilities. The right thing to
# do from the start is to design a clean module, at least using
# class methods so their origin is clearly visible in downstream source.
# It's on my TODO list ;)
our @EXPORT = qw(
func_loop ssh scp hostlist reap verbose my_tempfile tag_output
libssh2_connect libssh2_reconnect libssh2_slurp_cmd
$ssh_options $remote_user $retry_wait $hostname_pad
@opt_filter_excl @opt_filter_incl $opt_batch
lock unlock
BLACK RED GREEN YELLOW BLUE MAGENTA CYAN WHITE DKGRAY DKRED RESET
);
=head1 NAME
DshPerlHostLoop - loops for running ssh commands across large clusters in parallel
=head1 SYNOPSIS
use FindBin qw($Bin);
require "$Bin/DshPerlHostLoop.pm";
func_loop( sub { system( "ssh $_[1] uname -a" ); } );
=head1 GLOBAL SWITCHES
A few global CLI switches are implemented in this module in a BEGIN block.
--incl - a perl regular expression that filters out non-matching hostnames
--excl - a perl regular expression that filters matched hostnames out of the list
--batch - run in parallel on every N nodes, shifting by 1 until all are complete
--list - name of the list, e.g. ~/.dsh/machines.$NAME
--root - set remote user to root
--user - set the remote username to something other than $USER or root
-u - don't prefix output with the remote hostname
-v - verbose
-m - specify a file with a list of hosts to use (default is ~/.dsh/machines.list)
--excl RE's are run before --incl RE's.
=head1 FUNCTIONS
=over 4
=item func_loop()
Execute a callback in parallel for each host. The first argument passed to each callback will be the hostname.
# hello, cruel world
func_loop( sub { print "$@\n"; } );
=cut
sub func_loop {
my $f = shift;
if ( ref($f) ne 'CODE' ) {
confess "Argument to DshPerlHostLoop must be a subroutine/closure.";
}
my %pids;
tie my %hosts, 'Tie::IxHash';
%hosts = hostlist(keep_comments => 1);
my @hostnames = keys %hosts;
# support batched commands in increments of $opt_batch
# This is useful for large clusters where doing the whole cluster at once
# is a bad idea. Set --batch 1 for serial execution.
my @batches = ();
$opt_batch ||= 0;
if ($opt_batch > 0) {
my $batch_count = scalar(@hostnames) / $opt_batch;
for (my $b=0; $b<$batch_count; $b++) {
for (my $h=0; $h<$opt_batch; $h++) {
my $host = shift(@hostnames);
push @{$batches[$b]}, $host;
}
}
}
# default to one batch of all hosts
else {
@batches = (\@hostnames);
}
foreach my $batch (@batches) {
foreach my $hostname (@{$batch}) {
my $pid = fork();
if ( $pid ) {
$pids{$hostname} = $pid;
next;
}
else {
eval { $0 = "$0 -- $hostname"; };
my @out = eval { $f->( $hostname, $hosts{$hostname} ); };
if ( $@ ) {
confess $@;
}
exit 0;
}
}
# should block until all commands exit
reap( \%pids );
}
}
=item read_issue()
Read /etc/issue.net or if that doesn't exist, /etc/issue. This is used to filter out
issues from remote systems to keep your output readable.
Returns an array of chomped lines.
=cut
sub read_issue {
my @issue;
my $fh;
if ( -r '/etc/issue.net' ) {
open( my $fh, "< /etc/issue.net" ) or $fh = undef;
}
elsif ( -r '/etc/issue' ) {
open( my $fh, "< /etc/issue" ) or $fh = undef;
}
if ( $fh ) {
while ( my $line = <$fh> ) {
chomp $line;
push @issue, $line;
}
close $fh;
}
return @issue;
}
=item ssh()
Run a command over ssh.
func_loop(sub {
my $host = shift;
ssh( $host, 'ps -ef' );
});
=item scp()
scp a file.
my($local_file, $remote_file) = ("/etc/hosts", "/etc/hosts");
func_loop(sub {
my $host = shift;
scp( $local_file, "$host:$remote_file" );
});
=item scmd()
The actual function behind ssh/scp.
sub ssh { scmd('/usr/bin/ssh', @_) }
sub scp { scmd('/usr/bin/scp', '-v', @_) }
=cut
# archaic & insecure but fast and convenient
# in other words, don't let untrusted people sudo this!!
sub ssh { scmd('/usr/bin/ssh', '-o', "'User $remote_user'", @_) }
sub scp { scmd('/usr/bin/scp', '-o', "'User $remote_user'", '-v', @_) }
sub scmd {
my $scmd = shift;
my @output;
my( $in, $out, $err ) = (IO::Handle->new, IO::Handle->new, IO::Handle->new);
my $pid = open3( $in, $out, $err, "$scmd $ssh_options @_" );
if ( $verbose ) {
print STDERR "Command($$): $scmd $ssh_options @_\n";
}
my $select = IO::Select->new( $out, $err );
my $ofd = fileno($out);
my $efd = fileno($err);
my %eofs;
my $bytes = 0;
SELECT: while ( my @ready = $select->can_read(10) ) {
READY: foreach my $r ( @ready ) {
my $rfd = fileno($r);
if ( exists($eofs{$rfd}) ) {
$select->remove($rfd);
next SELECT;
}
elsif ( $rfd == $ofd ) {
my $line = <$out>;
chomp $line if ( $line );
$bytes += length($line) if ( $line );
push @output, $line if ( $line );
$eofs{$rfd} = 1 if ( eof($out) );
}
elsif ( $rfd == $efd ) {
my $line = <$err>;
chomp $line if ( $line );
# don't look for issue.net matches after its byte size has past
unless ( !$line or ($bytes < $issue_len and grep { $_ eq $line } @issue) ) {
$bytes += length($line) if ( $line );
# TODO: probably should detect a terminal or have an option to disable color
push @output, RED . $line . RESET;
}
$eofs{$rfd} = 1 if ( eof($err) );
}
else {
warn "Got read error on $rfd ...";
}
}
}
# this usually means success
if ( $bytes == 0 ) {
push @output, "''";
printf STDERR "%sGot zero bytes from command: $scmd @_%s\n", CYAN, RESET if ($verbose);
}
waitpid( $pid, 0 );
if ( $? != 0 ) {
printf STDERR "%sGot non-zero exit status from command: $scmd @_%s\n", RED, RESET;
}
return @output;
}
=item libssh2_connect()
Connect to the remote host over SSH using Net::SSH2 instead
of shelling out. This is a bit more efficient over the long run,
but does not work with ssh agent, and therefore doesn't work
with encrypted ssh keys.
=cut
# sets up the ssh2 connection
sub libssh2_connect {
my( $hostname, $port, $bundle ) = @_;
$port ||= 22;
# TODO: make this configurable
my @keys = (
# my monitor-rsa key is unencrypted to work with Net::SSH2
# it is restricted to '/bin/cat /proc/net/dev etc.' though so very low risk
[
$remote_user,
$ENV{HOME}.'/.ssh/monitor-rsa.pub',
$ENV{HOME}.'/.ssh/monitor-rsa'
],
[
$remote_user,
$sshkey . '.pub', # this should usually be correct
$sshkey # settable on the CLI with -i
]
);
my $ssh2 = Net::SSH2->new();
my $ok;
for (my $i=0; $i<@keys; $i++) {
$ssh2->connect( $hostname, $port, Timeout => 3 );
$ok = $ssh2->auth_agent( $remote_user );
last if ($ok);
$ok = $ssh2->auth_publickey( @{$keys[$i]} );
last if ($ok);
printf STDERR "%sFailed authentication as user %s with pubkey %s, trying %s:%s%s\n",
RED, $keys[0]->[0], $keys[0]->[1], $keys[1]->[0], $keys[1]->[1], RESET;
}
$ok or die "Could not authenticate.";
if ($ssh2) {
if ($bundle) {
$bundle->host($hostname);
$bundle->port($port);
$bundle->ssh2($ssh2);
}
else {
$bundle = bless {
host => $hostname,
port => $port,
ssh2 => $ssh2
}, 'DshPerlHostLoop::Bundle';
}
}
else {
$bundle->ssh2(undef);
$bundle->last_attempt(time);
}
return $bundle;
}
sub libssh2_reconnect {
my $bundle = shift;
# on connection failures, wait a minute and try again until it works
if (not defined $bundle->ssh2) {
if ($bundle->next_attempt < time) {
printf "%sretrying connection to %s ...", BLUE, $bundle->host;
eval {
libssh2_connect( $bundle->host, $bundle->port, $bundle );
};
if ($@) {
print RED, "FAILED. Trying again in $retry_wait seconds.\n";
$bundle->ssh2(undef);
$bundle->next_attempt(time + $retry_wait);
$bundle->retries($bundle->retries + 1);
return undef;
}
else {
$bundle->retries(0);
print GREEN, "SUCCESS!\n";
}
}
else {
return;
}
}
return $bundle;
}
=item libssh2_slurp_cmd()
Run a command over an existing libssh2 connection and capture all
of its output.
my $input = libssh2_slurp_cmd( $ssh2, $command );
=cut
sub libssh2_slurp_cmd {
my( $bundle, $cmd ) = @_;
libssh2_reconnect( $bundle ) unless ( ref $bundle && $bundle->ssh2 );
unless ($bundle && $bundle->ssh2) {
return undef;
}
my $data = '';
eval {
my $chan = $bundle->ssh2->channel();
$chan->exec( $cmd );
while ( !$chan->eof() ) {
$chan->read( my $buffer, 4096 );
$data .= $buffer;
}
$chan->close();
};
if ( $@ ) {
$bundle->ssh2(undef);
$bundle->next_attempt(time + $retry_wait);
$bundle->retries(0);
return undef;
}
else {
return [split(/[\r\n]+/, $data)];
}
}
=item hostlist()
Returns an array of hosts to be accessed. This reads the hostname list (-m $file or default ~/.dsh/machines.list)
then filters it based on --excl and --incl regular expressions.
my @hosts = hostlist();
# use Tie::IxHash to preserve insertion order if desired
tie my %hosts_and_comments, 'Tie::IxHash';
%hosts_and_comments = hostlist(want_comments => 1);
=cut
sub hostlist {
my %options = @_;
my @hostlist;
open( my $fh, "< $machines_list" )
or die "Could not open machine list file '$machines_list' for reading: $!";
HOST: while ( my $line = <$fh> ) {
chomp $line;
next unless ( $line && length $line );
next if ( $line =~ /^\s*#/ );
my($hostname, $comment) = split( /\s*#\s*/, $line, 2 );
$hostname =~ s/\s//g;
$comment ||= '';
$comment =~ s/^\s+//;
$comment =~ s/\s+$//;
next unless ( length $hostname );
FILTER_EX: foreach my $excl ( @opt_filter_excl ) {
if ( $hostname =~ /$excl/ ) {
printf "%sDshPerlHostLoop: Skipping $hostname because it matched filter $excl.%s\n", BLUE, RESET if ( $debug );
next HOST;
}
}
FILTER_IN: foreach my $incl ( @opt_filter_incl ) {
if ( $hostname !~ /$incl/i ) {
print "%sDshPerlHostLoop: Skipping $hostname because it didn't match filter $incl.%s\n", BLUE, RESET if ( $debug );
next HOST;
}
}
# update the global hostname padding variable used for pretty printing
if (length($hostname) + 2 > $hostname_pad) {
$hostname_pad = length($hostname) + 2;
}
if ($options{keep_comments}) {
push @hostlist, $hostname, $comment;
}
else {
push @hostlist, $hostname;
}
}
close $fh;
return @hostlist;
}
=item reap()
Reap child processes from the forkbomb. The hash is { $hostname => $pid }.
reap(\%pids);
=cut
sub reap {
my $pids = shift;
foreach my $host ( keys %$pids ) {
waitpid( $pids->{$host}, 0 );
delete $pids->{$host};
}
}
=item tag_output()
Get whether or not output should be prefixed with the hostname.
=cut
sub tag_output {
return $tag_output;
}
=item verbose()
Toggle/get whether or not to be verbose.
=cut
sub verbose {
if ( @_ == 1 ) {
$verbose = shift;
}
return $verbose;
}
# nasty brute force argument stealing :)
# BEGIN makes sure this runs before Getopt::* as long as that module
# isn't also called from within a BEGIN block
BEGIN {
my @to_kill;
for ( my $i=0; $i<@main::ARGV; $i++ ) {
# skip anything after -- by itself, just like GNU convention
# don't remove it though, so GetOptions can have a whack at processing
last if ( $main::ARGV[$i] eq '--' );
if ( $main::ARGV[$i] eq '--list' ) {
push @to_kill, $i, $i+1;
my $list = $main::ARGV[$i+1];
# absolute path
if ( -f $list ) {
$machines_list = $list;
}
# short name
elsif ( -f "$ENV{HOME}/.dsh/machines.$list" ) {
$machines_list = "$ENV{HOME}/.dsh/machines.$list";
}
else {
die "Could not read machine list in '$list' or '~/.dsh/machines.$list': $!";
}
}
if ( $main::ARGV[$i] eq '--incl' ) {
push @to_kill, $i, $i+1;
my $f = $main::ARGV[$i+1];
push @opt_filter_incl, qr/$f/;
}
if ( $main::ARGV[$i] eq '--excl' ) {
push @to_kill, $i, $i+1;
my $f = $main::ARGV[$i+1];
push @opt_filter_excl, qr/$f/;
}
if ( $main::ARGV[$i] eq '--batch' ) {
push @to_kill, $i, $i+1;
$opt_batch = $main::ARGV[$i+1];
}
if ( $main::ARGV[$i] eq '-u' ) {
push @to_kill, $i;
$tag_output = undef;
}
if ( $main::ARGV[$i] eq '-i' ) {
push @to_kill, $i, $i+1;
my $sshkey = $main::ARGV[$i+1];
$ssh_options .= " -o 'IdentityFile $sshkey'";
}
if ( $main::ARGV[$i] eq '-v' ) {
push @to_kill, $i;
$verbose = 1;
}
if ( $main::ARGV[$i] eq '--user' ) {
push @to_kill, $i, $i+1;
$remote_user = $main::ARGV[$i+1];
}
if ( $main::ARGV[$i] eq '--root' ) {
push @to_kill, $i;
$remote_user = 'root';
}
}
# do them in reverse order
foreach my $idx ( reverse sort @to_kill ) {
delete $main::ARGV[$idx];
}
}
=item set_screen_title()
Set the title in GNU screen if it's detected.
set_screen_title("Cluster Netstat, Cluster: foobar");
=cut
sub set_screen_title {
my $title = shift;
if ($ENV{TERM} eq 'screen' or ($ENV{TERMCAP} and $ENV{TERMCAP} =~ /screen/)) {
print "\033k$title\033\\";
}
}
=item lock()
Simple lock backed on flock. For printing mostly, flock doesn't work on STDOUT.
=cut
sub lock {
flock( $lock_fh, LOCK_EX );
}
=item unlock()
Opposite of above.
=cut
sub unlock {
flock( $lock_fh, LOCK_UN );
}
=item my_tempfile()
Not secure. Generates a parseable-by-humans tempfile so people can
tell what junk in /tmp is from.
my($fh, $name) = my_tempfile();
=cut
sub my_tempfile {
my @parts;
if ($ENV{USER} and $ENV{USER} ne 'root') {
push @parts, $ENV{USER};
}
push @parts, basename($0);
push @parts, Sys::Hostname::hostname;
push @parts, CORE::time;
my $filename = $tempdir . '/' . join('-', @parts);
$filename =~ s/\s+//g;
open(my $fh, "> $filename")
or die "Couldn't open tempfile for write: $!";
push @tempfiles, $filename;
return($fh, $filename);
}
END {
if ($$ == $mainpid) {
if (($verbose or $debug) and @tempfiles > 0) {
printf STDERR "Leaving tempfiles in $tempdir. They are:\n\t%s\n",
join("\n\t", @tempfiles);
}
else {
foreach my $tf (@tempfiles) {
if ($tf =~ m#^$tempdir#) {
unlink($tf);
}
}
}
}
eval { unlock(); }; # try to unlock
}
# track Net::SSH2 connections & related information in
# a separate object rather than having a bunch of globals
# stuff above will just bless right into this
# this AUTOLOAD just adds method syntax to a hash without dependencies
package DshPerlHostLoop::Bundle;
sub new {
my($type, $self) = @_;
return bless $self, $type;
}
sub AUTOLOAD {
my($self, $value) = @_;
our $AUTOLOAD;
( $_, $_, my $method ) = split /::/, $AUTOLOAD;
if ($value) {
$self->{$method} = $value;
}
return $self->{$method};
}
1;
# vim: et ts=4 sw=4 ai smarttab
__END__
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2007-2011 by Al Tobey.
This is free software; you can redistribute it and/or modify it under the terms
of the Artistic License 2.0. (Note that, unlike the Artistic License 1.0,
version 2.0 is GPL compatible by itself, hence there is no benefit to having an
Artistic 2.0 / GPL disjunction.) See the file LICENSE for details.
=cut