at⋅a⋅rax⋅is

remrun (c. 2002)

Before cfengine. Before Puppet. Before Ansible, Fabric, or any of the tools that eventually made parallel remote execution a solved problem.

This is what I used instead: a Perl script that forked an SSH process per host and waited for them all to come back. The host list was hardcoded. StrictHostKeyChecking no because you owned the machines and life was short. It did its job before it needed to.

The $Id$ tag at the top is CVS. This predates git.

#!/usr/bin/perl
#
# remrun - execute a command on remote hosts, as defined in a config file.
#
# $Id$
#

use Data::Dumper;
use Getopt::Long;
use Pod::Usage;
use POSIX ":sys_wait_h";

my @ssh = ('ssh', '-q', '-o', 'StrictHostKeyChecking no', '-o', 'ConnectTimeout 15', '-x');
$| = 1;

my %opts = (
	'verbose'           => \$verbose,
	'parallel'          => \$parallel,
	'help'              => \$help,
	'group=s'           => \$group,
	'os=s'              => \$os,
	'host=s'            => \$hostmatch,
	'debug'             => \$debug,
	'n'                 => \$dryrun,
	'execute|command=s' => \$command,
);

$usage = "Usage: remrun [-g <group>] [-H <host_regex>] <command>";

my @down_hosts = ();

GetOptions(%opts) or pod2usage($usage);

if($help) {
	pod2usage(-exitstatus => 0, -verbose => 2);
}

if ($dryrun) {
	$verbose = 1;
}

$conf = new Config::General(-ConfigFile => "/afs/cs/etc/remrun.conf", -LowerCaseNames => 1);
my %config = $conf->getall;

#$ldap = Net::LDAP->new( 'cs-ldap-2.stanford.edu' ) or die "$@";

if (! $command) {
	foreach $arg (@ARGV) {
		$command .= "$arg ";
	}
}

if (! $command) {
	die pod2usage($usage);
}

sub sortfunc {
	($new_a, $num_a) = split(/(\d+)/,$a,2);
	($new_b, $num_b) = split(/(\d+)/,$b,2);
	if ($new_a eq $new_b) {
		$num_a <=> $num_b;
	} else {
		$new_a cmp $new_b;
	}
}

print "Command: $command\n";

if(not $group) {
	$group="*";
}

$verbose && print "Group: $group\n";
$verbose && $hostmatch && print "Hosts matching: $hostmatch\n";
print "=====================================================================\n";

foreach $my_host (sort sortfunc keys %hosts) {
	if ($hostmatch) {
		next unless $my_host =~ /$hostmatch/;
	}
	$verbose && print "\t\t$my_host\n";
	if ($dryrun) { next; }

	my $pid = fork();
	if ($pid) {
		$pid_to_host{$pid} = $my_host;
		$debug && warn "$pid is processing $my_host\n";
		if (not $parallel) {
			$debug && print "Waiting...\n";
			wait;
			$host_result{$my_host} = $? ? 0 : 1;
		}
	} else {
		$verbose && print "\t\t\t";
		exec @ssh, "root\@$my_host", $command;
	}
}

while (keys %pid_to_host) {
	my $pid = wait;
	last if $pid < 0;
	my $host = delete $pid_to_host{$pid}
		or warn("Why did I see $pid ($?)\n"), next;
	$debug && warn "reaping $pid for $host\n";
	$host_result{$host} = $? ? 0 : 1;
}

for (sort keys %host_result) {
	if ($host_result{$_} == 0) {
		push(@down_hosts, $_);
	}
}

print "=====================================================================\n";
print "The following hosts were not reachable:\n";
foreach $host (@down_hosts) {
	print "\t$host\n";
}
print "=====================================================================\n";

exit(0);