2010-09-30 14:29:15 +00:00
|
|
|
#! @perl@ -w -I@nix@/libexec/nix
|
2009-01-13 14:02:07 +00:00
|
|
|
|
|
|
|
use strict;
|
|
|
|
use File::Path;
|
|
|
|
use File::Basename;
|
2011-11-30 14:25:28 +00:00
|
|
|
use Nix::Store;
|
2009-01-13 14:02:07 +00:00
|
|
|
use Hydra::Schema;
|
|
|
|
use Hydra::Helper::Nix;
|
2009-02-06 14:17:25 +00:00
|
|
|
use POSIX qw(strftime);
|
2009-01-13 14:02:07 +00:00
|
|
|
|
|
|
|
my $db = openHydraDB;
|
|
|
|
|
|
|
|
|
|
|
|
my %roots;
|
|
|
|
|
|
|
|
sub registerRoot {
|
|
|
|
my ($path) = @_;
|
2009-02-06 21:01:20 +00:00
|
|
|
Hydra::Helper::Nix::registerRoot($path);
|
2009-01-13 14:02:07 +00:00
|
|
|
$roots{$path} = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-06 14:17:25 +00:00
|
|
|
sub keepBuild {
|
|
|
|
my ($build) = @_;
|
2009-10-26 12:47:29 +00:00
|
|
|
print STDERR " keeping build ", $build->id, " (",
|
2010-02-15 10:21:11 +00:00
|
|
|
$build->system, "; ",
|
|
|
|
strftime("%Y-%m-%d %H:%M:%S", localtime($build->timestamp)), ")\n";
|
2009-02-06 14:17:25 +00:00
|
|
|
if (isValidPath($build->outpath)) {
|
|
|
|
registerRoot $build->outpath;
|
|
|
|
} else {
|
|
|
|
print STDERR "warning: output ", $build->outpath, " has disappeared\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-13 14:02:07 +00:00
|
|
|
|
2009-02-06 15:02:49 +00:00
|
|
|
# Go over all projects.
|
2009-01-13 14:02:07 +00:00
|
|
|
|
2010-08-10 08:00:28 +00:00
|
|
|
foreach my $project ($db->resultset('Projects')->all) {
|
2009-01-13 14:02:07 +00:00
|
|
|
|
2010-11-19 11:01:31 +00:00
|
|
|
# Go over all jobsets in this project.
|
|
|
|
foreach my $jobset ($project->jobsets->all) {
|
|
|
|
my $keepnr = $jobset->keepnr;
|
|
|
|
|
2010-11-23 09:05:09 +00:00
|
|
|
# If the jobset has been disabled for more than one week, than
|
|
|
|
# don't keep its builds anymore.
|
|
|
|
if ($jobset->enabled == 0 && (time() - $jobset->lastcheckedtime > (7 * 24 * 3600))) {
|
|
|
|
print STDERR "*** skipping disabled jobset ", $project->name, ":", $jobset->name, "\n";
|
|
|
|
next;
|
|
|
|
}
|
2011-04-01 07:40:06 +00:00
|
|
|
|
2010-11-23 09:05:09 +00:00
|
|
|
if ($keepnr <= 0 ) {
|
|
|
|
print STDERR "*** jobset ", $project->name, ":", $jobset->name, " set to keep 0 builds\n";
|
|
|
|
next;
|
|
|
|
}
|
2011-04-01 07:40:06 +00:00
|
|
|
|
2010-11-19 11:01:31 +00:00
|
|
|
# Go over all jobs in this jobset.
|
|
|
|
foreach my $job ($jobset->jobs->all) {
|
|
|
|
print STDERR "*** looking for builds to keep in job ",
|
|
|
|
$project->name, ":", $job->jobset->name, ":", $job->name, "\n";
|
|
|
|
|
|
|
|
# Keep the N most recent successful builds for each job
|
|
|
|
# and platform.
|
|
|
|
# !!! Take time into account? E.g. don't delete builds
|
|
|
|
# that are younger than N days.
|
|
|
|
my @systems = $job->builds->search({ }, { select => ["system"], distinct => 1 })->all;
|
|
|
|
foreach my $system (@systems) {
|
|
|
|
my @recentBuilds = $job->builds->search(
|
|
|
|
{ finished => 1
|
|
|
|
, buildStatus => 0 # == success
|
|
|
|
, system => $system->system
|
|
|
|
},
|
|
|
|
{ join => 'resultInfo'
|
2011-04-01 07:40:06 +00:00
|
|
|
, order_by => 'me.id DESC'
|
2010-11-19 11:01:31 +00:00
|
|
|
, rows => $keepnr
|
|
|
|
});
|
|
|
|
keepBuild $_ foreach @recentBuilds;
|
2010-11-23 09:05:09 +00:00
|
|
|
}
|
|
|
|
}
|
2009-02-06 15:02:49 +00:00
|
|
|
}
|
|
|
|
|
2009-10-20 12:35:01 +00:00
|
|
|
# Go over all views in this project.
|
|
|
|
foreach my $view ($project->views->all) {
|
|
|
|
print STDERR "*** looking for builds to keep in view ", $project->name, ":", $view->name, "\n";
|
2009-02-06 15:02:49 +00:00
|
|
|
|
2009-10-20 12:35:01 +00:00
|
|
|
(my $primaryJob) = $view->viewjobs->search({isprimary => 1});
|
|
|
|
my $jobs = [$view->viewjobs->all];
|
2009-02-06 15:02:49 +00:00
|
|
|
|
2009-10-20 12:35:01 +00:00
|
|
|
# Keep all builds belonging to the most recent successful view result.
|
|
|
|
my $latest = getLatestSuccessfulViewResult($project, $primaryJob, $jobs);
|
2009-02-06 15:02:49 +00:00
|
|
|
if (defined $latest) {
|
2009-10-20 12:35:01 +00:00
|
|
|
print STDERR "keeping latest successful view result ", $latest->id, " (", $latest->get_column('releasename'), ")\n";
|
|
|
|
my $result = getViewResult($latest, $jobs);
|
|
|
|
keepBuild $_->{build} foreach @{$result->{jobs}};
|
2009-02-06 15:02:49 +00:00
|
|
|
}
|
2009-01-13 14:02:07 +00:00
|
|
|
}
|
2009-10-26 12:47:29 +00:00
|
|
|
|
|
|
|
# Keep every build in every release in this project.
|
|
|
|
print STDERR "*** keeping releases in project ", $project->name, "\n"
|
|
|
|
if scalar $project->releases > 0;
|
|
|
|
foreach my $release ($project->releases->all) {
|
|
|
|
print STDERR "keeping release ", $release->name, "\n";
|
|
|
|
keepBuild $_->build foreach $release->releasemembers;
|
|
|
|
}
|
2009-01-13 14:02:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-06 14:17:25 +00:00
|
|
|
# Keep all builds that have been marked as "keep".
|
2009-03-14 23:56:57 +00:00
|
|
|
print STDERR "*** looking for kept builds\n";
|
2009-02-06 14:17:25 +00:00
|
|
|
my @buildsToKeep = $db->resultset('Builds')->search({finished => 1, keep => 1}, {join => 'resultInfo'});
|
|
|
|
keepBuild $_ foreach @buildsToKeep;
|
|
|
|
|
|
|
|
|
2009-03-15 11:56:11 +00:00
|
|
|
# For scheduled builds, we register the derivation as a GC root.
|
2009-03-14 23:56:57 +00:00
|
|
|
print STDERR "*** looking for scheduled builds\n";
|
2009-01-13 14:02:07 +00:00
|
|
|
foreach my $build ($db->resultset('Builds')->search({finished => 0}, {join => 'schedulingInfo'})) {
|
|
|
|
if (isValidPath($build->drvpath)) {
|
2009-03-14 23:56:57 +00:00
|
|
|
print STDERR "keeping scheduled build ", $build->id, " (",
|
2009-02-06 21:01:20 +00:00
|
|
|
strftime("%Y-%m-%d %H:%M:%S", localtime($build->timestamp)), ")\n";
|
2009-01-13 14:02:07 +00:00
|
|
|
registerRoot $build->drvpath;
|
2009-03-15 11:56:11 +00:00
|
|
|
registerRoot $build->outpath if -e $build->outpath;
|
2009-01-13 14:02:07 +00:00
|
|
|
} else {
|
|
|
|
print STDERR "warning: derivation ", $build->drvpath, " has disappeared\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# Remove existing roots that are no longer wanted. !!! racy
|
2009-03-14 23:56:57 +00:00
|
|
|
print STDERR "*** removing unneeded GC roots\n";
|
2009-02-06 21:01:20 +00:00
|
|
|
|
|
|
|
my $gcRootsDir = getGCRootsDir;
|
|
|
|
|
2009-01-13 14:02:07 +00:00
|
|
|
opendir DIR, $gcRootsDir or die;
|
|
|
|
|
|
|
|
foreach my $link (readdir DIR) {
|
|
|
|
next if !-l "$gcRootsDir/$link";
|
|
|
|
my $path = readlink "$gcRootsDir/$link" or die;
|
|
|
|
if (!defined $roots{$path}) {
|
|
|
|
print STDERR "removing root $path\n";
|
|
|
|
unlink "$gcRootsDir/$link" or die "cannot remove $gcRootsDir/$link";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir DIR;
|