lib/Rex/Box/Docker.pm

filter: Statementstmt 47.0% 40 / 85 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 65.0% 13 / 20 Podpod 85.7% 6 / 7 totaltotal 41.2% 59 / 143 CC 12 SCAR 27.0
CC
12
Cov
37%
import_vm
42.0
ip
30.0
new
12.0
CRAP 14.8
« lib/Rex/Interface/Cache/YAML.pm ↑ Summary lib/Rex/Virtualization/VBox/guestinfo.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 =head1 NAME
6  
7 Rex::Box::Docker - Rex/Boxes Docker Module
8  
9 =head1 DESCRIPTION
10  
11 This is a Rex/Boxes module to use Docker Images. You need to have dockerd installed.
12  
13 =head1 EXAMPLES
14  
15 To use this module inside your Rexfile you can use the following commands.
16  
17 use Rex::Commands::Box;
18 set box => "Docker";
19  
20 task "prepare_box", sub {
21 box {
22 my ($box) = @_;
23  
24 $box->name("mybox");
25 $box->url("http://box.rexify.org/box/ubuntu-server-12.10-amd64.tar.gz");
26 $box->url("debian:latest");
27  
28 $box->network(1 => {
29 name => "default",
30 });
31  
32 $box->auth(
33 user => "root",
34 password => "box",
35 );
36  
37 $box->setup("setup_task");
38 };
39 };
40  
41 If you want to use a YAML file you can use the following template.
42  
43 type: Docker
44 vms:
45 vmone:
46 url: debian:latest
47 setup: setup_task
48  
49 And then you can use it the following way in your Rexfile.
50  
51 use Rex::Commands::Box init_file => "file.yml";
52  
53 task "prepare_vms", sub {
54 boxes "init";
55 };
56  
57 =head1 METHODS
58  
59 See also the Methods of Rex::Box::Base. This module inherits all methods of it.
60  
61 =cut
62  
63 package Rex::Box::Docker;
64  
65 1 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 1 ▶use Rex::Box::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 1 ▶use Rex::Commands -no => [qw/auth/];
Subroutine BEGIN: called CC=1 SCAR=0.0
70 1 ▶use Rex::Commands::Fs;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 1 ▶use Rex::Commands::Virtualization;
Subroutine BEGIN: called CC=1 SCAR=0.0
72 1 ▶use Rex::Commands::SimpleCheck;
Subroutine BEGIN: called CC=1 SCAR=0.0
73 1 ▶use Rex::Virtualization::Docker::create;
Subroutine BEGIN: called CC=1 SCAR=0.0
74  
75 1 our $VERSION = '1.16.1'; # VERSION
76  
77 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
78 1 LWP::UserAgent->use;
79 }
80  
81 1 ▶use Time::HiRes qw(tv_interval gettimeofday);
Subroutine BEGIN: called CC=1 SCAR=0.0
82 1 ▶use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
83  
84 1 ▶use base qw(Rex::Box::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
85  
86 1 set virtualization => "Docker";
87  
88 1 $|++;
89  
90 ################################################################################
91 # BEGIN of class methods
92 ################################################################################
93  
94 =head2 new(name => $vmname)
95  
96 Constructor if used in OO mode.
97  
98 my $box = Rex::Box::Docker->new(name => "vmname");
99  
100 =cut
101  
102 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
103 0 my $class = shift;
104 0 ▶ my $proto = ref($class) || $class;
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
105 0 my $self = $proto->SUPER::new(@_);
106  
107 0 ▶ bless( $self, ref($class) || $class );
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
108  
109 0 return $self;
110 }
111  
112 =head2 memory($memory_size)
113  
114 Sets the memory of a VM in megabyte.
115  
116 =cut
117  
118 0 ▶sub memory {
Subroutine memory: not called CC=1 SCAR=6.9
Pod documented
119 0 my ( $self, $mem ) = @_;
120 0 Rex::Logger::debug("Memory option not supported.");
121 }
122  
123 0 ▶sub import_vm {
Subroutine import_vm: not called CC=6 SCAR=37.4
Pod documented
124 0 my ($self) = @_;
125  
126 # check if machine already exists
127 0 my $vms = vm list => "all";
128  
129 0 my $vm_exists = 0;
130  
131 0 for my $vm ( @{$vms} ) {
132 0 ▶ if ( $vm->{name} eq $self->{name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vm->{"name"} eq $self->{"name"}) 0 0 0
133 0 Rex::Logger::debug("VM already exists. Don't import anything.");
134 0 $vm_exists = 1;
135 }
136 }
137  
138 0 ▶ if ( !$vm_exists ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($vm_exists) 0 0 0
139  
140 # if not, create it
141 0 my ($filename);
142 0 ▶ if ( $self->{url} =~ m/^(http|https):/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'url'} =~ /^(http|https):/u) { } 0 0 0
143 0 $self->_download;
144 0 $filename = "./tmp/" . basename( $self->{url} );
145 0 Rex::Logger::info("Importing VM ./tmp/$filename");
146 my @options = (
147 import => $self->{name},
148 file => $filename,
149 0 %{$self},
150 );
151 0 vm @options;
152 }
153 else {
154 0 vm import => $self->{name}, file => $self->{url}, %{$self};
155 }
156 }
157  
158 0 my $vminfo = vm info => $self->{name};
159  
160 0 ▶ unless ( $vminfo->{State}->{Running} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($vminfo->{"State"}{"Running"}) 0 0 0
161 0 $self->start;
162 }
163  
164 0 $self->{info} = vm guestinfo => $self->{name};
165 }
166  
167 0 ▶sub list_boxes {
Subroutine list_boxes: not called CC=1 SCAR=6.9
Pod documented
168 0 my ($self) = @_;
169  
170 0 my $vms = vm list => "all";
171  
172 0 return @{$vms};
173 }
174  
175 =head2 info
176  
177 Returns a hashRef of vm information.
178  
179 =cut
180  
181 0 ▶sub info {
Subroutine info: not called CC=1 SCAR=6.9
Pod documented
182 0 my ($self) = @_;
183 0 $self->ip;
184 0 return $self->{info};
185 }
186  
187 =head2 ip
188  
189 This method return the ip of a vm on which the ssh daemon is listening.
190  
191 =cut
192  
193 0 ▶sub ip {
Subroutine ip: not called CC=5 SCAR=34.0
Pod documented
194 0 my ($self) = @_;
195 0 $self->{info} = vm guestinfo => $self->{name};
196  
197 0 ▶ if ( $self->{info}->{redirects}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'info'}{'redirects'} and $self->{'info'}{'redirects'}{'tcp'} and $self->{'info'}{'redirects'}{'tcp'}{'22'}) { } 0 0 0
Condition0%0/6
$self->{"info"}{"redirects"} and $self->{"info"}{"redirects"}{"tcp"} and $self->{"info"}{"redirects"}{"tcp"}{22}
!ll&&!rl&&r
000
$self->{"info"}{"redirects"} and $self->{"info"}{"redirects"}{"tcp"}
!ll&&!rl&&r
000
MC/DC0%0/3
$self->{"info"}{"redirects"} and $self->{"info"}{"redirects"}{"tcp"} and $self->{"info"}{"redirects"}{"tcp"}{22}
$self->{"info"}{"redirects"} $self->{"info"}{"redirects"}{"tcp"} $self->{"info"}{"redirects"}{"tcp"}{22}
Truth table0%0/4
A and B and C
A= $self->{"info"}{"redirects"}B= $self->{"info"}{"redirects"}{"tcp"}C= $self->{"info"}{"redirects"}{"tcp"}{22}
ABCresult
0XX0
10X0
1100
1111
198 && $self->{info}->{redirects}->{tcp}
199 && $self->{info}->{redirects}->{tcp}->{22} )
200 {
201 return (
202 $self->{info}->{redirects}->{tcp}->{22}->[0]->{ip} eq "0.0.0.0"
203 ? "127.0.0.1"
204 : $self->{info}->{redirects}->{tcp}->{22}->[0]->{ip}
205 )
206 . ":"
207 0 ▶ . $self->{info}->{redirects}->{tcp}->{22}->[0]->{port};
Branch: 1 branch
BranchTrueFalseTotal
$self->{'info'}{'redirects'}{'tcp'}{'22'}[0]{'ip'} eq '0.0.0.0' ? : 0 0 0
208 }
209 else {
210 0 return $self->{info}->{network}->[0]->{ip};
211 }
212 }
213  
214 0 ▶sub create {
Subroutine create: not called CC=1 SCAR=6.9
Pod undocumented
215 0 my ($self) = @_;
216 my @options = (
217 forward_port => [
218 $self->{__forward_port}->{ssh}->[0] . ":"
219 . $self->{__forward_port}->{ssh}->[1]
220 ],
221 image => $self->{url},
222 0 );
223  
224 0 Rex::Virtualization::Docker::create->execute( $self->{name}, @options );
225 }
226  
227 1 1;
« lib/Rex/Interface/Cache/YAML.pm ↑ Summary lib/Rex/Virtualization/VBox/guestinfo.pm »