2008-11-28 16:13:06 +00:00
|
|
|
#! /var/run/current-system/sw/bin/perl -w
|
2008-10-10 16:05:05 +00:00
|
|
|
|
|
|
|
use strict;
|
2009-03-09 13:04:46 +00:00
|
|
|
use feature 'switch';
|
2008-10-10 16:05:05 +00:00
|
|
|
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;
|
2009-10-26 15:39:14 +00:00
|
|
|
use Hydra::Helper::AddBuilds;
|
2008-11-25 13:27:57 +00:00
|
|
|
use IPC::Run;
|
2008-10-28 10:18:03 +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-05 04:52:52 +00:00
|
|
|
|
|
|
|
|
2008-11-25 13:27:57 +00:00
|
|
|
sub captureStdoutStderr {
|
|
|
|
my $stdin = ""; my $stdout; my $stderr;
|
|
|
|
my $res = IPC::Run::run(\@_, \$stdin, \$stdout, \$stderr);
|
|
|
|
return ($res, $stdout, $stderr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-09 13:04:46 +00:00
|
|
|
sub fetchInputs {
|
2009-03-09 13:58:43 +00:00
|
|
|
my ($project, $jobset, $inputInfo) = @_;
|
2009-03-09 13:04:46 +00:00
|
|
|
foreach my $input ($jobset->jobsetinputs->all) {
|
|
|
|
foreach my $alt ($input->jobsetinputalts->all) {
|
2009-10-26 15:39:14 +00:00
|
|
|
my $info = fetchInput($db, $project, $jobset, $input->name, $input->type, $alt->value);
|
2009-03-09 13:58:43 +00:00
|
|
|
push @{$$inputInfo{$input->name}}, $info if defined $info;
|
2009-03-09 13:04:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-26 15:55:19 +00:00
|
|
|
# Check whether to add the build described by $buildInfo.
|
|
|
|
sub checkBuild {
|
|
|
|
my ($project, $jobset, $inputInfo, $nixExprInput, $buildInfo, $currentBuilds) = @_;
|
2008-11-07 14:51:44 +00:00
|
|
|
|
2009-10-26 15:55:19 +00:00
|
|
|
my $jobName = $buildInfo->{jobName};
|
|
|
|
my $drvPath = $buildInfo->{drvPath};
|
|
|
|
my $outPath = $buildInfo->{outPath};
|
2008-11-07 14:51:44 +00:00
|
|
|
|
2008-11-26 14:43:16 +00:00
|
|
|
my $priority = 100;
|
2009-10-26 15:55:19 +00:00
|
|
|
$priority = int($buildInfo->{schedulingPriority})
|
|
|
|
if $buildInfo->{schedulingPriority} =~ /^\d+$/;
|
2008-11-26 14:43:16 +00:00
|
|
|
|
2009-04-22 22:43:04 +00:00
|
|
|
txn_do($db, sub {
|
2009-10-08 11:19:39 +00:00
|
|
|
# Update the last evaluation time in the database.
|
2009-10-26 15:55:19 +00:00
|
|
|
my $job = $jobset->jobs->update_or_create(
|
2009-03-13 14:49:25 +00:00
|
|
|
{ name => $jobName
|
|
|
|
, lastevaltime => time
|
|
|
|
});
|
|
|
|
|
2009-10-26 15:55:19 +00:00
|
|
|
$job->update({firstevaltime => time})
|
|
|
|
unless defined $job->firstevaltime;
|
2009-03-13 14:49:25 +00:00
|
|
|
|
2009-10-07 13:18:12 +00:00
|
|
|
# Don't add a build that has already been scheduled for this
|
|
|
|
# job, or has been built but is still a "current" build for
|
|
|
|
# this job. Note that this means that if the sources of a job
|
|
|
|
# are changed from A to B and then reverted to A, three builds
|
|
|
|
# will be performed (though the last one will probably use the
|
|
|
|
# cached result from the first). This ensures that the builds
|
|
|
|
# with the highest ID will always be the ones that we want in
|
|
|
|
# the channels.
|
2009-10-19 12:36:15 +00:00
|
|
|
# !!! Checking $outPath doesn't take meta-attributes into
|
2009-10-07 13:18:12 +00:00
|
|
|
# account. For instance, do we want a new build to be
|
|
|
|
# scheduled if the meta.maintainers field is changed?
|
2009-10-26 15:55:19 +00:00
|
|
|
my @previousBuilds = $job->builds->search({outPath => $outPath, isCurrent => 1});
|
2009-10-02 16:06:28 +00:00
|
|
|
if (scalar(@previousBuilds) > 0) {
|
2009-10-07 13:18:12 +00:00
|
|
|
print "already scheduled/built\n";
|
|
|
|
$currentBuilds->{$_->id} = 1 foreach @previousBuilds;
|
|
|
|
return;
|
2008-11-10 13:33:12 +00:00
|
|
|
}
|
2009-10-07 13:18:12 +00:00
|
|
|
|
2009-03-13 14:49:25 +00:00
|
|
|
# Nope, so add it.
|
2009-10-26 15:55:19 +00:00
|
|
|
my $build = $job->builds->create(
|
2008-11-11 12:54:37 +00:00
|
|
|
{ finished => 0
|
|
|
|
, timestamp => time()
|
2009-10-26 15:55:19 +00:00
|
|
|
, description => $buildInfo->{description}
|
|
|
|
, longdescription => $buildInfo->{longDescription}
|
|
|
|
, license => $buildInfo->{license}
|
|
|
|
, homepage => $buildInfo->{homepage}
|
|
|
|
, maintainers => $buildInfo->{maintainers}
|
|
|
|
, nixname => $buildInfo->{nixName}
|
2008-11-10 13:33:12 +00:00
|
|
|
, drvpath => $drvPath
|
|
|
|
, outpath => $outPath
|
2009-10-26 15:55:19 +00:00
|
|
|
, system => $buildInfo->{system}
|
2009-10-02 16:06:28 +00:00
|
|
|
, iscurrent => 1
|
2009-10-26 13:33:48 +00:00
|
|
|
, nixexprinput => $jobset->nixexprinput
|
|
|
|
, nixexprpath => $jobset->nixexprpath
|
2008-11-10 13:33:12 +00:00
|
|
|
});
|
|
|
|
|
2009-10-02 16:06:28 +00:00
|
|
|
print "added to queue as build ", $build->id, "\n";
|
|
|
|
|
|
|
|
$currentBuilds->{$build->id} = 1;
|
|
|
|
|
2009-07-07 13:59:59 +00:00
|
|
|
$build->create_related('buildschedulinginfo',
|
2009-03-13 14:49:25 +00:00
|
|
|
{ priority => $priority
|
2008-11-11 12:54:37 +00:00
|
|
|
, busy => 0
|
|
|
|
, locker => ""
|
|
|
|
});
|
|
|
|
|
2009-03-23 21:42:59 +00:00
|
|
|
my %inputs;
|
|
|
|
$inputs{$jobset->nixexprinput} = $nixExprInput;
|
2009-10-26 15:55:19 +00:00
|
|
|
foreach my $arg (@{$buildInfo->{arg}}) {
|
2009-03-23 21:42:59 +00:00
|
|
|
$inputs{$arg->{name}} = $inputInfo->{$arg->{name}}->[$arg->{altnr}]
|
|
|
|
|| die "invalid input";
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach my $name (keys %inputs) {
|
|
|
|
my $input = $inputs{$name};
|
2009-03-13 14:49:25 +00:00
|
|
|
$build->buildinputs_builds->create(
|
2009-03-23 21:42:59 +00:00
|
|
|
{ name => $name
|
2008-11-10 13:33:12 +00:00
|
|
|
, type => $input->{type}
|
|
|
|
, uri => $input->{uri}
|
2008-11-25 14:59:08 +00:00
|
|
|
, revision => $input->{revision}
|
2008-11-10 13:33:12 +00:00
|
|
|
, value => $input->{value}
|
|
|
|
, dependency => $input->{id}
|
2009-03-23 21:42:59 +00:00
|
|
|
, path => $input->{storePath} || "" # !!! temporary hack
|
2008-11-12 23:14:57 +00:00
|
|
|
, sha256hash => $input->{sha256hash}
|
2008-11-10 13:33:12 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2008-11-07 14:51:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-11-25 13:27:57 +00:00
|
|
|
sub setJobsetError {
|
|
|
|
my ($jobset, $errorMsg) = @_;
|
|
|
|
eval {
|
2009-04-22 22:43:04 +00:00
|
|
|
txn_do($db, sub {
|
2009-03-09 16:22:41 +00:00
|
|
|
$jobset->update({errormsg => $errorMsg, errortime => time});
|
2008-11-25 13:27:57 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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};
|
|
|
|
}
|
2009-03-09 13:58:43 +00:00
|
|
|
when (["svn", "path", "build"]) {
|
2009-03-09 13:04:46 +00:00
|
|
|
push @res, "--arg", $input, (
|
|
|
|
"{ outPath = builtins.storePath " . $alt->{storePath} . "" .
|
2009-03-09 13:58:43 +00:00
|
|
|
(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-25 13:27:57 +00:00
|
|
|
|
2009-04-23 15:40:36 +00:00
|
|
|
sub permute {
|
|
|
|
my @list = @_;
|
|
|
|
for (my $n = scalar @list - 1; $n > 0; $n--) {
|
|
|
|
my $k = int(rand($n + 1)); # 0 <= $k <= $n
|
|
|
|
@list[$n, $k] = @list[$k, $n];
|
|
|
|
}
|
|
|
|
return @list;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-20 14:50:09 +00:00
|
|
|
sub checkJobset {
|
2008-11-07 14:51:44 +00:00
|
|
|
my ($project, $jobset) = @_;
|
|
|
|
my $inputInfo = {};
|
2008-11-26 13:39:15 +00:00
|
|
|
|
2009-03-09 13:04:46 +00:00
|
|
|
# Fetch all values for all inputs.
|
2009-03-09 13:58:43 +00:00
|
|
|
fetchInputs($project, $jobset, $inputInfo);
|
2008-11-07 14:51:44 +00:00
|
|
|
|
2009-03-09 13:04:46 +00:00
|
|
|
# Evaluate the job expression.
|
2009-03-23 21:42:59 +00:00
|
|
|
my $nixExprInput = $inputInfo->{$jobset->nixexprinput}->[0]
|
2009-03-09 13:04:46 +00:00
|
|
|
or die "cannot find the input containing the job expression";
|
2009-04-22 22:43:04 +00:00
|
|
|
die "multiple alternatives for the input containing the Nix expression are not supported"
|
|
|
|
if scalar @{$inputInfo->{$jobset->nixexprinput}} != 1;
|
2009-03-23 21:42:59 +00:00
|
|
|
my $nixExprPath = $nixExprInput->{storePath} . "/" . $jobset->nixexprpath;
|
2008-11-25 13:27:57 +00:00
|
|
|
|
|
|
|
(my $res, my $jobsXml, my $stderr) = captureStdoutStderr(
|
2009-03-15 11:56:11 +00:00
|
|
|
"hydra_eval_jobs", $nixExprPath, "--gc-roots-dir", getGCRootsDir,
|
|
|
|
inputsToArgs($inputInfo));
|
2008-11-25 13:27:57 +00:00
|
|
|
die "cannot evaluate the Nix expression containing the jobs:\n$stderr" unless $res;
|
2008-11-04 18:23:28 +00:00
|
|
|
|
2009-03-15 11:56:11 +00:00
|
|
|
print STDERR "$stderr";
|
|
|
|
|
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.
|
2009-10-02 16:06:28 +00:00
|
|
|
my %currentBuilds;
|
2009-04-23 15:40:36 +00:00
|
|
|
foreach my $job (permute @{$jobs->{job}}) {
|
2009-03-13 14:49:25 +00:00
|
|
|
next if $job->{jobName} eq "";
|
2009-03-09 13:04:46 +00:00
|
|
|
print "considering job " . $job->{jobName} . "\n";
|
2009-10-26 15:55:19 +00:00
|
|
|
checkBuild($project, $jobset, $inputInfo, $nixExprInput, $job, \%currentBuilds);
|
2008-11-06 18:26:29 +00:00
|
|
|
}
|
2009-03-09 15:16:11 +00:00
|
|
|
|
2009-04-22 22:43:04 +00:00
|
|
|
txn_do($db, sub {
|
2009-10-02 16:06:28 +00:00
|
|
|
|
2009-10-08 11:19:39 +00:00
|
|
|
# Update the last checked times and error messages for each
|
|
|
|
# job.
|
2009-10-02 16:06:28 +00:00
|
|
|
my %failedJobNames;
|
|
|
|
push @{$failedJobNames{$_->{location}}}, $_->{msg} foreach @{$jobs->{error}};
|
|
|
|
|
2009-03-20 14:50:09 +00:00
|
|
|
$jobset->update({lastcheckedtime => time});
|
|
|
|
|
2009-10-26 15:55:19 +00:00
|
|
|
foreach my $job ($jobset->jobs->all) {
|
|
|
|
if ($failedJobNames{$job->name}) {
|
|
|
|
$job->update({errormsg => join '\n', @{$failedJobNames{$job->name}}});
|
2009-03-13 14:49:25 +00:00
|
|
|
} else {
|
2009-10-26 15:55:19 +00:00
|
|
|
$job->update({errormsg => undef});
|
2009-03-13 14:49:25 +00:00
|
|
|
}
|
|
|
|
}
|
2009-10-02 16:06:28 +00:00
|
|
|
|
|
|
|
# Clear the "current" flag on all builds that are no longer
|
|
|
|
# current.
|
|
|
|
foreach my $build ($jobset->builds->search({iscurrent => 1})) {
|
|
|
|
$build->update({iscurrent => 0}) unless $currentBuilds{$build->id};
|
|
|
|
}
|
2009-03-13 14:49:25 +00:00
|
|
|
|
2009-10-02 16:06:28 +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}}) {
|
2009-03-20 17:06:50 +00:00
|
|
|
my $bindings = "";
|
|
|
|
foreach my $arg (@{$error->{arg}}) {
|
|
|
|
my $input = $inputInfo->{$arg->{name}}->[$arg->{altnr}] or die "invalid input";
|
|
|
|
$bindings .= ", " if $bindings ne "";
|
|
|
|
$bindings .= $arg->{name} . " = ";
|
|
|
|
given ($input->{type}) {
|
|
|
|
when ("string") { $bindings .= "\"" . $input->{value} . "\""; }
|
|
|
|
when ("boolean") { $bindings .= $input->{value}; }
|
|
|
|
default { $bindings .= "..."; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$msg .= "at `" . $error->{location} . "' [$bindings]:\n" . $error->{msg} . "\n\n";
|
2009-03-09 15:16:11 +00:00
|
|
|
}
|
|
|
|
setJobsetError($jobset, $msg);
|
2008-11-04 18:23:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-20 14:50:09 +00:00
|
|
|
sub checkJobsetWrapped {
|
|
|
|
my ($project, $jobset) = @_;
|
|
|
|
|
|
|
|
print "considering jobset ", $jobset->name, " in ", $project->name, "\n";
|
|
|
|
|
|
|
|
eval {
|
|
|
|
checkJobset($project, $jobset);
|
|
|
|
};
|
|
|
|
|
|
|
|
if ($@) {
|
|
|
|
my $msg = $@;
|
|
|
|
print "error evaluating jobset ", $jobset->name, ": $msg";
|
2009-04-22 22:43:04 +00:00
|
|
|
txn_do($db, sub {
|
2009-03-20 14:50:09 +00:00
|
|
|
$jobset->update({lastcheckedtime => time});
|
|
|
|
setJobsetError($jobset, $msg);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2008-11-04 18:23:28 +00:00
|
|
|
|
2009-03-20 14:50:09 +00:00
|
|
|
|
2009-10-26 15:55:19 +00:00
|
|
|
sub checkProjects {
|
2008-11-18 12:48:58 +00:00
|
|
|
foreach my $project ($db->resultset('Projects')->search({enabled => 1})) {
|
2008-11-25 13:27:57 +00:00
|
|
|
print "considering project ", $project->name, "\n";
|
2009-10-08 11:39:16 +00:00
|
|
|
checkJobsetWrapped($project, $_)
|
|
|
|
foreach $project->jobsets->search({enabled => 1});
|
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;
|
2009-03-20 14:50:09 +00:00
|
|
|
checkJobsetWrapped($jobset->project, $jobset);
|
2009-03-05 12:32:14 +00:00
|
|
|
exit 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-11 10:27:36 +00:00
|
|
|
while (1) {
|
2009-03-23 01:13:37 +00:00
|
|
|
eval {
|
2009-10-26 15:55:19 +00:00
|
|
|
checkProjects;
|
2009-03-23 01:13:37 +00:00
|
|
|
};
|
|
|
|
if ($@) { print "$@"; }
|
2008-11-11 10:27:36 +00:00
|
|
|
print "sleeping...\n";
|
2008-11-29 01:20:13 +00:00
|
|
|
sleep 30;
|
2008-11-11 10:27:36 +00:00
|
|
|
}
|