| 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 | my $config = Eixo::Docker::HostConfig->new->populate(\%args); | ||
| 223 | |||
| 224 | $args{POST_DATA} = $config; | ||
| 225 | |||
| 226 | $self->__exec("start", %args); | ||
| 227 | |||
| 228 | } | ||
| 229 | |||
| 230 | sub stop { | ||
| 231 | my ($self, %args) = @_; | ||
| 232 | |||
| 233 | $args{GET_DATA}->{t} = (exists($args{timeout}))? | ||
| 234 | $args{timeout}: | ||
| 235 | (exists($args{t}))? | ||
| 236 | $args{t} : $DEFAULT_TIMEOUT; | ||
| 237 | $self->__exec("stop", %args); | ||
| 238 | |||
| 239 | } | ||
| 240 | |||
| 241 | |||
| 242 | sub restart { | ||
| 243 | my ($self, %args) = @_; | ||
| 244 | |||
| 245 | $args{GET_DATA}->{t} = (exists($args{timeout}))? | ||
| 246 | $args{timeout}: | ||
| 247 | (exists($args{t}))? | ||
| 248 | $args{t} : $DEFAULT_TIMEOUT; | ||
| 249 | $self->__exec("restart", %args); | ||
| 250 | |||
| 251 | } | ||
| 252 | |||
| 253 | sub kill { | ||
| 254 | my ($self, %args) = @_; | ||
| 255 | |||
| 256 | $self->__exec("kill", %args); | ||
| 257 | |||
| 258 | } | ||
| 259 | |||
| 260 | sub copy{ | ||
| 261 | my ($self, %args) = @_; | ||
| 262 | |||
| 263 | $args{id} = $self->Id unless($args{id}); | ||
| 264 | $args{action} = 'copy'; | ||
| 265 | |||
| 266 | $args{__format} = 'RAW'; | ||
| 267 | |||
| 268 | $self->api->postContainers( | ||
| 269 | |||
| 270 | needed=>[qw(Resource)], | ||
| 271 | |||
| 272 | args=>\%args, | ||
| 273 | |||
| 274 | post_params=>[qw(Resource)], | ||
| 275 | |||
| 276 | onProgress=>sub { | ||
| 277 | }, | ||
| 278 | |||
| 279 | __callback=>sub { | ||
| 280 | |||
| 281 | #use Data::Dumper; print Dumper(\@_); | ||
| 282 | |||
| 283 | return $_[0] || $_[1]->buffer; | ||
| 284 | |||
| 285 | } | ||
| 286 | ); | ||
| 287 | } | ||
| 288 | |||
| 289 | sub attach{ | ||
| 290 | my ($self, %args) = @_; | ||
| 291 | |||
| 292 | $args{id} = $self->Id unless($args{id}); | ||
| 293 | |||
| 294 | $args{action} = 'attach'; | ||
| 295 | |||
| 296 | $args{$_} = $args{$_} || 0 foreach(qw(logs stream stdin stdout stderr)); | ||
| 297 | |||
| 298 | |||
| 299 | Eixo::Docker::RequestRawStream->new( | ||
| 300 | |||
| 301 | entity=>'containers', | ||
| 302 | |||
| 303 | id=>$args{id}, | ||
| 304 | |||
| 305 | action=>'attach', | ||
| 306 | |||
| 307 | method=>'POST', | ||
| 308 | |||
| 309 | host=>$self->api->client->endpoint, | ||
| 310 | |||
| 311 | args=>\%args, | ||
| 312 | |||
| 313 | url_args=>[qw(logs stream stdin stdout stderr)], | ||
| 314 | |||
| 315 | f_line=>$args{f_line} || $Eixo::Docker::IDENTITY_FUNC, | ||
| 316 | |||
| 317 | f_process => $args{f_process} || $Eixo::Docker::IDENTITY_FUNC, | ||
| 318 | |||
| 319 | timeout => $args{timeout} || 60, | ||
| 320 | |||
| 321 | tty_mode => $self->get(id => $args{id})->Config->Tty, | ||
| 322 | |||
| 323 | )->process(); | ||
| 324 | |||
| 325 | } | ||
| 326 | |||
| 327 | sub top{ | ||
| 328 | my ($self, %args) = @_; | ||
| 329 | |||
| 330 | $args{id} = $self->Id unless($args{id}); | ||
| 331 | |||
| 332 | $args{__implicit_format} = 1; | ||
| 333 | |||
| 334 | $args{action} = 'top'; | ||
| 335 | |||
| 336 | $self->api->getContainers( | ||
| 337 | |||
| 338 | args=>\%args, | ||
| 339 | |||
| 340 | get_params=>[qw(ps_args)], | ||
| 341 | |||
| 342 | __callback=>sub{ | ||
| 343 | |||
| 344 | use Data::Dumper; print Dumper(\@_); | ||
| 345 | |||
| 346 | } | ||
| 347 | |||
| 348 | |||
| 349 | ); | ||
| 350 | |||
| 351 | } | ||
| 352 | |||
| 353 | sub rename{ | ||
| 354 | my ($self, %args) = @_; | ||
| 355 | |||
| 356 | $args{id} = $self->Id unless($args{id}); | ||
| 357 | |||
| 358 | $args{__implicit_format} = 1; | ||
| 359 | |||
| 360 | $args{action} = 'rename'; | ||
| 361 | |||
| 362 | $self->api->postContainers( | ||
| 363 | |||
| 364 | args=>\%args, | ||
| 365 | |||
| 366 | get_params=>[qw(name)], | ||
| 367 | |||
| 368 | needed=>[qw(name)], | ||
| 369 | |||
| 370 | __callback=>sub{ | ||
| 371 | |||
| 372 | my $result = $_[0]; | ||
| 373 | |||
| 374 | #return container fully loaded | ||
| 375 | $self->get(id => $result->{Id}); | ||
| 376 | |||
| 377 | $self; | ||
| 378 | } | ||
| 379 | |||
| 380 | ); | ||
| 381 | } | ||
| 382 | |||
| 383 | |||
| 384 | |||
| 385 | sub __exec { | ||
| 386 | my ($self, $action, %args) = @_; | ||
| 387 | |||
| 388 | $args{id} = $self->Id; | ||
| 389 | |||
| 390 | $args{action} = $action; | ||
| 391 | |||
| 392 | $self->api->postContainers( | ||
| 393 | |||
| 394 | needed => [qw(id)], | ||
| 395 | |||
| 396 | args => \%args, | ||
| 397 | |||
| 398 | ); | ||
| 399 | } | ||
| 400 | |||
| 401 | sub __error { | ||
| 402 | my ($self, $method, $reason,@args) = @_; | ||
| 403 | |||
| 404 | Eixo::Docker::ContainerException->new( | ||
| 405 | |||
| 406 | method => $method, | ||
| 407 | |||
| 408 | reason => $reason, | ||
| 409 | |||
| 410 | args => \@args | ||
| 411 | |||
| 412 | )->raise(); | ||
| 413 | |||
| 414 | } | ||
| 415 | |||
| 416 | |||
| 417 | 1; |