Subversion Repositories havirt

Rev

Rev 20 | 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("1.0.0");


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";
   push @return, "domain new [domainname]";
   push @return, "\tgenerates a virt-install command that will fill in several blanks";
   push @return, "\tNOTE: MAC address not guaranteed to be unique";
   push @return, "domain [--yes] migrate domainname targetnode";
   push @return, "\tMigates a domain from its current location to targetnode";
   push @return, "domain maintenance domainname [on|off]";
   push @return, "\tSet/Unset/display maintenance flag on domain";
   push @return, "\tIf maintenance flag is set, no havirt will refuse any actions";
   return join( "\n", @return ) . "\n";
}


sub list {
   &main::readDB();
   print Dumper( $main::statusDB->{'nodePopulation'} ) if $main::DEBUG > 2;

   my @header;
   my @data;
   my @found;
   
   foreach my $node ( sort keys %{$main::statusDB->{'nodePopulation'}} ) {
      foreach my $virt (sort 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
         push @found, $virt;
         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;
      }
   }
   my @allDomains = keys %{$main::statusDB->{'virt'} };
   my $notRunning = &main::diffArray( \@allDomains, \@found );
   my @padding;
   while ( @padding < @header - 2 ) {
      push @padding, '';
   }
   for ( my $i = 0; $i < @$notRunning; $i++ ) {
      my @line;
      push @line, 'Down';
      push @line, $notRunning->[$i];
      push @line, @padding;
      push @data, \@line;
   }

   return &main::report( \@header, \@data );
}

# reread the domain definition file and freshen the database
sub update {
   my @requiredFields = ( 'maintenance' );
   &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 );
      foreach my $field ( @requiredFields ) {
         $main::statusDB->{'virt'}->{$virt}->{$field} = '' 
            unless defined ( $main::statusDB->{'virt'}->{$virt}->{$field} );
      } # foreach
   } # while
   &main::writeDB( $main::domainDBName, $main::statusDB->{'virt'} );
   return "Updated\n";
}


# finds one xml value in file.
# since libvirt does not use good xml which can be parsed by
# several libraries, we must do it manually with regex's
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);
}


# parse an xml file to find the values we want to save
# if config file exists in conf, just read it. If it does not
# exist, or if force is set, do a dumpxml on the running
# domain, put it into conf/ and load it.
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;
}

# returns xml definition of a domain
# if the definition exists in conf/, simply open and read
# if the definition file does not exist, or if $force is set, find the 
# running domain, perform an xml dump of it, save it to conf/, then 
# return it.
sub getVirtConfig {
   my ($virt,$filename) = @_;
   my $return;
   print "In getVirtConfig looking for $virt with file $filename, force is $main::force\n" if $main::DEBUG;
   if ( -f $filename && ! $main::force) {
      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 ) = @_;
   my $return;
   $node = `hostname` unless $node;
   chomp $node;
   return "Domain $virt in maintenance mode, can not start\n" if $main::statusDB->{'virt'}->{$virt}->{'maintenance'};
   return "Node $node in maintenance mode, can not start\n" if $main::statusDB->{'node'}->{$node}->{'maintenance'};
   # these are replaced by the safer findDomain
   #&main::forceScan();
   #&main::readDB();
   if ( my $foundNode = &main::findDomain( $virt ) ) {
      die "$virt already running on $foundNode, not starting\n";
   }
   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";
   my $command = &main::makeCommand( $node, "virsh create $filename" );
   if ( $main::force ) { # we'll actually do it
      $return = ( &main::executeAndWait( $command, $node, $virt, 1 ) ? 'Success' : 'Can not start');
      &main::forceScan();
   } else {
      $return =  $command;;
   }
   return "$return\n";
}

sub shutdown {
   my $virt = shift;
   my $node = '';
   my $return;
   # these are replaced by the safer findDomain
   #&main::forceScan();
   #&main::readDB();
   $node = &main::findDomain( $virt );
   die "I could not find the domain $virt running\n" unless $node;
   print Dumper( $main::statusDB->{'nodePopulation'} ) if $main::DEBUG > 2;
   die "I can not find $virt on any node\n" unless $node;
   my $command = &main::makeCommand( $node, "virsh shutdown $virt" );
   if ( $main::force ) { # they want us to actually do it
      $return = ( &main::executeAndWait( $command, $node, $virt, 0 ) ? 'Success' : 'Time Out waiting for shutdown');
      &main::forceScan();
   } else {
      $return = $command;
   }
   return "$return\n";
}

sub migrate {
   my ( $virt, $target ) = @_;
   my $return;
   my $node;
   # these are replaced by the safer findDomain
   #&main::forceScan();
   #&main::readDB();
   $node = &main::findDomain( $virt );
   print Dumper( $main::statusDB->{'nodePopulation'} ) if $main::DEBUG > 2;
   die "I can not find $virt on any node\n" unless $node;
   die "Domain $virt in maintenance mode, can not migrate it\n" if $main::statusDB->{'virt'}->{$virt}->{'maintenance'};
   die "Node $target in maintenance mode, can not migrate anything to it\n" if $main::statusDB->{'node'}->{$target}->{'maintenance'};
   die "$virt already on $target\n" if $target eq $node;
   my $command = &main::makeCommand( $node, "virsh migrate --live --persistent --verbose  $virt qemu+ssh://$target/system" );
   if ( $main::force ) { # they want us to actually do it
      $return = ( &main::executeAndWait( $command, $node, $virt, 0 ) ? 'Success' : 'Time Out waiting for shutdown');
      &main::forceScan();
   } else {
      $return = $command;
   }
   return "$return\n";
}

# find an unused VNC port
sub findVNCPort {
   my $currentPorts = shift;
   my $return = 5900;
   while ( $currentPorts->{$return} ) {
      $return++;
   }
   return $return;
}


sub printMac {
   my $mac = shift;
   my @return;
   my $separator = ':';
   for ( my $i = 0; $i < length( $mac ); $i += 2 ) {
      push @return, substr( $mac, $i, 2 );
   }
   return join( $separator, @return );
}
      
      
sub makeMac {
   my $numDigits = 12; # 12 hex digits in a mac address
   my $macBaseXen = '00163e'; # Xen has 00:16:3E* assigned to it.
   my $hexDigits = '0123456789abcdef';

   my $baseMac = $macBaseXen;

   while (length( $baseMac ) < $numDigits ) {
      $baseMac .= substr($hexDigits, int(rand(length($hexDigits))),1 );
   }

   return  &printMac( $baseMac );
}

# generate a virt-install statement, verifying things are not already being used
sub new {
   my %config;
   $config{'name'} = shift;
   my $return;
   my $template = $main::scriptDir . '/virt-install.template';
   &main::readDB();
   my %current;
   foreach my $virt ( keys %{$main::statusDB->{'virt'} } ) {
      $current{'vnc'}{$main::statusDB->{'virt'}->{$virt}->{'vnc'}} = 1;
      #$current{'mac'}{'null'} = 1;
   }
   $config{'vnc'} = &findVNCPort( \%{$current{'vnc'}} );
   $config{'mac'} = &makeMac();
   $config{'uuid'} = `uuidgen`;
   chomp $config{'uuid'};
   if ( open TEMPLATE, "<$template" ) {
      $return = join( '', <TEMPLATE> );
      close TEMPLATE;
      foreach my $key ( keys %config ) {
         $return =~ s/<$key>/$config{$key}/gi if $config{$key};
      }
   } else {
      $return = "Template $template not found, the following values should be used\n";
      foreach my $key ( keys %config ) {
         $return .= "$key: $config{$key}\n";
      }
   }
   return $return;
}

sub maintenance {
   my ( $domain, $action ) = @_;
   &main::readDB(1);
   if ( $action ) {
      $main::statusDB->{'virt'}->{$domain}->{'maintenance'} = ( lc( $action ) eq 'on' ) ? 1 : 0;
   }
   &main::writeDB();
   return "Maintenance set to " . ( $main::statusDB->{'virt'}->{$domain}->{'maintenance'} ? 'On' : 'Off' ) . "\n";
}