| 1 | # | ||
| 2 | # (c) Jan Gehring <jan.gehring@gmail.com> | ||
| 3 | # | ||
| 4 | # vim: set ts=2 sw=2 tw=0: | ||
| 5 | # vim: set expandtab: | ||
| 6 | |||
| 7 | package Rex::Repositorio::Server::Docker::Repository; | ||
| 8 | |||
| 9 | use Mojo::Base 'Mojolicious::Controller'; | ||
| 10 | use Data::Dumper; | ||
| 11 | use File::Spec; | ||
| 12 | use File::Path 'make_path', 'remove_tree'; | ||
| 13 | use File::Basename qw'dirname'; | ||
| 14 | use JSON::XS; | ||
| 15 | use MIME::Base64; | ||
| 16 | require IO::All; | ||
| 17 | |||
| 18 | our $VERSION = '1.2.1'; # VERSION | ||
| 19 | |||
| 20 | sub get_repo_images { | ||
| 21 | my ($self) = @_; | ||
| 22 | |||
| 23 | my $requested_file = $self->req->url; | ||
| 24 | $self->app->log->debug("Requested-File: $requested_file"); | ||
| 25 | my $orig_url = $self->repo->{url}; | ||
| 26 | $orig_url =~ s/\/$//; | ||
| 27 | $self->app->log->debug("Upstream-URL: $orig_url"); | ||
| 28 | |||
| 29 | $requested_file =~ s/^\///; | ||
| 30 | my $orig_file_url = $orig_url . "/" . $requested_file; | ||
| 31 | $self->app->log->debug("Orig-File-URL: $orig_file_url"); | ||
| 32 | |||
| 33 | my $repo_name = $self->param("repo_namespace") . "/" . $self->param("name"); | ||
| 34 | |||
| 35 | my $repo_dir = | ||
| 36 | File::Spec->catdir( $self->app->get_repo_dir( repo => $self->repo->{name} ), | ||
| 37 | "repository", $repo_name ); | ||
| 38 | |||
| 39 | my $repo_file = File::Spec->catfile( $repo_dir, "repo.json" ); | ||
| 40 | |||
| 41 | if ( -f $repo_file ) { | ||
| 42 | my $content = IO::All->new($repo_file)->slurp; | ||
| 43 | $self->res->headers->add( 'Content-Type', 'application/json' ); | ||
| 44 | $self->render( text => $content ); | ||
| 45 | } | ||
| 46 | else { | ||
| 47 | my $base64_auth_string = MIME::Base64::encode_base64( | ||
| 48 | $self->repo->{upstream_user} . ":" . $self->repo->{upstream_password} ); | ||
| 49 | $self->proxy_to( | ||
| 50 | $orig_file_url, | ||
| 51 | sub { | ||
| 52 | my ( $c, $tx ) = @_; | ||
| 53 | $c->app->log->debug("Got data from upstream..."); | ||
| 54 | |||
| 55 | make_path( dirname($repo_file) ); | ||
| 56 | open my $fh, '>', $repo_file or die($!); | ||
| 57 | binmode $fh; | ||
| 58 | print $fh $tx->res->body; | ||
| 59 | close $fh; | ||
| 60 | |||
| 61 | my $ref = $tx->res->json; | ||
| 62 | my $repo_dir = $self->app->get_repo_dir( repo => $self->repo->{name} ); | ||
| 63 | for my $image_data ( @{$ref} ) { | ||
| 64 | my $image_dir = | ||
| 65 | File::Spec->catdir( $repo_dir, "images", $image_data->{id} ); | ||
| 66 | make_path($image_dir); | ||
| 67 | open my $image_ep, '>', | ||
| 68 | File::Spec->catfile( $image_dir, 'endpoint.data' ) | ||
| 69 | or die($!); | ||
| 70 | print $image_ep $tx->res->headers->header('R-Docker-Endpoints'); | ||
| 71 | close $image_ep; | ||
| 72 | |||
| 73 | open my $image_lib, '>', | ||
| 74 | File::Spec->catfile( $image_dir, 'library.data' ) | ||
| 75 | or die($!); | ||
| 76 | print $image_lib $orig_file_url; | ||
| 77 | close $image_lib; | ||
| 78 | } | ||
| 79 | |||
| 80 | open my $ep, '>', "$repo_file.endpoint" or die($!); | ||
| 81 | print $ep $tx->res->headers->header('R-Docker-Endpoints'); | ||
| 82 | close $ep; | ||
| 83 | }, | ||
| 84 | sub { | ||
| 85 | my ( $c, $tx ) = @_; | ||
| 86 | my $docker_token = $tx->res->headers->header('X-Docker-Token') || ""; | ||
| 87 | $c->app->log->debug("Got my docker token: $docker_token"); | ||
| 88 | $self->stash( 'upstream_docker_token', $docker_token ); | ||
| 89 | |||
| 90 | $self->_fix_docker_headers($tx); | ||
| 91 | }, | ||
| 92 | { # some custom headers to get the docker token, so that we can | ||
| 93 | # authenticate against the image servers. | ||
| 94 | 'X-Docker-Token' => 'true', | ||
| 95 | 'Authorization' => "Basic $base64_auth_string", | ||
| 96 | }, | ||
| 97 | ); | ||
| 98 | } | ||
| 99 | |||
| 100 | } | ||
| 101 | |||
| 102 | sub put_repo { | ||
| 103 | my ($self) = @_; | ||
| 104 | |||
| 105 | my $repo_name = $self->param("repo_namespace") . "/" . $self->param("name"); | ||
| 106 | |||
| 107 | my $repo_dir = | ||
| 108 | File::Spec->catdir( $self->app->get_repo_dir( repo => $self->repo->{name} ), | ||
| 109 | "repository", $repo_name ); | ||
| 110 | |||
| 111 | my $ref = decode_json( $self->req->body ); | ||
| 112 | my $store = [ map { { id => $_->{id} } } @{$ref} ]; | ||
| 113 | |||
| 114 | make_path($repo_dir); | ||
| 115 | open( my $fh, ">", File::Spec->catfile( $repo_dir, "repo.json" ) ) or die($!); | ||
| 116 | print $fh encode_json($store); | ||
| 117 | close($fh); | ||
| 118 | |||
| 119 | my $image_dir = | ||
| 120 | File::Spec->catdir( $self->app->get_repo_dir( repo => $self->repo->{name} ), | ||
| 121 | "images", $store->[-1]->{id} ); | ||
| 122 | |||
| 123 | # make_path $image_dir; | ||
| 124 | # my $ancestor_file = File::Spec->catfile( $image_dir, "ancestors" ); | ||
| 125 | # | ||
| 126 | # open( my $afh, ">", $ancestor_file ) or die($!); | ||
| 127 | # print $afh encode_json( [ map { $_ = $_->{id} } reverse @{$store} ] ); | ||
| 128 | # close($afh); | ||
| 129 | |||
| 130 | #$self->render( json => {}, status => 500 ); | ||
| 131 | #$self->render( json => {} ); | ||
| 132 | $self->render( text => '""' ); | ||
| 133 | } | ||
| 134 | |||
| 135 | sub get_repo_tag { | ||
| 136 | my ($self) = @_; | ||
| 137 | |||
| 138 | my $requested_file = $self->req->url; | ||
| 139 | $self->app->log->debug("Requested-File: $requested_file"); | ||
| 140 | my $orig_url = $self->repo->{url}; | ||
| 141 | $orig_url =~ s/\/$//; | ||
| 142 | $self->app->log->debug("Upstream-URL: $orig_url"); | ||
| 143 | |||
| 144 | $requested_file =~ s/^\///; | ||
| 145 | my $orig_file_url = $orig_url . "/" . $requested_file; | ||
| 146 | $self->app->log->debug("Orig-File-URL: $orig_file_url"); | ||
| 147 | |||
| 148 | my $repo_name = $self->param("repo_namespace") . "/" . $self->param("name"); | ||
| 149 | |||
| 150 | my $tag_dir = | ||
| 151 | File::Spec->catdir( $self->app->get_repo_dir( repo => $self->repo->{name} ), | ||
| 152 | "repository", $repo_name, "tags" ); | ||
| 153 | my $ret = {}; | ||
| 154 | |||
| 155 | if ( -d $tag_dir ) { | ||
| 156 | opendir( my $dh, $tag_dir ); | ||
| 157 | while ( my $entry = readdir($dh) ) { | ||
| 158 | next if ( $entry =~ m/^\./ ); | ||
| 159 | next if ( !-f File::Spec->catfile( $tag_dir, $entry ) ); | ||
| 160 | $ret->{$entry} = | ||
| 161 | IO::All->new( File::Spec->catfile( $tag_dir, $entry ) )->slurp; | ||
| 162 | } | ||
| 163 | closedir($dh); | ||
| 164 | |||
| 165 | $self->render( json => $ret ); | ||
| 166 | } | ||
| 167 | else { | ||
| 168 | $self->proxy_to( | ||
| 169 | $orig_file_url, | ||
| 170 | sub { | ||
| 171 | my ( $c, $tx ) = @_; | ||
| 172 | $c->app->log->debug("Got data from upstream..."); | ||
| 173 | make_path($tag_dir); | ||
| 174 | my $ref = $tx->res->json; | ||
| 175 | for my $e ( @{$ref} ) { | ||
| 176 | open my $fh, ">", File::Spec->catfile( $tag_dir, $e->{name} ) | ||
| 177 | or die($!); | ||
| 178 | print $fh $self->_get_image_directory( $e->{layer} ); | ||
| 179 | close $fh; | ||
| 180 | } | ||
| 181 | }, | ||
| 182 | sub { | ||
| 183 | # TODO: check why we need to rewrite content | ||
| 184 | # must rewrite content... don't know why... | ||
| 185 | my ( $c, $tx ) = @_; | ||
| 186 | my $content = Mojo::Content::Single->new; | ||
| 187 | $content->headers( $tx->res->headers() ); | ||
| 188 | my $ref = $tx->res->json; | ||
| 189 | my $new_ref = {}; | ||
| 190 | for my $e ( @{$ref} ) { | ||
| 191 | $new_ref->{ $e->{name} } = $self->_get_image_directory( $e->{layer} ); | ||
| 192 | } | ||
| 193 | |||
| 194 | my $new_content = Mojo::JSON::encode_json($new_ref); | ||
| 195 | my $asset = Mojo::Asset::Memory->new; | ||
| 196 | $asset->add_chunk($new_content); | ||
| 197 | $content->asset($asset); | ||
| 198 | $content->headers->content_length( $asset->size ); | ||
| 199 | |||
| 200 | $tx->res->content($content); | ||
| 201 | }, | ||
| 202 | sub { | ||
| 203 | my ( $c, $tx ) = @_; | ||
| 204 | $self->_fix_docker_headers($tx); | ||
| 205 | } | ||
| 206 | ); | ||
| 207 | } | ||
| 208 | } | ||
| 209 | |||
| 210 | sub put_repo_tag { | ||
| 211 | my ($self) = @_; | ||
| 212 | |||
| 213 | my $tag_sha = $self->req->body; | ||
| 214 | $tag_sha =~ s/"//g; | ||
| 215 | my $repo_name = $self->param("repo_namespace") . "/" . $self->param("name"); | ||
| 216 | |||
| 217 | my $url = $self->req->url; | ||
| 218 | my ($tag_name) = ( $url =~ m/.*\/(.+?)$/ ); | ||
| 219 | |||
| 220 | my $tag_dir = | ||
| 221 | File::Spec->catdir( $self->app->get_repo_dir( repo => $self->repo->{name} ), | ||
| 222 | "repository", $repo_name, "tags" ); | ||
| 223 | |||
| 224 | make_path($tag_dir); | ||
| 225 | open( my $fh, ">", File::Spec->catfile( $tag_dir, $tag_name ) ) or die($!); | ||
| 226 | print $fh $tag_sha; | ||
| 227 | close($fh); | ||
| 228 | |||
| 229 | $self->render( text => 'true' ); | ||
| 230 | } | ||
| 231 | |||
| 232 | sub put_repo_image { | ||
| 233 | my ($self) = @_; | ||
| 234 | |||
| 235 | $self->render( text => '', status => 204 ); | ||
| 236 | } | ||
| 237 | |||
| 238 | sub _get_image_directory { | ||
| 239 | my ( $self, $id ) = @_; | ||
| 240 | my $repo_dir = $self->app->get_repo_dir( repo => $self->repo->{name} ); | ||
| 241 | my $images_dir = File::Spec->catdir( $repo_dir, "images" ); | ||
| 242 | |||
| 243 | if ( -d File::Spec->catdir( $images_dir, $id ) ) { | ||
| 244 | return File::Spec->catdir( $images_dir, $id ); | ||
| 245 | } | ||
| 246 | |||
| 247 | # try to guess the image directory... | ||
| 248 | # TODO: look inside repo.json for better preci... genauigkeit | ||
| 249 | opendir( my $dh, $images_dir ) or die($!); | ||
| 250 | while ( my $entry = readdir($dh) ) { | ||
| 251 | if ( $entry =~ m/^\Q$id\E/ ) { | ||
| 252 | return $entry; | ||
| 253 | } | ||
| 254 | } | ||
| 255 | closedir($dh); | ||
| 256 | } | ||
| 257 | |||
| 258 | sub _fix_docker_headers { | ||
| 259 | |||
| 260 | my ( $self, $tx ) = @_; | ||
| 261 | |||
| 262 | my $docker_endpoint = $tx->res->headers->header('X-Docker-Endpoints'); | ||
| 263 | $tx->res->headers->remove('X-Docker-Endpoints'); | ||
| 264 | $tx->res->headers->remove('X-Docker-Token'); | ||
| 265 | $self->app->log->debug("Setting R-Docker-Endpoints to -> $docker_endpoint"); | ||
| 266 | $tx->res->headers->add( 'R-Docker-Endpoints', $docker_endpoint ) | ||
| 267 | if $docker_endpoint; | ||
| 268 | |||
| 269 | if ( $self->res->headers->header('WWW-Authenticate') ) { | ||
| 270 | $tx->res->headers->add( 'WWW-Authenticate', | ||
| 271 | $self->res->headers->header('WWW-Authenticate') ); | ||
| 272 | } | ||
| 273 | |||
| 274 | if ( $self->res->headers->header('X-Docker-Token') ) { | ||
| 275 | $tx->res->headers->add( 'X-Docker-Token', | ||
| 276 | $self->res->headers->header('X-Docker-Token') ); | ||
| 277 | } | ||
| 278 | |||
| 279 | $tx->res->headers->add( 'X-Docker-Endpoints' => $self->req->headers->host ); | ||
| 280 | $tx->res->headers->add( 'Pragma' => 'no-cache' ); | ||
| 281 | $tx->res->headers->add( 'Expires' => '-1' ); | ||
| 282 | |||
| 283 | } | ||
| 284 | |||
| 285 | 1; |