File Coverage

lib/Rex/Virtualization.pm
Criterion Covered Total %
statement 30 35 85.7
branch 6 8 75.0
condition 3 5 60.0
subroutine 7 8 87.5
pod 0 2 0.0
total 46 58 79.3


line stmt bran cond sub pod time code
1             #
2             # (c) Jan Gehring
3             #
4              
5             package Rex::Virtualization;
6              
7 4     4   68641 use v5.12.5;
  4         27  
8 4     4   20 use warnings;
  4         6  
  4         169  
9              
10             our $VERSION = '1.14.3'; # VERSION
11              
12 4     4   804 use Rex::Logger;
  4         10  
  4         85  
13 4     4   1174 use Rex::Config;
  4         10  
  4         30  
14              
15             my %VM_PROVIDER;
16              
17             Rex::Config->register_config_handler(
18             virtualization => sub {
19             my ($param) = @_;
20              
21             if ( ref($param) eq '' ) {
22              
23             #support 'set virtualization => 'LibVirt', but leave the way open for using a hash in future
24             #other virtualisation drivers may need more settings...
25             $param = { type => $param };
26             }
27              
28             if ( exists $param->{type} ) {
29             Rex::Config->set( virtualization => $param->{type} );
30             }
31             }
32             );
33              
34             sub register_vm_provider {
35 0     0 0 0 my ( $class, $service_name, $service_class ) = @_;
36 0         0 $VM_PROVIDER{"\L$service_name"} = $service_class;
37 0         0 return 1;
38             }
39              
40             sub create {
41 5     5 0 353 my ( $class, $wanted_provider ) = @_;
42              
43 5   100     29 $wanted_provider ||= Rex::Config->get("virtualization");
44 5 50       18 if ( ref($wanted_provider) ) {
45 0   0     0 $wanted_provider = $wanted_provider->{type} || "LibVirt";
46             }
47              
48 5 100       38 if ( !$wanted_provider ) {
49 1         8 die
50             "No virtualization provider set.\nPlease use `set virtualization => 'YourProvider';` to set one,\nor see `perldoc Rex::Commands::Virtualization` for more options";
51             }
52              
53 4         14 my $klass = "Rex::Virtualization::$wanted_provider";
54              
55 4 50       14 if ( exists $VM_PROVIDER{$wanted_provider} ) {
56 0         0 $klass = $VM_PROVIDER{$wanted_provider};
57             }
58              
59 4     3   241 eval "use $klass";
  3     1   472  
  2         7  
  2         15  
  1         387  
  1         3  
  1         7  
60              
61 4 100       63 if ($@) {
62 1         8 die
63             "Failed loading given virtualization module.\nTried to load <$klass>.\nError: $@\n";
64             }
65              
66 3         19 Rex::Logger::debug("Using $klass for virtualization");
67              
68 3         11 my $mod = $klass->new;
69 3         15 return $mod;
70             }
71              
72             1;