294 lines
9.4 KiB
Perl
Raw Normal View History

2008-11-25 11:01:42 +00:00
package Hydra::Controller::Root;
use strict;
use warnings;
use base 'Hydra::Base::Controller::ListBuilds';
2008-11-25 11:01:42 +00:00
use Hydra::Helper::Nix;
use Hydra::Helper::CatalystUtils;
2010-12-03 09:40:25 +00:00
use Digest::SHA1 qw(sha1_hex);
use Nix::Store;
use Nix::Config;
# Put this controller at top-level.
__PACKAGE__->config->{namespace} = '';
2008-11-13 09:25:38 +00:00
sub begin :Private {
my ($self, $c, @args) = @_;
2008-11-13 09:48:10 +00:00
$c->stash->{curUri} = $c->request->uri;
$c->stash->{version} = $ENV{"HYDRA_RELEASE"} || "<devel>";
$c->stash->{nixVersion} = $ENV{"NIX_RELEASE"} || "<devel>";
$c->stash->{curTime} = time;
$c->stash->{logo} = $ENV{"HYDRA_LOGO"} ? "/logo" : "";
2013-02-27 18:33:47 +01:00
$c->stash->{tracker} = $ENV{"HYDRA_TRACKER"};
$c->stash->{flashMsg} = $c->flash->{flashMsg};
$c->stash->{successMsg} = $c->flash->{successMsg};
2010-08-31 15:37:50 +00:00
if (scalar(@args) == 0 || $args[0] ne "static") {
2013-01-22 14:09:37 +01:00
$c->stash->{nrRunningBuilds} = $c->model('DB::Builds')->search({ finished => 0, busy => 1 }, {})->count();
$c->stash->{nrQueuedBuilds} = $c->model('DB::Builds')->search({ finished => 0 })->count();
}
2008-11-13 09:25:38 +00:00
}
sub index :Path :Args(0) {
my ($self, $c) = @_;
$c->stash->{template} = 'overview.tt';
2010-06-04 14:43:28 +00:00
$c->stash->{projects} = [$c->model('DB::Projects')->search(isAdmin($c) ? {} : {hidden => 0}, {order_by => 'name'})];
$c->stash->{newsItems} = [$c->model('DB::NewsItems')->search({}, { order_by => ['createtime DESC'], rows => 5 })];
}
sub queue :Local {
2008-11-26 19:48:04 +00:00
my ($self, $c) = @_;
$c->stash->{template} = 'queue.tt';
$c->stash->{queue} = [$c->model('DB::Builds')->search(
{finished => 0}, { join => ['project'], order_by => ["priority DESC", "timestamp"], columns => [@buildListColumns], '+select' => ['project.enabled'], '+as' => ['enabled'] })];
2013-02-27 18:33:47 +01:00
$c->stash->{flashMsg} //= $c->flash->{buildMsg};
}
sub timeline :Local {
my ($self, $c) = @_;
my $pit = time();
$c->stash->{pit} = $pit;
$pit = $pit-(24*60*60)-1;
$c->stash->{template} = 'timeline.tt';
$c->stash->{builds} = [ $c->model('DB::Builds')->search
( { finished => 1, stoptime => { '>' => $pit } }
2013-01-22 14:41:02 +01:00
, { order_by => ["starttime"] }
) ];
}
sub status :Local {
my ($self, $c) = @_;
$c->stash->{steps} = [ $c->model('DB::BuildSteps')->search(
{ 'me.busy' => 1, 'build.finished' => 0, 'build.busy' => 1 },
{ join => [ 'build' ]
, order_by => [ 'machine' ]
} ) ];
}
sub machines :Local Args(0) {
my ($self, $c) = @_;
$c->stash->{machines} = [$c->model('DB::BuildMachines')->search(
{},
{ order_by => ["enabled DESC", "hostname"]
, '+select' => ["(select bs.stoptime from buildsteps as bs where bs.machine = (me.username || '\@' || me.hostname) and not bs.stoptime is null order by bs.stoptime desc limit 1)"]
, '+as' => ['idle']
})];
$c->stash->{steps} = [ $c->model('DB::BuildSteps')->search(
{ finished => 0, 'me.busy' => 1, 'build.busy' => 1, },
{ join => [ 'build' ]
, order_by => [ 'machine', 'stepnr' ]
} ) ];
$c->stash->{template} = 'machine-status.tt';
}
# Hydra::Base::Controller::ListBuilds needs this.
sub get_builds : Chained('/') PathPart('') CaptureArgs(0) {
my ($self, $c) = @_;
$c->stash->{allBuilds} = $c->model('DB::Builds');
$c->stash->{jobStatus} = $c->model('DB')->resultset('JobStatus');
$c->stash->{allJobsets} = $c->model('DB::Jobsets');
$c->stash->{allJobs} = $c->model('DB::Jobs');
$c->stash->{latestSucceeded} = $c->model('DB')->resultset('LatestSucceeded');
$c->stash->{channelBaseName} = "everything";
}
2009-03-31 13:48:03 +00:00
sub robots_txt : Path('robots.txt') {
my ($self, $c) = @_;
sub uri_for {
2013-01-23 12:41:57 +00:00
my ($c, $controller, $action, @args) = @_;
return $c->uri_for($c->controller($controller)->action_for($action), @args)->path;
}
sub channelUris {
2013-01-23 12:41:57 +00:00
my ($c, $controller, $bindings) = @_;
return
2013-01-23 12:41:57 +00:00
( uri_for($c, $controller, 'closure', $bindings, "*")
, uri_for($c, $controller, 'manifest', $bindings)
, uri_for($c, $controller, 'pkg', $bindings, "*")
, uri_for($c, $controller, 'nixexprs', $bindings)
, uri_for($c, $controller, 'channel_contents', $bindings)
);
}
2009-03-31 13:48:03 +00:00
# Put actions that are expensive or not useful for indexing in
# robots.txt. Note: wildcards are not universally supported in
# robots.txt, but apparently Google supports them.
my @rules =
2013-01-23 12:41:57 +00:00
( uri_for($c, 'Build', 'deps', ["*"])
, uri_for($c, 'Build', 'view_nixlog', ["*"], "*")
, uri_for($c, 'Build', 'view_log', ["*"], "*")
, uri_for($c, 'Build', 'view_log', ["*"])
, uri_for($c, 'Build', 'download', ["*"], "*")
, uri_for($c, 'Root', 'nar', [], "*")
, uri_for($c, 'Root', 'status', [])
, uri_for($c, 'Root', 'all', [])
, uri_for($c, 'API', 'scmdiff', [])
, uri_for($c, 'API', 'logdiff', [],"*", "*")
, uri_for($c, 'Project', 'all', ["*"])
, channelUris($c, 'Root', ["*"])
, channelUris($c, 'Project', ["*", "*"])
, channelUris($c, 'Jobset', ["*", "*", "*"])
, channelUris($c, 'Job', ["*", "*", "*", "*"])
, channelUris($c, 'Build', ["*"])
2009-03-31 13:48:03 +00:00
);
2009-03-31 14:55:47 +00:00
$c->stash->{'plain'} = { data => "User-agent: *\n" . join('', map { "Disallow: $_\n" } @rules) };
2009-03-31 13:48:03 +00:00
$c->forward('Hydra::View::Plain');
}
sub default :Path {
my ($self, $c) = @_;
2009-02-25 14:34:29 +00:00
notFound($c, "Page not found.");
}
sub end : ActionClass('RenderView') {
my ($self, $c) = @_;
if (defined $c->stash->{json}) {
if (scalar @{$c->error}) {
$c->stash->{json}->{error} = join "\n", @{$c->error};
$c->clear_errors;
}
$c->forward('View::JSON');
}
elsif (scalar @{$c->error}) {
$c->stash->{template} = 'error.tt';
$c->stash->{errors} = $c->error;
2013-02-22 14:27:38 +01:00
$c->response->status(500) if $c->response->status == 200;
2009-02-25 16:29:54 +00:00
if ($c->response->status >= 300) {
$c->stash->{httpStatus} =
$c->response->status . " " . HTTP::Status::status_message($c->response->status);
}
$c->clear_errors;
}
}
sub nar :Local :Args(1) {
my ($self, $c, $path) = @_;
2011-03-23 13:03:40 +00:00
$path = ($ENV{NIX_STORE_DIR} || "/nix/store")."/$path";
if (!isValidPath($path)) {
$c->response->status(410); # "Gone"
error($c, "Path " . $path . " is no longer available.");
}
$c->stash->{current_view} = 'NixNAR';
$c->stash->{storePath} = $path;
}
sub nix_cache_info :Path('nix-cache-info') :Args(0) {
my ($self, $c) = @_;
2012-08-01 18:00:55 +00:00
$c->response->content_type('text/plain');
2013-01-22 14:41:02 +01:00
$c->stash->{'plain'} = { data =>
#"StoreDir: $Nix::Config::storeDir\n" . # FIXME
"StoreDir: /nix/store\n" .
2013-01-22 14:09:37 +01:00
"WantMassQuery: 0\n" .
# Give Hydra binary caches a very low priority (lower than the
# static binary cache http://nixos.org/binary-cache).
2013-01-22 14:09:37 +01:00
"Priority: 100\n"
};
$c->forward('Hydra::View::Plain');
}
sub hashToPath {
my ($c, $hash) = @_;
die if length($hash) != 32;
my $path = queryPathFromHashPart($hash);
notFound($c, "Store path with hash $hash does not exist.") unless $path;
return $path;
}
sub narinfo :LocalRegex('^([a-z0-9]+).narinfo$') :Args(0) {
my ($self, $c) = @_;
my $hash = $c->req->captures->[0];
$c->stash->{storePath} = hashToPath($c, $hash);
$c->stash->{current_view} = 'NARInfo';
}
sub logo :Local {
my ($self, $c) = @_;
my $path = $ENV{"HYDRA_LOGO"} or die("Logo not set!");
$c->serve_static_file($path);
}
sub evals :Local Args(0) {
my ($self, $c) = @_;
$c->stash->{template} = 'evals.tt';
my $page = int($c->req->param('page') || "1") || 1;
my $resultsPerPage = 20;
my $evals = $c->model('DB::JobsetEvals');
$c->stash->{page} = $page;
$c->stash->{resultsPerPage} = $resultsPerPage;
$c->stash->{total} = $evals->search({hasnewbuilds => 1})->count;
$c->stash->{evals} = getEvals($self, $c, $evals, ($page - 1) * $resultsPerPage, $resultsPerPage)
}
sub search :Local Args(0) {
my ($self, $c) = @_;
$c->stash->{template} = 'search.tt';
my $query = trim $c->request->params->{"query"};
error($c, "Query is empty.") if $query eq "";
error($c, "Invalid character in query.")
unless $query =~ /^[a-zA-Z0-9_\-]+$/;
2013-02-22 16:41:42 +01:00
$c->stash->{limit} = 500;
$c->stash->{projects} = [ $c->model('DB::Projects')->search(
{ -and =>
[ { -or => [ name => { ilike => "%$query%" }, displayName => { ilike => "%$query%" }, description => { ilike => "%$query%" } ] }
, { hidden => 0 }
]
},
{ order_by => ["name"] } ) ];
$c->stash->{jobsets} = [ $c->model('DB::Jobsets')->search(
{ -and =>
[ { -or => [ "me.name" => { ilike => "%$query%" }, "me.description" => { ilike => "%$query%" } ] }
, { "project.hidden" => 0, "me.hidden" => 0 }
]
},
{ order_by => ["project", "name"], join => ["project"] } ) ];
$c->stash->{jobs} = [ $c->model('DB::Jobs')->search(
{ "me.name" => { ilike => "%$query%" }
, "project.hidden" => 0
, "jobset.hidden" => 0
},
{ order_by => ["enabled_ desc", "project", "jobset", "name"], join => ["project", "jobset"]
, "+select" => [\ "(project.enabled = 1 and jobset.enabled = 1 and exists (select 1 from Builds where project = project.name and jobset = jobset.name and job = me.name and iscurrent = 1)) enabled_"]
, "+as" => ["enabled"]
2013-02-22 16:41:42 +01:00
, rows => $c->stash->{limit} + 1
} ) ];
}
1;