Rev 12 | Rev 14 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
#!/usr/bin/env perl
# All functions related to maniplating domains
# part of havirt.
# Copyright 2024 Daily Data, Inc.
#
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
# conditions are met:
#
# Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
# Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the distribution.
# Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
# NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
# THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# v0.0.1 20240602 RWR
# Initial setup
package domain;
use warnings;
use strict;
# define the version number
# see https://metacpan.org/pod/release/JPEACOCK/version-0.97/lib/version.pod
use version;
our $VERSION = version->declare("0.0.1");
use Data::Dumper;
use Exporter;
our @ISA = qw( Exporter );
our @EXPORT = qw(
&list
);
sub help {
my @return;
push @return, "domain update [domainname|-t domainname]";
push @return, "\tUpdates capabilities on one or more domains, default is all domains";
push @return, "domain list [--format|-f screen|tsv]";
push @return, "\tLists all domains with some statistics about them as screen or tsv (default screen)";
push @return, "domain start domainname [node]";
push @return, "\tstarts domainname on node. If node not set, will pick a node.";
push @return, "domain shutdown domainname";
push @return, "\tInitiates a shutdown on a running domain and puts it in to manual mode";
push @return, "domain migrate domainname [node]";
push @return, "\tmigrates domain from current node to target. If target node not specified";
push @return, "\twill be automatically selected";
return join( "\n", @return ) . "\n";
}
sub list {
&main::readDB();
print Dumper( $main::statusDB->{'nodePopulation'} ) if $main::DEBUG > 2;
my @header;
my @data;
foreach my $node ( keys %{$main::statusDB->{'nodePopulation'}} ) {
foreach my $virt (keys %{$main::statusDB->{'nodePopulation'}->{$node}->{'running'}} ) {
unless ( @header ) {
# if we don't have a header yet, create it from the keys in this one. Assumes every entry has same keys
@header = sort keys %{ $main::statusDB->{'virt'}->{$virt} };
unshift @header, 'Domain';
unshift @header, 'Node';
} # unless
my @line;
push @line, $node;
push @line, $virt;
foreach my $column ( sort keys %{ $main::statusDB->{'virt'}->{$virt} } ) {
push @line, $main::statusDB->{'virt'}->{$virt}->{$column};
}
push @data, \@line;
}
}
return &main::report( \@header, \@data );
}
sub update {
&main::readDB(1); # loading it for write, so lock
unless ( @_ ) {
# they didn't pass in anything, so do everything
@_ = keys %{ $main::statusDB->{'virt'} }
} # unless
print "Preparing to update " . join( "\n", @_ ) . "\n" if $main::DEBUG > 1;
while ( my $virt = shift ) {
&parseDomain( $virt );
} # while
&main::writeDB( $main::domainDBName, $main::statusDB->{'virt'} );
return "Updated\n";
}
sub getXMLValue {
my ( $key, $string ) = @_;
print "getXMLValue: looking for [$key] $string\n" if $main::DEBUG > 2;
my $start = "<$key";
my $end = "</$key>";
$string =~ m/$start([^>]*)>([^<]+)$end/;
return ($1,$2);
}
sub parseDomain {
my ($virt, $nodePopulations ) = @_;
my @keysToSave = ( 'uuid', 'memory', 'vcpu','vnc' );
my $filename = "$main::confDir/$virt.xml";
my $xml = &getVirtConfig( $virt, $filename );
my ($param,$value) = &getXMLValue( 'uuid', $xml );
$main::statusDB->{'virt'}->{$virt}->{'uuid'} = $value;
($param,$value) = &getXMLValue( 'memory', $xml );
$main::statusDB->{'virt'}->{$virt}->{'memory'} = $value;
($param,$value) = &getXMLValue( 'vcpu', $xml );
$main::statusDB->{'virt'}->{$virt}->{'vcpu'} = $value;
$xml =~ m/type='vnc' port='(\d+)'/;
$main::statusDB->{'virt'}->{$virt}->{'vnc'} = $1;
}
# get the XML definition file of a running domain off of whatever
# node it is running on, and save it to disk
sub getVirtConfig {
my ($virt,$filename) = @_;
my $return;
print "In getVirtConfig looking for $virt with file $filename\n" if $main::DEBUG;
if ( -f $filename ) {
open XML, "<$filename" or die "Could not read from $filename: $!\n";
$return = join( '', <XML> );
close XML;
} else {
&main::readDB();
foreach my $node ( keys %{$main::statusDB->{'nodePopulation'}} ) {
print "getVirtConfig Looking on $node for $virt\n" if $main::DEBUG > 1;;
if ( exists( $main::statusDB->{'nodePopulation'}->{$node}->{'running'}->{$virt} ) ) { # we found it
print "Found $virt on node $node\n" if $main::DEBUG;;
$return = `ssh $node 'virsh dumpxml $virt'`;
print "Writing config for $virt from $node into $filename\n" if $main::DEBUG;
open XML,">$filename" or die "Could not write to $filename: $!\n";
print XML $return;
close XML;
} # if
} # foreach
} # if..else
return $return;
} # sub getVirtConfig
# start a domain
sub start {
my ( $virt, $node ) = @_;
$node = `hostname` unless $node;
chomp $node;
&main::readDB();
for my $myNode ( keys %{$main::statusDB->{'nodePopulation'} } ) {
die "$virt already running on $myNode, not starting\n" if ( $main::statusDB->{'nodePopulation'}->{$myNode}->{'running'}->{$virt} );
}
die "I do not have a definition for $virt\n" unless exists( $main::statusDB->{'virt'}->{$virt} );
print Dumper( $main::statusDB->{'nodePopulation'} ) if $main::DEBUG > 2;
my $filename = "$main::confDir/$virt.xml";
return "ssh $node 'virsh create $filename'\n";
}
sub shutdown {
my $virt = shift;
return "This code not written yet\n";
}
sub migrate {
my ( $virt, $target ) = @_;
return "I don't know how to migrate yet\n";
}
Generated by GNU Enscript 1.6.5.90.