-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDocker.pm
105 lines (83 loc) · 1.99 KB
/
Docker.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#
# (c) Jan Gehring
#
# vim: set ts=2 sw=2 tw=0:
# vim: set expandtab:
package Application::Docker;
use Moose;
use Data::Dumper;
use Rex::Helper::Run;
use Rex::Commands::Run;
use Docker::Client;
use Application::Docker::Instance;
extends qw(Application::Base);
has name => (
is => 'ro',
isa => 'Str',
lazy => 1,
default => sub {
my $self = shift;
return $self->defaults->{instance_prefix};
}
);
has '+defaults' => (
default => sub {
my $self = shift;
return { instance_prefix => ( $ENV{"instance_prefix"} || "i" ), },;
},
);
has docker => (
is => 'ro',
isa => 'Docker::Client',
lazy => 1,
default => sub {
return Docker::Client->new;
},
);
has '+need_start_before_deploy' => ( default => sub { 0 }, );
override get_instances => sub {
my ($self) = @_;
my $instance_class = ref($self) . "::Instance";
my $app_name = $self->name;
my @containers =
grep { $app_name ? $self->_test( $app_name, $_->{Names} ) : 1 }
@{ $self->docker->containers->list };
my @ret;
for my $c_instance (@containers) {
push @ret,
$instance_class->new(
app => $self,
instance => $c_instance->{Names}->[0],
instance_path => $c_instance->{Id},
is_active => 1,
);
}
# push a new instance on the stack which is "inactive"
my $count = scalar(@containers) + 1;
my $new_name = $self->name . "-" . sprintf( "%02i", $count );
push @ret,
$instance_class->new(
app => $self,
instance => $new_name,
is_active => 0,
);
return @ret;
};
sub _test {
my ( $self, $name, $names ) = @_;
for my $n ( @{$names} ) {
if ( $n =~ m/^\/$name\-\d+$/ ) { return 1; }
}
return 0;
}
Project->register_app_type(
90,
__PACKAGE__,
sub {
if ( can_run("docker") ) {
return 1;
}
return 0;
}
);
1;