Subversion Repositories havirt

Rev

Rev 20 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

#!/usr/bin/env perl

# Common library for havirt. Basically, just a place to put things which may be used by any
# part of havirt. More for organizations purposes.

# 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 havirt;

use warnings;
use strict;  

use Data::Dumper qw(Dumper); # Import the Dumper() subroutine

# 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 Exporter;

our @ISA = qw( Exporter );
our @EXPORT = qw( 
                  &readDB &writeDB
                  &report &scan
                  &makeCommand &forceScan
                  &executeAndWait
                  &findDomain
                  &diffArray
                );

# read a DB file (just a YAML)
# if $lock is set, will create a "lock" file so other processes will
# not try to write to it. Using custom code as flock is automagically
# release when the file is read

sub readDB {
   my $lock = shift;
   my $lockFileName = "$main::statusDBName.lock";
   my $lockTime = 5; # maximum time to wait for lock to clear
   # wait for lock to clear if it exists, if we are wanting a lock
   # and we have tried it for $locktime iterations
   while ( $lock && -f $lockFileName && $lockTime-- ) {
      sleep 1; # wait one second, then try again
   }
   if ( $lock ) {
      die "Something has $main::statusDBName locked, aborting\n" if -f $lockFileName;
      `touch $lockFileName`;
   }
   my $yaml = YAML::Tiny->new( {} );
   if ( -f $main::statusDBName ) {
      $yaml = YAML::Tiny->read( $main::statusDBName );
   }
   $main::statusDB = $yaml->[0];
}

sub writeDB {
   my $yaml = YAML::Tiny->new( $main::statusDB );
   $yaml->write( $main::statusDBName );
   unlink "$main::statusDBName.lock" if -f "$main::statusDBName.lock"; # release any lock we might have on it
}

sub report {
   if ( $main::reportFormat eq 'tsv' ) {
      return &report_tsv( @_ );
   } else {
      return &report_screen( @_ );
   }
}

sub report_tsv {
   my ( $header, $data ) = @_;
   my @output;
   push @output, join( "\t", @$header );
   for( my $line = 0; $line < @$data; $line++ ) {
      push @output, join( "\t", @{$data->[$line]} );
   } # for
   return join( "\n", @output ) . "\n";
}

sub report_screen {
   my ( $header, $data ) = @_;
   my @output;
   my @widths;
   my $column;
   my $row;
   # First, initialize by using the length of the headers
   for ( $column = 0; $column < @$header; $column++ ) {
      @widths[$column] = length( $header->[$column] );
   }
   # now, go through all data in each row, for each column, and increment the width if it is larger
   for ( $row = 0; $row < @$data; $row++ ) {
      for ( $column = 0; $column < @$header; $column++ ) {
         $widths[$column] = length( $data->[$row][$column] ) 
            if length( $data->[$row][$column] ) > $widths[$column];
      } # for column
   } # for row
   # actually do the print now
   my @format;
   for ( $column = 0; $column < @widths; $column++ ) {
      push ( @format, '%' . $widths[$column] . 's' );
   }
   my $format = join( ' ', @format ) . "\n";
   my $output = sprintf( $format, @$header );
   for ( $row = 0; $row < @$data; $row++ ) {
      $output .= sprintf( $format, @{$data->[$row]} );
   } # for row
   return $output;
}

# scans a node to determine which domains are running on it
sub getDomainsOnNode {
   my $node = shift;
   my @nodeList = grep { /^\s*\d/ } `ssh $node 'virsh list'`;
   for ( my $i = 0; $i < @nodeList; $i++ ) {
      if ( $nodeList[$i] =~ m/\s*\d+\s*([^ ]+)/ ) {
         $nodeList[$i] = $1;
      }
   }
   my %hash = map{ $_ => time } @nodeList;
   return \%hash;
}

# find node a domain is on
# first parameter is the domain name
# rest of @_ is list of nodes to search
# if no nodes passed in, will search all known nodes
# returns first node found with the domain, or an empty string if not found
# possibly not being used??
sub findDomain {
   my $domainName = shift;
   my @node = @_;
   my $foundNode = '';
   &readDB();
   unless ( @node ) {
      @node = keys %{$main::statusDB->{'node'} };
      print "findDomain, nodes = " . join( "\t", @node ) . "\n" if $main::DEBUG > 1;
   }
   foreach my $thisNode ( @node ) {
      my $output = `ssh $thisNode 'virsh list'`;
      print "findDomain, $thisNode list =\n" . $output . "\n" if $main::DEBUG > 1;;
      return $thisNode if ( $output =~ m/$domainName/ );
   }
   return '';
}

# check one or more nodes and determine which domains are running on them.
# defaults to everything in the node database, but the -t can have it run on only one
# this is the function that should be run every few minutes on one of the servers
sub scan {
   if ( -f $main::lastScanFileName && ! $main::force ) {
      my $lastScan = time - ( stat( $main::lastScanFileName ) ) [9];
      return "Scan was run $lastScan seconds ago\n" unless $lastScan > $main::minScanTimes;
   }
   `touch $main::lastScanFileName`;
   &main::readDB(1);
   print Dumper( $main::statusDB->{'nodePopulation'} ) if $main::DEBUG > 2;
   my @targets;
   if ( $main::targetNode ) {
      push @targets, $main::targetNode;
   } else {
      @targets = keys %{$main::statusDB->{'node'}};
   }
   print "Scanning " . join( "\n", @targets ) . "\n" if $main::DEBUG;
   foreach my $node (@targets) {
      $main::statusDB->{'nodePopulation'}->{$node}->{'running'} = &getDomainsOnNode( $node );
      $main::statusDB->{'nodePopulation'}->{$node}->{'lastchecked'} = time;
      foreach my $domain ( keys %{$main::statusDB->{'nodePopulation'}->{$node}->{'running'}} ) {
         # make sure there is an entry for all of these domains
         $main::statusDB->{'virt'}->{$domain} = {} unless exists( $main::statusDB->{'virt'}->{$domain} );
      }
      print Dumper( $main::statusDB->{'nodePopulation'}->{$node} ) if $main::DEBUG > 2;
   }
   &main::writeDB();
   return "Node(s) updated\n";
}

# makes the command that will be run on a node
# Created as a sub so we can change format easily
sub makeCommand {
   my ( $node, $command ) = @_;
   return "ssh $node '$command'";
}

# force a node scan, even if time has not expired
sub forceScan {
   my $save = $main::force;
   $main::force = 1;
   &main::scan();
   $main::force = $save;
}


# executes command $command, then repeatedly runs virsh list
# on $scanNode, grep'ing for $scanDomain
# $condition is 1 (true) or 0 (false)
sub executeAndWait {
   my ( $command, $scanNode, $scanDomain, $condition ) = @_;
   my $waitSeconds = 5; # number of seconds to wait before checking again
   my $maxIterations = 60 / $waitSeconds; # maximum number of tries
   print "Running [$command], then waiting $waitSeconds to check if complete\n" if $main::DEBUG;
   `$command`;
   my $waitCommand = &makeCommand( $scanNode, "virsh list | grep $scanDomain" );
   my $output = '';
   do {
      return 0 unless ( $maxIterations-- ); # we've waited too long, so probably not working
      print '. ';
      sleep 5;
      $output = `$waitCommand`;
      print "[$waitCommand] returned [$output]\n" if $main::DEBUG > 1;
   } until ( $condition ? $output : !$output );
   return 1; # made it successful
} 

# find the differences between two arrays (passed by reference)
# first sorts the array, then walks through them one by one
# @$arr1 MUST be larger than @$arr2
sub diffArray {
   my ( $arr1, $arr2 ) = @_;
   my @result;

   @$arr1 = sort @$arr1;
   @$arr2 = sort @$arr2;
   my $i=0;
   my $j=0;

   while ( $i < @$arr1 ) {
      if ( $arr1->[$i] eq $arr2->[$j] ) {
         $i++;
         $j++;
      } elsif ( $arr1->[$i] lt $arr2->[$j] ) {
         push @result, $arr1->[$i];
         $i++;
      } else {
         push @result, $arr2->[$j];
         $j++;
      }
   }
   return \@result;
}