2011-10-11 13:45:36 +02:00
|
|
|
#! @perl@ -w @perlFlags@
|
2007-11-15 15:28:08 +01:00
|
|
|
|
|
|
|
use Fcntl ':flock';
|
|
|
|
use English '-no_match_vars';
|
2009-03-29 20:40:44 +02:00
|
|
|
use IO::Handle;
|
2011-11-23 13:21:35 +01:00
|
|
|
use Nix::Config;
|
2011-10-11 13:45:36 +02:00
|
|
|
use Nix::SSH qw/sshOpts openSSHConnection/;
|
2011-11-23 16:13:37 +01:00
|
|
|
use Nix::CopyClosure;
|
2010-10-04 14:30:46 +02:00
|
|
|
no warnings('once');
|
2010-02-03 21:12:18 +01:00
|
|
|
|
2007-11-15 15:28:08 +01:00
|
|
|
|
|
|
|
# General operation:
|
|
|
|
#
|
|
|
|
# Try to find a free machine of type $neededSystem. We do this as
|
|
|
|
# follows:
|
|
|
|
# - We acquire an exclusive lock on $currentLoad/main-lock.
|
|
|
|
# - For each machine $machine of type $neededSystem and for each $slot
|
|
|
|
# less than the maximum load for that machine, we try to get an
|
|
|
|
# exclusive lock on $currentLoad/$machine-$slot (without blocking).
|
|
|
|
# If we get such a lock, we send "accept" to the caller. Otherwise,
|
|
|
|
# we send "postpone" and exit.
|
|
|
|
# - We release the exclusive lock on $currentLoad/main-lock.
|
|
|
|
# - We perform the build on $neededSystem.
|
|
|
|
# - We release the exclusive lock on $currentLoad/$machine-$slot.
|
|
|
|
#
|
|
|
|
# The nice thing about this scheme is that if we die prematurely, the
|
|
|
|
# locks are released automatically.
|
|
|
|
|
2010-02-03 21:12:18 +01:00
|
|
|
|
|
|
|
# Make sure that we don't get any SSH passphrase or host key popups -
|
|
|
|
# if there is any problem it should fail, not do something
|
|
|
|
# interactive.
|
|
|
|
$ENV{"DISPLAY"} = "";
|
|
|
|
$ENV{"SSH_ASKPASS"} = "";
|
|
|
|
|
|
|
|
|
2007-11-15 15:28:08 +01:00
|
|
|
sub sendReply {
|
|
|
|
my $reply = shift;
|
2009-03-28 20:29:55 +01:00
|
|
|
print STDERR "# $reply\n";
|
2007-11-15 15:28:08 +01:00
|
|
|
}
|
|
|
|
|
2010-08-27 15:18:13 +02:00
|
|
|
sub all { $_ || return 0 for @_; 1 }
|
|
|
|
|
2007-11-15 15:28:08 +01:00
|
|
|
|
2010-08-25 22:44:28 +02:00
|
|
|
# Initialisation.
|
|
|
|
my $loadIncreased = 0;
|
|
|
|
|
2010-08-31 14:36:24 +02:00
|
|
|
my ($localSystem, $maxSilentTime, $printBuildTrace) = @ARGV;
|
2010-08-25 22:44:28 +02:00
|
|
|
$maxSilentTime = 0 unless defined $maxSilentTime;
|
2007-11-15 15:28:08 +01:00
|
|
|
|
2010-08-25 22:44:28 +02:00
|
|
|
my $currentLoad = $ENV{"NIX_CURRENT_LOAD"};
|
2007-11-15 15:28:08 +01:00
|
|
|
my $conf = $ENV{"NIX_REMOTE_SYSTEMS"};
|
|
|
|
|
2010-08-25 22:44:28 +02:00
|
|
|
|
|
|
|
sub openSlotLock {
|
|
|
|
my ($machine, $slot) = @_;
|
|
|
|
my $slotLockFn = "$currentLoad/" . (join '+', @{$machine->{systemTypes}}) . "-" . $machine->{hostName} . "-$slot";
|
|
|
|
my $slotLock = new IO::Handle;
|
|
|
|
open $slotLock, ">>$slotLockFn" or die;
|
|
|
|
return $slotLock;
|
|
|
|
}
|
2007-11-15 15:28:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
# Read the list of machines.
|
2009-09-17 17:48:17 +02:00
|
|
|
my @machines;
|
2010-08-25 22:44:28 +02:00
|
|
|
if (defined $conf && -e $conf) {
|
|
|
|
open CONF, "< $conf" or die;
|
|
|
|
while (<CONF>) {
|
|
|
|
chomp;
|
|
|
|
s/\#.*$//g;
|
|
|
|
next if /^\s*$/;
|
2010-08-27 15:18:13 +02:00
|
|
|
my @tokens = split /\s/, $_;
|
2010-08-25 22:44:28 +02:00
|
|
|
push @machines,
|
2010-08-27 15:18:13 +02:00
|
|
|
{ hostName => $tokens[0]
|
|
|
|
, systemTypes => [ split(/,/, $tokens[1]) ]
|
|
|
|
, sshKeys => $tokens[2]
|
|
|
|
, maxJobs => int($tokens[3])
|
|
|
|
, speedFactor => 1.0 * (defined $tokens[4] ? int($tokens[4]) : 1)
|
|
|
|
, features => [ split(/,/, $tokens[5] || "") ]
|
2010-08-25 22:44:28 +02:00
|
|
|
, enabled => 1
|
|
|
|
};
|
|
|
|
}
|
|
|
|
close CONF;
|
2007-11-15 15:28:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-08-25 22:44:28 +02:00
|
|
|
# Wait for the calling process to ask us whether we can build some derivation.
|
|
|
|
my ($drvPath, $hostName, $slotLock);
|
2007-11-15 15:28:08 +01:00
|
|
|
|
2010-08-25 22:44:28 +02:00
|
|
|
REQ: while (1) {
|
|
|
|
$_ = <STDIN> || exit 0;
|
|
|
|
my ($amWilling, $neededSystem);
|
2010-08-27 15:18:13 +02:00
|
|
|
($amWilling, $neededSystem, $drvPath, $requiredFeatures) = split;
|
|
|
|
my @requiredFeatures = split /,/, $requiredFeatures;
|
2007-11-15 15:28:08 +01:00
|
|
|
|
2010-08-25 22:44:28 +02:00
|
|
|
my $canBuildLocally = $amWilling && ($localSystem eq $neededSystem);
|
2010-02-03 21:35:37 +01:00
|
|
|
|
2010-08-25 22:44:28 +02:00
|
|
|
if (!defined $currentLoad) {
|
|
|
|
sendReply "decline";
|
|
|
|
next;
|
|
|
|
}
|
2010-02-03 21:35:37 +01:00
|
|
|
|
2010-08-25 22:44:28 +02:00
|
|
|
# Acquire the exclusive lock on $currentLoad/main-lock.
|
|
|
|
mkdir $currentLoad, 0777 or die unless -d $currentLoad;
|
|
|
|
my $mainLock = "$currentLoad/main-lock";
|
|
|
|
open MAINLOCK, ">>$mainLock" or die;
|
|
|
|
flock(MAINLOCK, LOCK_EX) or die;
|
|
|
|
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
# Find all machine that can execute this build, i.e., that
|
2010-08-27 15:18:13 +02:00
|
|
|
# support builds for the given platform and features, and are
|
|
|
|
# not at their job limit.
|
2010-08-25 22:44:28 +02:00
|
|
|
my $rightType = 0;
|
|
|
|
my @available = ();
|
|
|
|
LOOP: foreach my $cur (@machines) {
|
2010-08-27 15:18:13 +02:00
|
|
|
if ($cur->{enabled}
|
|
|
|
&& (grep { $neededSystem eq $_ } @{$cur->{systemTypes}})
|
|
|
|
&& all(map { my $f = $_; 0 != grep { $f eq $_ } @{$cur->{features}} } @requiredFeatures))
|
|
|
|
{
|
2010-08-25 22:44:28 +02:00
|
|
|
$rightType = 1;
|
|
|
|
|
|
|
|
# We have a machine of the right type. Determine the load on
|
|
|
|
# the machine.
|
|
|
|
my $slot = 0;
|
|
|
|
my $load = 0;
|
|
|
|
my $free;
|
|
|
|
while ($slot < $cur->{maxJobs}) {
|
|
|
|
my $slotLock = openSlotLock($cur, $slot);
|
|
|
|
if (flock($slotLock, LOCK_EX | LOCK_NB)) {
|
|
|
|
$free = $slot unless defined $free;
|
|
|
|
flock($slotLock, LOCK_UN) or die;
|
|
|
|
} else {
|
|
|
|
$load++;
|
|
|
|
}
|
|
|
|
close $slotLock;
|
|
|
|
$slot++;
|
2010-02-03 21:35:37 +01:00
|
|
|
}
|
2010-08-25 22:44:28 +02:00
|
|
|
|
|
|
|
push @available, { machine => $cur, load => $load, free => $free }
|
|
|
|
if $load < $cur->{maxJobs};
|
2008-12-04 15:29:41 +01:00
|
|
|
}
|
2010-02-03 21:35:37 +01:00
|
|
|
}
|
2007-11-15 15:28:08 +01:00
|
|
|
|
2010-08-25 22:44:28 +02:00
|
|
|
if (defined $ENV{NIX_DEBUG_HOOK}) {
|
|
|
|
print STDERR "load on " . $_->{machine}->{hostName} . " = " . $_->{load} . "\n"
|
|
|
|
foreach @available;
|
|
|
|
}
|
2007-11-15 15:28:08 +01:00
|
|
|
|
|
|
|
|
2010-08-25 22:44:28 +02:00
|
|
|
# Didn't find any available machine? Then decline or postpone.
|
|
|
|
if (scalar @available == 0) {
|
|
|
|
# Postpone if we have a machine of the right type, except
|
|
|
|
# if the local system can and wants to do the build.
|
|
|
|
if ($rightType && !$canBuildLocally) {
|
|
|
|
sendReply "postpone";
|
|
|
|
} else {
|
|
|
|
sendReply "decline";
|
|
|
|
}
|
|
|
|
close MAINLOCK;
|
|
|
|
next REQ;
|
2010-02-03 21:35:37 +01:00
|
|
|
}
|
2007-11-15 15:28:08 +01:00
|
|
|
|
2009-09-17 17:48:17 +02:00
|
|
|
|
2010-08-25 22:44:28 +02:00
|
|
|
# Prioritise the available machines as follows:
|
|
|
|
# - First by load divided by speed factor, rounded to the nearest
|
|
|
|
# integer. This causes fast machines to be preferred over slow
|
|
|
|
# machines with similar loads.
|
|
|
|
# - Then by speed factor.
|
|
|
|
# - Finally by load.
|
|
|
|
sub lf { my $x = shift; return int($x->{load} / $x->{machine}->{speedFactor} + 0.4999); }
|
|
|
|
@available = sort
|
|
|
|
{ lf($a) <=> lf($b)
|
|
|
|
|| $b->{machine}->{speedFactor} <=> $a->{machine}->{speedFactor}
|
|
|
|
|| $a->{load} <=> $b->{load}
|
|
|
|
} @available;
|
2009-09-17 17:48:17 +02:00
|
|
|
|
|
|
|
|
2010-08-25 22:44:28 +02:00
|
|
|
# Select the best available machine and lock a free slot.
|
|
|
|
my $selected = $available[0];
|
|
|
|
my $machine = $selected->{machine};
|
|
|
|
|
|
|
|
$slotLock = openSlotLock($machine, $selected->{free});
|
|
|
|
flock($slotLock, LOCK_EX | LOCK_NB) or die;
|
|
|
|
utime undef, undef, $slotLock;
|
2009-09-17 17:48:17 +02:00
|
|
|
|
2010-08-25 22:44:28 +02:00
|
|
|
close MAINLOCK;
|
2009-09-17 17:48:17 +02:00
|
|
|
|
2010-02-03 21:35:37 +01:00
|
|
|
|
2010-08-25 22:44:28 +02:00
|
|
|
# Connect to the selected machine.
|
|
|
|
@sshOpts = ("-i", $machine->{sshKeys}, "-x");
|
|
|
|
$hostName = $machine->{hostName};
|
|
|
|
last REQ if openSSHConnection $hostName;
|
2010-02-03 21:35:37 +01:00
|
|
|
|
2010-08-25 22:44:28 +02:00
|
|
|
warn "unable to open SSH connection to $hostName, trying other available machines...\n";
|
|
|
|
$machine->{enabled} = 0;
|
|
|
|
}
|
2010-02-03 21:35:37 +01:00
|
|
|
}
|
2009-09-17 17:48:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
# Tell Nix we've accepted the build.
|
2010-08-25 22:44:28 +02:00
|
|
|
sendReply "accept";
|
|
|
|
my @inputs = split /\s/, readline(STDIN);
|
|
|
|
my @outputs = split /\s/, readline(STDIN);
|
2007-11-15 15:28:08 +01:00
|
|
|
|
|
|
|
|
2010-08-31 14:36:24 +02:00
|
|
|
print STDERR "building `$drvPath' on `$hostName'\n";
|
|
|
|
print STDERR "@ build-remote $drvPath $hostName\n" if $printBuildTrace;
|
|
|
|
|
|
|
|
|
2007-11-15 15:28:08 +01:00
|
|
|
my $maybeSign = "";
|
2011-11-23 13:21:35 +01:00
|
|
|
$maybeSign = "--sign" if -e "$Nix::Config::confDir/signing-key.sec";
|
2007-11-15 15:28:08 +01:00
|
|
|
|
2010-08-25 13:54:11 +02:00
|
|
|
|
|
|
|
# Register the derivation as a temporary GC root. Note that $PPID is
|
|
|
|
# the PID of the remote SSH process, which, due to the use of a
|
|
|
|
# persistant SSH connection, should be the same across all remote
|
|
|
|
# command invocations for this session.
|
|
|
|
my $rootsDir = "@localstatedir@/nix/gcroots/tmp";
|
|
|
|
system("ssh $hostName @sshOpts 'mkdir -m 1777 -p $rootsDir; ln -sfn $drvPath $rootsDir/\$PPID.drv'");
|
|
|
|
|
|
|
|
sub removeRoots {
|
|
|
|
system("ssh $hostName @sshOpts 'rm -f $rootsDir/\$PPID.drv $rootsDir/\$PPID.out'");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# Copy the derivation and its dependencies to the build machine.
|
2011-11-23 16:13:37 +01:00
|
|
|
Nix::CopyClosure::copyTo($hostName, [ @sshOpts ], [ $drvPath, @inputs ], "", "", 0, 0, $maybeSign ne "");
|
2007-11-15 15:28:08 +01:00
|
|
|
|
|
|
|
|
2010-08-25 13:54:11 +02:00
|
|
|
# Perform the build.
|
2010-08-30 16:53:03 +02:00
|
|
|
my $buildFlags = "--max-silent-time $maxSilentTime --fallback --add-root $rootsDir/\$PPID.out --option verbosity 0";
|
2008-12-04 17:51:16 +01:00
|
|
|
|
2010-10-04 14:30:46 +02:00
|
|
|
# We let the remote side kill its process group when the connection is
|
|
|
|
# closed unexpectedly. This is necessary to ensure that no processes
|
|
|
|
# are left running on the remote system if the local Nix process is
|
|
|
|
# killed. (SSH itself doesn't kill child processes if the connection
|
|
|
|
# is interrupted unless the `-tt' flag is used to force a pseudo-tty,
|
|
|
|
# in which case every child receives SIGHUP; however, `-tt' doesn't
|
|
|
|
# work on some platforms when connection sharing is used.)
|
|
|
|
pipe STDIN, DUMMY; # make sure we have a readable STDIN
|
2011-11-29 11:51:22 +01:00
|
|
|
if (system("exec ssh $hostName @sshOpts '(read; kill -INT -\$\$) <&0 & nix-store -r $drvPath $buildFlags > /dev/null' 2>&4") != 0) {
|
2010-12-13 17:53:23 +01:00
|
|
|
# Note that if we get exit code 100 from `nix-store -r', it
|
|
|
|
# denotes a permanent build failure (as opposed to an SSH problem
|
|
|
|
# or a temporary Nix problem). We propagate this to the caller to
|
|
|
|
# allow it to distinguish between transient and permanent
|
|
|
|
# failures.
|
|
|
|
my $res = $? >> 8;
|
|
|
|
print STDERR "build of `$drvPath' on `$hostName' failed with exit code $res\n";
|
2010-08-25 13:54:11 +02:00
|
|
|
removeRoots;
|
2009-01-13 12:39:09 +01:00
|
|
|
exit $res;
|
|
|
|
}
|
2007-11-15 15:28:08 +01:00
|
|
|
|
2010-08-25 22:44:28 +02:00
|
|
|
#print "build of `$drvPath' on `$hostName' succeeded\n";
|
2007-11-15 15:28:08 +01:00
|
|
|
|
2010-08-25 13:54:11 +02:00
|
|
|
|
|
|
|
# Copy the output from the build machine.
|
2010-08-25 14:19:30 +02:00
|
|
|
foreach my $output (@outputs) {
|
2007-11-15 15:28:08 +01:00
|
|
|
my $maybeSignRemote = "";
|
|
|
|
$maybeSignRemote = "--sign" if $UID != 0;
|
|
|
|
|
2011-11-29 11:51:22 +01:00
|
|
|
system("exec ssh $hostName @sshOpts 'nix-store --export $maybeSignRemote $output'" .
|
2010-08-25 22:44:28 +02:00
|
|
|
"| NIX_HELD_LOCKS=$output @bindir@/nix-store --import > /dev/null") == 0
|
2008-12-04 13:20:06 +01:00
|
|
|
or die "cannot copy $output from $hostName: $?";
|
2007-11-15 15:28:08 +01:00
|
|
|
}
|
2010-08-25 13:54:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
# Get rid of the temporary GC roots.
|
|
|
|
removeRoots;
|