add create and cleanup the set command a bit

This commit is contained in:
Zane C. B-H 2021-10-13 07:53:11 -05:00
parent 0a5ac9db7d
commit 56693d02aa
2 changed files with 99 additions and 11 deletions

View File

@ -0,0 +1,98 @@
#
# (c) Zane C. Bowers-Hadley <vvelox@vvelox.net>
#
package Rex::Virtualization::CBSD::create;
use strict;
use warnings;
our $VERSION = '0.0.1'; # VERSION
use Rex::Logger;
use Rex::Helper::Run;
use Term::ANSIColor qw(colorstrip);
sub execute {
my ( $class, %opts ) = @_;
#make sure we have the very minimum needed
# also that each value is sane
my @required_keys = ( 'name', 'vm_os_type', 'vm_os_profile' );
foreach my $key (@required_keys) {
if ( !defined( $opts{$key} ) ) {
die 'Required key "' . $key . '" not defined';
}
# make sure it does not contain any tabs, spaces, =, \. /, ', ", or new lines.
if ( $opts{$key} =~ /[\t\ \=\\\/\'\"\n]/ ) {
die 'The value "'
. $opts{$key}
. '" for key "'
. $key
. '" matched /[\t\ \=\/\\\'\"\n]/, meaning it is not a valid value';
}
}
# get where CBSD is installed to
my %cbsd;
eval { %cbsd = get_user('cbsd'); } or do {
my $error = $@ || 'Unknown failure';
die( "get_user('cbsd') died with... " . $error );
};
my $command
= 'cbsd bcreate jname="'
. $opts{name}
. '" vm_os_type="'
. $opts{vm_os_type}
. '" vm_os_profile="'
. $opts{vm_os_profile}.'"';
# the variables to check for.
my @variables=(
'bhyve_vnc_tcp_bind',
'imgsize',
'inter',
'interface2',
'nic_flags',
'nic_flags2',
'quiet',
'removejconf',
'runasap',
'vm_cpus',
'vm_ram',
'zfs_snapsrc'
);
# add each found variable to the command
foreach my $key (@variables) {
# make sure it does not contain any tabs, spaces, =, \. /, ', ", or new lines.
if ( $opts{$key} =~ /[\t\ \=\\\/\'\"\n]/ ) {
die 'The value "'
. $opts{$key}
. '" for key "'
. $key
. '" matched /[\t\ \=\/\\\'\"\n]/, meaning it is not a valid value';
}
if (defined($opts{$key})) {
$command=' '.$key.'="'.$opts{$key}.'"';
}
}
Rex::Logger::debug("Creating a new CBSD VM via... ".$command);
my $returned = i_run( $command, fail_ok => 1 );
# the output is colorized
$returned = colorstrip($returned);
# test after no such as that will also exit non-zero
if ( $? != 0 ) {
die( "Error running '" . $command . "' returned... " . $returned );
}
return $returned;
}
1;

View File

@ -40,28 +40,18 @@ sub execute {
my $command = 'cbsd bset jname=' . $name . $to_set;
Rex::Logger::debug( "CBSD VM stop via " . $command );
Rex::Logger::debug( "Setting config value for a CBSD VM via... " . $command );
my $returned = i_run( $command, fail_ok => 1 );
# the output is colorized
$returned = colorstrip($returned);
# check for failures caused by it not existing
if ( $returned =~ /^No\ such/ ) {
die( '"' . $name . '" does not exist' );
}
# test after no such as that will also exit non-zero
if ( $? != 0 ) {
die( "Error running '" . $command . "' returned... " . $returned );
}
# this is warning message will be thrown if stop fails.... does not return 0 though
if ( $returned =~ /unable\ to\ determine\ bhyve\ pid/ ) {
die( "Either already stopped or other issue determining bhyve PID for '" . $name . "'" );
}
return 1;
}