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
use warnings;
31
use strict;  
32
 
25 rodolico 33
BEGIN {
34
   use FindBin;
35
   use File::Spec;
36
   # use libraries from the directory this script is in
37
   use Cwd 'abs_path';
38
   use File::Basename;
39
   use lib dirname( abs_path( __FILE__ ) );
40
}
41
 
3 rodolico 42
use Data::Dumper qw(Dumper); # Import the Dumper() subroutine
43
 
4 rodolico 44
# define the version number
45
# see https://metacpan.org/pod/release/JPEACOCK/version-0.97/lib/version.pod
46
use version;
19 rodolico 47
our $VERSION = version->declare("1.0.0");
4 rodolico 48
 
49
 
3 rodolico 50
use Exporter;
51
 
52
our @ISA = qw( Exporter );
53
our @EXPORT = qw( 
25 rodolico 54
                  &readDB
55
                  &writeDB
56
                  &report
57
                  &scan
58
                  &makeCommand
59
                  &forceScan
15 rodolico 60
                  &executeAndWait
18 rodolico 61
                  &findDomain
62
                  &diffArray
25 rodolico 63
                  &makeConfig
64
                  &readConfig
65
                  &getAvailableResources
66
                  &resource
67
                  &validateResources
68
                  &migrate
3 rodolico 69
                );
70
 
12 rodolico 71
# read a DB file (just a YAML)
72
# if $lock is set, will create a "lock" file so other processes will
73
# not try to write to it. Using custom code as flock is automagically
74
# release when the file is read
3 rodolico 75
 
76
sub readDB {
12 rodolico 77
   my $lock = shift;
25 rodolico 78
   my $lockFileName = "$main::config->{'status db filename'}.lock";
12 rodolico 79
   my $lockTime = 5; # maximum time to wait for lock to clear
80
   # wait for lock to clear if it exists, if we are wanting a lock
81
   # and we have tried it for $locktime iterations
82
   while ( $lock && -f $lockFileName && $lockTime-- ) {
83
      sleep 1; # wait one second, then try again
84
   }
85
   if ( $lock ) {
25 rodolico 86
      die "Something has $main::config->{'status db filename'} locked, aborting\n" if -f $lockFileName;
12 rodolico 87
      `touch $lockFileName`;
88
   }
3 rodolico 89
   my $yaml = YAML::Tiny->new( {} );
25 rodolico 90
   if ( -f $main::config->{'status db filename'} ) {
91
      $yaml = YAML::Tiny->read( $main::config->{'status db filename'} );
3 rodolico 92
   }
12 rodolico 93
   $main::statusDB = $yaml->[0];
3 rodolico 94
}
95
 
96
sub writeDB {
12 rodolico 97
   my $yaml = YAML::Tiny->new( $main::statusDB );
25 rodolico 98
   $yaml->write( $main::config->{'status db filename'} );
99
   unlink "$main::config->{'status db filename'}.lock" if -f "$main::config->{'status db filename'}.lock"; # release any lock we might have on it
3 rodolico 100
}
101
 
4 rodolico 102
sub report {
25 rodolico 103
   if ( $main::config->{'flags'}->{'format'} eq 'tsv' ) {
4 rodolico 104
      return &report_tsv( @_ );
105
   } else {
106
      return &report_screen( @_ );
107
   }
108
}
109
 
3 rodolico 110
sub report_tsv {
111
   my ( $header, $data ) = @_;
112
   my @output;
113
   push @output, join( "\t", @$header );
114
   for( my $line = 0; $line < @$data; $line++ ) {
115
      push @output, join( "\t", @{$data->[$line]} );
116
   } # for
117
   return join( "\n", @output ) . "\n";
118
}
119
 
120
sub report_screen {
121
   my ( $header, $data ) = @_;
122
   my @output;
123
   my @widths;
124
   my $column;
125
   my $row;
126
   # First, initialize by using the length of the headers
127
   for ( $column = 0; $column < @$header; $column++ ) {
128
      @widths[$column] = length( $header->[$column] );
129
   }
130
   # now, go through all data in each row, for each column, and increment the width if it is larger
131
   for ( $row = 0; $row < @$data; $row++ ) {
132
      for ( $column = 0; $column < @$header; $column++ ) {
133
         $widths[$column] = length( $data->[$row][$column] ) 
134
            if length( $data->[$row][$column] ) > $widths[$column];
135
      } # for column
136
   } # for row
137
   # actually do the print now
138
   my @format;
139
   for ( $column = 0; $column < @widths; $column++ ) {
140
      push ( @format, '%' . $widths[$column] . 's' );
141
   }
142
   my $format = join( ' ', @format ) . "\n";
143
   my $output = sprintf( $format, @$header );
144
   for ( $row = 0; $row < @$data; $row++ ) {
145
      $output .= sprintf( $format, @{$data->[$row]} );
146
   } # for row
147
   return $output;
148
}
10 rodolico 149
 
15 rodolico 150
# scans a node to determine which domains are running on it
151
sub getDomainsOnNode {
152
   my $node = shift;
25 rodolico 153
   my $command = &main::makeCommand( $node, 'virsh list' );
154
   print "havirt.pm:getDomainsOnNode, command is $command\n" if $main::config->{'flags'}->{'debug'} > 2;
155
   my @nodeList = grep { /^\s*\d/ } `$command`;
15 rodolico 156
   for ( my $i = 0; $i < @nodeList; $i++ ) {
157
      if ( $nodeList[$i] =~ m/\s*\d+\s*([^ ]+)/ ) {
158
         $nodeList[$i] = $1;
159
      }
160
   }
161
   my %hash = map{ $_ => time } @nodeList;
162
   return \%hash;
163
}
164
 
18 rodolico 165
# find node a domain is on
166
# first parameter is the domain name
167
# rest of @_ is list of nodes to search
168
# if no nodes passed in, will search all known nodes
169
# returns first node found with the domain, or an empty string if not found
170
# possibly not being used??
171
sub findDomain {
172
   my $domainName = shift;
173
   my @node = @_;
174
   my $foundNode = '';
175
   &readDB();
176
   unless ( @node ) {
177
      @node = keys %{$main::statusDB->{'node'} };
25 rodolico 178
      print "findDomain, nodes = " . join( "\t", @node ) . "\n" if $main::config->{'flags'}->{'debug'} > 1;
18 rodolico 179
   }
180
   foreach my $thisNode ( @node ) {
25 rodolico 181
      my $command = &main::makeCommand( $thisNode, 'virsh list' );
182
      my $output = `$command`;
183
      print "findDomain, $thisNode list =\n" . $output . "\n" if $main::config->{'flags'}->{'debug'} > 1;;
18 rodolico 184
      return $thisNode if ( $output =~ m/$domainName/ );
185
   }
186
   return '';
187
}
15 rodolico 188
 
189
# check one or more nodes and determine which domains are running on them.
190
# defaults to everything in the node database, but the -t can have it run on only one
191
# this is the function that should be run every few minutes on one of the servers
192
sub scan {
25 rodolico 193
   my @targets = @_;
194
   if ( -f $main::config->{'last scan filename'} && ! $main::config->{'flags'}->{'yes'} ) {
195
      my $lastScan = time - ( stat( $main::config->{'last scan filename'} ) ) [9];
196
      return "Scan was run $lastScan seconds ago\n" unless $lastScan > $main::config->{'minum scan time'};
15 rodolico 197
   }
25 rodolico 198
   `touch $main::config->{'last scan filename'}`;
15 rodolico 199
   &main::readDB(1);
25 rodolico 200
   print Dumper( $main::statusDB->{'nodePopulation'} ) if $main::config->{'flags'}->{'debug'} > 2;
201
   if ( $main::config->{'flags'}->{'target'} ) {
202
      push @targets, $main::config->{'flags'}->{'target'};
15 rodolico 203
   }
25 rodolico 204
   @targets = keys %{$main::statusDB->{'node'}} unless @targets;
205
   print "Scanning " . join( "\n", @targets ) . "\n" if $main::config->{'flags'}->{'debug'};
15 rodolico 206
   foreach my $node (@targets) {
207
      $main::statusDB->{'nodePopulation'}->{$node}->{'running'} = &getDomainsOnNode( $node );
208
      $main::statusDB->{'nodePopulation'}->{$node}->{'lastchecked'} = time;
209
      foreach my $domain ( keys %{$main::statusDB->{'nodePopulation'}->{$node}->{'running'}} ) {
210
         # make sure there is an entry for all of these domains
211
         $main::statusDB->{'virt'}->{$domain} = {} unless exists( $main::statusDB->{'virt'}->{$domain} );
212
      }
25 rodolico 213
      print Dumper( $main::statusDB->{'nodePopulation'}->{$node} ) if $main::config->{'flags'}->{'debug'} > 2;
15 rodolico 214
   }
215
   &main::writeDB();
216
   return "Node(s) updated\n";
217
}
218
 
18 rodolico 219
# makes the command that will be run on a node
220
# Created as a sub so we can change format easily
25 rodolico 221
# if node is the node we're on, we don't need to do a remote call
222
# if node is null, we'll assume we do the command here
223
# otherwise, we'll do an ssh to the node and run the command there
15 rodolico 224
sub makeCommand {
225
   my ( $node, $command ) = @_;
25 rodolico 226
   my $me = `hostname`;
227
   chomp $me;
228
   if ( ! $node || $node eq $me ) {
229
      return $command;
230
   } else {
231
      return "ssh $node '$command'";
232
   }
15 rodolico 233
}
234
 
18 rodolico 235
# force a node scan, even if time has not expired
15 rodolico 236
sub forceScan {
25 rodolico 237
   my $save = $main::config->{'flags'}->{'yes'};
238
   $main::config->{'flags'}->{'yes'} = 1;
15 rodolico 239
   &main::scan();
25 rodolico 240
   $main::config->{'flags'}->{'yes'} = $save;
15 rodolico 241
}
242
 
243
 
244
# executes command $command, then repeatedly runs virsh list
245
# on $scanNode, grep'ing for $scanDomain
246
# $condition is 1 (true) or 0 (false)
247
sub executeAndWait {
248
   my ( $command, $scanNode, $scanDomain, $condition ) = @_;
249
   my $waitSeconds = 5; # number of seconds to wait before checking again
250
   my $maxIterations = 60 / $waitSeconds; # maximum number of tries
25 rodolico 251
   print "Running [$command], then waiting $waitSeconds to check if complete\n" if $main::config->{'flags'}->{'debug'};
15 rodolico 252
   `$command`;
253
   my $waitCommand = &makeCommand( $scanNode, "virsh list | grep $scanDomain" );
254
   my $output = '';
255
   do {
256
      return 0 unless ( $maxIterations-- ); # we've waited too long, so probably not working
257
      print '. ';
25 rodolico 258
      sleep 1;
15 rodolico 259
      $output = `$waitCommand`;
25 rodolico 260
      print "[$waitCommand] returned [$output]\n" if $main::config->{'flags'}->{'debug'} > 1;
15 rodolico 261
   } until ( $condition ? $output : !$output );
262
   return 1; # made it successful
263
} 
264
 
18 rodolico 265
# find the differences between two arrays (passed by reference)
266
# first sorts the array, then walks through them one by one
267
# @$arr1 MUST be larger than @$arr2
268
sub diffArray {
269
   my ( $arr1, $arr2 ) = @_;
270
   my @result;
271
 
272
   @$arr1 = sort @$arr1;
273
   @$arr2 = sort @$arr2;
274
   my $i=0;
275
   my $j=0;
276
 
277
   while ( $i < @$arr1 ) {
278
      if ( $arr1->[$i] eq $arr2->[$j] ) {
279
         $i++;
280
         $j++;
281
      } elsif ( $arr1->[$i] lt $arr2->[$j] ) {
282
         push @result, $arr1->[$i];
283
         $i++;
284
      } else {
285
         push @result, $arr2->[$j];
286
         $j++;
287
      }
288
   }
289
   return \@result;
290
}
25 rodolico 291
 
292
 
293
# create a config file if one does not exist
294
sub makeConfig {
295
   my ( $config, $filename ) = @_;
296
   $config->{'script dir'} = $FindBin::RealBin;
297
   $config->{'script name'} = $FindBin::Script;
298
   $config->{'db dir'} = $config->{'script dir'} . '/var';
299
   $config->{'conf dir'} = $config->{'script dir'} . '/conf';
300
   $config->{'status db filename'} = $config->{'db dir'} . '/status.yaml';
301
   $config->{'last scan filename'} = $config->{'script dir'} . '/var/lastscan';
302
   $config->{'minum scan time'} = 5 * 60; # five minutes
303
   $config->{'node reserved memory'} = 8 * 1024 * 1024; # 8 gigabytes
304
   $config->{'node reserved vcpu' } = 0; # turn off reserved vcpu
305
   $config->{'flags'}->{'format'} = 'screen';
306
   $config->{'flags'}->{'yes'} = 0;
307
   $config->{'flags'}->{'quiet'} = 0;
308
   $config->{'flags'}->{'target'} = '';
309
   $config->{'flags'}->{'dryrun'} = 1;
310
   $config->{'flags'}->{'debug'} = 0;
311
   $config->{'flags'}->{'help'} = 0;
312
   $config->{'flags'}->{'version'} = 0;
313
   my $yaml = YAML::Tiny->new( $config );
314
   $yaml->write( $filename );
315
}
316
 
317
# read the config file and return it
318
sub readConfig {
319
   my $filename = shift;
320
   my $yaml = YAML::Tiny->new( {} );
321
   if ( -f $filename ) {
322
      $yaml = YAML::Tiny->read( $filename );
323
   }
324
   return $yaml->[0];
325
}
326
 
327
# find available resource on a node
328
sub resource {
329
   my $node = shift;
330
   die "Can not find node $node in havirt.pm:resource\n"
331
      unless $main::statusDB->{'node'}->{$node};
332
   my $return = {
333
      'memory' => 0,
334
      'cpu_count' => 0
335
      };
336
   foreach my $key ( keys %$return ) {
337
      $return->{$key} = $main::statusDB->{'node'}->{$node}->{$key}
338
         if defined $main::statusDB->{'node'}->{$node}->{$key};
339
   } # foreach
340
   return $return;
341
}
342
 
343
sub getAvailableResources {
344
   my $node = shift;
345
   &readDB();
346
   die "Can not find node $node in havirt.pm:resource\n"
347
      unless $main::statusDB->{'node'}->{$node};
348
   my $totalResources = &resource( $node );
349
   print Dumper( $totalResources ) if $main::config->{'flags'}->{'debug'};
350
   foreach my $domain ( keys %{ $main::statusDB->{'nodePopulation'}->{$node}->{'running'} } ) {
351
      $totalResources->{'memory'} -= $main::statusDB->{'virt'}->{$domain}->{'memory'};
352
      $totalResources->{'cpu_count'} -= $main::statusDB->{'virt'}->{$domain}->{'vcpu'};
353
   }
354
   return $totalResources;
355
}
356
 
357
# validate that node has enough resources for the domains which occupy the
358
# remainder of the stack
359
# returns 0 on success, or one or more error messages in a string on failure
360
sub validateResources {
361
   my $node = shift;
362
   &readDB();
363
   my @return;
364
   my $nodeResources = &getAvailableResources( $node );
365
   print "In havirt.pm:validateResources, checking if enough room on $node for\n" . join( "\n", @_ ) . "\n"
366
      if ( $main::config->{'flags'}->{'debug'} );
367
   # subtract the reserved memory from the node
368
   $nodeResources->{'memory'} -= $main::config->{'node reserved memory'};
369
   $nodeResources->{'cpu_count'} -= $main::config->{'node reserved vcpu'} if $main::config->{'node reserved vcpu'};
370
   while ( my $domain = shift ) {
371
      $nodeResources->{'memory'} -= $main::statusDB->{'virt'}->{$domain}->{'memory'};
372
      $nodeResources->{'cpu_count'} -= $main::statusDB->{'virt'}->{$domain}->{'vcpu'};
373
   }
374
   print "In havirt.pm:validateResources, $node will have $nodeResources->{memory} memory and $nodeResources->{cpu_count} vcpu's after task\n"
375
      if ( $main::config->{'flags'}->{'debug'} > 1 );
376
 
377
   push @return, "This action would result in memory of $nodeResources->{memory}" if $nodeResources->{'memory'} <= 0;
378
   push @return, "This action would result in virtual cpu count of $nodeResources->{cpu_count}" if $nodeResources->{'cpu_count'} <= 0 && $main::config->{'flags'}->{'node reserved vcpu'};
379
   return @return ? join( "\n", @return ) . "\n" : 0;
380
}
381
 
382
# migrate domain from current node it is on to $target
383
sub migrate {
384
   my ( $virt, $target ) = @_;
385
   my $return;
386
   my $node;
387
   # these are replaced by the safer findDomain
388
   #&main::forceScan();
389
   #&main::readDB();
390
   $node = &main::findDomain( $virt );
391
   print Dumper( $main::statusDB->{'nodePopulation'} ) if $main::config->{'flags'}->{'debug'} > 2;
392
   die "I can not find $virt on any node\n" unless $node;
393
   die "Domain $virt in maintenance mode, can not migrate it\n" if $main::statusDB->{'virt'}->{$virt}->{'maintenance'};
394
   die "Node $target in maintenance mode, can not migrate anything to it\n" if $main::statusDB->{'node'}->{$target}->{'maintenance'};
395
   die "$virt already on $target\n" if $target eq $node;
396
   my $command = &main::makeCommand( $node, "virsh migrate --live --persistent --verbose  $virt qemu+ssh://$target/system" );
397
   if ( $main::config->{'flags'}->{'yes'} ) { # they want us to actually do it
398
      $return = ( &main::executeAndWait( $command, $node, $virt, 0 ) ? 'Success' : 'Time Out waiting for shutdown');
399
      &main::forceScan();
400
   } else {
401
      $return = $command;
402
   }
403
   return "$return\n";
404
}
405