lib/Rex/Commands/Box.pm

filter: Statementstmt 22.7% 42 / 185 Branchbran 2.0% 1 / 48 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 52.1% 12 / 23 Podpod 83.3% 5 / 6 totaltotal 22.9% 60 / 262 CC 32 SCAR 37.8
CC
32
Cov
18%
boxes
132.0
__ANON__
56.0
__ANON__
42.0
CRAP 44.0
« lib/Rex/Virtualization/VBox/bridge.pm ↑ Summary lib/Rex/SCM/Git.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 =head1 NAME
6  
7 Rex::Commands::Box - Functions / Class to manage Virtual Machines
8  
9 =head1 DESCRIPTION
10  
11 This is a Module to manage Virtual Machines or Cloud Instances in a simple way. Currently it supports Amazon, KVM and VirtualBox.
12  
13 Version <= 1.0: All these functions will not be reported.
14  
15 =head1 SYNOPSIS
16  
17 use Rex::Commands::Box;
18  
19 set box => "VBox";
20  
21 group all_my_boxes => map { get_box($_->{name})->{ip} } list_boxes;
22  
23 task mytask => sub {
24  
25 box {
26 my ($box) = @_;
27 $box->name("boxname");
28 $box->url("http://box.rexify.org/box/base-image.box");
29  
30 $box->network(1 => {
31 type => "nat",
32 });
33  
34 $box->network(1 => {
35 type => "bridged",
36 bridge => "eth0",
37 });
38  
39 $box->forward_port(ssh => [2222, 22]);
40  
41 $box->share_folder(myhome => "/home/myuser");
42  
43 $box->auth(
44 user => "root",
45 password => "box",
46 );
47  
48 $box->setup(qw/task_to_customize_box/);
49  
50 };
51  
52 };
53  
54 =head1 EXPORTED FUNCTIONS
55  
56 =cut
57  
58 package Rex::Commands::Box;
59  
60 1 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 1 our $VERSION = '1.16.1'; # VERSION
64  
65 1 ▶use YAML;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
67  
68 1 ▶use Rex::Commands -no => [qw/auth/];
Subroutine BEGIN: called CC=1 SCAR=0.0
69 1 ▶use Rex::Commands::Fs;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 1 ▶use Rex::Commands::Virtualization;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 1 ▶use Rex::Commands::Gather;
Subroutine BEGIN: called CC=1 SCAR=0.0
72  
73 1 $|++;
74  
75 ################################################################################
76 # BEGIN of class methods
77 ################################################################################
78  
79 1 require Exporter;
80 1 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
81 1 ▶use vars qw(@EXPORT %vm_infos $VM_STRUCT);
Subroutine BEGIN: called CC=1 SCAR=0.0
82 1 ▶use Rex::Box;
Subroutine BEGIN: called CC=1 SCAR=0.0
83  
84 #@EXPORT = qw(box $box);
85 1 @EXPORT = qw(box list_boxes get_box boxes);
86  
87 Rex::Config->register_set_handler(
88 "box",
89 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
90 0 my ( $type, @data ) = @_;
91 0 Rex::Config->set( "box_type", $type );
92  
93 0 ▶ if ( ref( $data[0] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data[0]) { } 0 0 0
94 0 Rex::Config->set( "box_options", $data[0] );
95 }
96 else {
97 0 Rex::Config->set( "box_options", {@data} );
98 }
99 }
100 1 );
101  
102 =head2 new(name => $box_name)
103  
104 Constructor if used in OO mode.
105  
106 my $box = Rex::Commands::Box->new(name => "box_name");
107  
108 =cut
109  
110 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
111 0 my $class = shift;
112 0 return Rex::Box->create(@_);
113 }
114  
115 =head2 box(sub {})
116  
117 With this function you can create a new Rex/Box. The first parameter of this function is the Box object. With this object you can define your box.
118  
119 box {
120 my ($box) = @_;
121 $box->name("boxname");
122 $box->url("http://box.rexify.org/box/base-image.box");
123  
124 $box->network(1 => {
125 type => "nat",
126 });
127  
128 $box->network(1 => {
129 type => "bridged",
130 bridge => "eth0",
131 });
132  
133 $box->forward_port(ssh => [2222, 22]);
134  
135 $box->share_folder(myhome => "/home/myuser");
136  
137 $box->auth(
138 user => "root",
139 password => "box",
140 );
141  
142 $box->setup(qw/task_to_customize_box/);
143 };
144  
145  
146 =cut
147  
148 0 ▶sub box(&) {
Subroutine box: not called CC=1 SCAR=6.9
Pod documented
149 0 my $code = shift;
150  
151 #### too much black magic...
152 #my ($caller_box) = do {
153 # my $pkg = caller();
154 # no strict 'refs';
155 # \*{ $pkg . "::box" };
156 #};
157  
158 0 my $self = Rex::Box->create;
159  
160 #local( *$caller_box );
161 #*$caller_box = \$self;
162  
163 0 $code->($self);
164  
165 #*$caller_box = \{}; # undef $box
166  
167 0 $self->import_vm();
168  
169 0 $self->provision_vm();
170  
171 0 return $self->ip;
172 }
173  
174 =head2 list_boxes
175  
176 This function returns an array of hashes containing all information that can be gathered from the hypervisor about the Rex/Box. This function doesn't start a Rex/Box.
177  
178 use Data::Dumper;
179 task "get_infos", sub {
180 my @all_boxes = list_boxes;
181 print Dumper(\@all_boxes);
182 };
183  
184 =cut
185  
186 0 ▶sub list_boxes {
Subroutine list_boxes: not called CC=1 SCAR=6.9
Pod documented
187 0 my $box = Rex::Box->create;
188 0 my @ret = $box->list_boxes;
189  
190 0 ▶ my $ref = LOCAL {
Subroutine __ANON__: not called CC=6 SCAR=37.4
191 0 ▶ if ( -f ".box.cache" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f ".box.cache") 0 0 0
192 0 my $yaml_str = eval { local ( @ARGV, $/ ) = (".box.cache"); <>; };
193 0 $yaml_str .= "\n";
194 0 my $yaml_ref = Load($yaml_str);
195  
196 0 for my $box ( keys %{$yaml_ref} ) {
197 0 my ($found_box) = grep { $_->{name} eq $box } @ret;
198 0 ▶ if ( !$found_box ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found_box) 0 0 0
199 0 $yaml_ref->{$box} = undef;
200 0 delete $yaml_ref->{$box};
201 }
202 }
203  
204 0 ▶ open( my $fh, ">", ".box.cache" ) or die($!);
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", ".box.cache" or die $! 0 0 0
205 0 print $fh Dump($yaml_ref);
206 0 close($fh);
207 }
208  
209 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 0 0 0
210 0 return @ret;
211 }
212  
213 0 return \@ret;
214 0 };
215  
216 0 return @{$ref};
217 }
218  
219 =head2 get_box($box_name)
220  
221 This function tries to gather all information of a Rex/Box. This function also starts a Rex/Box to gather all information of the running system.
222  
223 use Data::Dumper;
224 task "get_box_info", sub {
225 my $data = get_box($box_name);
226 print Dumper($data);
227 };
228  
229 =cut
230  
231 0 ▶sub get_box {
Subroutine get_box: not called CC=2 SCAR=17.9
Pod documented
232 0 my ($box_name) = @_;
233  
234 0 my $box = Rex::Box->create( name => $box_name );
235 0 $box->info;
236  
237 0 ▶ if ( $box->status eq "stopped" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($box->status eq "stopped") 0 0 0
238 0 $box->start;
239 0 $box->wait_for_ssh;
240 }
241  
242 0 my $box_ip = $box->ip;
243 0 my $box_info = $box->info;
244  
245 0 ▶ return LOCAL {
Subroutine __ANON__: not called CC=7 SCAR=40.3
246  
247 0 ▶ if ( -f ".box.cache" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f ".box.cache") 0 0 0
248 0 Rex::Logger::debug("Loading box information of cache file: .box.cache.");
249 0 my $yaml_str = eval { local ( @ARGV, $/ ) = (".box.cache"); <>; };
250 0 $yaml_str .= "\n";
251 0 my $yaml_ref = Load($yaml_str);
252 0 %vm_infos = %{$yaml_ref};
253 }
254  
255 0 ▶ if ( exists $vm_infos{$box_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Rex::Commands::Box::vm_infos{$box_name}) 0 0 0
256 0 return $vm_infos{$box_name};
257 }
258  
259 0 my $pid = fork;
260 0 ▶ if ( $pid == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == 0) 0 0 0
261 0 print
262 "Gathering system information from $box_name.\nThis may take a while..";
263 0 while (1) {
264 0 print ".";
265 0 sleep 1;
266 }
267  
268 0 CORE::exit;
269 }
270  
271 0 my $old_q = $::QUIET;
272 0 $::QUIET = 1;
273  
274 eval {
275 0 $vm_infos{$box_name} = run_task "Commands:Box:get_sys_info",
276 on => $box_ip;
277 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $Rex::Commands::Box::vm_infos{$box_name} = run_task("Commands:Box:get_sys_info", "on", $box_ip) } } or do { $main::QUIET = $old_q; print STDERR "\n"; Rex::Logger::info("There was an error connecting to your Box. Please verify the login credentials.\nERROR: $@\n", "warn"); Rex::Logger::debug("You have to define login credentials before calling get_box()"); kill 9, $pid; CORE::exit 1 } 0 0 0
278 0 $::QUIET = $old_q;
279 0 print STDERR "\n";
280 0 Rex::Logger::info(
281 "There was an error connecting to your Box. Please verify the login credentials.\nERROR: $@\n",
282 "warn"
283 );
284 0 Rex::Logger::debug(
285 "You have to define login credentials before calling get_box()");
286  
287 # cleanup
288 0 kill 9, $pid;
289 0 CORE::exit(1);
290 };
291 0 $::QUIET = $old_q;
292  
293 0 for my $key ( keys %{$box_info} ) {
294 0 $vm_infos{$box_name}->{$key} = $box_info->{$key};
295 }
296  
297 0 ▶ open( my $fh, ">", ".box.cache" ) or die($!);
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", ".box.cache" or die $! 0 0 0
298 0 print $fh Dump( \%vm_infos );
299 0 close($fh);
300  
301 0 kill 9, $pid;
302 0 print "\n";
303  
304 0 return $vm_infos{$box_name};
305 0 };
306  
307 }
308  
309 =head2 boxes($action, @data)
310  
311 With this function you can control your boxes. Currently there are 3 actions.
312  
313 =over 4
314  
315 =item init
316  
317 This action can only be used if you're using a YAML file to describe your Rex/Boxes.
318  
319 task "prepare_boxes", sub {
320 boxes "init";
321 };
322  
323 =item start
324  
325 This action start one or more Rex/Boxes.
326  
327 task "start_boxes", sub {
328 boxes "start", "box1", "box2";
329 };
330  
331 =item stop
332  
333 This action stop one or more Rex/Boxes.
334  
335 task "stop_boxes", sub {
336 boxes "stop", "box1", "box2";
337 };
338  
339 =back
340  
341 =cut
342  
343 0 ▶sub boxes {
Subroutine boxes: not called CC=11 SCAR=48.8
Pod documented
344 0 my ( $action, @data ) = @_;
345  
346 0 ▶ if ( substr( $action, 0, 1 ) eq "-" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($action, 0, 1) eq "-") 0 0 0
347 0 $action = substr( $action, 1 );
348 }
349  
350 0 ▶ if ( $action eq "init" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action eq "init") 0 0 0
351  
352 0 ▶ if ( -f ".box.cache" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f ".box.cache") 0 0 0
353 0 unlink ".box.cache";
354 }
355  
356 0 my $yaml_ref = $VM_STRUCT;
357  
358 0 my @vms;
359  
360 0 ▶ if ( ref $yaml_ref->{vms} eq "HASH" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $yaml_ref->{'vms'} eq 'HASH') { } 0 0 0
361 0 for my $vm ( keys %{ $yaml_ref->{vms} } ) {
362 push(
363 @vms,
364 {
365 name => $vm,
366 0 %{ $yaml_ref->{vms}->{$vm} }
367 }
368 );
369 }
370 }
371 else {
372 0 @vms = @{ $yaml_ref->{vms} };
373 }
374  
375 0 my $box_vms = {};
376 0 for my $vm_ref (@vms) {
377 0 my $vm = $vm_ref->{name};
378 0 ▶ box {
Subroutine __ANON__: not called CC=4 SCAR=30.0
379 0 my ($box) = @_;
380  
381 0 $box->name($vm);
382  
383 0 for my $key ( keys %{$vm_ref} ) {
384 0 ▶ if ( ref( $vm_ref->{$key} ) eq "HASH" ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $vm_ref->{$key} eq 'HASH') { } 0 0 0
elsif (ref $vm_ref->{$key} eq 'ARRAY') { } 0 0 0
385 0 $box->$key( %{ $vm_ref->{$key} } );
386 }
387 elsif ( ref( $vm_ref->{$key} ) eq "ARRAY" ) {
388 0 $box->$key( @{ $vm_ref->{$key} } );
389 }
390 else {
391 0 $box->$key( $vm_ref->{$key} );
392 }
393 }
394  
395 0 $box_vms->{$vm} = $box;
396 0 };
397 }
398  
399 0 return $box_vms;
400 }
401  
402 0 ▶ if ( $action eq "stop" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action eq "stop") 0 0 0
403 0 for my $box (@data) {
404 0 my $o = Rex::Commands::Box->new( name => $box );
405 0 $o->stop;
406 }
407 }
408  
409 0 ▶ if ( $action eq "start" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action eq "start") 0 0 0
410 0 for my $box (@data) {
411 0 my $o = Rex::Commands::Box->new( name => $box );
412 0 $o->start;
413 }
414 }
415  
416 }
417  
418 0 ▶task 'get_sys_info', sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
419 0 return { get_system_information() };
420 1 }, { dont_register => 1, exit_on_connect_fail => 0 };
421  
422 0 ▶sub load_init_file {
Subroutine load_init_file: not called CC=5 SCAR=34.0
Pod undocumented
423 0 my ( $class, $file ) = @_;
424  
425 0 ▶ if ( !-f $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $file) 0 0 0
426 0 die("Error: Wrong configuration file: $file.");
427 }
428  
429 0 my $yaml_str = eval { local ( @ARGV, $/ ) = ($file); <>; };
430 0 $yaml_str .= "\n";
431  
432 0 my $yaml_ref = Load($yaml_str);
433  
434 0 ▶ if ( !exists $yaml_ref->{type} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $yaml_ref->{"type"}) 0 0 0
435 0 die("You have to define a type.");
436 }
437  
438 0 my $type = ucfirst $yaml_ref->{type};
439 0 set box_type => $type;
440  
441 # set special box options, like amazon out
442 0 ▶ if ( exists $yaml_ref->{"\L$type"} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $$yaml_ref{lc $type}) { } 0 0 0
elsif (exists $yaml_ref->{$type}) { } 0 0 0
443 0 set box_options => $yaml_ref->{"\L$type"};
444 }
445 elsif ( exists $yaml_ref->{$type} ) {
446 0 set box_options => $yaml_ref->{$type};
447 }
448  
449 0 $VM_STRUCT = $yaml_ref;
450 }
451  
452 3 ▶sub import {
Subroutine import: called CC=2 SCAR=9.2
453 3 my ( $class, %option ) = @_;
454  
455 3 ▶ if ( $option{init_file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option{"init_file"}) 0 3 3
456 0 my $file = $option{init_file};
457 0 $class->load_init_file($file);
458 0 @_ = ($class);
459 }
460  
461 3 __PACKAGE__->export_to_level( 1, @_ );
462 }
463  
464 1 1;
« lib/Rex/Virtualization/VBox/bridge.pm ↑ Summary lib/Rex/SCM/Git.pm »