Subversion Repositories havirt

Rev

Rev 42 | 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.
26 rodolico 35
#
36
# v1.2.0 20240826 RWR
37
# Modified the flag structure to conform to standard (ie, dryrun instead of yes)
38
# Added a lot of 'verbose' print lines, and modified for new flag structure
39
# Added a config file, which is auto-generated if it doesn't exist
42 rodolico 40
#
41
# v1.2.1 20250511 RWR
42
# changed so even if quiet set, if dryrun is set, will print what is returned from the called script
43
# Also, incorporates v1.3.0 on cluster.pm and v1.2.1 on havirt.pm
44 rodolico 44
#
45
# v1.2.2 20250511 RWR
46
# Added variance flag (--variance), and fixed the version flag (--version or -V)
3 rodolico 47
 
2 rodolico 48
#use experimental "switch";
49
 
50
# requires File::Slurp. 
51
# In Debian derivatives
52
# apt install libfile-slurp-perl
53
 
54
# apt install libxml-libxml-perl libyaml-tiny-perl
55
 
56
 
57
BEGIN {
58
   use FindBin;
59
   use File::Spec;
60
   # use libraries from the directory this script is in
6 rodolico 61
   use Cwd 'abs_path';
62
   use File::Basename;
63
   use lib dirname( abs_path( __FILE__ ) );
2 rodolico 64
}
65
 
3 rodolico 66
use havirt; # Load all our shared stuff
67
 
2 rodolico 68
use Data::Dumper;
69
use YAML::Tiny;
70
 
4 rodolico 71
# define the version number
72
# see https://metacpan.org/pod/release/JPEACOCK/version-0.97/lib/version.pod
73
use version;
44 rodolico 74
our $VERSION = version->declare("1.2.2");
4 rodolico 75
 
76
 
77
# see https://perldoc.perl.org/Getopt/Long.html
78
use Getopt::Long;
79
# allow -vvn (ie, --verbose --verbose --dryrun)
80
Getopt::Long::Configure ("bundling");
81
 
82
 
25 rodolico 83
our $config;
84
my $configFileName = $FindBin::RealBin . '/config.yaml';
2 rodolico 85
 
12 rodolico 86
# Big hash that contains all information about system
87
our $statusDB;
2 rodolico 88
 
89
sub help {
90
   print "$0 command [argument]\n";
91
   print "where command is one of\n";
14 rodolico 92
   print "\tnode [help] # work with a node\n";
93
   print "\tdomain [help] # work with individual domains\n";
94
   print "\tcluster [help] # report of memory and vcpu status on all nodes\n";
4 rodolico 95
   print "Some flags can be used where appropriate\n";
96
   print "\t--help|-h # show this screen\n";
97
   print "\t--version|-v # show version of program\n";
26 rodolico 98
   print "\t--format screen|tsv # output of list commands is either padded for screen or Tab Delim\n";
99
   print "\t--force|f force an action, even if not normally done\n";
4 rodolico 100
   print "\t--target|-t NODE # the action use NODE for the target of actions\n";
101
   print "\t--dryrun|-n # does not perform the actions, simply shows what commands would be executed\n";
9 rodolico 102
   print "\t--debug|d # increases verbosity, with -ddd, totally outragious\n";
15 rodolico 103
   print "\t--quiet|q # do not print anything except major errors\n";
42 rodolico 104
   print "\t--verbose|v # increase verbosity of program. May be used multiple times\n";
44 rodolico 105
   print "\t--variance int # Real time adjust variance for cluster balance\n";
2 rodolico 106
}
107
 
25 rodolico 108
&makeConfig( $config,$configFileName ) unless -f $configFileName;
109
$config = readConfig( $configFileName );
2 rodolico 110
 
4 rodolico 111
# handle any command line parameters that may have been passed in
112
 
113
GetOptions (
25 rodolico 114
   $config->{'flags'}, 
26 rodolico 115
      'debug|d+',  # integer, can be incremented like -ddd
116
      'dryrun|n!', # negatable with --nodryrun
117
      'force|f',
118
      'format=s',  # must be --format (no short form)
119
      'help|h', 
120
      'quiet|q',
25 rodolico 121
      'target|t=s',
41 rodolico 122
      'testing',
26 rodolico 123
      'verbose|v+',# integer, can be incremented like -vv
44 rodolico 124
      'version|V',  # note, the short form is a capital 'V'
125
      'variance=f' # adjust variance for cluster balance
4 rodolico 126
) or die "Error parsing command line\n";
127
 
41 rodolico 128
# if they pass the testing flag, turn verbose on, dry run on, and append
129
# .testing to the status db filename
130
if ( $config->{'flags'}->{'testing'} ) { 
131
   $config->{'flags'}->{'verbose'} = 1;
132
   $config->{'flags'}->{'dryrun'} = 1; 
133
   $config->{'status db filename'} .= '.testing';
134
}
135
 
33 rodolico 136
print "Parameters are " . Dumper( $config ) . "\n" if $config->{'flags'}->{'debug'};
137
die if $config->{'flags'}->{'debug'} > 3;
25 rodolico 138
 
44 rodolico 139
if ( $config->{'flags'}->{'version'} ) { use File::Basename; print basename($0) . " v$VERSION\n"; exit; }
140
 
141
# adjust variance if they passed in the flag
142
$config->{'balance_max_variance'} = $config->{'flags'}->{'variance'} if $config->{'flags'}->{'variance'};
143
 
6 rodolico 144
my $command = shift; # the first one is the actual subsection
145
my $action = shift; # second is action to run
14 rodolico 146
$action = 'help' unless $action;
44 rodolico 147
 
25 rodolico 148
if ( $config->{'flags'}->{'help'} || $command eq 'help' || ! $command ) { &help() ; exit; }
4 rodolico 149
 
44 rodolico 150
 
25 rodolico 151
print "Command = $command\nAction = $action\n" if $config->{'flags'}->{'debug'};
4 rodolico 152
 
26 rodolico 153
 
8 rodolico 154
# we allow a three part command for some actions on a domain, ie start, shutdown, migrate and destroy
155
# for simplicity, if the command is one of the above, allow the user to enter like that, but we will
156
# restructure the command as if they had used the full, three part (ie, with domain as the command)
157
# so, the following are equivilent
158
# havirt domain start nameofdomain
159
# havirt start nameofdomain
160
 
161
if ( $command eq 'start' || $command eq 'shutdown' || $command eq 'migrate' || $command eq 'destroy' ) { # shortcut for working with domains
162
   push @ARGV, $action; # this is the domain we are working with
163
   $action = $command; # this is what we want to do (start, shutdown, etc...)
164
   $command = 'domain'; # keywork domain, for correct module
2 rodolico 165
}
166
 
8 rodolico 167
# ok, this is some serious weirdness. $command is actually the name of a module, and $action is a method
168
# defined in the module.
2 rodolico 169
 
8 rodolico 170
# I use 'require' which loads at runtime, not compile time, so it will only load a module if needed.
171
# then, I check to see if a method named in $action is defined within that module and, if so
172
# execute it and return the result. If not, gives an error message.
173
 
174
# This means to add functionality, we simply add a method (sub) in a given module, or create a whole
175
# new module.
176
 
177
 
178
# we have to concat here since the double colon causes some interpretation problems
179
my $execute = $command . '::' . $action;
42 rodolico 180
 
11 rodolico 181
# load the module, die if it doesn't exist.
25 rodolico 182
if (-f $config->{'script dir'} . "/$command.pm" ) {
11 rodolico 183
   require "$command.pm";
184
   Module->import( $command ); # for require, you must manually import
185
} else {
186
   die "Error, I don't know the command [$command]\n";
187
}
8 rodolico 188
 
189
if ( defined &{\&{$execute}} ) { # check if module::sub exists (ie, $command::action)
12 rodolico 190
  my $message =  &{\&{$execute}}(@ARGV); # yes, it exists, so call it with any remaining arguments
44 rodolico 191
  print $message unless !$message || $main::config->{'flags'}->{'quiet'} && !$main::config->{'flags'}->{'dryrun'};
8 rodolico 192
} else { # method $action does not exist in module $command, so just a brief error message
11 rodolico 193
  die "Error, could not find action [$action] for module [$command]\n";
8 rodolico 194
} 
195
 
2 rodolico 196
1;