From 9de336de7c45d056a9dc0669347f8ce04b32749f Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 26 Feb 2016 17:27:30 +0100 Subject: [PATCH] Proxy local binary caches via hydra-server --- src/lib/Hydra/Controller/Root.pm | 94 +++++++++++++++++++++++--------- 1 file changed, 68 insertions(+), 26 deletions(-) diff --git a/src/lib/Hydra/Controller/Root.pm b/src/lib/Hydra/Controller/Root.pm index 48319479..8827d4f0 100644 --- a/src/lib/Hydra/Controller/Root.pm +++ b/src/lib/Hydra/Controller/Root.pm @@ -257,48 +257,90 @@ sub serialize : ActionClass('Serialize') { } sub nar :Local :Args(1) { my ($self, $c, $path) = @_; - $path = ($ENV{NIX_STORE_DIR} || "/nix/store")."/$path"; + die if $path =~ /\//; - gone($c, "Path " . $path . " is no longer available.") unless isValidPath($path); + my $storeMode = $c->config->{store_mode} // "direct"; - $c->stash->{current_view} = 'NixNAR'; - $c->stash->{storePath} = $path; + if ($storeMode eq "s3-binary-cache") { + notFound($c, "There is no binary cache here."); + } + + elsif ($storeMode eq "local-binary-cache") { + my $dir = $c->config->{binary_cache_dir}; + $c->serve_static_file($dir . "/nar/" . $path); + } + + else { + $path = $Nix::Config::storeDir . "/$path"; + + gone($c, "Path " . $path . " is no longer available.") unless isValidPath($path); + + $c->stash->{current_view} = 'NixNAR'; + $c->stash->{storePath} = $path; + } } sub nix_cache_info :Path('nix-cache-info') :Args(0) { my ($self, $c) = @_; - $c->response->content_type('text/plain'); - $c->stash->{plain}->{data} = - #"StoreDir: $Nix::Config::storeDir\n" . # FIXME - "StoreDir: " . ($ENV{NIX_STORE_DIR} || "/nix/store") . "\n" . - "WantMassQuery: 0\n" . - # Give Hydra binary caches a very low priority (lower than the - # static binary cache http://nixos.org/binary-cache). - "Priority: 100\n"; - setCacheHeaders($c, 24 * 60 * 60); - $c->forward('Hydra::View::Plain'); + + my $storeMode = $c->config->{store_mode} // "direct"; + + if ($storeMode eq "s3-binary-cache") { + notFound($c, "There is no binary cache here."); + } + + elsif ($storeMode eq "local-binary-cache") { + my $dir = $c->config->{binary_cache_dir}; + $c->serve_static_file($dir . "/nix-cache-info"); + } + + else { + $c->response->content_type('text/plain'); + $c->stash->{plain}->{data} = + "StoreDir: $Nix::Config::storeDir\n" . + "WantMassQuery: 0\n" . + # Give Hydra binary caches a very low priority (lower than the + # static binary cache http://nixos.org/binary-cache). + "Priority: 100\n"; + setCacheHeaders($c, 24 * 60 * 60); + $c->forward('Hydra::View::Plain'); + } } sub narinfo :LocalRegex('^([a-z0-9]+).narinfo$') :Args(0) { my ($self, $c) = @_; - my $hash = $c->req->captures->[0]; - die if length($hash) != 32; - my $path = queryPathFromHashPart($hash); + my $storeMode = $c->config->{store_mode} // "direct"; - if (!$path) { - $c->response->status(404); - $c->response->content_type('text/plain'); - $c->stash->{plain}->{data} = "does not exist\n"; - $c->forward('Hydra::View::Plain'); - setCacheHeaders($c, 60 * 60); - return; + if ($storeMode eq "s3-binary-cache") { + notFound($c, "There is no binary cache here."); } - $c->stash->{storePath} = $path; - $c->forward('Hydra::View::NARInfo'); + elsif ($storeMode eq "local-binary-cache") { + my $dir = $c->config->{binary_cache_dir}; + $c->serve_static_file($dir . "/" . $c->req->captures->[0] . ".narinfo"); + } + + else { + my $hash = $c->req->captures->[0]; + + die if length($hash) != 32; + my $path = queryPathFromHashPart($hash); + + if (!$path) { + $c->response->status(404); + $c->response->content_type('text/plain'); + $c->stash->{plain}->{data} = "does not exist\n"; + $c->forward('Hydra::View::Plain'); + setCacheHeaders($c, 60 * 60); + return; + } + + $c->stash->{storePath} = $path; + $c->forward('Hydra::View::NARInfo'); + } }