line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# (c) Jan Gehring |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
package Rex::Virtualization::Docker::create; |
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
14
|
use v5.12.5; |
|
1
|
|
|
|
|
4
|
|
8
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
48
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '1.14.2.2'; # TRIAL VERSION |
11
|
|
|
|
|
|
|
|
12
|
1
|
|
|
1
|
|
5
|
use Rex::Logger; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
5
|
|
13
|
1
|
|
|
1
|
|
29
|
use Rex::Commands::Gather; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
9
|
|
14
|
1
|
|
|
1
|
|
7
|
use Rex::Hardware; |
|
1
|
|
|
|
|
22
|
|
|
1
|
|
|
|
|
9
|
|
15
|
1
|
|
|
1
|
|
36
|
use Rex::Commands::Fs; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
9
|
|
16
|
1
|
|
|
1
|
|
7
|
use Rex::Helper::Run; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
50
|
|
17
|
1
|
|
|
1
|
|
9
|
use Rex::Commands::File; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
17
|
|
18
|
1
|
|
|
1
|
|
10
|
use Rex::File::Parser::Data; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
12
|
|
19
|
1
|
|
|
1
|
|
43
|
use Rex::Template; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
8
|
|
20
|
|
|
|
|
|
|
|
21
|
1
|
|
|
1
|
|
26
|
use XML::Simple; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
9
|
|
22
|
|
|
|
|
|
|
|
23
|
1
|
|
|
1
|
|
76
|
use Data::Dumper; |
|
1
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
683
|
|
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub execute { |
26
|
0
|
|
|
0
|
0
|
|
my ( $class, $name, %opt ) = @_; |
27
|
|
|
|
|
|
|
|
28
|
0
|
|
|
|
|
|
my $opts = \%opt; |
29
|
0
|
|
|
|
|
|
$opts->{name} = $name; |
30
|
|
|
|
|
|
|
|
31
|
0
|
0
|
|
|
|
|
unless ($opts) { |
32
|
0
|
|
|
|
|
|
die("You have to define the create options!"); |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
0
|
0
|
|
|
|
|
if ( !exists $opts->{"image"} ) { |
36
|
0
|
|
|
|
|
|
die("You have to set a base image."); |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
0
|
0
|
|
|
|
|
if ( !exists $opts->{"command"} ) { |
40
|
0
|
|
|
|
|
|
$opts->{command} = ""; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
0
|
|
|
|
|
|
my $options = _format_opts($opts); |
44
|
|
|
|
|
|
|
|
45
|
0
|
|
|
|
|
|
my @out = i_run "docker run -d $options $opts->{'image'} $opts->{'command'}"; |
46
|
0
|
|
|
|
|
|
my $id = pop @out; |
47
|
|
|
|
|
|
|
|
48
|
0
|
|
|
|
|
|
return $id; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub _format_opts { |
52
|
0
|
|
|
0
|
|
|
my ($opts) = @_; |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# -name="" |
55
|
|
|
|
|
|
|
# Assign the specified name to the container. If no name is specific docker will generate a random name |
56
|
0
|
0
|
|
|
|
|
if ( !exists $opts->{"name"} ) { |
57
|
0
|
|
|
|
|
|
die("You have to give a name."); |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
# -m="" |
61
|
|
|
|
|
|
|
# Memory limit (format: , where unit = b, k, m or g) |
62
|
0
|
0
|
|
|
|
|
if ( !exists $opts->{"memory"} ) { |
63
|
0
|
|
|
|
|
|
$opts->{"memory"} = '512m'; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
else { |
66
|
0
|
|
|
|
|
|
$opts->{memory} = $opts->{memory}; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
# -c=0 |
70
|
|
|
|
|
|
|
# CPU shares (relative weight) |
71
|
0
|
0
|
|
|
|
|
if ( !exists $opts->{"cpus"} ) { |
72
|
0
|
|
|
|
|
|
$opts->{"cpus"} = 0; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
0
|
|
|
|
|
|
my $str = "--name $opts->{'name'} -m $opts->{'memory'} -c $opts->{'cpus'} "; |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
# -e=[] |
78
|
|
|
|
|
|
|
# Set environment variables |
79
|
0
|
0
|
|
|
|
|
if ( exists $opts->{"env"} ) { |
80
|
0
|
|
|
|
|
|
$str .= '-e ' . join( '-e ', @{ $opts->{'env'} } ) . ' '; |
|
0
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
# -h="" |
84
|
|
|
|
|
|
|
# Container host name |
85
|
0
|
0
|
|
|
|
|
if ( exists $opts->{"hostname"} ) { |
86
|
0
|
|
|
|
|
|
$str .= "-h $opts->{'hostname'} "; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
# -privileged=false |
90
|
|
|
|
|
|
|
# Give extended privileges to this container |
91
|
0
|
0
|
|
|
|
|
if ( exists $opts->{"privileged"} ) { |
92
|
0
|
|
|
|
|
|
$str .= "--privileged $opts->{'privileged'} "; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
# -p=[] |
96
|
|
|
|
|
|
|
# Map a network port to the container |
97
|
0
|
0
|
|
|
|
|
if ( exists $opts->{"forward_port"} ) { |
98
|
0
|
|
|
|
|
|
$str .= '-p ' . join( '-p ', @{ $opts->{'forward_port'} } ) . ' '; |
|
0
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
# -expose=[] |
102
|
|
|
|
|
|
|
# Expose a port from the container without publishing it to your host |
103
|
0
|
0
|
|
|
|
|
if ( exists $opts->{"expose_port"} ) { |
104
|
0
|
|
|
|
|
|
$str .= "--expose $opts->{'expose_port'} "; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
# -dns=[] |
108
|
|
|
|
|
|
|
# Set custom dns servers for the container |
109
|
0
|
0
|
|
|
|
|
if ( exists $opts->{"dns"} ) { |
110
|
0
|
|
|
|
|
|
$str .= '--dns ' . join( '-dns ', @{ $opts->{'dns'} } ) . ' '; |
|
0
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
# -v=[]: |
114
|
|
|
|
|
|
|
# Create a bind mount with: [host-dir]:[container-dir]:[rw|ro]. |
115
|
|
|
|
|
|
|
# If "container-dir" is missing, then docker creates a new volume. |
116
|
0
|
0
|
|
|
|
|
if ( exists $opts->{"share_folder"} ) { |
117
|
0
|
|
|
|
|
|
$str .= '-v ' . join( '-v ', @{ $opts->{'share_folder'} } ) . ' '; |
|
0
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
# -volumes-from="" |
121
|
|
|
|
|
|
|
# Mount all volumes from the given container(s) |
122
|
0
|
0
|
|
|
|
|
if ( exists $opts->{"volumes-from"} ) { |
123
|
0
|
|
|
|
|
|
$str .= "--volumes-from \"$opts->{'volumes-from'}\" "; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
# -lxc-conf=[] |
127
|
|
|
|
|
|
|
# Add custom lxc options -lxc-conf="lxc.cgroup.cpuset.cpus = 0,1" |
128
|
0
|
0
|
|
|
|
|
if ( exists $opts->{"lxc-conf"} ) { |
129
|
0
|
|
|
|
|
|
$str .= "--lxc-conf \"$opts->{'lxc-conf'}\" "; |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
# -link="" |
133
|
|
|
|
|
|
|
# Add link to another container (name:alias) |
134
|
0
|
0
|
|
|
|
|
if ( exists $opts->{"link"} ) { |
135
|
0
|
|
|
|
|
|
$str .= '--link ' . join( '-link ', @{ $opts->{'link'} } ) . ' '; |
|
0
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
0
|
|
|
|
|
|
return $str; |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
1; |