CBSD/bcreate.pm

filter: Statementstmt 44.7% 17 / 38 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 83.3% 5 / 6 Podpod 0.0% 0 / 1 totaltotal 38.6% 22 / 57 CC 9 SCAR 40.6
CC
9
Cov
34%
execute
90.0
BEGIN
1.0
BEGIN
1.0
CRAP 58.2
« CBSD/bclone.pm ↑ Summary CBSD/vm_os_profiles_hash.pm »
1 #
2 # (c) Zane C. Bowers-Hadley <vvelox@vvelox.net>
3 #
4  
5 package Rex::Virtualization::CBSD::bcreate;
6  
7 1 ▶use strict;
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 = '0.0.1'; # VERSION
11  
12 1 ▶use Rex::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Rex::Helper::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Term::ANSIColor qw(colorstrip);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 0 ▶sub execute {
Subroutine execute: not called CC=9 SCAR=45.0
Pod undocumented
17 0 my ( $class, %opts ) = @_;
18  
19 #make sure we have the very minimum needed
20 # also that each value is sane
21 0 my @required_keys = ( 'name', 'vm_os_type', 'vm_os_profile' );
22 0 foreach my $key (@required_keys) {
23 0 ▶ if ( !defined( $opts{$key} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opts{$key}) 0 0 0
24 0 die 'Required key "' . $key . '" not defined';
25 }
26  
27 # make sure it does not contain any tabs, spaces, =, \. /, ', ", or new lines.
28 0 ▶ if ( $opts{$key} =~ /[\t\ \=\\\/\'\"\n]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{$key} =~ m[[\t\ \=\\/\'\"\n]]) 0 0 0
29 die 'The value "'
30 0 . $opts{$key}
31 . '" for key "'
32 . $key
33 . '" matched /[\t\ \=\/\\\'\"\n]/, meaning it is not a valid value';
34 }
35 }
36  
37 my $command
38 = 'env NOINTER=1 cbsd bcreate jname="'
39 . $opts{name}
40 . '" vm_os_type="'
41 . $opts{vm_os_type}
42 . '" vm_os_profile="'
43 . $opts{vm_os_profile}
44 0 . '" inter=0';
45  
46 # the variables to check for.
47 0 my @variables = (
48 'bhyve_vnc_tcp_bind', 'imgsize', 'interface2', 'nic_flags',
49 'nic_flags2', 'quiet', 'removejconf', 'runasap',
50 'vm_cpus', 'vm_ram', 'zfs_snapsrc', 'ci_gw4',
51 'ci_interface2', 'ci_interface_mtu', 'ci_interface_mtu2', 'ci_ip4_addr',
52 'ci_ip4_addr2', 'ci_user_pubkey', 'ci_user_pw_user', 'ci_user_pw_root'
53 );
54  
55 # add each found variable to the command
56 0 foreach my $key (@variables) {
57  
58 0 ▶ if ( defined( $opts{$key} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{$key}) 0 0 0
59  
60 # make sure it does not contain any tabs, single/double quotes, and new lines
61 0 ▶ if ( $opts{$key} =~ /[\t\'\"\n]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{$key} =~ /[\t\'\"\n]/) 0 0 0
62 die 'The value "'
63 0 . $opts{$key}
64 . '" for key "'
65 . $key
66 . '" matched /[\t\'\"\n]/, meaning it is not a valid value';
67 }
68  
69 0 ▶ if ( defined( $opts{$key} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{$key}) 0 0 0
70 0 $command = $command.' ' . $key . '="' . $opts{$key} . '"';
71 }
72 }
73 }
74  
75 0 Rex::Logger::debug( "Creating a new CBSD VM via... " . $command );
76  
77 0 my $returned = i_run( $command, fail_ok => 1 );
78  
79 # the output is colorized
80 0 $returned = colorstrip($returned);
81  
82 # test after no such as that will also exit non-zero
83 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
84 0 die( "Error running '" . $command . "' returned... " . $returned );
85 }
86  
87 0 return $returned;
88 }
89  
90 1 1;
« CBSD/bclone.pm ↑ Summary CBSD/vm_os_profiles_hash.pm »