Subversion Repositories havirt

Rev

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