| 1 | package Eixo::Docker::Image; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Eixo::Base::Clase qw(Eixo::Rest::Product); | ||
| 6 | |||
| 7 | #use Eixo::Docker; | ||
| 8 | use Eixo::Docker::Config; | ||
| 9 | use Eixo::Docker::ImageResume; | ||
| 10 | use Archive::Tar; | ||
| 11 | use Cwd; | ||
| 12 | |||
| 13 | my @BUILD_QUERY_PARAMS = qw(t tag q quiet nocache rm forcerm); | ||
| 14 | my @CALLBACKS = qw(onStart onProgress onError onSuccess); | ||
| 15 | |||
| 16 | has( | ||
| 17 | Id => undef, | ||
| 18 | Parent => undef, | ||
| 19 | Created => undef, | ||
| 20 | Container => undef, | ||
| 21 | ContainerConfig => {}, | ||
| 22 | Size => 0, | ||
| 23 | |||
| 24 | Config => {}, | ||
| 25 | Comment => undef, | ||
| 26 | Architecture => undef, | ||
| 27 | DockerVersion => undef, | ||
| 28 | Os => undef, | ||
| 29 | History=>[], | ||
| 30 | ); | ||
| 31 | |||
| 32 | # Alias to fix name attribute changes in api#v1.12 | ||
| 33 | sub id { &Id(@_) } | ||
| 34 | sub parent { &Parent(@_) } | ||
| 35 | sub created { &Created(@_) } | ||
| 36 | sub container { &Container(@_) } | ||
| 37 | sub container_config { &ContainerConfig(@_) } | ||
| 38 | sub config {&Config(@_)} | ||
| 39 | sub comment {&Comment(@_)} | ||
| 40 | sub architecture {&Architecture(@_)} | ||
| 41 | sub docker_version {&DockerVersion(@_)} | ||
| 42 | sub os {&Os(@_)} | ||
| 43 | #sub history {&History(@_)} | ||
| 44 | |||
| 45 | |||
| 46 | sub initialize{ | ||
| 47 | my $self = $_[0]; | ||
| 48 | |||
| 49 | $self->SUPER::initialize(@_[1..$#_]); | ||
| 50 | |||
| 51 | # set default Docker::Rest::Client error callback | ||
| 52 | # to call when API response error is received | ||
| 53 | $self->api->client->error_callback( | ||
| 54 | sub { $self->error(@_) } | ||
| 55 | ); | ||
| 56 | |||
| 57 | $self; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub get{ | ||
| 61 | my ($self, %args) = @_; | ||
| 62 | |||
| 63 | $args{id} = $self->id if($self->id); | ||
| 64 | |||
| 65 | $self->api->getImages( | ||
| 66 | |||
| 67 | needed=>[qw(id)], | ||
| 68 | |||
| 69 | args=>\%args, | ||
| 70 | |||
| 71 | __callback=>sub { | ||
| 72 | |||
| 73 | $self->populate($_[0]); | ||
| 74 | |||
| 75 | if(my %h = %{$self->container_config}){ | ||
| 76 | $self->container_config(Eixo::Docker::Config->new(%h)); | ||
| 77 | } | ||
| 78 | |||
| 79 | if(my %h = %{$self->config}){ | ||
| 80 | $self->config(Eixo::Docker::Config->new(%h)); | ||
| 81 | } | ||
| 82 | |||
| 83 | $self; | ||
| 84 | |||
| 85 | } | ||
| 86 | ); | ||
| 87 | |||
| 88 | |||
| 89 | } | ||
| 90 | |||
| 91 | |||
| 92 | sub history{ | ||
| 93 | my ($self, %args) = @_; | ||
| 94 | |||
| 95 | $args{id} = $self->id unless($args{id}); | ||
| 96 | |||
| 97 | $args{action} = 'history'; | ||
| 98 | |||
| 99 | $args{__implicit_format} = 1; | ||
| 100 | |||
| 101 | $self->api->getImages( | ||
| 102 | |||
| 103 | needed=>[qw(id)], | ||
| 104 | |||
| 105 | args=>\%args, | ||
| 106 | |||
| 107 | __callback=>sub { | ||
| 108 | |||
| 109 | |||
| 110 | map { | ||
| 111 | |||
| 112 | Eixo::Docker::ImageResume->new(%{$_}) | ||
| 113 | |||
| 114 | } @{$_[0]}; | ||
| 115 | |||
| 116 | } | ||
| 117 | |||
| 118 | ); | ||
| 119 | } | ||
| 120 | |||
| 121 | sub getAll{ | ||
| 122 | my ($self, %args) = @_; | ||
| 123 | |||
| 124 | my $list = []; | ||
| 125 | |||
| 126 | $args{all} = 1; | ||
| 127 | |||
| 128 | |||
| 129 | $self->api->getImages( | ||
| 130 | |||
| 131 | args=>\%args, | ||
| 132 | |||
| 133 | get_data => [qw(all)], | ||
| 134 | |||
| 135 | __callback=>sub { | ||
| 136 | |||
| 137 | foreach(@{$_[0]}){ | ||
| 138 | push @$list, Eixo::Docker::ImageResume->new(%$_); | ||
| 139 | } | ||
| 140 | |||
| 141 | $list; | ||
| 142 | } | ||
| 143 | |||
| 144 | ); | ||
| 145 | |||
| 146 | |||
| 147 | } | ||
| 148 | |||
| 149 | sub create{ | ||
| 150 | my ($self, %args) = @_; | ||
| 151 | |||
| 152 | |||
| 153 | # actually, only fromImage it's supported | ||
| 154 | |||
| 155 | my $image = $args{fromImage}; | ||
| 156 | my $tag = $args{tag} || 'latest'; | ||
| 157 | |||
| 158 | $args{action} = 'create'; | ||
| 159 | |||
| 160 | $args{__format} = 'RAW'; | ||
| 161 | |||
| 162 | $self->api->postImages( | ||
| 163 | |||
| 164 | needed=>[qw(fromImage)], | ||
| 165 | |||
| 166 | args=>\%args, | ||
| 167 | |||
| 168 | get_params=>[qw(fromImage repo tag registry)], | ||
| 169 | |||
| 170 | __callback=>sub { | ||
| 171 | |||
| 172 | $self->get(id=>"$image:$tag"); | ||
| 173 | |||
| 174 | return $self; | ||
| 175 | } | ||
| 176 | ); | ||
| 177 | |||
| 178 | } | ||
| 179 | |||
| 180 | # TODO | ||
| 181 | # sub import{} | ||
| 182 | |||
| 183 | sub build{ | ||
| 184 | my ($self, %args) = @_; | ||
| 185 | |||
| 186 | # args must be string-files to use in build (except build query params) | ||
| 187 | # Dockerfile is a must | ||
| 188 | |||
| 189 | die("Dockerfile arg doesn't exists") unless(defined($args{Dockerfile})); | ||
| 190 | |||
| 191 | my $tar = Archive::Tar->new; | ||
| 192 | |||
| 193 | while(my ($name, $data) = each (%args)){ | ||
| 194 | |||
| 195 | next if (grep {$_ eq $name} @BUILD_QUERY_PARAMS); | ||
| 196 | next if (grep {$_ eq $name} @CALLBACKS); | ||
| 197 | |||
| 198 | $tar->add_data($name, $data); | ||
| 199 | |||
| 200 | delete($args{$name}); | ||
| 201 | } | ||
| 202 | |||
| 203 | $self->_build($tar, %args); | ||
| 204 | } | ||
| 205 | |||
| 206 | |||
| 207 | sub build_from_dir{ | ||
| 208 | |||
| 209 | my ($self, %args) = @_; | ||
| 210 | |||
| 211 | die("DIR arg is not present") unless(defined($args{DIR})); | ||
| 212 | |||
| 213 | my $dir = delete($args{DIR}); | ||
| 214 | |||
| 215 | |||
| 216 | my $olddir = getcwd(); | ||
| 217 | |||
| 218 | chdir($dir); | ||
| 219 | |||
| 220 | my @list = Eixo::Docker::get_dir_files('.'); | ||
| 221 | |||
| 222 | my $tar = Archive::Tar->new; | ||
| 223 | |||
| 224 | $tar->add_files(@list); | ||
| 225 | |||
| 226 | chdir($olddir); | ||
| 227 | |||
| 228 | $self->_build($tar, %args); | ||
| 229 | |||
| 230 | |||
| 231 | } | ||
| 232 | |||
| 233 | # | ||
| 234 | # Image search | ||
| 235 | # | ||
| 236 | |||
| 237 | sub search{ | ||
| 238 | my ($self, %args) = @_; | ||
| 239 | |||
| 240 | $args{__implicit_format} = 1; | ||
| 241 | $args{action} = "search"; | ||
| 242 | |||
| 243 | my @images; | ||
| 244 | |||
| 245 | $self->api->getImages( | ||
| 246 | |||
| 247 | needed=>[qw(term)], | ||
| 248 | |||
| 249 | get_params=>[qw(term)], | ||
| 250 | |||
| 251 | args=>\%args, | ||
| 252 | |||
| 253 | __callback=>sub { | ||
| 254 | |||
| 255 | @images = @{$_[0]}; | ||
| 256 | } | ||
| 257 | |||
| 258 | ); | ||
| 259 | |||
| 260 | } | ||
| 261 | |||
| 262 | sub _build { | ||
| 263 | |||
| 264 | my ($self, $tar, %args) = @_; | ||
| 265 | |||
| 266 | my $image_name = $args{t} || $args{tag} || die("Lacks 'tag' param"); | ||
| 267 | |||
| 268 | my $get_data = { | ||
| 269 | t => $image_name, | ||
| 270 | rm => "1", # remove intermediate containers | ||
| 271 | }; | ||
| 272 | |||
| 273 | $get_data->{q} = $args{q} || $args{quiet} if(defined($args{q}||$args{quiet})); | ||
| 274 | $get_data->{nocache} = $args{nocache} if(defined($args{nocache})); | ||
| 275 | |||
| 276 | my $params = { | ||
| 277 | |||
| 278 | GET_DATA => $get_data, | ||
| 279 | POST_DATA => $tar->write(), | ||
| 280 | HEADER_DATA => {"Content-Type", "application/tar"}, | ||
| 281 | __format => "RAW" | ||
| 282 | }; | ||
| 283 | |||
| 284 | my $PROGRESS_ERROR = undef; | ||
| 285 | |||
| 286 | $self->api->postBuild( | ||
| 287 | |||
| 288 | args => $params, | ||
| 289 | |||
| 290 | onProgress => sub { | ||
| 291 | my $resp; | ||
| 292 | |||
| 293 | # Must eval json decode, because json string could be chunked, | ||
| 294 | # so decode could launch exception and breaks image creation | ||
| 295 | # see https://github.com/alambike/eixo-docker/issues/4 | ||
| 296 | eval{ | ||
| 297 | $resp = JSON->new->utf8->decode($_[0]); | ||
| 298 | }; | ||
| 299 | |||
| 300 | if($resp->{"error"}){ | ||
| 301 | $PROGRESS_ERROR = "Error building image: ".$resp->{errorDetail}->{message}; | ||
| 302 | } | ||
| 303 | |||
| 304 | &{$args{onProgress}}(@_) if(exists($args{onProgress})); | ||
| 305 | }, | ||
| 306 | |||
| 307 | __callback=>sub { | ||
| 308 | $self->error("build", $PROGRESS_ERROR) if($PROGRESS_ERROR); | ||
| 309 | |||
| 310 | $self->get(id=>$image_name); | ||
| 311 | |||
| 312 | return $self; | ||
| 313 | } | ||
| 314 | ); | ||
| 315 | |||
| 316 | |||
| 317 | } | ||
| 318 | |||
| 319 | |||
| 320 | |||
| 321 | # Deprecated since 1.12 | ||
| 322 | |||
| 323 | # sub insertFile{ | ||
| 324 | # my ($self, %args) = @_; | ||
| 325 | |||
| 326 | # $args{action} = 'insert'; | ||
| 327 | |||
| 328 | # $args{__format} = 'RAW'; | ||
| 329 | |||
| 330 | # my $ID_NEW_IMAGE; | ||
| 331 | |||
| 332 | # my $f_get_id = sub { | ||
| 333 | |||
| 334 | # ($ID_NEW_IMAGE) = $_[0] =~ /\"status\"\:\"([^"]+)\"\}/; | ||
| 335 | |||
| 336 | # }; | ||
| 337 | |||
| 338 | # $self->api->postImages( | ||
| 339 | |||
| 340 | # needed=>[qw(url path id)], | ||
| 341 | |||
| 342 | # args=>\%args, | ||
| 343 | |||
| 344 | # get_params=>[qw(url path)], | ||
| 345 | |||
| 346 | # onProgress => sub { | ||
| 347 | |||
| 348 | # &$f_get_id($_[0]); | ||
| 349 | |||
| 350 | |||
| 351 | # }, | ||
| 352 | |||
| 353 | # __callback=>sub { | ||
| 354 | |||
| 355 | # # | ||
| 356 | # # Take the last id and use it to get the new image | ||
| 357 | # # | ||
| 358 | # &$f_get_id($_[0]) if($_[0]); | ||
| 359 | |||
| 360 | # $self->api->images->get(id=>$ID_NEW_IMAGE); | ||
| 361 | |||
| 362 | # }, | ||
| 363 | |||
| 364 | |||
| 365 | # ); | ||
| 366 | # } | ||
| 367 | |||
| 368 | |||
| 369 | sub delete{ | ||
| 370 | my ($self, %args) = @_; | ||
| 371 | |||
| 372 | $args{id} = $self->id unless($args{id}); | ||
| 373 | |||
| 374 | $self->api->deleteImages( | ||
| 375 | |||
| 376 | needed=>[qw(id)], | ||
| 377 | |||
| 378 | args=>\%args | ||
| 379 | |||
| 380 | ); | ||
| 381 | } | ||
| 382 | |||
| 383 | 1; |