hydra/src/script/hydra_scheduler.pl

386 lines
12 KiB
Perl
Raw Normal View History

2008-11-28 16:13:06 +00:00
#! /var/run/current-system/sw/bin/perl -w
use strict;
2009-03-09 13:04:46 +00:00
use feature 'switch';
use XML::Simple;
2008-11-25 11:09:15 +00:00
use Hydra::Schema;
2008-11-28 14:36:04 +00:00
use Hydra::Helper::Nix;
use IPC::Run;
use POSIX qw(strftime);
2008-11-28 14:36:04 +00:00
my $db = openHydraDB;
2008-11-05 04:52:52 +00:00
sub captureStdoutStderr {
my $stdin = ""; my $stdout; my $stderr;
my $res = IPC::Run::run(\@_, \$stdin, \$stdout, \$stderr);
return ($res, $stdout, $stderr);
}
2008-11-12 23:14:57 +00:00
sub getStorePathHash {
my ($storePath) = @_;
my $hash = `nix-store --query --hash $storePath`
or die "cannot get hash of $storePath";
chomp $hash;
die unless $hash =~ /^sha256:(.*)$/;
$hash = $1;
$hash = `nix-hash --to-base16 --type sha256 $hash`
or die "cannot convert hash";
chomp $hash;
return $hash;
}
2009-03-09 13:04:46 +00:00
sub fetchInputAlt {
my ($project, $jobset, $input, $alt) = @_;
2008-11-04 18:23:28 +00:00
my $type = $input->type;
if ($type eq "path") {
2008-11-17 13:36:58 +00:00
my $uri = $alt->value;
my $timestamp = time;
my $sha256;
my $storePath;
# Some simple caching: don't check a path more than once every N seconds.
(my $cachedInput) = $db->resultset('CachedPathInputs')->search(
{srcpath => $uri, lastseen => {">", $timestamp - 60}},
{rows => 1, order_by => "lastseen DESC"});
if (defined $cachedInput && isValidPath($cachedInput->storepath)) {
$storePath = $cachedInput->storepath;
$sha256 = $cachedInput->sha256hash;
$timestamp = $cachedInput->timestamp;
} else {
print "copying input ", $input->name, " from $uri\n";
$storePath = `nix-store --add "$uri"`
or die "cannot copy path $uri to the Nix store";
chomp $storePath;
$sha256 = getStorePathHash $storePath;
($cachedInput) = $db->resultset('CachedPathInputs')->search(
{srcpath => $uri, sha256hash => $sha256});
# Path inputs don't have a natural notion of a "revision",
# so we simulate it by using the timestamp that we first
# saw this path have this SHA-256 hash. So if the
# contents of the path changes, we get a new "revision",
# but if it doesn't change (or changes back), we don't get
# a new "revision".
if (!defined $cachedInput) {
$db->txn_do(sub {
$db->resultset('CachedPathInputs')->create(
{ srcpath => $uri
, timestamp => $timestamp
, lastseen => $timestamp
, sha256hash => $sha256
, storepath => $storePath
});
});
} else {
$timestamp = $cachedInput->timestamp;
$db->txn_do(sub {
2009-03-09 16:22:41 +00:00
$cachedInput->update({lastseen => time});
});
}
}
2009-03-09 13:04:46 +00:00
return
2008-11-12 23:14:57 +00:00
{ type => $type
, uri => $uri
, storePath => $storePath
, sha256hash => $sha256
, revision => strftime "%Y%m%d%H%M%S", gmtime($timestamp)
2008-11-12 23:14:57 +00:00
};
2008-11-04 18:23:28 +00:00
}
2008-11-25 18:13:55 +00:00
elsif ($type eq "svn") {
my $uri = $alt->value;
my $sha256;
my $storePath;
# First figure out the last-modified revision of the URI.
my @cmd = (["svn", "ls", "-v", "--depth", "empty", $uri],
"|", ["sed", 's/^ *\([0-9]*\).*/\1/']);
my $stdout; my $stderr;
die "cannot get head revision of Subversion repository at `$uri':\n$stderr"
unless IPC::Run::run(@cmd, \$stdout, \$stderr);
my $revision = $stdout; chomp $revision;
die unless $revision =~ /^\d+$/;
(my $cachedInput) = $db->resultset('CachedSubversionInputs')->search(
2008-11-25 18:13:55 +00:00
{uri => $uri, revision => $revision});
if (defined $cachedInput && isValidPath($cachedInput->storepath)) {
$storePath = $cachedInput->storepath;
$sha256 = $cachedInput->sha256hash;
} else {
# Then download this revision into the store.
print "checking out Subversion input ", $input->name, " from $uri revision $revision\n";
$ENV{"NIX_HASH_ALGO"} = "sha256";
$ENV{"PRINT_PATH"} = "1";
(my $res, $stdout, $stderr) = captureStdoutStderr(
"nix-prefetch-svn", $uri, $revision);
die "cannot check out Subversion repository `$uri':\n$stderr" unless $res;
($sha256, $storePath) = split ' ', $stdout;
$db->txn_do(sub {
$db->resultset('CachedSubversionInputs')->create(
2008-11-25 18:13:55 +00:00
{ uri => $uri
, revision => $revision
, sha256hash => $sha256
, storepath => $storePath
});
});
}
2009-03-09 13:04:46 +00:00
return
2008-11-25 18:13:55 +00:00
{ type => $type
, uri => $uri
, storePath => $storePath
, sha256hash => $sha256
, revision => $revision
};
}
elsif ($type eq "build") {
my $jobName = $alt->value or die;
# Pick the most recent successful build of the specified job.
(my $prevBuild) = $db->resultset('Builds')->search(
{finished => 1, project => $project->name, jobset => $jobset->name, attrname => $jobName, buildStatus => 0},
{join => 'resultInfo', order_by => "timestamp DESC", rows => 1});
if (!defined $prevBuild || !isValidPath($prevBuild->outpath)) {
print STDERR "no previous build available for `$jobName'";
return undef;
}
my $pkgNameRE = "(?:(?:[A-Za-z0-9]|(?:-[^0-9]))+)";
my $versionRE = "(?:[A-Za-z0-9\.\-]+)";
my $relName = ($prevBuild->resultInfo->releasename or $prevBuild->nixname);
my $version = $2 if $relName =~ /^($pkgNameRE)-($versionRE)$/;
return
{ type => "build"
, storePath => $prevBuild->outpath
, id => $prevBuild->id
, version => $version
};
}
2008-11-25 18:13:55 +00:00
2008-11-06 18:26:29 +00:00
elsif ($type eq "string") {
die unless defined $alt->value;
2009-03-09 13:04:46 +00:00
return {type => $type, value => $alt->value};
2008-11-06 18:26:29 +00:00
}
2008-11-25 18:34:24 +00:00
elsif ($type eq "boolean") {
die unless defined $alt->value && ($alt->value eq "true" || $alt->value eq "false");
2009-03-09 13:04:46 +00:00
return {type => $type, value => $alt->value};
2008-11-25 18:34:24 +00:00
}
2008-11-04 18:23:28 +00:00
else {
2008-11-25 18:13:55 +00:00
die "input `" . $input->name . "' has unknown type `$type'";
2008-11-04 18:23:28 +00:00
}
}
2009-03-09 13:04:46 +00:00
sub fetchInputs {
my ($project, $jobset, $inputInfo) = @_;
2009-03-09 13:04:46 +00:00
foreach my $input ($jobset->jobsetinputs->all) {
foreach my $alt ($input->jobsetinputalts->all) {
my $info = fetchInputAlt($project, $jobset, $input, $alt);
push @{$$inputInfo{$input->name}}, $info if defined $info;
2009-03-09 13:04:46 +00:00
}
}
}
2008-11-07 14:51:44 +00:00
sub checkJob {
2009-03-09 13:04:46 +00:00
my ($project, $jobset, $inputInfo, $job) = @_;
2008-11-07 14:51:44 +00:00
2009-03-09 13:04:46 +00:00
my $jobName = $job->{jobName};
my $drvPath = $job->{drvPath};
2008-11-07 14:51:44 +00:00
my $outPath = $job->{outPath};
my $priority = 100;
2009-03-09 13:04:46 +00:00
$priority = int($job->{schedulingPriority})
if $job->{schedulingPriority} =~ /^\d+$/;
2008-11-10 13:33:12 +00:00
$db->txn_do(sub {
if (scalar($db->resultset('Builds')->search(
{ project => $project->name, jobset => $jobset->name
, attrname => $jobName, outPath => $outPath })) > 0)
{
print "already scheduled/done\n";
2008-11-10 13:33:12 +00:00
return;
}
print "adding to queue\n";
2008-11-11 12:54:37 +00:00
my $build = $db->resultset('Builds')->create(
{ finished => 0
, timestamp => time()
2008-11-10 13:33:12 +00:00
, project => $project->name
, jobset => $jobset->name
, attrname => $jobName
2009-03-09 13:04:46 +00:00
, description => $job->{description}
, longdescription => $job->{longDescription}
, license => $job->{license}
, nixname => $job->{nixName}
2008-11-10 13:33:12 +00:00
, drvpath => $drvPath
, outpath => $outPath
, system => $job->{system}
});
$db->resultset('BuildSchedulingInfo')->create(
2008-11-11 12:54:37 +00:00
{ id => $build->id
, priority => $priority
2008-11-11 12:54:37 +00:00
, busy => 0
, locker => ""
});
2009-03-09 13:04:46 +00:00
foreach my $arg (@{$job->{arg}}) {
my $input = $inputInfo->{$arg->{name}}->[$arg->{altnr}] or die "invalid input";
$db->resultset('BuildInputs')->create(
2008-11-11 12:54:37 +00:00
{ build => $build->id
2009-03-09 13:04:46 +00:00
, name => $arg->{name}
2008-11-10 13:33:12 +00:00
, type => $input->{type}
, uri => $input->{uri}
, revision => $input->{revision}
2008-11-10 13:33:12 +00:00
, value => $input->{value}
, dependency => $input->{id}
, path => ($input->{storePath} or "") # !!! temporary hack
2008-11-12 23:14:57 +00:00
, sha256hash => $input->{sha256hash}
2008-11-10 13:33:12 +00:00
});
}
2009-03-09 13:04:46 +00:00
# !!! this should really by done by nix-instantiate to prevent a GC race.
registerRoot $drvPath;
2008-11-10 13:33:12 +00:00
});
2008-11-07 14:51:44 +00:00
};
sub setJobsetError {
my ($jobset, $errorMsg) = @_;
eval {
$db->txn_do(sub {
2009-03-09 16:22:41 +00:00
$jobset->update({errormsg => $errorMsg, errortime => time});
});
};
}
2009-03-09 13:04:46 +00:00
sub inputsToArgs {
my ($inputInfo) = @_;
my @res = ();
foreach my $input (keys %{$inputInfo}) {
foreach my $alt (@{$inputInfo->{$input}}) {
given ($alt->{type}) {
when ("string") {
push @res, "--argstr", $input, $alt->{value};
}
when ("boolean") {
push @res, "--arg", $input, $alt->{value};
}
when (["svn", "path", "build"]) {
2009-03-09 13:04:46 +00:00
push @res, "--arg", $input, (
"{ outPath = builtins.storePath " . $alt->{storePath} . "" .
(defined $alt->{revision} ? "; rev = \"" . $alt->{revision} . "\"" : "") .
(defined $alt->{version} ? "; version = \"" . $alt->{version} . "\"" : "") .
2009-03-09 13:04:46 +00:00
";}"
);
}
2008-11-07 14:51:44 +00:00
}
}
}
2009-03-09 13:04:46 +00:00
return @res;
2008-11-07 14:51:44 +00:00
}
2008-11-07 14:51:44 +00:00
sub checkJobSet {
my ($project, $jobset) = @_;
my $inputInfo = {};
2008-11-26 13:39:15 +00:00
$db->txn_do(sub {
2009-03-09 16:22:41 +00:00
$jobset->update({lastcheckedtime => time});
2008-11-26 13:39:15 +00:00
});
2008-11-07 14:51:44 +00:00
2009-03-09 13:04:46 +00:00
# Fetch all values for all inputs.
fetchInputs($project, $jobset, $inputInfo);
2008-11-07 14:51:44 +00:00
2009-03-09 13:04:46 +00:00
# Evaluate the job expression.
my $nixExprPath = $inputInfo->{$jobset->nixexprinput}->[0]->{storePath}
or die "cannot find the input containing the job expression";
$nixExprPath .= "/" . $jobset->nixexprpath;
(my $res, my $jobsXml, my $stderr) = captureStdoutStderr(
2009-03-09 15:16:11 +00:00
"hydra_eval_jobs", $nixExprPath, inputsToArgs($inputInfo));
die "cannot evaluate the Nix expression containing the jobs:\n$stderr" unless $res;
2008-11-04 18:23:28 +00:00
my $jobs = XMLin($jobsXml,
2009-03-09 13:04:46 +00:00
ForceArray => ['error', 'job', 'arg'],
KeyAttr => [],
SuppressEmpty => '')
2008-11-04 18:23:28 +00:00
or die "cannot parse XML output";
2009-03-09 13:04:46 +00:00
# Schedule each successfully evaluated job.
foreach my $job (@{$jobs->{job}}) {
print "considering job " . $job->{jobName} . "\n";
checkJob($project, $jobset, $inputInfo, $job);
2008-11-06 18:26:29 +00:00
}
2009-03-09 15:16:11 +00:00
# Store the errors messages for jobs that failed to evaluate.
my $msg = "";
foreach my $error (@{$jobs->{error}}) {
$msg .= "at `" . $error->{location} . "': " . $error->{msg} . "\n";
}
setJobsetError($jobset, $msg);
2008-11-04 18:23:28 +00:00
}
sub checkJobs {
2008-11-18 12:48:58 +00:00
foreach my $project ($db->resultset('Projects')->search({enabled => 1})) {
print "considering project ", $project->name, "\n";
2008-11-04 18:23:28 +00:00
foreach my $jobset ($project->jobsets->all) {
print "considering jobset ", $jobset->name, " in ", $project->name, "\n";
2008-11-18 12:48:58 +00:00
eval {
checkJobSet($project, $jobset);
};
if ($@) {
print "error evaluating jobset ", $jobset->name, ": $@";
2008-11-25 18:13:55 +00:00
setJobsetError($jobset, $@);
}
}
2008-11-04 18:23:28 +00:00
}
}
2009-03-05 12:32:14 +00:00
# For testing: evaluate a single jobset, then exit.
if (scalar @ARGV == 2) {
my $projectName = $ARGV[0];
my $jobsetName = $ARGV[1];
my $jobset = $db->resultset('Jobsets')->find($projectName, $jobsetName) or die;
checkJobSet($jobset->project, $jobset);
exit 0;
}
while (1) {
checkJobs;
print "sleeping...\n";
sleep 30;
}