Subversion Repositories havirt

Rev

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