| 1 | package Eixo::Docker::Container; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Eixo::Base::Clase qw(Eixo::Rest::Product); | ||
| 7 | |||
| 8 | use Eixo::Docker::Config; | ||
| 9 | use Eixo::Docker::ContainerResume; | ||
| 10 | use Eixo::Docker::ContainerException; | ||
| 11 | use Eixo::Docker::HostConfig; | ||
| 12 | use Eixo::Docker::RequestRawStream; | ||
| 13 | |||
| 14 | my $DEFAULT_TIMEOUT = 30; | ||
| 15 | |||
| 16 | has( | ||
| 17 | |||
| 18 | Id => undef, # new ID attribute from api#v1.12 | ||
| 19 | Created => '', | ||
| 20 | Path => '', | ||
| 21 | Args => [], | ||
| 22 | State => {}, | ||
| 23 | Image => '', | ||
| 24 | NetworkSettings => {}, | ||
| 25 | HostnamePath => '', | ||
| 26 | ResolvConfPath => '', | ||
| 27 | HostsPath => '', | ||
| 28 | LogPath => '', | ||
| 29 | Name => '', | ||
| 30 | RestartCount => undef, | ||
| 31 | Driver => '', | ||
| 32 | ExecDriver => '', | ||
| 33 | ProcessLabel => '', | ||
| 34 | MountLabel => '', | ||
| 35 | AppArmorProfile => "", | ||
| 36 | ExecIDs => undef, | ||
| 37 | |||
| 38 | HostConfig => {}, | ||
| 39 | GraphDriver => {}, | ||
| 40 | Mounts => [], | ||
| 41 | Config => {}, | ||
| 42 | |||
| 43 | ); | ||
| 44 | |||
| 45 | # Alias to fix name attribute changes in api#v1.12 | ||
| 46 | sub ID { &Id(@_)} | ||
| 47 | |||
| 48 | sub initialize { | ||
| 49 | my $self = $_[0]; | ||
| 50 | |||
| 51 | $self->SUPER::initialize(@_[1..$#_]); | ||
| 52 | |||
| 53 | # set default Docker::Rest::Client error callback | ||
| 54 | # to call when API response error is received | ||
| 55 | $self->api->client->error_callback( | ||
| 56 | sub { $self->error(@_) } | ||
| 57 | ); | ||
| 58 | |||
| 59 | $self; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub get{ | ||
| 63 | my ($self, %args) = @_; | ||
| 64 | |||
| 65 | $args{id} = $self->Id || $args{id}; | ||
| 66 | |||
| 67 | $self->api->getContainers( | ||
| 68 | |||
| 69 | needed=>[qw(id)], | ||
| 70 | |||
| 71 | args=>\%args, | ||
| 72 | |||
| 73 | __callback => sub { | ||
| 74 | |||
| 75 | $self->populate($_[0]); | ||
| 76 | |||
| 77 | |||
| 78 | # load config obj replacing config hash | ||
| 79 | if(my %h = %{$self->Config}){ | ||
| 80 | $self->Config(Eixo::Docker::Config->new(%h)) | ||
| 81 | } | ||
| 82 | |||
| 83 | if(my %h = %{$self->HostConfig}){ | ||
| 84 | $self->HostConfig(Eixo::Docker::HostConfig->new(%h)) | ||
| 85 | } | ||
| 86 | |||
| 87 | $self; | ||
| 88 | }, | ||
| 89 | ); | ||
| 90 | |||
| 91 | } | ||
| 92 | |||
| 93 | |||
| 94 | sub getByName { | ||
| 95 | my ($self, $name) = @_; | ||
| 96 | |||
| 97 | return undef unless($name); | ||
| 98 | |||
| 99 | $name = '/'.$name unless($name =~ /^\//); | ||
| 100 | |||
| 101 | #get all containers | ||
| 102 | my $list = $self->getAll(); | ||
| 103 | |||
| 104 | foreach my $c (@$list){ | ||
| 105 | return $self->get(id => $c->Id) if (grep {$name eq $_} @{$c->Names}); | ||
| 106 | } | ||
| 107 | |||
| 108 | } | ||
| 109 | |||
| 110 | |||
| 111 | sub getAll { | ||
| 112 | |||
| 113 | my ($self,%args) = @_; | ||
| 114 | |||
| 115 | my $list = []; | ||
| 116 | |||
| 117 | my $args = { | ||
| 118 | all => 1, | ||
| 119 | limit => $args{limit} || 1000, | ||
| 120 | }; | ||
| 121 | |||
| 122 | $self->api->getContainers( | ||
| 123 | |||
| 124 | args => $args, | ||
| 125 | |||
| 126 | get_params => [qw(all limit)], | ||
| 127 | |||
| 128 | __callback => sub { | ||
| 129 | |||
| 130 | foreach my $r (@{$_[0]}){ | ||
| 131 | |||
| 132 | push @$list, Eixo::Docker::ContainerResume->new(%$r) | ||
| 133 | } | ||
| 134 | |||
| 135 | $list; | ||
| 136 | } | ||
| 137 | ); | ||
| 138 | |||
| 139 | } | ||
| 140 | |||
| 141 | |||
| 142 | sub create { | ||
| 143 | |||
| 144 | my ($self , %attrs) = @_; | ||
| 145 | |||
| 146 | my $args = \%attrs; | ||
| 147 | |||
| 148 | # convert args to correct params | ||
| 149 | $args->{name} = $attrs{Name} if(exists($attrs{Name})); | ||
| 150 | |||
| 151 | # validate attrs and initialize default values not setted | ||
| 152 | my $config = Eixo::Docker::Config->new(%attrs); | ||
| 153 | my $hostconfig = Eixo::Docker::HostConfig->new(%{$attrs{HostConfig}}); | ||
| 154 | delete($config->{api}); | ||
| 155 | delete($hostconfig->{api}); | ||
| 156 | |||
| 157 | my $create_data = { | ||
| 158 | |||
| 159 | %$config, | ||
| 160 | HostConfig => {%$hostconfig}, | ||
| 161 | |||
| 162 | }; | ||
| 163 | |||
| 164 | $args->{action} = 'create'; | ||
| 165 | $args->{POST_DATA} = $create_data; | ||
| 166 | |||
| 167 | |||
| 168 | my $res = $self->api->postContainers( | ||
| 169 | |||
| 170 | args => $args, | ||
| 171 | |||
| 172 | get_params => [qw(name)], | ||
| 173 | |||
| 174 | __callback => sub { | ||
| 175 | my $result = $_[0]; | ||
| 176 | |||
| 177 | #return container fully loaded | ||
| 178 | $self->get(id => $result->{Id}); | ||
| 179 | |||
| 180 | $self; | ||
| 181 | |||
| 182 | } | ||
| 183 | ); | ||
| 184 | |||
| 185 | } | ||
| 186 | |||
| 187 | |||
| 188 | sub delete{ | ||
| 189 | my ($self, %args) = @_; | ||
| 190 | |||
| 191 | $args{id} = $self->Id || $args{id}; | ||
| 192 | |||
| 193 | my $delete_volumes = (exists($args{delete_volumes}))? | ||
| 194 | $args{delete_volumes}: | ||
| 195 | (exists($args{v}))? | ||
| 196 | $args{v} :0; | ||
| 197 | |||
| 198 | |||
| 199 | |||
| 200 | $args{GET_DATA} = { | ||
| 201 | v => $delete_volumes | ||
| 202 | }; | ||
| 203 | |||
| 204 | $self->api->deleteContainers( | ||
| 205 | needed=>[qw(id)], | ||
| 206 | args => \%args, | ||
| 207 | ); | ||
| 208 | |||
| 209 | } | ||
| 210 | |||
| 211 | |||
| 212 | sub status{ | ||
| 213 | my ($self, %args) = @_; | ||
| 214 | |||
| 215 | $self->get(%args)->State; | ||
| 216 | } | ||
| 217 | |||
| 218 | |||
| 219 | sub start { | ||
| 220 | my ($self, %args) = @_; | ||
| 221 | |||
| 222 | # From API v1.24, start command does not accepts request body content anymore | ||
| 223 | # https://docs.docker.com/engine/api/version-history/#v124-api-changes | ||
| 224 | if( $self->api->version->get()->ApiVersion < '1.24' ) { | ||
| 225 | my $config = Eixo::Docker::HostConfig->new->populate(\%args); | ||
| 226 | $args{POST_DATA} = $config; | ||
| 227 | } | ||
| 228 | |||
| 229 | $self->__exec("start", %args); | ||
| 230 | |||
| 231 | } | ||
| 232 | |||
| 233 | sub stop { | ||
| 234 | my ($self, %args) = @_; | ||
| 235 | |||
| 236 | $args{GET_DATA}->{t} = (exists($args{timeout}))? | ||
| 237 | $args{timeout}: | ||
| 238 | (exists($args{t}))? | ||
| 239 | $args{t} : $DEFAULT_TIMEOUT; | ||
| 240 | $self->__exec("stop", %args); | ||
| 241 | |||
| 242 | } | ||
| 243 | |||
| 244 | |||
| 245 | sub restart { | ||
| 246 | my ($self, %args) = @_; | ||
| 247 | |||
| 248 | $args{GET_DATA}->{t} = (exists($args{timeout}))? | ||
| 249 | $args{timeout}: | ||
| 250 | (exists($args{t}))? | ||
| 251 | $args{t} : $DEFAULT_TIMEOUT; | ||
| 252 | $self->__exec("restart", %args); | ||
| 253 | |||
| 254 | } | ||
| 255 | |||
| 256 | sub kill { | ||
| 257 | my ($self, %args) = @_; | ||
| 258 | |||
| 259 | $self->__exec("kill", %args); | ||
| 260 | |||
| 261 | } | ||
| 262 | |||
| 263 | sub copy{ | ||
| 264 | my ($self, %args) = @_; | ||
| 265 | |||
| 266 | $args{id} = $self->Id unless($args{id}); | ||
| 267 | $args{action} = 'copy'; | ||
| 268 | |||
| 269 | $args{__format} = 'RAW'; | ||
| 270 | |||
| 271 | $self->api->postContainers( | ||
| 272 | |||
| 273 | needed=>[qw(Resource)], | ||
| 274 | |||
| 275 | args=>\%args, | ||
| 276 | |||
| 277 | post_params=>[qw(Resource)], | ||
| 278 | |||
| 279 | onProgress=>sub { | ||
| 280 | }, | ||
| 281 | |||
| 282 | __callback=>sub { | ||
| 283 | |||
| 284 | #use Data::Dumper; print Dumper(\@_); | ||
| 285 | |||
| 286 | return $_[0] || $_[1]->buffer; | ||
| 287 | |||
| 288 | } | ||
| 289 | ); | ||
| 290 | } | ||
| 291 | |||
| 292 | sub attach{ | ||
| 293 | my ($self, %args) = @_; | ||
| 294 | |||
| 295 | $args{id} = $self->Id unless($args{id}); | ||
| 296 | |||
| 297 | $args{action} = 'attach'; | ||
| 298 | |||
| 299 | $args{$_} = $args{$_} || 0 foreach(qw(logs stream stdin stdout stderr)); | ||
| 300 | |||
| 301 | |||
| 302 | Eixo::Docker::RequestRawStream->new( | ||
| 303 | |||
| 304 | entity=>'containers', | ||
| 305 | |||
| 306 | id=>$args{id}, | ||
| 307 | |||
| 308 | action=>'attach', | ||
| 309 | |||
| 310 | method=>'POST', | ||
| 311 | |||
| 312 | host=>$self->api->client->endpoint, | ||
| 313 | |||
| 314 | args=>\%args, | ||
| 315 | |||
| 316 | url_args=>[qw(logs stream stdin stdout stderr)], | ||
| 317 | |||
| 318 | f_line=>$args{f_line} || $Eixo::Docker::IDENTITY_FUNC, | ||
| 319 | |||
| 320 | f_process => $args{f_process} || $Eixo::Docker::IDENTITY_FUNC, | ||
| 321 | |||
| 322 | timeout => $args{timeout} || 60, | ||
| 323 | |||
| 324 | tty_mode => $self->get(id => $args{id})->Config->Tty, | ||
| 325 | |||
| 326 | )->process(); | ||
| 327 | |||
| 328 | } | ||
| 329 | |||
| 330 | sub top{ | ||
| 331 | my ($self, %args) = @_; | ||
| 332 | |||
| 333 | $args{id} = $self->Id unless($args{id}); | ||
| 334 | |||
| 335 | $args{__implicit_format} = 1; | ||
| 336 | |||
| 337 | $args{action} = 'top'; | ||
| 338 | |||
| 339 | $self->api->getContainers( | ||
| 340 | |||
| 341 | args=>\%args, | ||
| 342 | |||
| 343 | get_params=>[qw(ps_args)], | ||
| 344 | |||
| 345 | __callback=>sub{ | ||
| 346 | |||
| 347 | use Data::Dumper; print Dumper(\@_); | ||
| 348 | |||
| 349 | } | ||
| 350 | |||
| 351 | |||
| 352 | ); | ||
| 353 | |||
| 354 | } | ||
| 355 | |||
| 356 | sub rename{ | ||
| 357 | my ($self, %args) = @_; | ||
| 358 | |||
| 359 | $args{id} = $self->Id unless($args{id}); | ||
| 360 | |||
| 361 | $args{__implicit_format} = 1; | ||
| 362 | |||
| 363 | $args{action} = 'rename'; | ||
| 364 | |||
| 365 | $self->api->postContainers( | ||
| 366 | |||
| 367 | args=>\%args, | ||
| 368 | |||
| 369 | get_params=>[qw(name)], | ||
| 370 | |||
| 371 | needed=>[qw(name)], | ||
| 372 | |||
| 373 | __callback=>sub{ | ||
| 374 | |||
| 375 | my $result = $_[0]; | ||
| 376 | |||
| 377 | #return container fully loaded | ||
| 378 | $self->get(id => $result->{Id}); | ||
| 379 | |||
| 380 | $self; | ||
| 381 | } | ||
| 382 | |||
| 383 | ); | ||
| 384 | } | ||
| 385 | |||
| 386 | |||
| 387 | |||
| 388 | sub __exec { | ||
| 389 | my ($self, $action, %args) = @_; | ||
| 390 | |||
| 391 | $args{id} = $self->Id; | ||
| 392 | |||
| 393 | $args{action} = $action; | ||
| 394 | |||
| 395 | $self->api->postContainers( | ||
| 396 | |||
| 397 | needed => [qw(id)], | ||
| 398 | |||
| 399 | args => \%args, | ||
| 400 | |||
| 401 | ); | ||
| 402 | } | ||
| 403 | |||
| 404 | sub __error { | ||
| 405 | my ($self, $method, $reason,@args) = @_; | ||
| 406 | |||
| 407 | Eixo::Docker::ContainerException->new( | ||
| 408 | |||
| 409 | method => $method, | ||
| 410 | |||
| 411 | reason => $reason, | ||
| 412 | |||
| 413 | args => \@args | ||
| 414 | |||
| 415 | )->raise(); | ||
| 416 | |||
| 417 | } | ||
| 418 | |||
| 419 | |||
| 420 | 1; |