Subversion Repositories havirt

Rev

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