Subversion Repositories havirt

Rev

Rev 15 | Rev 19 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 15 Rev 18
Line 25... Line 25...
25
# v0.0.1 20240602 RWR
25
# v0.0.1 20240602 RWR
26
# Initial setup
26
# Initial setup
27
 
27
 
28
package havirt;
28
package havirt;
29
 
29
 
30
 
-
 
31
use warnings;
30
use warnings;
32
use strict;  
31
use strict;  
33
 
32
 
34
use Data::Dumper qw(Dumper); # Import the Dumper() subroutine
33
use Data::Dumper qw(Dumper); # Import the Dumper() subroutine
35
 
34
 
Line 45... Line 44...
45
our @EXPORT = qw( 
44
our @EXPORT = qw( 
46
                  &readDB &writeDB
45
                  &readDB &writeDB
47
                  &report &scan
46
                  &report &scan
48
                  &makeCommand &forceScan
47
                  &makeCommand &forceScan
49
                  &executeAndWait
48
                  &executeAndWait
-
 
49
                  &findDomain
-
 
50
                  &diffArray
50
                );
51
                );
51
 
52
 
52
# read a DB file (just a YAML)
53
# read a DB file (just a YAML)
53
# if $lock is set, will create a "lock" file so other processes will
54
# 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
# not try to write to it. Using custom code as flock is automagically
Line 139... Line 140...
139
   }
140
   }
140
   my %hash = map{ $_ => time } @nodeList;
141
   my %hash = map{ $_ => time } @nodeList;
141
   return \%hash;
142
   return \%hash;
142
}
143
}
143
 
144
 
-
 
145
# find node a domain is on
-
 
146
# first parameter is the domain name
-
 
147
# rest of @_ is list of nodes to search
-
 
148
# if no nodes passed in, will search all known nodes
-
 
149
# returns first node found with the domain, or an empty string if not found
-
 
150
# possibly not being used??
-
 
151
sub findDomain {
-
 
152
   my $domainName = shift;
-
 
153
   my @node = @_;
-
 
154
   my $foundNode = '';
-
 
155
   &readDB();
-
 
156
   unless ( @node ) {
-
 
157
      @node = keys %{$main::statusDB->{'node'} };
-
 
158
      print "findDomain, nodes = " . join( "\t", @node ) . "\n" if $main::DEBUG > 1;
-
 
159
   }
-
 
160
   foreach my $thisNode ( @node ) {
-
 
161
      my $output = `ssh $thisNode 'virsh list'`;
-
 
162
      print "findDomain, $thisNode list =\n" . $output . "\n" if $main::DEBUG > 1;;
-
 
163
      return $thisNode if ( $output =~ m/$domainName/ );
-
 
164
   }
-
 
165
   return '';
144
 
166
}
145
 
167
 
146
# check one or more nodes and determine which domains are running on them.
168
# 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
169
# 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
170
# this is the function that should be run every few minutes on one of the servers
149
sub scan {
171
sub scan {
Line 172... Line 194...
172
   }
194
   }
173
   &main::writeDB();
195
   &main::writeDB();
174
   return "Node(s) updated\n";
196
   return "Node(s) updated\n";
175
}
197
}
176
 
198
 
-
 
199
# makes the command that will be run on a node
-
 
200
# Created as a sub so we can change format easily
177
sub makeCommand {
201
sub makeCommand {
178
   my ( $node, $command ) = @_;
202
   my ( $node, $command ) = @_;
179
   return "ssh $node '$command'";
203
   return "ssh $node '$command'";
180
}
204
}
181
 
205
 
-
 
206
# force a node scan, even if time has not expired
182
sub forceScan {
207
sub forceScan {
183
   my $save = $main::force;
208
   my $save = $main::force;
184
   $main::force = 1;
209
   $main::force = 1;
185
   &main::scan();
210
   &main::scan();
186
   $main::force = $save;
211
   $main::force = $save;
Line 206... Line 231...
206
      print "[$waitCommand] returned [$output]\n" if $main::DEBUG > 1;
231
      print "[$waitCommand] returned [$output]\n" if $main::DEBUG > 1;
207
   } until ( $condition ? $output : !$output );
232
   } until ( $condition ? $output : !$output );
208
   return 1; # made it successful
233
   return 1; # made it successful
209
} 
234
} 
210
 
235
 
-
 
236
# find the differences between two arrays (passed by reference)
-
 
237
# first sorts the array, then walks through them one by one
-
 
238
# @$arr1 MUST be larger than @$arr2
-
 
239
sub diffArray {
-
 
240
   my ( $arr1, $arr2 ) = @_;
-
 
241
   my @result;
-
 
242
 
-
 
243
   @$arr1 = sort @$arr1;
-
 
244
   @$arr2 = sort @$arr2;
-
 
245
   my $i=0;
-
 
246
   my $j=0;
-
 
247
 
-
 
248
   while ( $i < @$arr1 ) {
-
 
249
      if ( $arr1->[$i] eq $arr2->[$j] ) {
-
 
250
         $i++;
-
 
251
         $j++;
-
 
252
      } elsif ( $arr1->[$i] lt $arr2->[$j] ) {
-
 
253
         push @result, $arr1->[$i];
-
 
254
         $i++;
-
 
255
      } else {
-
 
256
         push @result, $arr2->[$j];
-
 
257
         $j++;
-
 
258
      }
-
 
259
   }
-
 
260
   return \@result;
-
 
261
}