hydra/src/script/hydra_build.pl

309 lines
10 KiB
Perl
Raw Normal View History

2008-11-28 16:13:06 +00:00
#! /var/run/current-system/sw/bin/perl -w
2008-11-10 13:33:12 +00:00
use strict;
use File::Basename;
2008-11-12 14:29:32 +00:00
use File::stat;
2008-11-25 11:09:15 +00:00
use Hydra::Schema;
2008-11-28 14:36:04 +00:00
use Hydra::Helper::Nix;
2008-11-10 13:33:12 +00:00
2008-11-28 14:36:04 +00:00
my $db = openHydraDB;
2008-11-10 13:33:12 +00:00
2008-11-11 12:54:37 +00:00
sub doBuild {
my ($build) = @_;
2008-11-10 13:33:12 +00:00
2008-11-11 12:54:37 +00:00
my $drvPath = $build->drvpath;
my $outPath = $build->outpath;
2008-11-10 13:33:12 +00:00
my $isCachedBuild = 1;
my $outputCreated = 1; # i.e., the Nix build succeeded (but it could be a positive failure)
my $startTime = 0;
my $stopTime = 0;
my $buildStatus = 0; # = succeeded
my $errormsg = undef;
2008-11-10 13:33:12 +00:00
registerRoot $outPath;
2008-11-10 13:33:12 +00:00
if (!isValidPath($outPath)) {
$isCachedBuild = 0;
$startTime = time();
my $thisBuildFailed = 0;
my $someBuildFailed = 0;
# Run Nix to perform the build, and monitor the stderr output
# to get notifications about specific build steps, the
# associated log files, etc.
my $cmd = "nix-store --max-silent-time 1800 --keep-going --no-build-output " .
"--log-type flat --print-build-trace --realise $drvPath 2>&1";
my $buildStepNr = 1;
open OUT, "$cmd |" or die;
while (<OUT>) {
$errormsg .= $_;
unless (/^@\s+/) {
print STDERR "$_";
next;
}
if (/^@\s+build-started\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)$/) {
$db->txn_do(sub {
$db->resultset('BuildSteps')->create(
{ id => $build->id
, stepnr => $buildStepNr++
, type => 0 # = build
, drvpath => $1
, outpath => $2
, logfile => $4
, busy => 1
2008-11-12 11:09:21 +00:00
, starttime => time
});
});
}
2008-11-12 13:00:56 +00:00
elsif (/^@\s+build-succeeded\s+(\S+)\s+(\S+)$/) {
my $drvPath = $1;
$db->txn_do(sub {
(my $step) = $db->resultset('BuildSteps')->search(
{id => $build->id, type => 0, drvpath => $drvPath}, {});
die unless $step;
$step->busy(0);
$step->status(0);
2008-11-12 11:09:21 +00:00
$step->stoptime(time);
$step->update;
});
}
2008-11-12 13:00:56 +00:00
elsif (/^@\s+build-failed\s+(\S+)\s+(\S+)\s+(\S+)\s+(.*)$/) {
my $drvPathStep = $1;
$someBuildFailed = 1;
$thisBuildFailed = 1 if $drvPath eq $drvPathStep;
$db->txn_do(sub {
(my $step) = $db->resultset('BuildSteps')->search(
{id => $build->id, type => 0, drvpath => $drvPathStep}, {});
2008-11-12 11:09:21 +00:00
if ($step) {
die unless $step;
$step->busy(0);
$step->status(1);
$step->errormsg($4);
$step->stoptime(time);
$step->update;
} else {
$db->resultset('BuildSteps')->create(
2008-11-12 11:09:21 +00:00
{ id => $build->id
, stepnr => $buildStepNr++
, type => 0 # = build
, drvpath => $drvPathStep
2008-11-12 11:09:21 +00:00
, outpath => $2
, logfile => $4
, busy => 0
, status => 1
, starttime => time
, stoptime => time
, errormsg => $4
});
}
});
}
2008-11-12 13:00:56 +00:00
elsif (/^@\s+substituter-started\s+(\S+)\s+(\S+)$/) {
my $outPath = $1;
$db->txn_do(sub {
$db->resultset('BuildSteps')->create(
2008-11-12 13:00:56 +00:00
{ id => $build->id
, stepnr => $buildStepNr++
, type => 1 # = substitution
, outpath => $1
, busy => 1
, starttime => time
});
});
}
elsif (/^@\s+substituter-succeeded\s+(\S+)$/) {
my $outPath = $1;
$db->txn_do(sub {
(my $step) = $db->resultset('BuildSteps')->search(
2008-11-12 13:00:56 +00:00
{id => $build->id, type => 1, outpath => $outPath}, {});
die unless $step;
$step->busy(0);
$step->status(0);
$step->stoptime(time);
$step->update;
});
}
elsif (/^@\s+substituter-failed\s+(\S+)\s+(\S+)\s+(\S+)$/) {
my $outPath = $1;
$db->txn_do(sub {
(my $step) = $db->resultset('BuildSteps')->search(
2008-11-12 13:00:56 +00:00
{id => $build->id, type => 1, outpath => $outPath}, {});
die unless $step;
$step->busy(0);
$step->status(1);
$step->errormsg($3);
$step->stoptime(time);
$step->update;
});
}
else {
print STDERR "unknown Nix trace message: $_";
}
}
close OUT;
my $res = $?;
2008-11-10 13:33:12 +00:00
$stopTime = time();
if ($res != 0) {
if ($thisBuildFailed) { $buildStatus = 1; }
elsif ($someBuildFailed) { $buildStatus = 2; }
else { $buildStatus = 3; }
}
2008-11-10 13:33:12 +00:00
# Only store the output of running Nix if we have a miscellaneous error.
$errormsg = undef unless $buildStatus == 3;
2008-11-10 13:33:12 +00:00
}
$db->txn_do(sub {
2008-11-11 12:54:37 +00:00
$build->finished(1);
$build->timestamp(time());
$build->update;
my $logPath = "/nix/var/log/nix/drvs/" . basename $drvPath;
$logPath = undef unless -e $logPath;
2008-11-25 16:13:22 +00:00
my $releaseName;
if (-e "$outPath/nix-support/hydra-release-name") {
open FILE, "$outPath/nix-support/hydra-release-name" or die;
$releaseName = <FILE>;
chomp $releaseName;
close FILE;
}
$db->resultset('BuildResultInfo')->create(
2008-11-11 12:54:37 +00:00
{ id => $build->id
2008-11-10 13:33:12 +00:00
, iscachedbuild => $isCachedBuild
, buildstatus => $buildStatus
, starttime => $startTime
, stoptime => $stopTime
, logfile => $logPath
, errormsg => $errormsg
2008-11-25 16:13:22 +00:00
, releasename => $releaseName
2008-11-10 13:33:12 +00:00
});
if ($buildStatus == 0) {
2008-11-10 13:33:12 +00:00
2008-11-12 14:29:32 +00:00
my $productnr = 1;
2008-11-10 13:33:12 +00:00
if (-e "$outPath/nix-support/hydra-build-products") {
open LIST, "$outPath/nix-support/hydra-build-products" or die;
while (<LIST>) {
/^([\w\-]+)\s+([\w\-]+)\s+(\S+)(\s+(\S+))?$/ or next;
2008-11-10 13:33:12 +00:00
my $type = $1;
2008-11-12 16:42:07 +00:00
my $subtype = $2 eq "none" ? "" : $2;
2008-11-10 13:33:12 +00:00
my $path = $3;
my $defaultPath = $5;
2008-11-29 01:27:17 +00:00
next unless -e $path;
2008-11-12 14:29:32 +00:00
my $fileSize, my $sha1, my $sha256;
# !!! validate $path, $defaultPath
if (-f $path) {
my $st = stat($path) or die "cannot stat $path: $!";
2008-11-12 16:42:07 +00:00
$fileSize = $st->size;
$sha1 = `nix-hash --flat --type sha1 $path`
or die "cannot hash $path: $?";;
chomp $sha1;
2008-11-12 14:29:32 +00:00
$sha256 = `nix-hash --flat --type sha256 $path`
or die "cannot hash $path: $?";;
chomp $sha256;
}
my $name = $path eq $outPath ? "" : basename $path;
2008-11-12 14:29:32 +00:00
$db->resultset('BuildProducts')->create(
2008-11-10 13:33:12 +00:00
{ build => $build->id
2008-11-12 14:29:32 +00:00
, productnr => $productnr++
2008-11-10 13:33:12 +00:00
, type => $type
, subtype => $subtype
, path => $path
, filesize => $fileSize
2008-11-12 14:29:32 +00:00
, sha1hash => $sha1
, sha256hash => $sha256
, name => $name
, defaultpath => $defaultPath
2008-11-10 13:33:12 +00:00
});
}
close LIST;
2008-11-12 14:29:32 +00:00
}
else {
$db->resultset('BuildProducts')->create(
2008-11-10 13:33:12 +00:00
{ build => $build->id
2008-11-12 14:29:32 +00:00
, productnr => $productnr++
2008-11-10 13:33:12 +00:00
, type => "nix-build"
, subtype => ""
, path => $outPath
2008-11-12 16:42:07 +00:00
, name => $build->nixname
2008-11-10 13:33:12 +00:00
});
}
}
2008-11-11 12:54:37 +00:00
$build->schedulingInfo->delete;
2008-11-10 13:33:12 +00:00
});
}
2008-11-11 12:54:37 +00:00
my $buildId = $ARGV[0] or die;
print STDERR "performing build $buildId\n";
2008-11-10 13:33:12 +00:00
2008-11-11 12:54:37 +00:00
# Lock the build. If necessary, steal the lock from the parent
# process (runner.pl). This is so that if the runner dies, the
# children (i.e. the build.pl instances) can continue to run and won't
# have the lock taken away.
my $build;
2008-11-10 13:33:12 +00:00
$db->txn_do(sub {
$build = $db->resultset('Builds')->find($buildId);
2008-11-11 12:54:37 +00:00
die "build $buildId doesn't exist" unless defined $build;
die "build $buildId already done" if defined $build->resultInfo;
2008-11-11 12:54:37 +00:00
if ($build->schedulingInfo->busy != 0 && $build->schedulingInfo->locker != getppid) {
die "build $buildId is already being built";
2008-11-10 13:33:12 +00:00
}
2008-11-11 12:54:37 +00:00
$build->schedulingInfo->busy(1);
$build->schedulingInfo->locker($$);
$build->schedulingInfo->update;
$build->buildsteps->delete_all;
$build->buildproducts->delete_all;
2008-11-10 13:33:12 +00:00
});
2008-11-11 12:54:37 +00:00
die unless $build;
2008-11-10 13:33:12 +00:00
2008-11-11 12:54:37 +00:00
# Do the build. If it throws an error, unlock the build so that it
# can be retried.
2008-11-10 13:33:12 +00:00
eval {
2008-11-11 12:54:37 +00:00
doBuild $build;
2008-11-12 14:29:32 +00:00
print "done\n";
2008-11-10 13:33:12 +00:00
};
if ($@) {
warn $@;
$db->txn_do(sub {
2008-11-11 12:54:37 +00:00
$build->schedulingInfo->busy(0);
$build->schedulingInfo->locker($$);
$build->schedulingInfo->update;
2008-11-10 13:33:12 +00:00
});
}