File Coverage

blib/lib/Rex/Virtualization/CBSD/bcreate.pm
Criterion Covered Total %
statement 15 36 41.6
branch 0 12 0.0
condition n/a
subroutine 5 6 83.3
pod 0 1 0.0
total 20 55 36.3


line stmt bran cond sub pod time code
1             #
2             # (c) Zane C. Bowers-Hadley
3             #
4              
5             package Rex::Virtualization::CBSD::bcreate;
6              
7 1     1   74503 use strict;
  1         11  
  1         34  
8 1     1   5 use warnings;
  1         2  
  1         58  
9              
10             our $VERSION = '0.0.1'; # VERSION
11              
12 1     1   482 use Rex::Logger;
  1         11348  
  1         64  
13 1     1   571 use Rex::Helper::Run;
  1         84361  
  1         81  
14 1     1   10 use Term::ANSIColor qw(colorstrip);
  1         3  
  1         1334  
15              
16             sub execute {
17 0     0 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 0         if ( !defined( $opts{$key} ) ) {
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 0         if ( $opts{$key} =~ /[\t\ \=\\\/\'\"\n]/ ) {
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 0         if ( defined( $opts{$key} ) ) {
59              
60             # make sure it does not contain any tabs, single/double quotes, and new lines
61 0 0         if ( $opts{$key} =~ /[\t\'\"\n]/ ) {
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 0         if ( defined( $opts{$key} ) ) {
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 0         if ( $? != 0 ) {
84 0           die( "Error running '" . $command . "' returned... " . $returned );
85             }
86              
87 0           return $returned;
88             }
89              
90             1;