File Coverage

lib/Rex/Virtualization/Docker/create.pm
Criterion Covered Total %
statement 35 83 42.1
branch 0 32 0.0
condition n/a
subroutine 12 14 85.7
pod 0 1 0.0
total 47 130 36.1


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   15 use v5.12.5;
  1         4  
8 1     1   7 use warnings;
  1         2  
  1         54  
9              
10             our $VERSION = '1.14.2.3'; # TRIAL VERSION
11              
12 1     1   12 use Rex::Logger;
  1         4  
  1         7  
13 1     1   27 use Rex::Commands::Gather;
  1         3  
  1         7  
14 1     1   8 use Rex::Hardware;
  1         4  
  1         5  
15 1     1   33 use Rex::Commands::Fs;
  1         2  
  1         7  
16 1     1   7 use Rex::Helper::Run;
  1         3  
  1         65  
17 1     1   8 use Rex::Commands::File;
  1         2  
  1         9  
18 1     1   10 use Rex::File::Parser::Data;
  1         4  
  1         21  
19 1     1   24 use Rex::Template;
  1         2  
  1         10  
20              
21 1     1   32 use XML::Simple;
  1         3  
  1         9  
22              
23 1     1   71 use Data::Dumper;
  1         2  
  1         634  
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;