2009-02-25 12:03:13 +00:00
|
|
|
package Hydra::Helper::CatalystUtils;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use Exporter;
|
2009-03-02 10:23:40 +00:00
|
|
|
use Readonly;
|
2009-03-04 13:08:09 +00:00
|
|
|
use Hydra::Helper::Nix;
|
2009-02-25 12:03:13 +00:00
|
|
|
|
|
|
|
our @ISA = qw(Exporter);
|
2009-03-02 10:23:40 +00:00
|
|
|
our @EXPORT = qw(
|
2010-02-22 13:21:34 +00:00
|
|
|
getBuild getPreviousBuild getPreviousSuccessfulBuild getBuildStats joinWithResultInfo getChannelData
|
2009-03-04 10:59:14 +00:00
|
|
|
error notFound
|
2010-06-04 14:43:28 +00:00
|
|
|
requireLogin requireProjectOwner requireAdmin requirePost isAdmin isProjectOwner
|
2009-03-04 10:59:14 +00:00
|
|
|
trim
|
2010-03-07 11:24:06 +00:00
|
|
|
$pathCompRE $relPathRE $relNameRE $jobNameRE $systemRE
|
2009-03-02 10:23:40 +00:00
|
|
|
);
|
2009-02-25 12:03:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
sub getBuild {
|
|
|
|
my ($c, $id) = @_;
|
|
|
|
my $build = $c->model('DB::Builds')->find($id);
|
|
|
|
return $build;
|
|
|
|
}
|
|
|
|
|
2010-02-22 13:21:34 +00:00
|
|
|
sub getPreviousBuild {
|
|
|
|
my ($c, $build) = @_;
|
|
|
|
(my $prevBuild) = $c->model('DB::Builds')->search(
|
|
|
|
{ finished => 1
|
|
|
|
, system => $build->system
|
|
|
|
, project => $build->project->name
|
|
|
|
, jobset => $build->jobset->name
|
|
|
|
, job => $build->job->name
|
|
|
|
, 'me.id' => { '<' => $build->id }
|
|
|
|
}, {rows => 1, order_by => "id DESC"});
|
|
|
|
|
|
|
|
return $prevBuild;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub getPreviousSuccessfulBuild {
|
|
|
|
my ($c, $build) = @_;
|
|
|
|
(my $prevBuild) = joinWithResultInfo($c, $c->model('DB::Builds'))->search(
|
|
|
|
{ finished => 1
|
|
|
|
, system => $build->system
|
|
|
|
, project => $build->project->name
|
|
|
|
, jobset => $build->jobset->name
|
|
|
|
, job => $build->job->name
|
|
|
|
, buildstatus => 0
|
|
|
|
, 'me.id' => { '<' => $build->id }
|
|
|
|
}, {rows => 1, order_by => "id DESC"});
|
|
|
|
|
|
|
|
return $prevBuild;
|
|
|
|
}
|
2009-02-25 12:03:13 +00:00
|
|
|
|
2009-03-04 10:59:14 +00:00
|
|
|
sub getBuildStats {
|
|
|
|
my ($c, $builds) = @_;
|
|
|
|
|
|
|
|
$c->stash->{finishedBuilds} = $builds->search({finished => 1}) || 0;
|
|
|
|
|
|
|
|
$c->stash->{succeededBuilds} = $builds->search(
|
|
|
|
{finished => 1, buildStatus => 0},
|
|
|
|
{join => 'resultInfo'}) || 0;
|
|
|
|
|
|
|
|
$c->stash->{scheduledBuilds} = $builds->search({finished => 0}) || 0;
|
|
|
|
|
|
|
|
$c->stash->{busyBuilds} = $builds->search(
|
|
|
|
{finished => 0, busy => 1},
|
|
|
|
{join => 'schedulingInfo'}) || 0;
|
2009-11-18 12:59:58 +00:00
|
|
|
|
|
|
|
my $res;
|
|
|
|
$res = $builds->search({},
|
2009-03-04 10:59:14 +00:00
|
|
|
{join => 'resultInfo', select => {sum => 'stoptime - starttime'}, as => ['sum']})
|
2009-11-18 12:59:58 +00:00
|
|
|
->first ;
|
|
|
|
|
|
|
|
$c->stash->{totalBuildTime} = defined ($res) ? $res->get_column('sum') : 0 ;
|
|
|
|
|
2009-03-04 10:59:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-03 15:37:21 +00:00
|
|
|
# Add the releaseName and buildStatus attributes from the
|
|
|
|
# BuildResultInfo table for each build.
|
|
|
|
sub joinWithResultInfo {
|
|
|
|
my ($c, $source) = @_;
|
2009-03-04 10:59:14 +00:00
|
|
|
|
2009-04-03 15:37:21 +00:00
|
|
|
return $source->search(
|
|
|
|
{ },
|
|
|
|
{ join => 'resultInfo'
|
|
|
|
, '+select' => ["resultInfo.releasename", "resultInfo.buildstatus"]
|
|
|
|
, '+as' => ["releasename", "buildstatus"]
|
|
|
|
});
|
2009-03-04 10:59:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-04 13:08:09 +00:00
|
|
|
sub getChannelData {
|
|
|
|
my ($c, $builds) = @_;
|
2009-04-03 15:37:21 +00:00
|
|
|
|
|
|
|
my @builds2 = joinWithResultInfo($c, $builds)
|
2010-02-12 14:49:32 +00:00
|
|
|
->search_literal("exists (select 1 from buildproducts where build = resultInfo.id and type = 'nix-build')");
|
2009-03-04 13:08:09 +00:00
|
|
|
|
|
|
|
my @storePaths = ();
|
2009-04-03 15:37:21 +00:00
|
|
|
foreach my $build (@builds2) {
|
2009-03-04 13:08:09 +00:00
|
|
|
next unless isValidPath($build->outpath);
|
2010-01-19 16:47:32 +00:00
|
|
|
if (isValidPath($build->drvpath)) {
|
|
|
|
# Adding `drvpath' implies adding `outpath' because of the
|
|
|
|
# `--include-outputs' flag passed to `nix-store'.
|
|
|
|
push @storePaths, $build->drvpath;
|
|
|
|
} else {
|
|
|
|
push @storePaths, $build->outpath;
|
|
|
|
}
|
2009-03-04 14:49:21 +00:00
|
|
|
my $pkgName = $build->nixname . "-" . $build->system . "-" . $build->id;
|
|
|
|
$c->stash->{nixPkgs}->{"${pkgName}.nixpkg"} = {build => $build, name => $pkgName};
|
2009-03-04 13:08:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
$c->stash->{storePaths} = [@storePaths];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-25 12:03:13 +00:00
|
|
|
sub error {
|
|
|
|
my ($c, $msg) = @_;
|
|
|
|
$c->error($msg);
|
2009-03-02 16:03:41 +00:00
|
|
|
$c->detach; # doesn't return
|
2009-02-25 12:03:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-25 14:34:29 +00:00
|
|
|
sub notFound {
|
|
|
|
my ($c, $msg) = @_;
|
|
|
|
$c->response->status(404);
|
|
|
|
error($c, $msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-02 16:03:41 +00:00
|
|
|
sub requireLogin {
|
|
|
|
my ($c) = @_;
|
|
|
|
$c->flash->{afterLogin} = $c->request->uri;
|
|
|
|
$c->response->redirect($c->uri_for('/login'));
|
|
|
|
$c->detach; # doesn't return
|
|
|
|
}
|
|
|
|
|
2010-06-04 14:43:28 +00:00
|
|
|
sub isProjectOwner {
|
|
|
|
my ($c, $project) = @_;
|
|
|
|
|
|
|
|
return $c->user_exists && ($c->check_user_roles('admin') || $c->user->username eq $project->owner->username || defined $c->model('DB::ProjectMembers')->find({ project => $project, userName => $c->user->username }));
|
|
|
|
}
|
2009-03-02 16:03:41 +00:00
|
|
|
|
|
|
|
sub requireProjectOwner {
|
|
|
|
my ($c, $project) = @_;
|
|
|
|
|
|
|
|
requireLogin($c) if !$c->user_exists;
|
2010-03-10 10:02:04 +00:00
|
|
|
|
|
|
|
error($c, "Only the project members or administrators can perform this operation.")
|
2010-06-04 14:43:28 +00:00
|
|
|
unless isProjectOwner($c, $project);
|
2009-03-02 16:03:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-04 14:43:28 +00:00
|
|
|
sub isAdmin {
|
|
|
|
my ($c) = @_;
|
|
|
|
|
|
|
|
return $c->user_exists && $c->check_user_roles('admin');
|
|
|
|
}
|
|
|
|
|
2009-03-04 10:59:14 +00:00
|
|
|
sub requireAdmin {
|
|
|
|
my ($c) = @_;
|
|
|
|
|
|
|
|
requireLogin($c) if !$c->user_exists;
|
|
|
|
|
|
|
|
error($c, "Only administrators can perform this operation.")
|
2010-06-04 14:43:28 +00:00
|
|
|
unless isAdmin($c);
|
2009-03-04 10:59:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-02 16:15:57 +00:00
|
|
|
sub requirePost {
|
|
|
|
my ($c) = @_;
|
|
|
|
error($c, "Request must be POSTed.") if $c->request->method ne "POST";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-04 10:59:14 +00:00
|
|
|
sub trim {
|
|
|
|
my $s = shift;
|
|
|
|
$s =~ s/^\s+|\s+$//g;
|
|
|
|
return $s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-02 10:23:40 +00:00
|
|
|
# Security checking of filenames.
|
|
|
|
Readonly::Scalar our $pathCompRE => "(?:[A-Za-z0-9-\+][A-Za-z0-9-\+\._]*)";
|
2009-10-26 15:39:14 +00:00
|
|
|
Readonly::Scalar our $relPathRE => "(?:$pathCompRE(?:/$pathCompRE)*)";
|
|
|
|
Readonly::Scalar our $relNameRE => "(?:[A-Za-z0-9-][A-Za-z0-9-\.]*)";
|
|
|
|
Readonly::Scalar our $attrNameRE => "(?:[A-Za-z_][A-Za-z0-9_]*)";
|
|
|
|
Readonly::Scalar our $jobNameRE => "(?:$attrNameRE(?:\\.$attrNameRE)*)";
|
2010-03-07 11:24:06 +00:00
|
|
|
Readonly::Scalar our $systemRE => "(?:[a-z0-9_]+-[a-z0-9_]+)";
|
2009-03-02 10:23:40 +00:00
|
|
|
|
|
|
|
|
2009-02-25 12:03:13 +00:00
|
|
|
1;
|