Subversion Repositories havirt

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 rodolico 1
#!/usr/bin/env perl
2
 
3
# Common library for havirt. Basically, just a place to put things which may be used by any
4 rodolico 4
# part of havirt. More for organizations purposes.
3 rodolico 5
 
4 rodolico 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
 
3 rodolico 25
# v0.0.1 20240602 RWR
26
# Initial setup
27
 
28
package havirt;
29
 
30
 
31
use warnings;
32
use strict;  
33
 
34
use Data::Dumper qw(Dumper); # Import the Dumper() subroutine
35
 
4 rodolico 36
# define the version number
37
# see https://metacpan.org/pod/release/JPEACOCK/version-0.97/lib/version.pod
38
use version;
39
our $VERSION = version->declare("0.0.1");
40
 
41
 
3 rodolico 42
use Exporter;
43
 
44
our @ISA = qw( Exporter );
45
our @EXPORT = qw( 
46
                  &readDB &writeDB
15 rodolico 47
                  &report &scan
48
                  &makeCommand &forceScan
49
                  &executeAndWait
3 rodolico 50
                );
51
 
12 rodolico 52
# read a DB file (just a YAML)
53
# if $lock is set, will create a "lock" file so other processes will
54
# not try to write to it. Using custom code as flock is automagically
55
# release when the file is read
3 rodolico 56
 
57
sub readDB {
12 rodolico 58
   my $lock = shift;
59
   my $lockFileName = "$main::statusDBName.lock";
60
   my $lockTime = 5; # maximum time to wait for lock to clear
61
   # wait for lock to clear if it exists, if we are wanting a lock
62
   # and we have tried it for $locktime iterations
63
   while ( $lock && -f $lockFileName && $lockTime-- ) {
64
      sleep 1; # wait one second, then try again
65
   }
66
   if ( $lock ) {
67
      die "Something has $main::statusDBName locked, aborting\n" if -f $lockFileName;
68
      `touch $lockFileName`;
69
   }
3 rodolico 70
   my $yaml = YAML::Tiny->new( {} );
12 rodolico 71
   if ( -f $main::statusDBName ) {
72
      $yaml = YAML::Tiny->read( $main::statusDBName );
3 rodolico 73
   }
12 rodolico 74
   $main::statusDB = $yaml->[0];
3 rodolico 75
}
76
 
77
sub writeDB {
12 rodolico 78
   my $yaml = YAML::Tiny->new( $main::statusDB );
79
   $yaml->write( $main::statusDBName );
80
   unlink "$main::statusDBName.lock" if -f "$main::statusDBName.lock"; # release any lock we might have on it
3 rodolico 81
}
82
 
4 rodolico 83
sub report {
84
   if ( $main::reportFormat eq 'tsv' ) {
85
      return &report_tsv( @_ );
86
   } else {
87
      return &report_screen( @_ );
88
   }
89
}
90
 
3 rodolico 91
sub report_tsv {
92
   my ( $header, $data ) = @_;
93
   my @output;
94
   push @output, join( "\t", @$header );
95
   for( my $line = 0; $line < @$data; $line++ ) {
96
      push @output, join( "\t", @{$data->[$line]} );
97
   } # for
98
   return join( "\n", @output ) . "\n";
99
}
100
 
101
sub report_screen {
102
   my ( $header, $data ) = @_;
103
   my @output;
104
   my @widths;
105
   my $column;
106
   my $row;
107
   # First, initialize by using the length of the headers
108
   for ( $column = 0; $column < @$header; $column++ ) {
109
      @widths[$column] = length( $header->[$column] );
110
   }
111
   # now, go through all data in each row, for each column, and increment the width if it is larger
112
   for ( $row = 0; $row < @$data; $row++ ) {
113
      for ( $column = 0; $column < @$header; $column++ ) {
114
         $widths[$column] = length( $data->[$row][$column] ) 
115
            if length( $data->[$row][$column] ) > $widths[$column];
116
      } # for column
117
   } # for row
118
   # actually do the print now
119
   my @format;
120
   for ( $column = 0; $column < @widths; $column++ ) {
121
      push ( @format, '%' . $widths[$column] . 's' );
122
   }
123
   my $format = join( ' ', @format ) . "\n";
124
   my $output = sprintf( $format, @$header );
125
   for ( $row = 0; $row < @$data; $row++ ) {
126
      $output .= sprintf( $format, @{$data->[$row]} );
127
   } # for row
128
   return $output;
129
}
10 rodolico 130
 
15 rodolico 131
# scans a node to determine which domains are running on it
132
sub getDomainsOnNode {
133
   my $node = shift;
134
   my @nodeList = grep { /^\s*\d/ } `ssh $node 'virsh list'`;
135
   for ( my $i = 0; $i < @nodeList; $i++ ) {
136
      if ( $nodeList[$i] =~ m/\s*\d+\s*([^ ]+)/ ) {
137
         $nodeList[$i] = $1;
138
      }
139
   }
140
   my %hash = map{ $_ => time } @nodeList;
141
   return \%hash;
142
}
143
 
144
 
145
 
146
# check one or more nodes and determine which domains are running on them.
147
# defaults to everything in the node database, but the -t can have it run on only one
148
# this is the function that should be run every few minutes on one of the servers
149
sub scan {
150
   if ( -f $main::lastScanFileName && ! $main::force ) {
151
      my $lastScan = time - ( stat( $main::lastScanFileName ) ) [9];
152
      return "Scan was run $lastScan seconds ago\n" unless $lastScan > $main::minScanTimes;
153
   }
154
   `touch $main::lastScanFileName`;
155
   &main::readDB(1);
156
   print Dumper( $main::statusDB->{'nodePopulation'} ) if $main::DEBUG > 2;
157
   my @targets;
158
   if ( $main::targetNode ) {
159
      push @targets, $main::targetNode;
160
   } else {
161
      @targets = keys %{$main::statusDB->{'node'}};
162
   }
163
   print "Scanning " . join( "\n", @targets ) . "\n" if $main::DEBUG;
164
   foreach my $node (@targets) {
165
      $main::statusDB->{'nodePopulation'}->{$node}->{'running'} = &getDomainsOnNode( $node );
166
      $main::statusDB->{'nodePopulation'}->{$node}->{'lastchecked'} = time;
167
      foreach my $domain ( keys %{$main::statusDB->{'nodePopulation'}->{$node}->{'running'}} ) {
168
         # make sure there is an entry for all of these domains
169
         $main::statusDB->{'virt'}->{$domain} = {} unless exists( $main::statusDB->{'virt'}->{$domain} );
170
      }
171
      print Dumper( $main::statusDB->{'nodePopulation'}->{$node} ) if $main::DEBUG > 2;
172
   }
173
   &main::writeDB();
174
   return "Node(s) updated\n";
175
}
176
 
177
sub makeCommand {
178
   my ( $node, $command ) = @_;
179
   return "ssh $node '$command'";
180
}
181
 
182
sub forceScan {
183
   my $save = $main::force;
184
   $main::force = 1;
185
   &main::scan();
186
   $main::force = $save;
187
}
188
 
189
 
190
# executes command $command, then repeatedly runs virsh list
191
# on $scanNode, grep'ing for $scanDomain
192
# $condition is 1 (true) or 0 (false)
193
sub executeAndWait {
194
   my ( $command, $scanNode, $scanDomain, $condition ) = @_;
195
   my $waitSeconds = 5; # number of seconds to wait before checking again
196
   my $maxIterations = 60 / $waitSeconds; # maximum number of tries
197
   print "Running [$command], then waiting $waitSeconds to check if complete\n" if $main::DEBUG;
198
   `$command`;
199
   my $waitCommand = &makeCommand( $scanNode, "virsh list | grep $scanDomain" );
200
   my $output = '';
201
   do {
202
      return 0 unless ( $maxIterations-- ); # we've waited too long, so probably not working
203
      print '. ';
204
      sleep 5;
205
      $output = `$waitCommand`;
206
      print "[$waitCommand] returned [$output]\n" if $main::DEBUG > 1;
207
   } until ( $condition ? $output : !$output );
208
   return 1; # made it successful
209
} 
210