Subversion Repositories havirt

Rev

Rev 11 | Rev 14 | Go to most recent revision | Details | Compare with Previous | 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";
12 rodolico 81
our $statusDBName = "$dbDir/status.yaml";
82
our $lastScanFileName = "$scriptDir/lastscan";
83
our $minScanTimes = 5 * 60; # do not scan more than once every 5 minutes
2 rodolico 84
 
12 rodolico 85
# Big hash that contains all information about system
86
our $statusDB;
2 rodolico 87
 
4 rodolico 88
# options variables
89
our $reportFormat = 'screen';
12 rodolico 90
our $force = 0;
91
our $quiet = 0;
4 rodolico 92
our $targetNode = '';
93
our $dryRun = 1;
94
our $DEBUG = 0;
95
my $help = 0;
96
my $version = 0;
2 rodolico 97
 
98
sub help {
99
   print "$0 command [argument]\n";
100
   print "where command is one of\n";
9 rodolico 101
   print "\tnode update|list|scan # work with a node\n";
102
   print "\tdomain update|list # work with individual domains\n";
2 rodolico 103
   print "\tcluster status # report of memory and vcpu status on all nodes\n";
9 rodolico 104
   print "For additional help, run command help\n";
105
   print "\tnode help # or domain, or cluster\n";
4 rodolico 106
   print "Some flags can be used where appropriate\n";
107
   print "\t--help|-h # show this screen\n";
108
   print "\t--version|-v # show version of program\n";
109
   print "\t--format|-f screen|tsv # output of list commands is either padded for screen or Tab Delim\n";
110
   print "\t--target|-t NODE # the action use NODE for the target of actions\n";
111
   print "\t--dryrun|-n # does not perform the actions, simply shows what commands would be executed\n";
9 rodolico 112
   print "\t--debug|d # increases verbosity, with -ddd, totally outragious\n";
12 rodolico 113
   print "\t--yes|y # force an action (like scan) even if it is not a good idea\n";
114
   print "\t-quiet|q # do not print anything except major errors\n";
2 rodolico 115
}
116
 
117
 
118
 
4 rodolico 119
# handle any command line parameters that may have been passed in
120
 
121
GetOptions (
122
   'format|f=s' => \$reportFormat,
123
   'target|t=s' => \$targetNode,
124
   'dryrun|n!' => \$dryRun,
125
   'debug|d+' => \$DEBUG,
126
   'help|h' => \$help,
12 rodolico 127
   'yes|y' => \$force,
128
   'version|v' => \$version,
129
   'quiet|q' => \$quiet
4 rodolico 130
) or die "Error parsing command line\n";
131
 
6 rodolico 132
my $command = shift; # the first one is the actual subsection
133
my $action = shift; # second is action to run
4 rodolico 134
 
6 rodolico 135
if ( $help || ! $command ) { &help() ; exit; }
4 rodolico 136
if ( $version ) { use File::Basename; print basename($0) . " v$VERSION\n"; exit; }
137
 
2 rodolico 138
 
5 rodolico 139
print "Parameters are\nreportFormat\t$reportFormat\ntargetNode\t$targetNode\ndryRun\t$dryRun\nDEBUG\t$DEBUG\n" if $DEBUG;
140
print "Command = $command\nAction = $action\n" if $DEBUG;
4 rodolico 141
 
8 rodolico 142
# we allow a three part command for some actions on a domain, ie start, shutdown, migrate and destroy
143
# for simplicity, if the command is one of the above, allow the user to enter like that, but we will
144
# restructure the command as if they had used the full, three part (ie, with domain as the command)
145
# so, the following are equivilent
146
# havirt domain start nameofdomain
147
# havirt start nameofdomain
148
 
149
if ( $command eq 'start' || $command eq 'shutdown' || $command eq 'migrate' || $command eq 'destroy' ) { # shortcut for working with domains
150
   push @ARGV, $action; # this is the domain we are working with
151
   $action = $command; # this is what we want to do (start, shutdown, etc...)
152
   $command = 'domain'; # keywork domain, for correct module
2 rodolico 153
}
154
 
8 rodolico 155
# ok, this is some serious weirdness. $command is actually the name of a module, and $action is a method
156
# defined in the module.
2 rodolico 157
 
8 rodolico 158
# I use 'require' which loads at runtime, not compile time, so it will only load a module if needed.
159
# then, I check to see if a method named in $action is defined within that module and, if so
160
# execute it and return the result. If not, gives an error message.
161
 
162
# This means to add functionality, we simply add a method (sub) in a given module, or create a whole
163
# new module.
164
 
165
 
166
# we have to concat here since the double colon causes some interpretation problems
167
my $execute = $command . '::' . $action;
11 rodolico 168
# load the module, die if it doesn't exist.
169
if (-f $scriptDir . "/$command.pm" ) {
170
   require "$command.pm";
171
   Module->import( $command ); # for require, you must manually import
172
} else {
173
   die "Error, I don't know the command [$command]\n";
174
}
8 rodolico 175
 
176
if ( defined &{\&{$execute}} ) { # check if module::sub exists (ie, $command::action)
12 rodolico 177
  my $message =  &{\&{$execute}}(@ARGV); # yes, it exists, so call it with any remaining arguments
178
  print $message unless $quiet;
8 rodolico 179
} else { # method $action does not exist in module $command, so just a brief error message
11 rodolico 180
  die "Error, could not find action [$action] for module [$command]\n";
8 rodolico 181
} 
182
 
183
 
2 rodolico 184
1;