perl tidy ran

This commit is contained in:
Zane C. B-H 2021-10-12 08:38:48 -05:00
parent aa7167572f
commit f0cb8d1029
9 changed files with 141 additions and 103 deletions

View File

@ -7,36 +7,46 @@ package Rex::Virtualization::CBSD::disk_list;
use strict;
use warnings;
our $VERSION = '0.0.1'; # VERSION
our $VERSION = '0.0.1'; # VERSION
use Rex::Logger;
use Rex::Helper::Run;
use Term::ANSIColor qw(colorstrip);
sub execute {
my ( $class ) = @_;
my ($class) = @_;
Rex::Logger::debug("Getting CBSD VM list of disk images via cbsd bhyve-dsk-list display=jname,dsk_controller,dsk_path,dsk_size,dsk_sectorsize,bootable,dsk_zfs_guid header=0");
Rex::Logger::debug(
"Getting CBSD VM list of disk images via cbsd bhyve-dsk-list display=jname,dsk_controller,dsk_path,dsk_size,dsk_sectorsize,bootable,dsk_zfs_guid header=0"
);
my @disks;
# header=0 is needed to avoid including code to exclude it
# if display= is changed, the parsing order needs updated
my $found=i_run ('cbsd bhyve-dsk-list display=jname,dsk_controller,dsk_path,dsk_size,dsk_sectorsize,bootable,dsk_zfs_guid header=0' , fail_ok => 1);
my $found = i_run(
'cbsd bhyve-dsk-list display=jname,dsk_controller,dsk_path,dsk_size,dsk_sectorsize,bootable,dsk_zfs_guid header=0',
fail_ok => 1
);
if ( $? != 0 ) {
die("Error running 'cbsd bhyve-dsk-list display=jname,dsk_controller,dsk_path,dsk_size,dsk_sectorsize,bootable,dsk_zfs_guid header=0'");
die(
"Error running 'cbsd bhyve-dsk-list display=jname,dsk_controller,dsk_path,dsk_size,dsk_sectorsize,bootable,dsk_zfs_guid header=0'"
);
}
# remove it here so the data can be safely used else where
$found=colorstrip($found);
$found = colorstrip($found);
my @found_lines=split(/\n/, $found);
my @found_lines = split( /\n/, $found );
foreach my $line (@found_lines) {
my %disk;
# needs to be updated if display= is ever changed
( $disk{'vm'}, $disk{'controller'}, $disk{'path'}, $disk{'size'},
$disk{'sectorsize'}, $disk{'bootable'}, $disk{'zfs_guid'} ) = split(/[\ \t]+/, $line);
(
$disk{'vm'}, $disk{'controller'}, $disk{'path'}, $disk{'size'},
$disk{'sectorsize'}, $disk{'bootable'}, $disk{'zfs_guid'}
) = split( /[\ \t]+/, $line );
push( @disks, \%disk );
}

View File

@ -7,7 +7,7 @@ package Rex::Virtualization::CBSD::info;
use strict;
use warnings;
our $VERSION = '0.0.1'; # VERSION
our $VERSION = '0.0.1'; # VERSION
use Rex::Logger;
use Rex::Helper::Run;
@ -16,29 +16,31 @@ use Term::ANSIColor qw(colorstrip);
sub execute {
my ( $class, $name ) = @_;
if (!defined( $name ) ){
if ( !defined($name) ) {
die('No VM name defined');
}
Rex::Logger::debug("CBSD VM info via cbsd bget jname=".$name);
Rex::Logger::debug( "CBSD VM info via cbsd bget jname=" . $name );
#
my $returned=i_run ('cbsd bget jname='.$name , fail_ok => 1);
my $returned = i_run( 'cbsd bget jname=' . $name, fail_ok => 1 );
# the output is colorized, if there is an error
$returned=colorstrip($returned);
if ( $returned =~ /^No\ such/ ){
die('"'.$name.'" does not exist');
$returned = colorstrip($returned);
if ( $returned =~ /^No\ such/ ) {
die( '"' . $name . '" does not exist' );
}
# check for this second as no VM will also exit non-zero
if ( $? != 0 ) {
die("Error running 'cbsd bget jname=".$name."'");
die( "Error running 'cbsd bget jname=" . $name . "'" );
}
my %info;
my @lines=split(/\n/, $returned);
my @lines = split( /\n/, $returned );
foreach my $line (@lines) {
my ( $vname, $vval )=split(/\:\ /, $line);
$info{$vname}=$vval;
my ( $vname, $vval ) = split( /\:\ /, $line );
$info{$vname} = $vval;
}
return \%info;

View File

@ -7,39 +7,48 @@ package Rex::Virtualization::CBSD::list;
use strict;
use warnings;
our $VERSION = '0.0.1'; # VERSION
our $VERSION = '0.0.1'; # VERSION
use Rex::Logger;
use Rex::Helper::Run;
use Term::ANSIColor qw(colorstrip);
sub execute {
my ( $class ) = @_;
my ($class) = @_;
Rex::Logger::debug("Getting CBSD VM list via cbsd bls display=nodename,jname,jid,vm_ram,vm_curmem,vm_cpus,pcpu,vm_os_type,ip4_addr,status,vnc,path header=0");
Rex::Logger::debug(
"Getting CBSD VM list via cbsd bls display=nodename,jname,jid,vm_ram,vm_curmem,vm_cpus,pcpu,vm_os_type,ip4_addr,status,vnc,path header=0"
);
my %VMs;
# header=0 is needed to avoid including code to exclude it
# if display= is changed, the parsing order needs updated
my $found=i_run ('cbsd bls display=nodename,jname,jid,vm_ram,vm_curmem,vm_cpus,pcpu,vm_os_type,ip4_addr,status,vnc,path header=0' , fail_ok => 1);
my $found = i_run(
'cbsd bls display=nodename,jname,jid,vm_ram,vm_curmem,vm_cpus,pcpu,vm_os_type,ip4_addr,status,vnc,path header=0',
fail_ok => 1
);
if ( $? != 0 ) {
die("Error running 'cbsd bls display=nodename,jname,jid,vm_ram,vm_curmem,vm_cpus,pcpu,vm_os_type,ip4_addr,status,vnc,path header=0'");
die(
"Error running 'cbsd bls display=nodename,jname,jid,vm_ram,vm_curmem,vm_cpus,pcpu,vm_os_type,ip4_addr,status,vnc,path header=0'"
);
}
# cbsd bls is colorized with no off mode for the color
# remove it here so the data can be safely used else where
$found=colorstrip($found);
$found = colorstrip($found);
my @found_lines=split(/\n/, $found);
my @found_lines = split( /\n/, $found );
foreach my $line (@found_lines) {
my %VM;
# needs to be updated if display= is ever changed
( $VM{'node'}, $VM{'vm'}, $VM{'pid'}, $VM{'ram'}, $VM{'curmem'},
$VM{'cpus'}, $VM{'pcpu'}, $VM{'os'}, $VM{'ip4'}, $VM{'status'},
$VM{'vnc'}, $VM{'path'} ) = split(/[\ \t]+/, $line);
$VMs{$VM{'name'}}=\%VM;
(
$VM{'node'}, $VM{'vm'}, $VM{'pid'}, $VM{'ram'}, $VM{'curmem'}, $VM{'cpus'},
$VM{'pcpu'}, $VM{'os'}, $VM{'ip4'}, $VM{'status'}, $VM{'vnc'}, $VM{'path'}
) = split( /[\ \t]+/, $line );
$VMs{ $VM{'name'} } = \%VM;
}
return \%VMs;

View File

@ -7,14 +7,14 @@ package Rex::Virtualization::CBSD::nic_list;
use strict;
use warnings;
our $VERSION = '0.0.1'; # VERSION
our $VERSION = '0.0.1'; # VERSION
use Rex::Logger;
use Rex::Helper::Run;
use Term::ANSIColor qw(colorstrip);
sub execute {
my ( $class ) = @_;
my ($class) = @_;
Rex::Logger::debug("Getting CBSD NIC list");
@ -22,21 +22,29 @@ sub execute {
# header=0 is needed to avoid including code to exclude it
# if display= is changed, the parsing order needs updated
my $found=i_run ('cbsd bhyve-nic-list display=jname,nic_driver,nic_type,nic_parent,nic_hwaddr,nic_address,nic_mtu,nic_persistent,nic_ratelimit header=0' , fail_ok => 1);
my $found = i_run(
'cbsd bhyve-nic-list display=jname,nic_driver,nic_type,nic_parent,nic_hwaddr,nic_address,nic_mtu,nic_persistent,nic_ratelimit header=0',
fail_ok => 1
);
if ( $? != 0 ) {
die("Error running 'cbsd bhyve-nic-list display=jname,nic_driver,nic_type,nic_parent,nic_hwaddr,nic_address,nic_mtu,nic_persistent,nic_ratelimit header=0'");
die(
"Error running 'cbsd bhyve-nic-list display=jname,nic_driver,nic_type,nic_parent,nic_hwaddr,nic_address,nic_mtu,nic_persistent,nic_ratelimit header=0'"
);
}
# remove it here so the data can be safely used else where
$found=colorstrip($found);
$found = colorstrip($found);
my @found_lines=split(/\n/, $found);
my @found_lines = split( /\n/, $found );
foreach my $line (@found_lines) {
my %nic;
# needs to be updated if display= is ever changed
( $nic{'vm'}, $nic{'driver'}, $nic{'type'}, $nic{'parent'},
$nic{'hwaddr'}, $nic{'address'}, $nic{'mtu'}, $nic{'persistent'}, $nic{'ratelimit'} ) = split(/[\ \t]+/, $line);
(
$nic{'vm'}, $nic{'driver'}, $nic{'type'}, $nic{'parent'}, $nic{'hwaddr'},
$nic{'address'}, $nic{'mtu'}, $nic{'persistent'}, $nic{'ratelimit'}
) = split( /[\ \t]+/, $line );
push( @nics, \%nic );
}

View File

@ -7,7 +7,7 @@ package Rex::Virtualization::CBSD::pause;
use strict;
use warnings;
our $VERSION = '0.0.1'; # VERSION
our $VERSION = '0.0.1'; # VERSION
use Rex::Logger;
use Rex::Helper::Run;
@ -17,36 +17,37 @@ sub execute {
my ( $class, $name, $mode ) = @_;
# make sure we have a jname to pass
if (!defined( $name ) ){
if ( !defined($name) ) {
die('No VM name defined');
}
# set the mode to auto if none is set
if (!defined( $mode )) {
$mode='auto';
if ( !defined($mode) ) {
$mode = 'auto';
}
# make sure mode is something valid
if (
( $mode eq 'auto' ) ||
( $mode eq 'on' ) ||
( $mode eq 'off' )
) {
die "Mode specified is something other than 'auto', 'on', or 'off'."
if ( ( $mode eq 'auto' )
|| ( $mode eq 'on' )
|| ( $mode eq 'off' ) )
{
die "Mode specified is something other than 'auto', 'on', or 'off'.";
}
Rex::Logger::debug("CBSD VM start via cbsd brebstart ".$name);
Rex::Logger::debug( "CBSD VM start via cbsd brebstart " . $name );
# run it
my $returned=i_run ('cbsd bpause '.$name. ' mode='.$mode , fail_ok => 1);
my $returned = i_run( 'cbsd bpause ' . $name . ' mode=' . $mode, fail_ok => 1 );
# the output is colorized
$returned=colorstrip($returned);
$returned = colorstrip($returned);
# check for failures caused by it not existing
if ( $returned =~ /^No\ such/ ){
die('"'.$name.'" does not exist');
if ( $returned =~ /^No\ such/ ) {
die( '"' . $name . '" does not exist' );
}
if ( $? != 0 ) {
die("Error running 'cbsd brestart ".$name."'");
die( "Error running 'cbsd brestart " . $name . "'" );
}
return 1;

View File

@ -7,7 +7,7 @@ package Rex::Virtualization::CBSD::remove;
use strict;
use warnings;
our $VERSION = '0.0.1'; # VERSION
our $VERSION = '0.0.1'; # VERSION
use Rex::Logger;
use Rex::Helper::Run;
@ -16,26 +16,26 @@ use Term::ANSIColor qw(colorstrip);
sub execute {
my ( $class, $name ) = @_;
if (!defined( $name ) ){
if ( !defined($name) ) {
die('No VM name defined');
}
Rex::Logger::debug("CBSD VM remove via cbsd bremove ".$name);
Rex::Logger::debug( "CBSD VM remove via cbsd bremove " . $name );
my %VMs;
# note
my $returned=i_run ('cbsd bdestroy '.$name , fail_ok => 1);
my $returned = i_run( 'cbsd bdestroy ' . $name, fail_ok => 1 );
if ( $? != 0 ) {
die("Error running 'cbsd remove ".$name."'");
die( "Error running 'cbsd remove " . $name . "'" );
}
# the output is colorized
$returned=colorstrip($returned);
$returned = colorstrip($returned);
# as of CBSD 12.1.7, it won't exit non-zero for this, so check here
if ( $returned =~ /^No\ such/ ){
die('"'.$name.'" does not exist');
if ( $returned =~ /^No\ such/ ) {
die( '"' . $name . '" does not exist' );
}
return 1;

View File

@ -7,7 +7,7 @@ package Rex::Virtualization::CBSD::restart;
use strict;
use warnings;
our $VERSION = '0.0.1'; # VERSION
our $VERSION = '0.0.1'; # VERSION
use Rex::Logger;
use Rex::Helper::Run;
@ -16,23 +16,23 @@ use Term::ANSIColor qw(colorstrip);
sub execute {
my ( $class, $name, %opt ) = @_;
if (!defined( $name ) ){
if ( !defined($name) ) {
die('No VM name defined');
}
Rex::Logger::debug("CBSD VM start via cbsd brebstart ".$name);
Rex::Logger::debug( "CBSD VM start via cbsd brebstart " . $name );
my $returned = i_run( 'cbsd brestart ' . $name, fail_ok => 1 );
my $returned=i_run ('cbsd brestart '.$name , fail_ok => 1);
# the output is colorized
$returned=colorstrip($returned);
$returned = colorstrip($returned);
# check for failures caused by it not existing
if ( $returned =~ /^No\ such/ ){
die('"'.$name.'" does not exist');
if ( $returned =~ /^No\ such/ ) {
die( '"' . $name . '" does not exist' );
}
if ( $? != 0 ) {
die("Error running 'cbsd brestart ".$name."'");
die( "Error running 'cbsd brestart " . $name . "'" );
}
return 1;

View File

@ -7,7 +7,7 @@ package Rex::Virtualization::CBSD::start;
use strict;
use warnings;
our $VERSION = '0.0.1'; # VERSION
our $VERSION = '0.0.1'; # VERSION
use Rex::Logger;
use Rex::Helper::Run;
@ -16,26 +16,30 @@ use Term::ANSIColor qw(colorstrip);
sub execute {
my ( $class, $name ) = @_;
if (!defined( $name ) ){
if ( !defined($name) ) {
die('No VM name defined');
}
Rex::Logger::debug("CBSD VM start via cbsd bstart ".$name);
Rex::Logger::debug( "CBSD VM start via cbsd bstart " . $name );
my $returned = i_run( 'cbsd bstart jname=' . $name, fail_ok => 1 );
my $returned=i_run ('cbsd bstart jname='.$name , fail_ok => 1);
# the output is colorized
$returned=colorstrip($returned);
$returned = colorstrip($returned);
# check for failures caused by it not existing
if ( $returned =~ /^No\ such/ ){
die('"'.$name.'" does not exist');
if ( $returned =~ /^No\ such/ ) {
die( '"' . $name . '" does not exist' );
}
# check for failures caused by it already running
if ( $returned =~ /already\ running/ ){
die('"'.$name.'" is already running');
if ( $returned =~ /already\ running/ ) {
die( '"' . $name . '" is already running' );
}
# test after no such as that will also exit non-zero
if ( $? != 0 ) {
die("Error running 'cbsd bstart ".$name."'");
die( "Error running 'cbsd bstart " . $name . "'" );
}
return 1;

View File

@ -7,7 +7,7 @@ package Rex::Virtualization::CBSD::stop;
use strict;
use warnings;
our $VERSION = '0.0.1'; # VERSION
our $VERSION = '0.0.1'; # VERSION
use Rex::Logger;
use Rex::Helper::Run;
@ -17,52 +17,56 @@ sub execute {
my ( $class, $name, %opts ) = @_;
# set the hard_timeout if needed
my $hard_timeout='';
my $hard_timeout = '';
if ( defined( $opts{hard_timeout} ) ) {
# make sure we have a valid value
if ( $opts{hard_timeout} !~ /^[0123456789]+$/ ) {
die 'hard_timeout value,"'.$opts{hard_timeout}.'", is not numeric';
die 'hard_timeout value,"' . $opts{hard_timeout} . '", is not numeric';
}
my $hard_timeout='hard_timeout='.$opts{hard_timeout};
my $hard_timeout = 'hard_timeout=' . $opts{hard_timeout};
}
# set the noacpi value if needed
my $noacpi='';
my $noacpi = '';
if ( defined( $opts{noacpi} ) ) {
# make sure we have a valid value
if (
( $opts{noacpi} ne '0' ) &&
( $opts{noacpi} ne '1' )
) {
if ( ( $opts{noacpi} ne '0' )
&& ( $opts{noacpi} ne '1' ) )
{
die 'noacpi is set and it is not equal to "0" or "1"';
}
$noacpi='noacpi='.$opts{noacpi};
$noacpi = 'noacpi=' . $opts{noacpi};
}
# make sure we have a
if (!defined( $name ) ){
# make sure we have a
if ( !defined($name) ) {
die('No VM name defined');
}
Rex::Logger::debug("CBSD VM stop via cbsd bstop ".$name);
Rex::Logger::debug( "CBSD VM stop via cbsd bstop " . $name );
my $returned = i_run( 'cbsd bstop jname=' . $name . ' ' . $hard_timeout . ' ' . $noacpi, fail_ok => 1 );
my $returned=i_run ('cbsd bstop jname='.$name.' '.$hard_timeout.' '.$noacpi , fail_ok => 1);
# the output is colorized
$returned=colorstrip($returned);
$returned = colorstrip($returned);
# check for failures caused by it not existing
if ( $returned =~ /^No\ such/ ){
die('"'.$name.'" does not exist');
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 'cbsd bstop ".$name."'");
die( "Error running 'cbsd bstop " . $name . "'" );
}
# 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."'");
if ( $returned =~ /unable\ to\ determine\ bhyve\ pid/ ) {
die( "Either already stopped or other issue determining bhyve PID for '" . $name . "'" );
}
return 1;