Subversion Repositories havirt

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
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
 
6 rodolico 55
 
56
 
57
 
58
 
3 rodolico 59
use havirt; # Load all our shared stuff
60
 
2 rodolico 61
use Data::Dumper;
62
use YAML::Tiny;
63
 
4 rodolico 64
# define the version number
65
# see https://metacpan.org/pod/release/JPEACOCK/version-0.97/lib/version.pod
66
use version;
67
our $VERSION = version->declare("0.0.1");
68
 
69
 
70
# see https://perldoc.perl.org/Getopt/Long.html
71
use Getopt::Long;
72
# allow -vvn (ie, --verbose --verbose --dryrun)
73
Getopt::Long::Configure ("bundling");
74
 
75
 
2 rodolico 76
# global variables
77
my $scriptDir = $FindBin::RealBin;
78
my $scriptName = $FindBin::Script;
79
my $dbDir = "$scriptDir/var";
9 rodolico 80
our $confDir = "$scriptDir/conf";
3 rodolico 81
our $nodeDBName = "$dbDir/node.yaml";
4 rodolico 82
our $domainDBName = "$dbDir/domains.yaml";
3 rodolico 83
our $nodePopulationDBName = "$dbDir/node_population.yaml";
2 rodolico 84
 
85
# these contain the values from the databases
86
# loaded on demand
3 rodolico 87
our $nodeDB;
88
our $virtDB;
89
our $nodePopulations;
2 rodolico 90
 
4 rodolico 91
# options variables
92
our $reportFormat = 'screen';
93
our $targetNode = '';
94
our $dryRun = 1;
95
our $DEBUG = 0;
96
my $help = 0;
97
my $version = 0;
2 rodolico 98
 
99
sub help {
100
   print "$0 command [argument]\n";
101
   print "where command is one of\n";
9 rodolico 102
   print "\tnode update|list|scan # work with a node\n";
103
   print "\tdomain update|list # work with individual domains\n";
2 rodolico 104
   print "\tcluster status # report of memory and vcpu status on all nodes\n";
9 rodolico 105
   print "For additional help, run command help\n";
106
   print "\tnode help # or domain, or cluster\n";
4 rodolico 107
   print "Some flags can be used where appropriate\n";
108
   print "\t--help|-h # show this screen\n";
109
   print "\t--version|-v # show version of program\n";
110
   print "\t--format|-f screen|tsv # output of list commands is either padded for screen or Tab Delim\n";
111
   print "\t--target|-t NODE # the action use NODE for the target of actions\n";
112
   print "\t--dryrun|-n # does not perform the actions, simply shows what commands would be executed\n";
9 rodolico 113
   print "\t--debug|d # increases verbosity, with -ddd, totally outragious\n";
2 rodolico 114
}
115
 
116
 
117
 
4 rodolico 118
# handle any command line parameters that may have been passed in
119
 
120
GetOptions (
121
   'format|f=s' => \$reportFormat,
122
   'target|t=s' => \$targetNode,
123
   'dryrun|n!' => \$dryRun,
124
   'debug|d+' => \$DEBUG,
125
   'help|h' => \$help,
126
   'version|v' => \$version
127
) or die "Error parsing command line\n";
128
 
6 rodolico 129
my $command = shift; # the first one is the actual subsection
130
my $action = shift; # second is action to run
4 rodolico 131
 
6 rodolico 132
if ( $help || ! $command ) { &help() ; exit; }
4 rodolico 133
if ( $version ) { use File::Basename; print basename($0) . " v$VERSION\n"; exit; }
134
 
2 rodolico 135
 
5 rodolico 136
print "Parameters are\nreportFormat\t$reportFormat\ntargetNode\t$targetNode\ndryRun\t$dryRun\nDEBUG\t$DEBUG\n" if $DEBUG;
137
print "Command = $command\nAction = $action\n" if $DEBUG;
4 rodolico 138
 
8 rodolico 139
# we allow a three part command for some actions on a domain, ie start, shutdown, migrate and destroy
140
# for simplicity, if the command is one of the above, allow the user to enter like that, but we will
141
# restructure the command as if they had used the full, three part (ie, with domain as the command)
142
# so, the following are equivilent
143
# havirt domain start nameofdomain
144
# havirt start nameofdomain
145
 
146
if ( $command eq 'start' || $command eq 'shutdown' || $command eq 'migrate' || $command eq 'destroy' ) { # shortcut for working with domains
147
   push @ARGV, $action; # this is the domain we are working with
148
   $action = $command; # this is what we want to do (start, shutdown, etc...)
149
   $command = 'domain'; # keywork domain, for correct module
2 rodolico 150
}
151
 
8 rodolico 152
# ok, this is some serious weirdness. $command is actually the name of a module, and $action is a method
153
# defined in the module.
2 rodolico 154
 
8 rodolico 155
# I use 'require' which loads at runtime, not compile time, so it will only load a module if needed.
156
# then, I check to see if a method named in $action is defined within that module and, if so
157
# execute it and return the result. If not, gives an error message.
158
 
159
# This means to add functionality, we simply add a method (sub) in a given module, or create a whole
160
# new module.
161
 
162
 
163
# we have to concat here since the double colon causes some interpretation problems
164
my $execute = $command . '::' . $action;
11 rodolico 165
# load the module, die if it doesn't exist.
166
if (-f $scriptDir . "/$command.pm" ) {
167
   require "$command.pm";
168
   Module->import( $command ); # for require, you must manually import
169
} else {
170
   die "Error, I don't know the command [$command]\n";
171
}
8 rodolico 172
 
173
if ( defined &{\&{$execute}} ) { # check if module::sub exists (ie, $command::action)
174
  print &{\&{$execute}}(@ARGV); # yes, it exists, so call it with any remaining arguments
175
} else { # method $action does not exist in module $command, so just a brief error message
11 rodolico 176
  die "Error, could not find action [$action] for module [$command]\n";
8 rodolico 177
} 
178
 
179
 
2 rodolico 180
1;