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(
|
2009-03-04 13:08:09 +00:00
|
|
|
getBuild getBuildStats getLatestBuilds getChannelData
|
2009-03-04 10:59:14 +00:00
|
|
|
error notFound
|
2009-04-02 16:15:57 +00:00
|
|
|
requireLogin requireProjectOwner requireAdmin requirePost
|
2009-03-04 10:59:14 +00:00
|
|
|
trim
|
2009-03-02 10:23:40 +00:00
|
|
|
$pathCompRE $relPathRE
|
|
|
|
);
|
2009-02-25 12:03:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
sub getBuild {
|
|
|
|
my ($c, $id) = @_;
|
|
|
|
my $build = $c->model('DB::Builds')->find($id);
|
|
|
|
return $build;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
$c->stash->{totalBuildTime} = $builds->search({},
|
|
|
|
{join => 'resultInfo', select => {sum => 'stoptime - starttime'}, as => ['sum']})
|
|
|
|
->first->get_column('sum') || 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# Return the latest build for each job.
|
|
|
|
sub getLatestBuilds {
|
2009-03-13 17:32:08 +00:00
|
|
|
my ($c, $jobs, $extraAttrs) = @_;
|
2009-03-04 10:59:14 +00:00
|
|
|
|
|
|
|
my @res = ();
|
|
|
|
|
2009-03-13 17:32:08 +00:00
|
|
|
# !!! this could be done more efficiently.
|
|
|
|
|
|
|
|
foreach my $job (ref $jobs eq "ARRAY" ? @{$jobs} : $jobs->all) {
|
|
|
|
foreach my $system ($job->builds->search({}, {select => ['system'], distinct => 1})) {
|
|
|
|
my ($build) = $job->builds->search(
|
|
|
|
{ finished => 1, system => $system->system, %$extraAttrs },
|
2009-03-23 13:52:24 +00:00
|
|
|
{ join => 'resultInfo', order_by => 'timestamp DESC', rows => 1
|
|
|
|
, '+select' => ["resultInfo.releasename", "resultInfo.buildstatus"]
|
|
|
|
, '+as' => ["releasename", "buildstatus"]
|
|
|
|
});
|
2009-03-13 17:32:08 +00:00
|
|
|
push @res, $build if defined $build;
|
|
|
|
}
|
2009-03-04 10:59:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return [@res];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-04 13:08:09 +00:00
|
|
|
sub getChannelData {
|
|
|
|
my ($c, $builds) = @_;
|
|
|
|
|
|
|
|
my @storePaths = ();
|
2009-03-04 16:36:23 +00:00
|
|
|
foreach my $build (@{$builds}) {
|
2009-03-04 13:08:09 +00:00
|
|
|
# !!! better do this in getLatestBuilds with a join.
|
|
|
|
next unless $build->buildproducts->find({type => "nix-build"});
|
|
|
|
next unless isValidPath($build->outpath);
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sub requireProjectOwner {
|
|
|
|
my ($c, $project) = @_;
|
|
|
|
|
|
|
|
requireLogin($c) if !$c->user_exists;
|
|
|
|
|
2009-03-04 10:59:14 +00:00
|
|
|
error($c, "Only the project owner or administrators can perform this operation.")
|
2009-03-02 16:03:41 +00:00
|
|
|
unless $c->check_user_roles('admin') || $c->user->username eq $project->owner->username;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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.")
|
|
|
|
unless $c->check_user_roles('admin');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-\+\._]*)";
|
|
|
|
Readonly::Scalar our $relPathRE => "(?:$pathCompRE(?:\/$pathCompRE)*)";
|
|
|
|
|
|
|
|
|
2009-02-25 12:03:13 +00:00
|
|
|
1;
|