lib/Rex/Box/Base.pm

filter: Statementstmt 22.5% 35 / 155 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 34.2% 12 / 35 Podpod 90.4% 19 / 21 totaltotal 26.2% 66 / 251 CC 19 SCAR 28.1
CC
19
Cov
18%
_download
72.0
provision_vm
30.0
wait_for_ssh
12.0
CRAP 16.6
« lib/Rex/Virtualization/VBox/option.pm ↑ Summary lib/Rex/Virtualization/Lxc/create.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 =head1 NAME
6  
7 Rex::Box::Base - Rex/Boxes Base Module
8  
9 =head1 DESCRIPTION
10  
11 This is a Rex/Boxes base module.
12  
13 =head1 METHODS
14  
15 These methods are shared across all other Rex::Box modules.
16  
17 =cut
18  
19 package Rex::Box::Base;
20  
21 1 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 1 our $VERSION = '1.16.1'; # VERSION
25  
26 1 ▶use Rex::Commands -no => [qw/auth/];
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use Rex::Helper::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use Rex::Commands::Fs;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use Rex::Commands::Virtualization;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use Rex::Commands::SimpleCheck;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use Rex::Helper::IP;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 LWP::UserAgent->use;
35 }
36  
37 1 ▶use Time::HiRes qw(tv_interval gettimeofday);
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 ▶use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
42 0 my $that = shift;
43 0 ▶ my $proto = ref($that) || $that;
Condition0%0/3
ref $that || $that
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table0%0/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
44 0 my $self = {@_};
45  
46 0 bless( $self, $proto );
47  
48 # default auth for rex boxes
49 $self->{__auth} = {
50 0 user => Rex::Config->get_user(),
51 password => Rex::Config->get_password(),
52 private_key => Rex::Config->get_private_key(),
53 public_key => Rex::Config->get_public_key(),
54 };
55  
56 # for box this is needed, because we have changing ips
57 0 Rex::Config->set_openssh_opt(
58 StrictHostKeyChecking => "no",
59 UserKnownHostsFile => "/dev/null",
60 LogLevel => "QUIET"
61 );
62  
63 0 return $self;
64 }
65  
66 =head2 info
67  
68 Returns a hashRef of vm information.
69  
70 =cut
71  
72 0 ▶sub info {
Subroutine info: not called CC=1 SCAR=6.9
Pod documented
73 0 my ($self) = @_;
74 0 return $self->{info};
75 }
76  
77 =head2 name($vmname)
78  
79 Sets the name of the virtual machine.
80  
81 =cut
82  
83 0 ▶sub name {
Subroutine name: not called CC=1 SCAR=6.9
Pod documented
84 0 my ( $self, $name ) = @_;
85 0 $self->{name} = $name;
86 }
87  
88 =head2 setup(@tasks)
89  
90 Sets the tasks that should be executed as soon as the VM is available through SSH.
91  
92 =cut
93  
94 =head2 storage('path/to/vm/disk')
95  
96 Sets the disk path of the virtual machine. Works only on KVM
97  
98 =cut
99  
100 0 ▶sub storage {
Subroutine storage: not called CC=1 SCAR=6.9
Pod documented
101 0 my ( $self, $folder ) = @_;
102  
103 0 $self->{storage_path} = $folder;
104 }
105  
106 0 ▶sub setup {
Subroutine setup: not called CC=1 SCAR=6.9
Pod documented
107 0 my ( $self, @tasks ) = @_;
108 0 $self->{__tasks} = \@tasks;
109 }
110  
111 =head2 import_vm()
112  
113 This method must be overwritten by the implementing class.
114  
115 =cut
116  
117 0 ▶sub import_vm {
Subroutine import_vm: not called CC=1 SCAR=6.9
Pod documented
118 0 my ($self) = @_;
119 0 die("This method must be overwritten.");
120 }
121  
122 =head2 stop()
123  
124 Stops the VM.
125  
126 =cut
127  
128 0 ▶sub stop {
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
129 0 my ($self) = @_;
130 0 $self->info;
131 0 vm shutdown => $self->{name};
132 }
133  
134 =head2 destroy()
135  
136 Destroy the VM.
137  
138 =cut
139  
140 0 ▶sub destroy {
Subroutine destroy: not called CC=1 SCAR=6.9
Pod documented
141 0 my ($self) = @_;
142 0 $self->info;
143 0 vm destroy => $self->{name};
144 }
145  
146 =head2 start()
147  
148 Starts the VM.
149  
150 =cut
151  
152 0 ▶sub start {
Subroutine start: not called CC=1 SCAR=6.9
Pod documented
153 0 my ($self) = @_;
154 0 $self->info;
155 0 vm start => $self->{name};
156  
157 }
158  
159 =head2 ip()
160  
161 Return the ip:port to which rex will connect to.
162  
163 =cut
164  
165 0 ▶sub ip { die("Must be implemented by box class.") }
Subroutine ip: not called CC=1 SCAR=6.9
Pod documented
166  
167 =head2 status()
168  
169 Returns the status of a VM.
170  
171 Valid return values are "running" and "stopped".
172  
173 =cut
174  
175 0 ▶sub status {
Subroutine status: not called CC=1 SCAR=6.9
Pod documented
176 0 my ($self) = @_;
177 0 return vm status => $self->{name};
178 }
179  
180 =head2 provision_vm([@tasks])
181  
182 Executes the given tasks on the VM.
183  
184 =cut
185  
186 0 ▶sub provision_vm {
Subroutine provision_vm: not called CC=5 SCAR=34.0
Pod documented
187 0 my ( $self, @tasks ) = @_;
188  
189 0 ▶ if ( !@tasks ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@tasks) 0 0 0
190 0 ▶ @tasks = @{ $self->{__tasks} } if ( exists $self->{__tasks} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"__tasks"} 0 0 0
191 }
192  
193 0 $self->wait_for_ssh();
194  
195 0 for my $task (@tasks) {
196 0 my $task_o = Rex::TaskList->create()->get_task($task);
197 0 ▶ if ( !$task_o ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($task_o) 0 0 0
198 0 die "Task $task not found.";
199 }
200  
201 0 $task_o->set_auth( %{ $self->{__auth} } );
202 0 Rex::Commands::set( "box_object", $self );
203 0 $task_o->run( $self->ip );
204 0 Rex::Commands::set( "box_object", undef );
205 }
206 }
207  
208 =head2 cpus($count)
209  
210 Set the amount of CPUs for the VM.
211  
212 =cut
213  
214 0 ▶sub cpus {
Subroutine cpus: not called CC=1 SCAR=6.9
Pod documented
215 0 my ( $self, $cpus ) = @_;
216 0 $self->{cpus} = $cpus;
217 }
218  
219 =head2 memory($memory_size)
220  
221 Sets the memory of a VM in megabyte.
222  
223 =cut
224  
225 0 ▶sub memory {
Subroutine memory: not called CC=1 SCAR=6.9
Pod documented
226 0 my ( $self, $mem ) = @_;
227 0 $self->{memory} = $mem;
228 }
229  
230 =head2 network(%option)
231  
232 Configure the network for a VM.
233  
234 Currently it supports 2 modes: I<nat> and I<bridged>. Currently it supports only one network card.
235  
236 $box->network(
237 1 => {
238 type => "nat",
239 },
240 }
241  
242 $box->network(
243 1 => {
244 type => "bridged",
245 bridge => "eth0",
246 },
247 );
248  
249 =cut
250  
251 0 ▶sub network {
Subroutine network: not called CC=1 SCAR=6.9
Pod documented
252 0 my ( $self, %option ) = @_;
253 0 $self->{__network} = \%option;
254 }
255  
256 =head2 forward_port(%option)
257  
258 Set ports to be forwarded to the VM. This is not supported by all Box providers.
259  
260 $box->forward_port(
261 name => [$from_host_port, $to_vm_port],
262 name2 => [$from_host_port_2, $to_vm_port_2],
263 ...
264 );
265  
266 =cut
267  
268 0 ▶sub forward_port {
Subroutine forward_port: not called CC=1 SCAR=6.9
Pod documented
269 0 my ( $self, %option ) = @_;
270 0 $self->{__forward_port} = \%option;
271 }
272  
273 =head2 list_boxes
274  
275 List all available boxes.
276  
277 =cut
278  
279 0 ▶sub list_boxes {
Subroutine list_boxes: not called CC=1 SCAR=6.9
Pod documented
280 0 my ($self) = @_;
281  
282 0 my $vms = vm list => "all";
283  
284 0 return @{$vms};
285 }
286  
287 =head2 url($url)
288  
289 The URL where to download the Base VM Image. You can use self-made images or prebuild images from L<http://box.rexify.org/>.
290  
291 =cut
292  
293 0 ▶sub url {
Subroutine url: not called CC=1 SCAR=6.9
Pod documented
294 0 my ( $self, $url, $force ) = @_;
295 0 $self->{url} = $url;
296 0 $self->{force} = $force;
297 }
298  
299 =head2 auth(%option)
300  
301 Configure the authentication to the VM.
302  
303 $box->auth(
304 user => $user,
305 password => $password,
306 private_key => $private_key,
307 public_key => $public_key,
308 );
309  
310 =cut
311  
312 0 ▶sub auth {
Subroutine auth: not called CC=2 SCAR=17.9
Pod documented
313 0 my ( $self, %auth ) = @_;
314 0 ▶ if (%auth) {
Branch: 1 branch
BranchTrueFalseTotal
if (%auth) { } 0 0 0
315 0 $self->{__auth} = \%auth;
316 }
317 else {
318 0 return $self->{__auth};
319 }
320 }
321  
322 =head2 options(%option)
323  
324 Addition options for boxes
325  
326 $box->options(
327 opt1 => $val1,
328 opt2 => $val2,
329 );
330  
331 =cut
332  
333 0 ▶sub options {
Subroutine options: not called CC=2 SCAR=17.9
Pod documented
334 0 my ( $self, %opt ) = @_;
335 0 ▶ if (%opt) {
Branch: 1 branch
BranchTrueFalseTotal
if (%opt) { } 0 0 0
336 0 $self->{__options} = \%opt;
337 }
338 else {
339 0 return $self->{__options};
340 }
341 }
342  
343 0 ▶sub wait_for_ssh {
Subroutine wait_for_ssh: not called CC=3 SCAR=24.8
Pod undocumented
344 0 my ( $self, $ip, $port ) = @_;
345  
346 0 ▶ if ( !$ip ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ip) 0 0 0
347 0 ( $ip, $port ) = Rex::Helper::IP::get_server_and_port( $self->ip, 22 );
348 }
349  
350 0 print "Waiting for SSH to come up on $ip:$port.";
351 0 ▶ while ( !is_port_open( $ip, $port ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_port_open($ip, $port)) 0 0 0
352 0 print ".";
353 0 sleep 1;
354 }
355  
356 0 print "\n";
357 }
358  
359 0 ▶sub _download {
Subroutine _download: not called CC=8 SCAR=42.8
360 0 my ($self) = @_;
361  
362 0 my $filename = basename( $self->{url} );
363 0 ▶ my $force = $self->{force} || FALSE;
Condition0%0/3
$self->{'force'} || FALSE()
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'force'} || FALSE()
$self->{'force'} FALSE()
Truth table0%0/3
A || B
A= $self->{'force'}B= FALSE()
ABresult
1X1
011
000
364 0 my $fs = Rex::Interface::Fs->create;
365  
366 0 ▶ if ( $fs->is_file("./tmp/$filename") ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fs->is_file("./tmp/$filename")) { } 0 0 0
367 0 Rex::Logger::info(
368 "File already downloaded. Please remove the file ./tmp/$filename if you want to download a fresh copy."
369 );
370 }
371 else {
372 0 $force = TRUE;
373 }
374  
375 0 ▶ if ($force) {
Branch: 1 branch
BranchTrueFalseTotal
if ($force) 0 0 0
376 0 Rex::Logger::info("Downloading $self->{url} to ./tmp/$filename");
377 0 mkdir "tmp";
378 0 ▶ if ( Rex::is_local() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (Rex::is_local()) { } 0 0 0
379 0 my $ua = LWP::UserAgent->new();
380 0 $ua->env_proxy;
381 0 my $final_data = "";
382 0 my $current_size = 0;
383 0 my $current_modulo = 0;
384 0 my $start_time = [ gettimeofday() ];
385 0 ▶ open( my $fh, ">", "./tmp/$filename" )
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", "./tmp/$filename" or die "Failed to open ./tmp/$filename for writing: $!" 0 0 0
386 or die("Failed to open ./tmp/$filename for writing: $!");
387 0 binmode $fh;
388 my $resp = $ua->get(
389 $self->{url},
390 0 ▶ ':content_cb' => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
391 0 my ( $data, $response, $protocol ) = @_;
392  
393 0 $current_size += length($data);
394  
395 0 my $content_length = $response->header("content-length");
396  
397 0 print $fh $data;
398  
399 0 my $current_time = [ gettimeofday() ];
400 0 my $time_diff = tv_interval( $start_time, $current_time );
401  
402 0 my $bytes_per_seconds = $current_size / $time_diff;
403  
404 0 my $mbytes_per_seconds = $bytes_per_seconds / 1024 / 1024;
405  
406 0 my $mbytes_current = $current_size / 1024 / 1024;
407 0 my $mbytes_total = $content_length / 1024 / 1024;
408  
409 0 my $left_bytes = $content_length - $current_size;
410  
411 0 my $time_one_byte = $time_diff / $current_size;
412 0 my $time_all_bytes =
413 $time_one_byte * ( $content_length - $current_size );
414  
415 0 ▶ if ( ( ( $current_size / ( 1024 * 1024 ) ) % ( 1024 * 1024 ) ) >
Branch: 1 branch
BranchTrueFalseTotal
if ($current_size / 1048576 % 1048576 > $current_modulo) 0 0 0
416 $current_modulo )
417 {
418 0 print ".";
419 0 $current_modulo++;
420  
421 0 ▶ if ( $current_modulo % 10 == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_modulo % 10 == 0) 0 0 0
422 0 printf(
423 ". %.2f MBytes/s (%.2f MByte / %.2f MByte) %.2f secs left\n",
424 $mbytes_per_seconds, $mbytes_current,
425 $mbytes_total, $time_all_bytes
426 );
427 }
428  
429 }
430  
431 }
432 0 );
433 0 close($fh);
434  
435 0 ▶ if ( $resp->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp->is_success) { } 0 0 0
436 0 print " done.\n";
437 }
438 else {
439 0 Rex::Logger::info( "Error downloading box image.", "warn" );
440 0 unlink "./tmp/$filename";
441 }
442  
443 }
444 else {
445 0 i_exec "wget", "-c", "-qO", "./tmp/$filename", $self->{url};
446  
447 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
448 0 die(
449 "Downloading of $self->{url} failed. Please verify if wget is installed and if you have the right permissions to download this box."
450 );
451 }
452 }
453 }
454 }
455  
456 1 1;
« lib/Rex/Virtualization/VBox/option.pm ↑ Summary lib/Rex/Virtualization/Lxc/create.pm »