| 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
| |||||||||||
| 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
| |||||||||||
| 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
| |||||||||||
| 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
| |||||||||||
| 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
| |||||||||||
| 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
| |||||||||||
| 84 | 0 | die( "Error running '" . $command . "' returned... " . $returned ); | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | 0 | return $returned; | |||||||||
| 88 | } | ||||||||||
| 89 | |||||||||||
| 90 | 1 | 1; | |||||||||