lib/Rex/Virtualization/VBox/create.pm

filter: Statementstmt 31.9% 37 / 116 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 70.5% 12 / 17 Podpod 0.0% 0 / 1 totaltotal 24.0% 49 / 204 CC 33 SCAR 43.4
CC
33
Cov
21%
_set_storage_defaults
182.0
execute
90.0
_set_defaults
56.0
CRAP 76.9
« lib/Rex/Hardware/Network/OpenBSD.pm ↑ Summary lib/Rex/Virtualization/VBox/start.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 package Rex::Virtualization::VBox::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=9 SCAR=45.0
Pod undocumented
26 0 my ( $class, $name, %opt ) = @_;
27  
28 0 my $opts = \%opt;
29 0 $opts->{name} = $name;
30 0 ▶ $opts->{type} ||= "Linux26"; # default to Linux 2.6
Condition0%0/2
$opts->{"type"} ||= "Linux26"
l!l
00
MC/DC0%0/1
$opts->{"type"} ||= "Linux26"
$opts->{"type"}
Truth table0%0/2
A
A= $opts->{"type"}
Aresult
11
00
31  
32 0 ▶ unless ($opts) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts) 0 0 0
33 0 die("You have to define the create options!");
34 }
35  
36 0 _set_defaults($opts);
37  
38 i_run "VBoxManage createvm --name \""
39 . $name
40 . "\" --ostype \""
41 . $opts->{type}
42 0 . "\" --register";
43  
44 ### add controller
45 0 i_run
46 "VBoxManage storagectl \"$name\" --name \"SATA Controller\" --add sata --controller IntelAhci";
47 0 i_run
48 "VBoxManage storagectl \"$name\" --name \"IDE Controller\" --add ide --controller PIIX4";
49  
50 ### create hds
51 0 my $hdd_ports = {
52 "sata" => 0,
53 "ide" => 0,
54 };
55  
56 0 for my $hd ( @{ $opts->{storage} } ) {
57 0 ▶ if ( $hd->{type} eq "file" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hd->{"type"} eq "file") 0 0 0
58 0 my $filename = $hd->{file};
59 0 my $size = $hd->{size};
60 0 my $format = $hd->{format};
61 0 ▶ if ( !$filename ) { die("You have to specify 'file'."); }
Branch: 1 branch
BranchTrueFalseTotal
unless ($filename) 0 0 0
62  
63 0 ▶ if ( $hd->{device} eq "disk" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hd->{"device"} eq "disk") 0 0 0
64  
65 0 ▶ if ( !-f $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $filename) 0 0 0
66 0 i_run
67 "VBoxManage createhd --filename \"$filename\" --size $size --format $format 2>&1";
68 }
69  
70 i_run
71 "VBoxManage storageattach \"$name\" --storagectl \"SATA Controller\" --port "
72 . $hdd_ports->{sata}
73 0 . " --device 0 --type hdd --medium \"$filename\"";
74 0 $hdd_ports->{sata}++;
75 }
76  
77 0 ▶ if ( $hd->{device} eq "cdrom" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hd->{"device"} eq "cdrom") 0 0 0
78 i_run
79 "VBoxManage storageattach \"$name\" --storagectl \"IDE Controller\" --port "
80 . $hdd_ports->{ide}
81 0 . " --device 0 --type dvddrive --medium \"$filename\"";
82 0 $hdd_ports->{sata}++;
83 }
84  
85 }
86 }
87  
88 # memory
89 0 i_run "VBoxManage modifyvm \"$name\" --memory " . $opts->{memory};
90  
91 # cpus
92 0 i_run "VBoxManage modifyvm \"$name\" --cpus " . $opts->{cpus};
93  
94 # boot
95 0 i_run "VBoxManage modifyvm \"$name\" --boot1 " . $opts->{boot};
96  
97 0 return;
98 }
99  
100 0 ▶sub _set_defaults {
Subroutine _set_defaults: not called CC=7 SCAR=40.3
101 0 my ($opts) = @_;
102  
103 0 ▶ if ( !exists $opts->{"name"} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $opts->{"name"}) 0 0 0
104 0 die("You have to give a name.");
105 }
106  
107 0 ▶ if ( !exists $opts->{"storage"} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $opts->{"storage"}) 0 0 0
108 0 die("You have to add at least one storage disk.");
109 }
110  
111 0 ▶ if ( !exists $opts->{"memory"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $opts->{'memory'}) { } 0 0 0
112 0 $opts->{"memory"} = 512;
113 }
114 else {
115 # default is mega byte
116 0 $opts->{memory} = $opts->{memory};
117 }
118  
119 0 ▶ if ( !exists $opts->{"cpus"} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $opts->{"cpus"}) 0 0 0
120 0 $opts->{"cpus"} = 1;
121 }
122  
123 0 ▶ if ( !exists $opts->{"boot"} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $opts->{"boot"}) 0 0 0
124 0 $opts->{"boot"} = "disk";
125 }
126  
127 # normalize
128 0 ▶ if ( $opts->{boot} eq "hd" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"boot"} eq "hd") 0 0 0
129 0 $opts->{boot} = "disk";
130 }
131  
132 0 _set_storage_defaults($opts);
133  
134 0 _set_network_defaults($opts);
135  
136 }
137  
138 0 ▶sub _set_storage_defaults {
Subroutine _set_storage_defaults: not called CC=13 SCAR=52.0
139 0 my ($opts) = @_;
140  
141 0 my $store_letter = "a";
142 0 for my $store ( @{ $opts->{"storage"} } ) {
143  
144 0 ▶ if ( !exists $store->{"type"} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $store->{"type"}) 0 0 0
145 0 $store->{"type"} = "file";
146 }
147  
148 0 ▶ if ( $store->{type} eq "file" && !exists $store->{format} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($store->{"type"} eq "file" and not exists $store->{"format"}) 0 0 0
Condition0%0/3
$store->{"type"} eq "file" and not exists $store->{"format"}
!ll&&!rl&&r
000
MC/DC0%0/2
$store->{"type"} eq "file" and not exists $store->{"format"}
$store->{"type"} eq "file" not exists $store->{"format"}
Truth table0%0/3
A and not(B)
A= $store->{"type"} eq "file"B= not exists $store->{"format"}
ABresult
0X0
100
111
149 0 $store->{format} = "VDI";
150 }
151  
152 0 ▶ if ( !exists $store->{"size"} && $store->{"type"} eq "file" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $store->{"size"} and $store->{"type"} eq "file") 0 0 0
Condition0%0/3
not exists $store->{"size"} and $store->{"type"} eq "file"
!ll&&!rl&&r
000
MC/DC0%0/2
not exists $store->{"size"} and $store->{"type"} eq "file"
not exists $store->{"size"} $store->{"type"} eq "file"
Truth table0%0/3
not(A) and B
A= not exists $store->{"size"}B= $store->{"type"} eq "file"
ABresult
0X0
100
111
153 0 $store->{"size"} = "10G";
154 }
155  
156 0 ▶ if ( $store->{type} eq "file" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($store->{"type"} eq "file") 0 0 0
157 0 $store->{size} = _calc_size( $store->{size} );
158 }
159  
160 0 ▶ if ( exists $store->{"file"}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $store->{"file"} and $store->{"file"} =~ /\.iso$/u and not exists $store->{"device"}) 0 0 0
Condition0%0/6
exists $store->{"file"} and $store->{"file"} =~ /\.iso$/u and not exists $store->{"device"}
!ll&&!rl&&r
000
exists $store->{"file"} and $store->{"file"} =~ /\.iso$/u
!ll&&!rl&&r
000
MC/DC0%0/3
exists $store->{"file"} and $store->{"file"} =~ /\.iso$/u and not exists $store->{"device"}
exists $store->{"file"} $store->{"file"} =~ /\.iso$/u not exists $store->{"device"}
Truth table0%0/4
A and B and not(C)
A= exists $store->{"file"}B= $store->{"file"} =~ /\.iso$/uC= not exists $store->{"device"}
ABCresult
0XX0
10X0
1100
1111
161 && $store->{"file"} =~ m/\.iso$/
162 && !exists $store->{"device"} )
163 {
164 0 $store->{"device"} = "cdrom";
165 }
166  
167 0 ▶ if ( !exists $store->{"device"} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $store->{"device"}) 0 0 0
168 0 $store->{"device"} = "disk";
169 }
170  
171 0 ▶ if ( $store->{"device"} eq "cdrom" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($store->{"device"} eq "cdrom") 0 0 0
172 0 $store->{"readonly"} = 1;
173 }
174  
175 }
176  
177 }
178  
179 0 ▶sub _set_network_defaults {
Subroutine _set_network_defaults: not called CC=5 SCAR=34.0
180 0 my ( $opts, $hyper ) = @_;
181  
182 0 ▶ if ( !exists $opts->{"network"} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $opts->{"network"}) 0 0 0
183 0 $opts->{"network"} = [
184 {
185 type => "bridge",
186 bridge => "eth0",
187 },
188 ];
189 }
190  
191 0 for my $netdev ( @{ $opts->{"network"} } ) {
192  
193 0 ▶ if ( !exists $netdev->{"type"} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $netdev->{"type"}) 0 0 0
194 0 $netdev->{"type"} = "bridge";
195 }
196  
197 0 ▶ if ( !exists $netdev->{"bridge"} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $netdev->{"bridge"}) 0 0 0
198 0 $netdev->{"bridge"} = "eth0";
199 }
200  
201 }
202 }
203  
204 0 ▶sub _calc_size {
Subroutine _calc_size: not called CC=3 SCAR=24.8
205 0 my ($size) = @_;
206  
207 0 my $ret_size = 0;
208 0 ▶ if ( $size =~ m/^(\d+)G$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($size =~ /^(\d+)G$/u) { } 0 0 0
elsif ($size =~ /^(\d+)M$/u) { } 0 0 0
209 0 $ret_size = $1 * 1024;
210 }
211  
212 elsif ( $size =~ m/^(\d+)M$/ ) {
213 0 $ret_size = $1;
214 }
215 }
216  
217 1 1;
« lib/Rex/Hardware/Network/OpenBSD.pm ↑ Summary lib/Rex/Virtualization/VBox/start.pm »