clean up assorted commands, more documentation, and add pause

This commit is contained in:
Zane C. B-H 2021-10-12 04:51:29 -05:00
parent 5ad1d298ee
commit 113630f7ed
6 changed files with 315 additions and 8 deletions

View File

@ -9,7 +9,7 @@ use base qw(Rex::Virtualization::Base);
=head1 NAME
Rex::Virtualization::CBSD - The great new Rex::Virtualization::CBSD!
Rex::Virtualization::CBSD - CBSD virtualization modulem for bhyve
=head1 VERSION
@ -22,6 +22,12 @@ our $VERSION = '0.0.1';
=head1 SYNOPSIS
use Rex::Commands::Virtualization;
set virtualization => "CBSD";
use Data::Dumper;
=cut
sub new {
@ -34,6 +40,224 @@ sub new {
return $self;
}
=head1 Methods
=head2 disk_list
This returns a list of disks setup for use with Bhyve in CBSD via parsing
the output of the command below.
cbsd bhyve-dsk-list display=jname,dsk_controller,dsk_path,dsk_size,dsk_sectorsize,bootable,dsk_zfs_guid header=0
This returned data is a array of hashes.
The keys are as below.
vm - The name of the VM in question.
controller - Controller type configured for this.
path - The path to the disk.
size - Size of the disk in question.
sectorsize - size of the sectors in question.
bootable - If it is bootable. true/false
zfs_guid - ZFS GUID of the disk.
This dies upon failure.
my @disks
eval{
@disks=vm disk_list;
} or do {
my $error = $@ || 'Unknown failure';
warn('Failed to the disk list... '.$error);
}
print Dumper(\@disks);
=head2 info
This fetches the available configuration information for a VM via
the command below.
cbsd bget jname=$vm
The returned value is a flat hash of key value pairs.
my %vm_info
eval{
%vm_info=vm info => 'foo';
} or do {
my $error = $@ || 'Unknown failure';
warn('Failed to get settings for the VM... '.$error);
}
foreach my $vm_info_key (@{keys(%vm_info)}){
print $vm_info_key.": ".$vm_info{$vm_info_key}."\n";
}
=head2 list
List available VMs.
The returned array is a hash of hashes. The first level hash is the jname.
nodename - The node name that this is set to run on.
jname - Name of the VM.
jid - Jail ID/process ID of the VM if running. IF '0' it is not running.
vm_ram - Max RAM for the VM.
vm_curmem - Current RAM in use by the VM.
vm_cpus - Number of virtual CPUs.
pcpu - Current CPU usage.
vm_os_type - OS type for the VM.
ip4_addr - Expected IPv4 address for the VM.
status - Current status of the VM.
vnc - VNC address and port for the VM.
path - Path to where the VM is stored.
This dies upon failure.
my %vm_list;
eval{
%vm_list=vm list;
} or do {
my $error = $@ || 'Unknown failure';
warn('Failed to list the VM... '.$error);
}
foreach my $vm_name (@{keys( %vm_list )}){
print
"---------------------------\n".
'VM: '.$vm_name."\n".
"---------------------------\n".
'jid: '.$vm_list{$vm_name}{jid}."\n".
'vm_ram: '.$vm_list{$vm_name}{vm_ram}."\n".
'vm_curmem: '.$vm_list{$vm_name}{vm_curmem}."\n".
'vm_cpus: '.$vm_list{$vm_name}{vm_cpus}."\n".
'vm_ram: '.$vm_list{$vm_name}{pcpu}."\n".
'vm_os_type: '.$vm_list{$vm_name}{vm_os_type}."\n".
'ip4_addr: '.$vm_list{$vm_name}{ip4_addr}."\n".
'status: '.$vm_list{$vm_name}{status}."\n".
'vnc: '.$vm_list{$vm_name}{vnc}."\n".
'path: '.$vm_list{$vm_name}{path}."\n".
"\n"
}
=head2 pause
This pauses a VM in question. The following modes are available. If no
more is specified, audo is used.
auto - (by default) triggering - e.g, if vm active then pause
on - pause, stop
off - unpause, continue
The command called is as below.
cbsd bpause $vm mode=$mode
This dies upon failure.
eval{
vm pause => 'foo';
} or do {
my $error = $@ || 'Unknown failure';
warn('Failed to pause the VM foo... '.$error);
}
=head2 remove
This removes the selected VM and remove the data. This is done via the command...
cbsd bdestroy $vm
One argument is taken and that is the name of the VM.
This dies upon failure.
eval{
vm remove => 'foo'
} or do {
my $error = $@ || 'Unknown failure';
warn('Failed to remove the VM foo... '.$error);
}
=head2 restart
This restarts the selected VM. This is done via the command...
cbsd brestart $vm
One argument is taken and that is the name of the VM.
This dies upon failure.
eval{
vm restart => 'foo'
} or do {
my $error = $@ || 'Unknown failure';
warn('Failed to restart the VM foo... '.$error);
}
=head2 start
This starts a VM. This is done via the command...
cbsd bstart jname=$vm
One argument is taken and that is the name of the VM. If '*' or 'vm*' then
start all VM whose names begin with 'vm', e.g. 'vm1', 'vm2'...
This dies upon failure.
eval{
vm start => 'foo'
} or do {
my $error = $@ || 'Unknown failure';
warn('Failed to start the VM foo... '.$error);
}
=head2 stop
This stops a VM. This is done via the command below...
cbsd bstop jname=$vm [hard_timeout=$timeout] [noacpi=$noacpi]
One argument is required and that is the name of the VM.
The following options are optional.
hard_timeout - Wait N seconds (30 by default) before hard reset.
noacpi - 0,1. Set to 1 to prevent ACPI signal sending, just kill.
By default it will attempt to use ACPI to ask it to shutdown.
This dies upon failure.
eval{
vm stop => 'foo',
hard_timeout => 60;
} or do {
my $error = $@ || 'Unknown failure';
warn('Failed to stop the VM foo... '.$error);
}
=head1 AUTHOR
Zane C. Bowers-HAdley, C<< <vvelox at vvelox.net> >>
@ -41,8 +265,8 @@ Zane C. Bowers-HAdley, C<< <vvelox at vvelox.net> >>
=head1 BUGS
Please report any bugs or feature requests to C<bug-rex-virtualization-cbsd at rt.cpan.org>, or through
the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Rex-Virtualization-CBSD>. I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.
the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Rex-Virtualization-CBSD>.
I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

View File

@ -0,0 +1,55 @@
#
# (c) Zane C. Bowers-Hadley <vvelox@vvelox.net>
#
package Rex::Virtualization::CBSD::pause;
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, $name, $mode ) = @_;
# make sure we have a jname to pass
if (!defined( $name ) ){
die('No VM name defined');
}
# set the mode to auto if none is set
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'."
}
Rex::Logger::debug("CBSD VM start via cbsd brebstart ".$name);
# run it
my $returned=i_run ('cbsd bpause '.$name. ' mode='.$mode , 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');
}
if ( $? != 0 ) {
die("Error running 'cbsd brestart ".$name."'");
}
return 1;
}
1;

View File

@ -25,7 +25,7 @@ sub execute {
my %VMs;
# note
my $returned=i_run ('cbsd bremove '.$name , fail_ok => 1);
my $returned=i_run ('cbsd bdestroy '.$name , fail_ok => 1);
if ( $? != 0 ) {
die("Error running 'cbsd remove ".$name."'");
}

View File

@ -14,7 +14,9 @@ use Rex::Helper::Run;
use Term::ANSIColor qw(colorstrip);
sub execute {
my ( $class, $name ) = @_;
my ( $class, $name, %opt ) = @_;
if (!defined( $name ) ){
die('No VM name defined');

View File

@ -22,7 +22,7 @@ sub execute {
Rex::Logger::debug("CBSD VM start via cbsd bstart ".$name);
my $returned=i_run ('cbsd bstart '.$name , fail_ok => 1);
my $returned=i_run ('cbsd bstart jname='.$name , fail_ok => 1);
# the output is colorized
$returned=colorstrip($returned);
# check for failures caused by it not existing

View File

@ -14,15 +14,41 @@ use Rex::Helper::Run;
use Term::ANSIColor qw(colorstrip);
sub execute {
my ( $class, $name ) = @_;
my ( $class, $name, %opts ) = @_;
# set the hard_timeout if needed
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';
}
my $hard_timeout='hard_timeout='.$opts{hard_timeout};
}
# set the noacpi value if needed
my $noacpi='';
if ( defined( $opts{noacpi} ) ) {
# make sure we have a valid value
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};
}
# make sure we have a
if (!defined( $name ) ){
die('No VM name defined');
}
Rex::Logger::debug("CBSD VM stop via cbsd bstop ".$name);
my $returned=i_run ('cbsd bstop '.$name , fail_ok => 1);
my $returned=i_run ('cbsd bstop jname='.$name.' '.$hard_timeout.' '.$noacpi , fail_ok => 1);
# the output is colorized
$returned=colorstrip($returned);
# check for failures caused by it not existing