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;
|
2009-07-07 16:15:38 +00:00
|
|
|
use Email::Sender::Simple qw(sendmail);
|
|
|
|
use Email::Sender::Transport::SMTP;
|
|
|
|
use Email::Simple;
|
|
|
|
use Email::Simple::Creator;
|
2009-07-08 15:52:55 +00:00
|
|
|
use Sys::Hostname::Long;
|
2009-07-10 09:47:42 +00:00
|
|
|
use Config::General;
|
2008-11-10 13:33:12 +00:00
|
|
|
|
|
|
|
|
2009-04-22 22:59:54 +00:00
|
|
|
STDOUT->autoflush();
|
|
|
|
|
2008-11-28 14:36:04 +00:00
|
|
|
my $db = openHydraDB;
|
2008-11-10 13:33:12 +00:00
|
|
|
|
|
|
|
|
2009-07-10 09:47:42 +00:00
|
|
|
my %config = new Config::General($ENV{"HYDRA_CONF"})->getall;
|
|
|
|
|
|
|
|
|
2009-03-26 12:53:39 +00:00
|
|
|
sub getBuildLog {
|
|
|
|
my ($drvPath) = @_;
|
|
|
|
my $logPath = "/nix/var/log/nix/drvs/" . basename $drvPath;
|
|
|
|
return -e $logPath ? $logPath : undef;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-07 16:15:38 +00:00
|
|
|
sub sendEmailNotification {
|
|
|
|
my ($build) = @_;
|
|
|
|
|
2009-07-08 15:52:55 +00:00
|
|
|
die unless defined $build->resultInfo;
|
|
|
|
|
2009-07-07 16:15:38 +00:00
|
|
|
return if !$build->maintainers;
|
|
|
|
|
2009-07-08 15:52:55 +00:00
|
|
|
# Do we want to send mail?
|
|
|
|
|
|
|
|
if ($build->resultInfo->buildstatus == 0) {
|
|
|
|
# Build succeeded. Only send mail if the previous build for
|
|
|
|
# the same platform failed.
|
|
|
|
return; # TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
# Send mail.
|
|
|
|
|
|
|
|
# !!! should use the Template Toolkit here.
|
|
|
|
|
|
|
|
print STDERR "sending mail notification to ", $build->maintainers, "\n";
|
|
|
|
|
|
|
|
my $jobName = $build->project->name . ":" . $build->jobset->name . ":" . $build->job->name;
|
|
|
|
|
|
|
|
my $status =
|
|
|
|
$build->resultInfo->buildstatus == 0 ? "SUCCEEDED" : "FAILED";
|
|
|
|
|
|
|
|
my $sender = ($ENV{'USER'} || "hydra") . "@" . hostname_long . "\n";
|
|
|
|
|
2009-07-10 09:47:42 +00:00
|
|
|
my $selfURI = $config{'base_uri'} || "http://localhost:3000";
|
2009-07-08 15:52:55 +00:00
|
|
|
|
|
|
|
my $body = "Hi,\n"
|
|
|
|
. "\n"
|
|
|
|
. "This is to let you know that Hydra build " . $build->id
|
|
|
|
. " of job " . $jobName . " has $status.\n"
|
|
|
|
. "\n"
|
|
|
|
. "The build information page can be found here: "
|
|
|
|
. "$selfURI/build/" . $build->id . "\n"
|
|
|
|
. "\n"
|
|
|
|
. "Regards,\n\nThe Hydra build daemon.\n";
|
|
|
|
|
2009-07-07 16:15:38 +00:00
|
|
|
my $email = Email::Simple->create(
|
|
|
|
header => [
|
|
|
|
To => $build->maintainers,
|
2009-07-08 15:52:55 +00:00
|
|
|
From => "Hydra Build Daemon <$sender>",
|
|
|
|
Subject => "Hydra job $jobName build " . $build->id . " $status",
|
2009-07-07 16:15:38 +00:00
|
|
|
],
|
2009-07-08 15:52:55 +00:00
|
|
|
body => $body,
|
2009-07-07 16:15:38 +00:00
|
|
|
);
|
|
|
|
|
2009-07-08 15:52:55 +00:00
|
|
|
print $email->as_string if $ENV{'HYDRA_MAIL_TEST'};
|
2009-07-07 16:15:38 +00:00
|
|
|
|
|
|
|
sendmail($email);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
2008-11-25 00:38:16 +00:00
|
|
|
|
|
|
|
my $buildStatus = 0; # = succeeded
|
|
|
|
|
|
|
|
my $errormsg = undef;
|
2009-03-09 17:21:10 +00:00
|
|
|
|
2008-11-10 13:33:12 +00:00
|
|
|
if (!isValidPath($outPath)) {
|
|
|
|
$isCachedBuild = 0;
|
|
|
|
|
2009-03-09 16:22:41 +00:00
|
|
|
# Do the build.
|
2008-11-10 13:33:12 +00:00
|
|
|
$startTime = time();
|
|
|
|
|
2008-11-25 00:38:16 +00:00
|
|
|
my $thisBuildFailed = 0;
|
|
|
|
my $someBuildFailed = 0;
|
|
|
|
|
2008-11-11 17:49:50 +00:00
|
|
|
# Run Nix to perform the build, and monitor the stderr output
|
|
|
|
# to get notifications about specific build steps, the
|
|
|
|
# associated log files, etc.
|
2008-12-16 10:42:37 +00:00
|
|
|
my $cmd = "nix-store --max-silent-time 1800 --keep-going --no-build-output " .
|
2009-03-15 11:56:11 +00:00
|
|
|
"--log-type flat --print-build-trace --realise $drvPath " .
|
|
|
|
"--add-root " . gcRootFor $outPath . " 2>&1";
|
2008-11-11 17:49:50 +00:00
|
|
|
|
2009-03-16 16:56:47 +00:00
|
|
|
my $buildStepNr = $build->buildsteps->find({},
|
|
|
|
{select => {max => 'stepnr + 1'}, as => ['max']})->get_column('max') || 1;
|
|
|
|
|
|
|
|
my %buildSteps;
|
2008-11-11 17:49:50 +00:00
|
|
|
|
|
|
|
open OUT, "$cmd |" or die;
|
|
|
|
|
|
|
|
while (<OUT>) {
|
2008-11-25 00:38:16 +00:00
|
|
|
$errormsg .= $_;
|
|
|
|
|
2008-11-11 17:49:50 +00:00
|
|
|
unless (/^@\s+/) {
|
|
|
|
print STDERR "$_";
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (/^@\s+build-started\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)$/) {
|
2009-03-16 17:46:46 +00:00
|
|
|
my $drvPathStep = $1;
|
2009-04-22 22:43:04 +00:00
|
|
|
txn_do($db, sub {
|
2009-03-12 14:18:30 +00:00
|
|
|
$build->buildsteps->create(
|
2009-03-16 17:46:46 +00:00
|
|
|
{ stepnr => ($buildSteps{$drvPathStep} = $buildStepNr++)
|
2008-11-11 17:49:50 +00:00
|
|
|
, type => 0 # = build
|
2009-03-16 17:46:46 +00:00
|
|
|
, drvpath => $drvPathStep
|
2008-11-11 17:49:50 +00:00
|
|
|
, outpath => $2
|
|
|
|
, logfile => $4
|
|
|
|
, busy => 1
|
2008-11-12 11:09:21 +00:00
|
|
|
, starttime => time
|
2008-11-11 17:49:50 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2008-11-12 13:00:56 +00:00
|
|
|
elsif (/^@\s+build-succeeded\s+(\S+)\s+(\S+)$/) {
|
2009-03-16 17:46:46 +00:00
|
|
|
my $drvPathStep = $1;
|
2009-04-22 22:43:04 +00:00
|
|
|
txn_do($db, sub {
|
2009-03-16 17:46:46 +00:00
|
|
|
my $step = $build->buildsteps->find({stepnr => $buildSteps{$drvPathStep}}) or die;
|
2009-03-10 08:52:42 +00:00
|
|
|
$step->update({busy => 0, status => 0, stoptime => time});
|
2008-11-11 17:49:50 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2008-11-12 13:00:56 +00:00
|
|
|
elsif (/^@\s+build-failed\s+(\S+)\s+(\S+)\s+(\S+)\s+(.*)$/) {
|
2008-11-25 00:38:16 +00:00
|
|
|
my $drvPathStep = $1;
|
|
|
|
$someBuildFailed = 1;
|
|
|
|
$thisBuildFailed = 1 if $drvPath eq $drvPathStep;
|
2009-03-26 12:53:39 +00:00
|
|
|
my $errorMsg = $4;
|
|
|
|
$errorMsg = "build failed previously (cached)" if $3 eq "cached";
|
2009-04-22 22:43:04 +00:00
|
|
|
txn_do($db, sub {
|
2009-03-16 16:56:47 +00:00
|
|
|
if ($buildSteps{$drvPathStep}) {
|
|
|
|
my $step = $build->buildsteps->find({stepnr => $buildSteps{$drvPathStep}}) or die;
|
2009-03-26 12:53:39 +00:00
|
|
|
$step->update({busy => 0, status => 1, errormsg => $errorMsg, stoptime => time});
|
2009-04-22 14:41:12 +00:00
|
|
|
}
|
|
|
|
# Don't write a record if this derivation already
|
|
|
|
# failed previously. This can happen if this is a
|
|
|
|
# restarted build.
|
|
|
|
elsif (scalar $build->buildsteps->search({drvpath => $drvPathStep, type => 0, busy => 0, status => 1}) == 0) {
|
2009-03-12 14:18:30 +00:00
|
|
|
$build->buildsteps->create(
|
2009-03-26 12:53:39 +00:00
|
|
|
{ stepnr => ($buildSteps{$drvPathStep} = $buildStepNr++)
|
2008-11-12 11:09:21 +00:00
|
|
|
, type => 0 # = build
|
2008-11-25 00:38:16 +00:00
|
|
|
, drvpath => $drvPathStep
|
2008-11-12 11:09:21 +00:00
|
|
|
, outpath => $2
|
2009-03-26 12:53:39 +00:00
|
|
|
, logfile => getBuildLog($drvPathStep)
|
2008-11-12 11:09:21 +00:00
|
|
|
, busy => 0
|
|
|
|
, status => 1
|
|
|
|
, starttime => time
|
|
|
|
, stoptime => time
|
2009-03-26 12:53:39 +00:00
|
|
|
, errormsg => $errorMsg
|
2008-11-12 11:09:21 +00:00
|
|
|
});
|
|
|
|
}
|
2008-11-11 17:49:50 +00:00
|
|
|
});
|
|
|
|
}
|
2008-11-12 13:00:56 +00:00
|
|
|
|
|
|
|
elsif (/^@\s+substituter-started\s+(\S+)\s+(\S+)$/) {
|
|
|
|
my $outPath = $1;
|
2009-04-22 22:43:04 +00:00
|
|
|
txn_do($db, sub {
|
2009-03-12 14:18:30 +00:00
|
|
|
$build->buildsteps->create(
|
2009-03-16 16:56:47 +00:00
|
|
|
{ stepnr => ($buildSteps{$outPath} = $buildStepNr++)
|
2008-11-12 13:00:56 +00:00
|
|
|
, type => 1 # = substitution
|
|
|
|
, outpath => $1
|
|
|
|
, busy => 1
|
|
|
|
, starttime => time
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
elsif (/^@\s+substituter-succeeded\s+(\S+)$/) {
|
|
|
|
my $outPath = $1;
|
2009-04-22 22:43:04 +00:00
|
|
|
txn_do($db, sub {
|
2009-03-16 16:56:47 +00:00
|
|
|
my $step = $build->buildsteps->find({stepnr => $buildSteps{$outPath}}) or die;
|
2009-03-09 16:22:41 +00:00
|
|
|
$step->update({busy => 0, status => 0, stoptime => time});
|
2008-11-12 13:00:56 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
elsif (/^@\s+substituter-failed\s+(\S+)\s+(\S+)\s+(\S+)$/) {
|
|
|
|
my $outPath = $1;
|
2009-04-22 22:43:04 +00:00
|
|
|
txn_do($db, sub {
|
2009-03-16 16:56:47 +00:00
|
|
|
my $step = $build->buildsteps->find({stepnr => $buildSteps{$outPath}}) or die;
|
2009-03-09 16:22:41 +00:00
|
|
|
$step->update({busy => 0, status => 1, errormsg => $3, stoptime => time});
|
2008-11-12 13:00:56 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
print STDERR "unknown Nix trace message: $_";
|
|
|
|
}
|
2008-11-11 17:49:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
close OUT;
|
|
|
|
|
|
|
|
my $res = $?;
|
2008-11-10 13:33:12 +00:00
|
|
|
|
|
|
|
$stopTime = time();
|
|
|
|
|
2008-11-25 00:38:16 +00:00
|
|
|
if ($res != 0) {
|
|
|
|
if ($thisBuildFailed) { $buildStatus = 1; }
|
|
|
|
elsif ($someBuildFailed) { $buildStatus = 2; }
|
|
|
|
else { $buildStatus = 3; }
|
|
|
|
}
|
2008-11-10 13:33:12 +00:00
|
|
|
|
2008-11-25 00:38:16 +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
|
|
|
}
|
|
|
|
|
2009-03-09 17:21:10 +00:00
|
|
|
done:
|
|
|
|
|
2009-04-22 22:43:04 +00:00
|
|
|
txn_do($db, sub {
|
2009-03-09 17:21:10 +00:00
|
|
|
$build->update({finished => 1, timestamp => time});
|
2008-11-11 12:54:37 +00:00
|
|
|
|
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;
|
|
|
|
}
|
2008-11-25 00:38:16 +00:00
|
|
|
|
2008-12-16 16:26:33 +00:00
|
|
|
$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
|
2009-03-26 15:32:19 +00:00
|
|
|
, logfile => getBuildLog($drvPath)
|
2008-11-25 00:38:16 +00:00
|
|
|
, errormsg => $errormsg
|
2008-11-25 16:13:22 +00:00
|
|
|
, releasename => $releaseName
|
2008-11-10 13:33:12 +00:00
|
|
|
});
|
|
|
|
|
2008-11-25 00:38:16 +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>) {
|
2009-03-06 13:34:53 +00:00
|
|
|
/^([\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;
|
2009-03-06 13:34:53 +00:00
|
|
|
my $defaultPath = $5;
|
2008-11-29 01:27:17 +00:00
|
|
|
next unless -e $path;
|
2008-11-12 14:29:32 +00:00
|
|
|
|
2008-11-12 15:36:50 +00:00
|
|
|
my $fileSize, my $sha1, my $sha256;
|
|
|
|
|
2009-03-06 13:34:53 +00:00
|
|
|
# !!! validate $path, $defaultPath
|
|
|
|
|
2008-11-12 15:36:50 +00:00
|
|
|
if (-f $path) {
|
|
|
|
my $st = stat($path) or die "cannot stat $path: $!";
|
2008-11-12 16:42:07 +00:00
|
|
|
$fileSize = $st->size;
|
2008-11-12 15:36:50 +00:00
|
|
|
|
|
|
|
$sha1 = `nix-hash --flat --type sha1 $path`
|
|
|
|
or die "cannot hash $path: $?";;
|
|
|
|
chomp $sha1;
|
2008-11-12 14:29:32 +00:00
|
|
|
|
2008-11-12 15:36:50 +00:00
|
|
|
$sha256 = `nix-hash --flat --type sha256 $path`
|
|
|
|
or die "cannot hash $path: $?";;
|
|
|
|
chomp $sha256;
|
|
|
|
}
|
2009-03-06 13:34:53 +00:00
|
|
|
|
|
|
|
my $name = $path eq $outPath ? "" : basename $path;
|
2008-11-12 14:29:32 +00:00
|
|
|
|
2008-12-16 16:26:33 +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
|
2008-11-12 15:36:50 +00:00
|
|
|
, filesize => $fileSize
|
2008-11-12 14:29:32 +00:00
|
|
|
, sha1hash => $sha1
|
|
|
|
, sha256hash => $sha256
|
2009-03-06 13:34:53 +00:00
|
|
|
, name => $name
|
|
|
|
, defaultpath => $defaultPath
|
2008-11-10 13:33:12 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
close LIST;
|
2008-11-12 14:29:32 +00:00
|
|
|
}
|
|
|
|
|
2008-11-25 00:38:16 +00:00
|
|
|
else {
|
2008-12-16 16:26:33 +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 => "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
|
|
|
});
|
2009-07-07 16:15:38 +00:00
|
|
|
|
2009-07-08 15:52:55 +00:00
|
|
|
sendEmailNotification $build;
|
2008-11-10 13:33:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-11 12:54:37 +00:00
|
|
|
my $buildId = $ARGV[0] or die;
|
2008-11-11 17:49:50 +00:00
|
|
|
print STDERR "performing build $buildId\n";
|
2008-11-10 13:33:12 +00:00
|
|
|
|
2009-07-08 15:52:55 +00:00
|
|
|
if ($ENV{'HYDRA_MAIL_TEST'}) {
|
|
|
|
sendEmailNotification $db->resultset('Builds')->find($buildId);
|
|
|
|
exit 0;
|
|
|
|
}
|
2009-07-07 16:15:38 +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;
|
2009-04-22 22:43:04 +00:00
|
|
|
txn_do($db, sub {
|
2008-11-27 15:16:06 +00:00
|
|
|
$build = $db->resultset('Builds')->find($buildId);
|
2008-11-11 12:54:37 +00:00
|
|
|
die "build $buildId doesn't exist" unless defined $build;
|
2009-03-06 13:34:53 +00:00
|
|
|
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
|
|
|
}
|
2009-03-09 16:22:41 +00:00
|
|
|
$build->schedulingInfo->update({busy => 1, locker => $$});
|
2009-03-16 16:56:47 +00:00
|
|
|
$build->buildsteps->search({busy => 1})->delete_all;
|
2009-03-06 13:34:53 +00:00
|
|
|
$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 $@;
|
2009-04-22 22:43:04 +00:00
|
|
|
txn_do($db, sub {
|
2009-03-09 16:22:41 +00:00
|
|
|
$build->schedulingInfo->update({busy => 0, locker => $$});
|
2008-11-10 13:33:12 +00:00
|
|
|
});
|
|
|
|
}
|