lib/Rex/Virtualization/Docker/create.pm

filter: Statementstmt 43.5% 37 / 85 Branchbran 0.0% 0 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 85.7% 12 / 14 Podpod 0.0% 0 / 1 totaltotal 37.1% 49 / 132 CC 17 SCAR 46.2
CC
17
Cov
32%
_format_opts
210.0
execute
20.0
BEGIN
1.0
CRAP 101.1
« lib/Rex/Commands/Kernel.pm ↑ Summary lib/Rex/Commands/Mkfs.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 package Rex::Virtualization::Docker::create;
6  
7 1 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $VERSION = '1.16.1'; # VERSION
11  
12 1 ▶use Rex::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Rex::Commands::Gather;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Rex::Hardware;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Rex::Commands::Fs;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Rex::Helper::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Rex::Commands::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Rex::File::Parser::Data;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Rex::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 0 ▶sub execute {
Subroutine execute: not called CC=4 SCAR=30.0
Pod undocumented
26 0 my ( $class, $name, %opt ) = @_;
27  
28 0 my $opts = \%opt;
29 0 $opts->{name} = $name;
30  
31 0 ▶ unless ($opts) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts) 0 0 0
32 0 die("You have to define the create options!");
33 }
34  
35 0 ▶ if ( !exists $opts->{"image"} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $opts->{"image"}) 0 0 0
36 0 die("You have to set a base image.");
37 }
38  
39 0 ▶ if ( !exists $opts->{"command"} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $opts->{"command"}) 0 0 0
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 0 ▶sub _format_opts {
Subroutine _format_opts: not called CC=14 SCAR=53.5
52 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 ▶ if ( !exists $opts->{"name"} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $opts->{"name"}) 0 0 0
57 0 die("You have to give a name.");
58 }
59  
60 # -m=""
61 # Memory limit (format: <number><optional unit>, where unit = b, k, m or g)
62 0 ▶ if ( !exists $opts->{"memory"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $opts->{'memory'}) { } 0 0 0
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 ▶ if ( !exists $opts->{"cpus"} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $opts->{"cpus"}) 0 0 0
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 ▶ if ( exists $opts->{"env"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"env"}) 0 0 0
80 0 $str .= '-e ' . join( '-e ', @{ $opts->{'env'} } ) . ' ';
81 }
82  
83 # -h=""
84 # Container host name
85 0 ▶ if ( exists $opts->{"hostname"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"hostname"}) 0 0 0
86 0 $str .= "-h $opts->{'hostname'} ";
87 }
88  
89 # -privileged=false
90 # Give extended privileges to this container
91 0 ▶ if ( exists $opts->{"privileged"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"privileged"}) 0 0 0
92 0 $str .= "--privileged $opts->{'privileged'} ";
93 }
94  
95 # -p=[]
96 # Map a network port to the container
97 0 ▶ if ( exists $opts->{"forward_port"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"forward_port"}) 0 0 0
98 0 $str .= '-p ' . join( '-p ', @{ $opts->{'forward_port'} } ) . ' ';
99 }
100  
101 # -expose=[]
102 # Expose a port from the container without publishing it to your host
103 0 ▶ if ( exists $opts->{"expose_port"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"expose_port"}) 0 0 0
104 0 $str .= "--expose $opts->{'expose_port'} ";
105 }
106  
107 # -dns=[]
108 # Set custom dns servers for the container
109 0 ▶ if ( exists $opts->{"dns"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"dns"}) 0 0 0
110 0 $str .= '--dns ' . join( '-dns ', @{ $opts->{'dns'} } ) . ' ';
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 ▶ if ( exists $opts->{"share_folder"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"share_folder"}) 0 0 0
117 0 $str .= '-v ' . join( '-v ', @{ $opts->{'share_folder'} } ) . ' ';
118 }
119  
120 # -volumes-from=""
121 # Mount all volumes from the given container(s)
122 0 ▶ if ( exists $opts->{"volumes-from"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"volumes-from"}) 0 0 0
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 ▶ if ( exists $opts->{"lxc-conf"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"lxc-conf"}) 0 0 0
129 0 $str .= "--lxc-conf \"$opts->{'lxc-conf'}\" ";
130 }
131  
132 # -link=""
133 # Add link to another container (name:alias)
134 0 ▶ if ( exists $opts->{"link"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"link"}) 0 0 0
135 0 $str .= '--link ' . join( '-link ', @{ $opts->{'link'} } ) . ' ';
136 }
137  
138 0 return $str;
139  
140 }
141  
142 1 1;
« lib/Rex/Commands/Kernel.pm ↑ Summary lib/Rex/Commands/Mkfs.pm »