2 |
rodolico |
1 |
#! /usr/bin/env perl
|
|
|
2 |
|
|
|
3 |
use strict;
|
|
|
4 |
use warnings;
|
3 |
rodolico |
5 |
|
|
|
6 |
# havirt
|
|
|
7 |
# Basically an extension of virsh which will perform actions on virtuals
|
|
|
8 |
# running on multiple, connected hypervisors (virsh calls them nodes)
|
|
|
9 |
# existing as a cluster of hypervisors where virtuals can be shut down,
|
|
|
10 |
# started and migrated at need.
|
|
|
11 |
#
|
|
|
12 |
# Progam consists of one executable (havirt) and multiple Perl Modules
|
|
|
13 |
# (*.pm), each of which encompasses a function. However, this is NOT
|
|
|
14 |
# written as an Object Oriented system.
|
|
|
15 |
#
|
|
|
16 |
# havirt --help gives a brief help screen.
|
|
|
17 |
|
|
|
18 |
# Copyright 2024 Daily Data, Inc.
|
|
|
19 |
#
|
|
|
20 |
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
|
|
|
21 |
# conditions are met:
|
|
|
22 |
#
|
|
|
23 |
# Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
|
24 |
# Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
|
|
|
25 |
# in the documentation and/or other materials provided with the distribution.
|
|
|
26 |
# Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived
|
|
|
27 |
# from this software without specific prior written permission.
|
|
|
28 |
#
|
|
|
29 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
|
|
|
30 |
# NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
|
|
31 |
# THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
32 |
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
33 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
|
|
34 |
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
35 |
|
|
|
36 |
|
2 |
rodolico |
37 |
#use experimental "switch";
|
|
|
38 |
|
|
|
39 |
# requires File::Slurp.
|
|
|
40 |
# In Debian derivatives
|
|
|
41 |
# apt install libfile-slurp-perl
|
|
|
42 |
|
|
|
43 |
# apt install libxml-libxml-perl libyaml-tiny-perl
|
|
|
44 |
|
|
|
45 |
|
|
|
46 |
BEGIN {
|
|
|
47 |
use FindBin;
|
|
|
48 |
use File::Spec;
|
|
|
49 |
# use libraries from the directory this script is in
|
6 |
rodolico |
50 |
use Cwd 'abs_path';
|
|
|
51 |
use File::Basename;
|
|
|
52 |
use lib dirname( abs_path( __FILE__ ) );
|
2 |
rodolico |
53 |
}
|
|
|
54 |
|
3 |
rodolico |
55 |
use havirt; # Load all our shared stuff
|
|
|
56 |
|
2 |
rodolico |
57 |
use Data::Dumper;
|
|
|
58 |
use YAML::Tiny;
|
|
|
59 |
|
4 |
rodolico |
60 |
# define the version number
|
|
|
61 |
# see https://metacpan.org/pod/release/JPEACOCK/version-0.97/lib/version.pod
|
|
|
62 |
use version;
|
19 |
rodolico |
63 |
our $VERSION = version->declare("1.0.0");
|
4 |
rodolico |
64 |
|
|
|
65 |
|
|
|
66 |
# see https://perldoc.perl.org/Getopt/Long.html
|
|
|
67 |
use Getopt::Long;
|
|
|
68 |
# allow -vvn (ie, --verbose --verbose --dryrun)
|
|
|
69 |
Getopt::Long::Configure ("bundling");
|
|
|
70 |
|
|
|
71 |
|
2 |
rodolico |
72 |
# global variables
|
14 |
rodolico |
73 |
our $scriptDir = $FindBin::RealBin;
|
2 |
rodolico |
74 |
my $scriptName = $FindBin::Script;
|
|
|
75 |
my $dbDir = "$scriptDir/var";
|
9 |
rodolico |
76 |
our $confDir = "$scriptDir/conf";
|
12 |
rodolico |
77 |
our $statusDBName = "$dbDir/status.yaml";
|
|
|
78 |
our $lastScanFileName = "$scriptDir/lastscan";
|
|
|
79 |
our $minScanTimes = 5 * 60; # do not scan more than once every 5 minutes
|
2 |
rodolico |
80 |
|
12 |
rodolico |
81 |
# Big hash that contains all information about system
|
|
|
82 |
our $statusDB;
|
2 |
rodolico |
83 |
|
4 |
rodolico |
84 |
# options variables
|
|
|
85 |
our $reportFormat = 'screen';
|
12 |
rodolico |
86 |
our $force = 0;
|
|
|
87 |
our $quiet = 0;
|
4 |
rodolico |
88 |
our $targetNode = '';
|
|
|
89 |
our $dryRun = 1;
|
|
|
90 |
our $DEBUG = 0;
|
|
|
91 |
my $help = 0;
|
|
|
92 |
my $version = 0;
|
2 |
rodolico |
93 |
|
|
|
94 |
sub help {
|
|
|
95 |
print "$0 command [argument]\n";
|
|
|
96 |
print "where command is one of\n";
|
14 |
rodolico |
97 |
print "\tnode [help] # work with a node\n";
|
|
|
98 |
print "\tdomain [help] # work with individual domains\n";
|
|
|
99 |
print "\tcluster [help] # report of memory and vcpu status on all nodes\n";
|
4 |
rodolico |
100 |
print "Some flags can be used where appropriate\n";
|
|
|
101 |
print "\t--help|-h # show this screen\n";
|
|
|
102 |
print "\t--version|-v # show version of program\n";
|
|
|
103 |
print "\t--format|-f screen|tsv # output of list commands is either padded for screen or Tab Delim\n";
|
|
|
104 |
print "\t--target|-t NODE # the action use NODE for the target of actions\n";
|
|
|
105 |
print "\t--dryrun|-n # does not perform the actions, simply shows what commands would be executed\n";
|
9 |
rodolico |
106 |
print "\t--debug|d # increases verbosity, with -ddd, totally outragious\n";
|
12 |
rodolico |
107 |
print "\t--yes|y # force an action (like scan) even if it is not a good idea\n";
|
15 |
rodolico |
108 |
print "\t--quiet|q # do not print anything except major errors\n";
|
2 |
rodolico |
109 |
}
|
|
|
110 |
|
|
|
111 |
|
|
|
112 |
|
4 |
rodolico |
113 |
# handle any command line parameters that may have been passed in
|
|
|
114 |
|
|
|
115 |
GetOptions (
|
|
|
116 |
'format|f=s' => \$reportFormat,
|
|
|
117 |
'target|t=s' => \$targetNode,
|
|
|
118 |
'dryrun|n!' => \$dryRun,
|
|
|
119 |
'debug|d+' => \$DEBUG,
|
|
|
120 |
'help|h' => \$help,
|
12 |
rodolico |
121 |
'yes|y' => \$force,
|
|
|
122 |
'version|v' => \$version,
|
|
|
123 |
'quiet|q' => \$quiet
|
4 |
rodolico |
124 |
) or die "Error parsing command line\n";
|
|
|
125 |
|
6 |
rodolico |
126 |
my $command = shift; # the first one is the actual subsection
|
|
|
127 |
my $action = shift; # second is action to run
|
14 |
rodolico |
128 |
$action = 'help' unless $action;
|
4 |
rodolico |
129 |
|
14 |
rodolico |
130 |
if ( $help || $command eq 'help' || ! $command ) { &help() ; exit; }
|
4 |
rodolico |
131 |
if ( $version ) { use File::Basename; print basename($0) . " v$VERSION\n"; exit; }
|
|
|
132 |
|
2 |
rodolico |
133 |
|
5 |
rodolico |
134 |
print "Parameters are\nreportFormat\t$reportFormat\ntargetNode\t$targetNode\ndryRun\t$dryRun\nDEBUG\t$DEBUG\n" if $DEBUG;
|
|
|
135 |
print "Command = $command\nAction = $action\n" if $DEBUG;
|
4 |
rodolico |
136 |
|
8 |
rodolico |
137 |
# we allow a three part command for some actions on a domain, ie start, shutdown, migrate and destroy
|
|
|
138 |
# for simplicity, if the command is one of the above, allow the user to enter like that, but we will
|
|
|
139 |
# restructure the command as if they had used the full, three part (ie, with domain as the command)
|
|
|
140 |
# so, the following are equivilent
|
|
|
141 |
# havirt domain start nameofdomain
|
|
|
142 |
# havirt start nameofdomain
|
|
|
143 |
|
|
|
144 |
if ( $command eq 'start' || $command eq 'shutdown' || $command eq 'migrate' || $command eq 'destroy' ) { # shortcut for working with domains
|
|
|
145 |
push @ARGV, $action; # this is the domain we are working with
|
|
|
146 |
$action = $command; # this is what we want to do (start, shutdown, etc...)
|
|
|
147 |
$command = 'domain'; # keywork domain, for correct module
|
2 |
rodolico |
148 |
}
|
|
|
149 |
|
8 |
rodolico |
150 |
# ok, this is some serious weirdness. $command is actually the name of a module, and $action is a method
|
|
|
151 |
# defined in the module.
|
2 |
rodolico |
152 |
|
8 |
rodolico |
153 |
# I use 'require' which loads at runtime, not compile time, so it will only load a module if needed.
|
|
|
154 |
# then, I check to see if a method named in $action is defined within that module and, if so
|
|
|
155 |
# execute it and return the result. If not, gives an error message.
|
|
|
156 |
|
|
|
157 |
# This means to add functionality, we simply add a method (sub) in a given module, or create a whole
|
|
|
158 |
# new module.
|
|
|
159 |
|
|
|
160 |
|
|
|
161 |
# we have to concat here since the double colon causes some interpretation problems
|
|
|
162 |
my $execute = $command . '::' . $action;
|
11 |
rodolico |
163 |
# load the module, die if it doesn't exist.
|
|
|
164 |
if (-f $scriptDir . "/$command.pm" ) {
|
|
|
165 |
require "$command.pm";
|
|
|
166 |
Module->import( $command ); # for require, you must manually import
|
|
|
167 |
} else {
|
|
|
168 |
die "Error, I don't know the command [$command]\n";
|
|
|
169 |
}
|
8 |
rodolico |
170 |
|
|
|
171 |
if ( defined &{\&{$execute}} ) { # check if module::sub exists (ie, $command::action)
|
12 |
rodolico |
172 |
my $message = &{\&{$execute}}(@ARGV); # yes, it exists, so call it with any remaining arguments
|
|
|
173 |
print $message unless $quiet;
|
8 |
rodolico |
174 |
} else { # method $action does not exist in module $command, so just a brief error message
|
11 |
rodolico |
175 |
die "Error, could not find action [$action] for module [$command]\n";
|
8 |
rodolico |
176 |
}
|
|
|
177 |
|
|
|
178 |
|
2 |
rodolico |
179 |
1;
|