Rev 33 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
#! /usr/bin/env perl
use strict;
use warnings;
# havirt
# Basically an extension of virsh which will perform actions on virtuals
# running on multiple, connected hypervisors (virsh calls them nodes)
# existing as a cluster of hypervisors where virtuals can be shut down,
# started and migrated at need.
#
# Progam consists of one executable (havirt) and multiple Perl Modules
# (*.pm), each of which encompasses a function. However, this is NOT
# written as an Object Oriented system.
#
# havirt --help gives a brief help screen.
# Copyright 2024 Daily Data, Inc.
#
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
# conditions are met:
#
# Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
# Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the distribution.
# Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
# NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
# THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# v1.2.0 20240826 RWR
# Modified the flag structure to conform to standard (ie, dryrun instead of yes)
# Added a lot of 'verbose' print lines, and modified for new flag structure
# Added a config file, which is auto-generated if it doesn't exist
#use experimental "switch";
# requires File::Slurp.
# In Debian derivatives
# apt install libfile-slurp-perl
# apt install libxml-libxml-perl libyaml-tiny-perl
BEGIN {
use FindBin;
use File::Spec;
# use libraries from the directory this script is in
use Cwd 'abs_path';
use File::Basename;
use lib dirname( abs_path( __FILE__ ) );
}
use havirt; # Load all our shared stuff
use Data::Dumper;
use YAML::Tiny;
# define the version number
# see https://metacpan.org/pod/release/JPEACOCK/version-0.97/lib/version.pod
use version;
our $VERSION = version->declare("1.2.1");
# see https://perldoc.perl.org/Getopt/Long.html
use Getopt::Long;
# allow -vvn (ie, --verbose --verbose --dryrun)
Getopt::Long::Configure ("bundling");
our $config;
my $configFileName = $FindBin::RealBin . '/config.yaml';
# Big hash that contains all information about system
our $statusDB;
sub help {
print "$0 command [argument]\n";
print "where command is one of\n";
print "\tnode [help] # work with a node\n";
print "\tdomain [help] # work with individual domains\n";
print "\tcluster [help] # report of memory and vcpu status on all nodes\n";
print "Some flags can be used where appropriate\n";
print "\t--help|-h # show this screen\n";
print "\t--version|-v # show version of program\n";
print "\t--format screen|tsv # output of list commands is either padded for screen or Tab Delim\n";
print "\t--force|f force an action, even if not normally done\n";
print "\t--target|-t NODE # the action use NODE for the target of actions\n";
print "\t--dryrun|-n # does not perform the actions, simply shows what commands would be executed\n";
print "\t--debug|d # increases verbosity, with -ddd, totally outragious\n";
print "\t--quiet|q # do not print anything except major errors\n";
}
&makeConfig( $config,$configFileName ) unless -f $configFileName;
$config = readConfig( $configFileName );
# handle any command line parameters that may have been passed in
GetOptions (
$config->{'flags'},
'debug|d+', # integer, can be incremented like -ddd
'dryrun|n!', # negatable with --nodryrun
'force|f',
'format=s', # must be --format (no short form)
'help|h',
'quiet|q',
'target|t=s',
'verbose|v+',# integer, can be incremented like -vv
'version|V' # note, the short form is a capital 'V'
) or die "Error parsing command line\n";
print "Parameters are " . Dumper( $config ) . "\n" if $config->{'flags'}->{'debug'};
die if $config->{'flags'}->{'debug'} > 3;
my $command = shift; # the first one is the actual subsection
my $action = shift; # second is action to run
$action = 'help' unless $action;
if ( $config->{'flags'}->{'help'} || $command eq 'help' || ! $command ) { &help() ; exit; }
if ( $config->{'flags'}->{'version'} ) { use File::Basename; print basename($0) . " v$VERSION\n"; exit; }
print "Command = $command\nAction = $action\n" if $config->{'flags'}->{'debug'};
# we allow a three part command for some actions on a domain, ie start, shutdown, migrate and destroy
# for simplicity, if the command is one of the above, allow the user to enter like that, but we will
# restructure the command as if they had used the full, three part (ie, with domain as the command)
# so, the following are equivilent
# havirt domain start nameofdomain
# havirt start nameofdomain
if ( $command eq 'start' || $command eq 'shutdown' || $command eq 'migrate' || $command eq 'destroy' ) { # shortcut for working with domains
push @ARGV, $action; # this is the domain we are working with
$action = $command; # this is what we want to do (start, shutdown, etc...)
$command = 'domain'; # keywork domain, for correct module
}
# ok, this is some serious weirdness. $command is actually the name of a module, and $action is a method
# defined in the module.
# I use 'require' which loads at runtime, not compile time, so it will only load a module if needed.
# then, I check to see if a method named in $action is defined within that module and, if so
# execute it and return the result. If not, gives an error message.
# This means to add functionality, we simply add a method (sub) in a given module, or create a whole
# new module.
# we have to concat here since the double colon causes some interpretation problems
my $execute = $command . '::' . $action;
# load the module, die if it doesn't exist.
if (-f $config->{'script dir'} . "/$command.pm" ) {
require "$command.pm";
Module->import( $command ); # for require, you must manually import
} else {
die "Error, I don't know the command [$command]\n";
}
if ( defined &{\&{$execute}} ) { # check if module::sub exists (ie, $command::action)
my $message = &{\&{$execute}}(@ARGV); # yes, it exists, so call it with any remaining arguments
print $message unless $main::config->{'flags'}->{'quiet'};
} else { # method $action does not exist in module $command, so just a brief error message
die "Error, could not find action [$action] for module [$command]\n";
}
1;