5 |
rodolico |
1 |
#!/usr/bin/env perl
|
|
|
2 |
|
|
|
3 |
# All functions related to maniplating domains
|
|
|
4 |
# part of havirt.
|
|
|
5 |
|
|
|
6 |
# Copyright 2024 Daily Data, Inc.
|
|
|
7 |
#
|
|
|
8 |
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
|
|
|
9 |
# conditions are met:
|
|
|
10 |
#
|
|
|
11 |
# Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
|
12 |
# Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
|
|
|
13 |
# in the documentation and/or other materials provided with the distribution.
|
|
|
14 |
# Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived
|
|
|
15 |
# from this software without specific prior written permission.
|
|
|
16 |
#
|
|
|
17 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
|
|
|
18 |
# NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
|
|
19 |
# THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
20 |
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
21 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
|
|
22 |
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
23 |
|
|
|
24 |
# v0.0.1 20240602 RWR
|
|
|
25 |
# Initial setup
|
26 |
rodolico |
26 |
#
|
|
|
27 |
# v1.2.0 20240826 RWR
|
|
|
28 |
# Added some code to migrate domains if node placed in maintenance mode
|
|
|
29 |
# Added a lot of 'verbose' print lines, and modified for new flag structure
|
|
|
30 |
#
|
5 |
rodolico |
31 |
|
26 |
rodolico |
32 |
|
5 |
rodolico |
33 |
package domain;
|
|
|
34 |
|
|
|
35 |
use warnings;
|
|
|
36 |
use strict;
|
|
|
37 |
|
|
|
38 |
# define the version number
|
|
|
39 |
# see https://metacpan.org/pod/release/JPEACOCK/version-0.97/lib/version.pod
|
|
|
40 |
use version;
|
26 |
rodolico |
41 |
our $VERSION = version->declare("1.2.0");
|
5 |
rodolico |
42 |
|
|
|
43 |
|
|
|
44 |
use Data::Dumper;
|
|
|
45 |
|
|
|
46 |
use Exporter;
|
|
|
47 |
|
|
|
48 |
our @ISA = qw( Exporter );
|
|
|
49 |
our @EXPORT = qw(
|
7 |
rodolico |
50 |
&list
|
5 |
rodolico |
51 |
);
|
|
|
52 |
|
9 |
rodolico |
53 |
sub help {
|
|
|
54 |
my @return;
|
|
|
55 |
push @return, "domain update [domainname|-t domainname]";
|
|
|
56 |
push @return, "\tUpdates capabilities on one or more domains, default is all domains";
|
|
|
57 |
push @return, "domain list [--format|-f screen|tsv]";
|
|
|
58 |
push @return, "\tLists all domains with some statistics about them as screen or tsv (default screen)";
|
12 |
rodolico |
59 |
push @return, "domain start domainname [node]";
|
|
|
60 |
push @return, "\tstarts domainname on node. If node not set, will pick a node.";
|
13 |
rodolico |
61 |
push @return, "domain shutdown domainname";
|
|
|
62 |
push @return, "\tInitiates a shutdown on a running domain and puts it in to manual mode";
|
|
|
63 |
push @return, "domain migrate domainname [node]";
|
|
|
64 |
push @return, "\tmigrates domain from current node to target. If target node not specified";
|
|
|
65 |
push @return, "\twill be automatically selected";
|
14 |
rodolico |
66 |
push @return, "domain new [domainname]";
|
|
|
67 |
push @return, "\tgenerates a virt-install command that will fill in several blanks";
|
|
|
68 |
push @return, "\tNOTE: MAC address not guaranteed to be unique";
|
29 |
rodolico |
69 |
push @return, "domain migrate domainname targetnode";
|
17 |
rodolico |
70 |
push @return, "\tMigates a domain from its current location to targetnode";
|
|
|
71 |
push @return, "domain maintenance domainname [on|off]";
|
|
|
72 |
push @return, "\tSet/Unset/display maintenance flag on domain";
|
|
|
73 |
push @return, "\tIf maintenance flag is set, no havirt will refuse any actions";
|
9 |
rodolico |
74 |
return join( "\n", @return ) . "\n";
|
|
|
75 |
}
|
|
|
76 |
|
7 |
rodolico |
77 |
|
26 |
rodolico |
78 |
# dipslay a list of domains, which node they are on and some information on them
|
7 |
rodolico |
79 |
sub list {
|
12 |
rodolico |
80 |
&main::readDB();
|
25 |
rodolico |
81 |
print Dumper( $main::statusDB->{'nodePopulation'} ) if $main::config->{'flags'}->{'debug'} > 2;
|
7 |
rodolico |
82 |
|
|
|
83 |
my @header;
|
|
|
84 |
my @data;
|
18 |
rodolico |
85 |
my @found;
|
7 |
rodolico |
86 |
|
14 |
rodolico |
87 |
foreach my $node ( sort keys %{$main::statusDB->{'nodePopulation'}} ) {
|
|
|
88 |
foreach my $virt (sort keys %{$main::statusDB->{'nodePopulation'}->{$node}->{'running'}} ) {
|
7 |
rodolico |
89 |
unless ( @header ) {
|
|
|
90 |
# if we don't have a header yet, create it from the keys in this one. Assumes every entry has same keys
|
12 |
rodolico |
91 |
@header = sort keys %{ $main::statusDB->{'virt'}->{$virt} };
|
|
|
92 |
unshift @header, 'Domain';
|
7 |
rodolico |
93 |
unshift @header, 'Node';
|
|
|
94 |
} # unless
|
18 |
rodolico |
95 |
push @found, $virt;
|
7 |
rodolico |
96 |
my @line;
|
|
|
97 |
push @line, $node;
|
|
|
98 |
push @line, $virt;
|
12 |
rodolico |
99 |
foreach my $column ( sort keys %{ $main::statusDB->{'virt'}->{$virt} } ) {
|
|
|
100 |
push @line, $main::statusDB->{'virt'}->{$virt}->{$column};
|
7 |
rodolico |
101 |
}
|
|
|
102 |
push @data, \@line;
|
|
|
103 |
}
|
|
|
104 |
}
|
18 |
rodolico |
105 |
my @allDomains = keys %{$main::statusDB->{'virt'} };
|
|
|
106 |
my $notRunning = &main::diffArray( \@allDomains, \@found );
|
|
|
107 |
my @padding;
|
|
|
108 |
while ( @padding < @header - 2 ) {
|
|
|
109 |
push @padding, '';
|
|
|
110 |
}
|
|
|
111 |
for ( my $i = 0; $i < @$notRunning; $i++ ) {
|
|
|
112 |
my @line;
|
|
|
113 |
push @line, 'Down';
|
|
|
114 |
push @line, $notRunning->[$i];
|
|
|
115 |
push @line, @padding;
|
|
|
116 |
push @data, \@line;
|
|
|
117 |
}
|
|
|
118 |
|
7 |
rodolico |
119 |
return &main::report( \@header, \@data );
|
|
|
120 |
}
|
|
|
121 |
|
15 |
rodolico |
122 |
# reread the domain definition file and freshen the database
|
7 |
rodolico |
123 |
sub update {
|
18 |
rodolico |
124 |
my @requiredFields = ( 'maintenance' );
|
12 |
rodolico |
125 |
&main::readDB(1); # loading it for write, so lock
|
8 |
rodolico |
126 |
unless ( @_ ) {
|
|
|
127 |
# they didn't pass in anything, so do everything
|
12 |
rodolico |
128 |
@_ = keys %{ $main::statusDB->{'virt'} }
|
8 |
rodolico |
129 |
} # unless
|
26 |
rodolico |
130 |
print "Preparing to update " . join( "\n", @_ ) . "\n"
|
|
|
131 |
if $main::config->{'flags'}->{'debug'} > 1 or $main::config->{'flags'}->{'verbose'};
|
|
|
132 |
while ( my $virt = shift ) { # for every domain they passed in
|
|
|
133 |
&parseDomain( $virt ); # parse it and update definition
|
|
|
134 |
foreach my $field ( @requiredFields ) { # make sure the required fields are in there
|
18 |
rodolico |
135 |
$main::statusDB->{'virt'}->{$virt}->{$field} = ''
|
|
|
136 |
unless defined ( $main::statusDB->{'virt'}->{$virt}->{$field} );
|
|
|
137 |
} # foreach
|
7 |
rodolico |
138 |
} # while
|
12 |
rodolico |
139 |
&main::writeDB( $main::domainDBName, $main::statusDB->{'virt'} );
|
26 |
rodolico |
140 |
return "Domain(s) updated\n";
|
7 |
rodolico |
141 |
}
|
|
|
142 |
|
15 |
rodolico |
143 |
|
|
|
144 |
# finds one xml value in file.
|
|
|
145 |
# since libvirt does not use good xml which can be parsed by
|
|
|
146 |
# several libraries, we must do it manually with regex's
|
7 |
rodolico |
147 |
sub getXMLValue {
|
|
|
148 |
my ( $key, $string ) = @_;
|
25 |
rodolico |
149 |
print "getXMLValue: looking for [$key] $string\n" if $main::config->{'flags'}->{'debug'} > 2;
|
7 |
rodolico |
150 |
my $start = "<$key";
|
|
|
151 |
my $end = "</$key>";
|
|
|
152 |
$string =~ m/$start([^>]*)>([^<]+)$end/;
|
|
|
153 |
return ($1,$2);
|
|
|
154 |
}
|
|
|
155 |
|
15 |
rodolico |
156 |
|
|
|
157 |
# parse an xml file to find the values we want to save
|
|
|
158 |
# if config file exists in conf, just read it. If it does not
|
|
|
159 |
# exist, or if force is set, do a dumpxml on the running
|
|
|
160 |
# domain, put it into conf/ and load it.
|
7 |
rodolico |
161 |
sub parseDomain {
|
|
|
162 |
my ($virt, $nodePopulations ) = @_;
|
25 |
rodolico |
163 |
print "Parsing domain $virt in domain.pm:parseDomain\n" if $main::config->{'flags'}->{'debug'};
|
26 |
rodolico |
164 |
print "\tParsing domain $virt\n" if $main::config->{'flags'}->{'verbose'};
|
|
|
165 |
|
7 |
rodolico |
166 |
my @keysToSave = ( 'uuid', 'memory', 'vcpu','vnc' );
|
25 |
rodolico |
167 |
my $filename = "$main::config->{'conf dir'}/$virt.xml";
|
7 |
rodolico |
168 |
my $xml = &getVirtConfig( $virt, $filename );
|
|
|
169 |
my ($param,$value) = &getXMLValue( 'uuid', $xml );
|
12 |
rodolico |
170 |
$main::statusDB->{'virt'}->{$virt}->{'uuid'} = $value;
|
7 |
rodolico |
171 |
($param,$value) = &getXMLValue( 'memory', $xml );
|
12 |
rodolico |
172 |
$main::statusDB->{'virt'}->{$virt}->{'memory'} = $value;
|
7 |
rodolico |
173 |
($param,$value) = &getXMLValue( 'vcpu', $xml );
|
12 |
rodolico |
174 |
$main::statusDB->{'virt'}->{$virt}->{'vcpu'} = $value;
|
7 |
rodolico |
175 |
|
|
|
176 |
$xml =~ m/type='vnc' port='(\d+)'/;
|
12 |
rodolico |
177 |
$main::statusDB->{'virt'}->{$virt}->{'vnc'} = $1;
|
25 |
rodolico |
178 |
print "After Parsing $virt in domain.pm:parseDomain\n" . Dumper($main::statusDB->{'virt'}->{$virt}) . "\n" if $main::config->{'flags'}->{'debug'} > 2;
|
7 |
rodolico |
179 |
}
|
|
|
180 |
|
15 |
rodolico |
181 |
# returns xml definition of a domain
|
|
|
182 |
# if the definition exists in conf/, simply open and read
|
|
|
183 |
# if the definition file does not exist, or if $force is set, find the
|
|
|
184 |
# running domain, perform an xml dump of it, save it to conf/, then
|
|
|
185 |
# return it.
|
7 |
rodolico |
186 |
sub getVirtConfig {
|
|
|
187 |
my ($virt,$filename) = @_;
|
|
|
188 |
my $return;
|
25 |
rodolico |
189 |
print "In getVirtConfig looking for $virt with file $filename, force is $main::config->{'flags'}->{'yes'}\n" if $main::config->{'flags'}->{'debug'};
|
26 |
rodolico |
190 |
if ( -f $filename && ! $main::config->{'flags'}->{'force'}) {
|
7 |
rodolico |
191 |
open XML, "<$filename" or die "Could not read from $filename: $!\n";
|
|
|
192 |
$return = join( '', <XML> );
|
|
|
193 |
close XML;
|
|
|
194 |
} else {
|
12 |
rodolico |
195 |
&main::readDB();
|
|
|
196 |
foreach my $node ( keys %{$main::statusDB->{'nodePopulation'}} ) {
|
25 |
rodolico |
197 |
print "getVirtConfig Looking on $node for $virt\n" if $main::config->{'flags'}->{'debug'} > 1;;
|
12 |
rodolico |
198 |
if ( exists( $main::statusDB->{'nodePopulation'}->{$node}->{'running'}->{$virt} ) ) { # we found it
|
26 |
rodolico |
199 |
print "Getting copy of XML file for $virt from $node\n" if $main::config->{'flags'}->{'verbose'};
|
25 |
rodolico |
200 |
print "Found $virt on node $node\n" if $main::config->{'flags'}->{'debug'};;
|
|
|
201 |
my $command = &main::makeCommand($node, "virsh dumpxml $virt");
|
|
|
202 |
$return = `$command`;
|
|
|
203 |
print "Writing config for $virt from $node into $filename\n" if $main::config->{'flags'}->{'debug'};
|
7 |
rodolico |
204 |
open XML,">$filename" or die "Could not write to $filename: $!\n";
|
|
|
205 |
print XML $return;
|
|
|
206 |
close XML;
|
|
|
207 |
} # if
|
|
|
208 |
} # foreach
|
|
|
209 |
} # if..else
|
|
|
210 |
return $return;
|
|
|
211 |
} # sub getVirtConfig
|
12 |
rodolico |
212 |
|
26 |
rodolico |
213 |
|
12 |
rodolico |
214 |
# start a domain
|
|
|
215 |
sub start {
|
|
|
216 |
my ( $virt, $node ) = @_;
|
16 |
rodolico |
217 |
my $return;
|
12 |
rodolico |
218 |
$node = `hostname` unless $node;
|
|
|
219 |
chomp $node;
|
18 |
rodolico |
220 |
return "Domain $virt in maintenance mode, can not start\n" if $main::statusDB->{'virt'}->{$virt}->{'maintenance'};
|
|
|
221 |
return "Node $node in maintenance mode, can not start\n" if $main::statusDB->{'node'}->{$node}->{'maintenance'};
|
|
|
222 |
if ( my $foundNode = &main::findDomain( $virt ) ) {
|
|
|
223 |
die "$virt already running on $foundNode, not starting\n";
|
12 |
rodolico |
224 |
}
|
|
|
225 |
die "I do not have a definition for $virt\n" unless exists( $main::statusDB->{'virt'}->{$virt} );
|
25 |
rodolico |
226 |
print Dumper( $main::statusDB->{'nodePopulation'} ) if $main::config->{'flags'}->{'debug'} > 2;
|
|
|
227 |
if ( my $error = &main::validateResources( $node, $virt ) ) {
|
|
|
228 |
die $error;
|
|
|
229 |
}
|
|
|
230 |
my $filename = "$main::config->{'conf dir'}/$virt.xml";
|
16 |
rodolico |
231 |
my $command = &main::makeCommand( $node, "virsh create $filename" );
|
26 |
rodolico |
232 |
print "Starting $virt on $node\n" if $main::config->{'flags'}->{'verbose'};
|
|
|
233 |
if ( $main::config->{'flags'}->{'dryrun'} ) { # we'll actually do it
|
|
|
234 |
$return = $command;;
|
|
|
235 |
} else {
|
16 |
rodolico |
236 |
$return = ( &main::executeAndWait( $command, $node, $virt, 1 ) ? 'Success' : 'Can not start');
|
|
|
237 |
&main::forceScan();
|
|
|
238 |
}
|
|
|
239 |
return "$return\n";
|
12 |
rodolico |
240 |
}
|
13 |
rodolico |
241 |
|
|
|
242 |
sub shutdown {
|
|
|
243 |
my $virt = shift;
|
14 |
rodolico |
244 |
my $node = '';
|
15 |
rodolico |
245 |
my $return;
|
18 |
rodolico |
246 |
$node = &main::findDomain( $virt );
|
|
|
247 |
die "I could not find the domain $virt running\n" unless $node;
|
25 |
rodolico |
248 |
print Dumper( $main::statusDB->{'nodePopulation'} ) if $main::config->{'flags'}->{'debug'} > 2;
|
14 |
rodolico |
249 |
die "I can not find $virt on any node\n" unless $node;
|
15 |
rodolico |
250 |
my $command = &main::makeCommand( $node, "virsh shutdown $virt" );
|
26 |
rodolico |
251 |
print "Attempting to shut down $virt currently on $node\n" if $main::config->{'flags'}->{'verbose'};
|
|
|
252 |
if ( $main::config->{'flags'}->{'dryrun'} ) { # they want us to actually do it
|
|
|
253 |
$return = $command;
|
|
|
254 |
} else {
|
15 |
rodolico |
255 |
$return = ( &main::executeAndWait( $command, $node, $virt, 0 ) ? 'Success' : 'Time Out waiting for shutdown');
|
|
|
256 |
&main::forceScan();
|
|
|
257 |
}
|
|
|
258 |
return "$return\n";
|
13 |
rodolico |
259 |
}
|
|
|
260 |
|
26 |
rodolico |
261 |
# Migrate a single domain
|
|
|
262 |
# this is defined in havirt.pm, which is set up to migrate multiple domains
|
|
|
263 |
# but has an entry point here for just one
|
|
|
264 |
# domain is domain to migrate, target is where to put it
|
13 |
rodolico |
265 |
sub migrate {
|
25 |
rodolico |
266 |
my ( $domain, $target ) = @_;
|
|
|
267 |
if ( my $error = &main::validateResources( $target, $domain ) ) {
|
|
|
268 |
die $error;
|
17 |
rodolico |
269 |
}
|
25 |
rodolico |
270 |
return &main::migrate( $domain, $target );
|
14 |
rodolico |
271 |
}
|
|
|
272 |
|
|
|
273 |
# find an unused VNC port
|
|
|
274 |
sub findVNCPort {
|
|
|
275 |
my $currentPorts = shift;
|
|
|
276 |
my $return = 5900;
|
|
|
277 |
while ( $currentPorts->{$return} ) {
|
|
|
278 |
$return++;
|
|
|
279 |
}
|
|
|
280 |
return $return;
|
|
|
281 |
}
|
|
|
282 |
|
26 |
rodolico |
283 |
# print MAC address in the correct format
|
|
|
284 |
# two character hex digits separated by colons, lower case
|
14 |
rodolico |
285 |
sub printMac {
|
|
|
286 |
my $mac = shift;
|
|
|
287 |
my @return;
|
|
|
288 |
my $separator = ':';
|
|
|
289 |
for ( my $i = 0; $i < length( $mac ); $i += 2 ) {
|
|
|
290 |
push @return, substr( $mac, $i, 2 );
|
|
|
291 |
}
|
|
|
292 |
return join( $separator, @return );
|
|
|
293 |
}
|
|
|
294 |
|
26 |
rodolico |
295 |
|
|
|
296 |
# generate a random MAC address for new function.
|
|
|
297 |
# NOTE: this is not checked for duplication at this time
|
14 |
rodolico |
298 |
sub makeMac {
|
|
|
299 |
my $numDigits = 12; # 12 hex digits in a mac address
|
|
|
300 |
my $macBaseXen = '00163e'; # Xen has 00:16:3E* assigned to it.
|
|
|
301 |
my $hexDigits = '0123456789abcdef';
|
|
|
302 |
|
|
|
303 |
my $baseMac = $macBaseXen;
|
|
|
304 |
|
|
|
305 |
while (length( $baseMac ) < $numDigits ) {
|
|
|
306 |
$baseMac .= substr($hexDigits, int(rand(length($hexDigits))),1 );
|
|
|
307 |
}
|
|
|
308 |
|
|
|
309 |
return &printMac( $baseMac );
|
|
|
310 |
}
|
|
|
311 |
|
|
|
312 |
# generate a virt-install statement, verifying things are not already being used
|
|
|
313 |
sub new {
|
|
|
314 |
my %config;
|
|
|
315 |
$config{'name'} = shift;
|
|
|
316 |
my $return;
|
25 |
rodolico |
317 |
my $template = $main::config->{'script dir'} . '/virt-install.template';
|
14 |
rodolico |
318 |
&main::readDB();
|
|
|
319 |
my %current;
|
|
|
320 |
foreach my $virt ( keys %{$main::statusDB->{'virt'} } ) {
|
|
|
321 |
$current{'vnc'}{$main::statusDB->{'virt'}->{$virt}->{'vnc'}} = 1;
|
|
|
322 |
#$current{'mac'}{'null'} = 1;
|
|
|
323 |
}
|
|
|
324 |
$config{'vnc'} = &findVNCPort( \%{$current{'vnc'}} );
|
|
|
325 |
$config{'mac'} = &makeMac();
|
|
|
326 |
$config{'uuid'} = `uuidgen`;
|
|
|
327 |
chomp $config{'uuid'};
|
|
|
328 |
if ( open TEMPLATE, "<$template" ) {
|
|
|
329 |
$return = join( '', <TEMPLATE> );
|
|
|
330 |
close TEMPLATE;
|
|
|
331 |
foreach my $key ( keys %config ) {
|
|
|
332 |
$return =~ s/<$key>/$config{$key}/gi if $config{$key};
|
|
|
333 |
}
|
|
|
334 |
} else {
|
|
|
335 |
$return = "Template $template not found, the following values should be used\n";
|
|
|
336 |
foreach my $key ( keys %config ) {
|
|
|
337 |
$return .= "$key: $config{$key}\n";
|
|
|
338 |
}
|
|
|
339 |
}
|
|
|
340 |
return $return;
|
|
|
341 |
}
|
17 |
rodolico |
342 |
|
26 |
rodolico |
343 |
|
|
|
344 |
# put domain in maintenance mode
|
|
|
345 |
# in maintenance mode, it can not be started, stopped or migrated by havirt
|
17 |
rodolico |
346 |
sub maintenance {
|
|
|
347 |
my ( $domain, $action ) = @_;
|
|
|
348 |
&main::readDB(1);
|
|
|
349 |
if ( $action ) {
|
|
|
350 |
$main::statusDB->{'virt'}->{$domain}->{'maintenance'} = ( lc( $action ) eq 'on' ) ? 1 : 0;
|
|
|
351 |
}
|
|
|
352 |
&main::writeDB();
|
|
|
353 |
return "Maintenance set to " . ( $main::statusDB->{'virt'}->{$domain}->{'maintenance'} ? 'On' : 'Off' ) . "\n";
|
|
|
354 |
}
|
|
|
355 |
|