Subversion Repositories havirt

Rev

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

Rev 12 Rev 15
Line 42... Line 42...
42
use Exporter;
42
use Exporter;
43
 
43
 
44
our @ISA = qw( Exporter );
44
our @ISA = qw( Exporter );
45
our @EXPORT = qw( 
45
our @EXPORT = qw( 
46
                  &readDB &writeDB
46
                  &readDB &writeDB
47
                  &report
47
                  &report &scan
-
 
48
                  &makeCommand &forceScan
-
 
49
                  &executeAndWait
48
                );
50
                );
49
 
51
 
50
# read a DB file (just a YAML)
52
# read a DB file (just a YAML)
51
# if $lock is set, will create a "lock" file so other processes will
53
# if $lock is set, will create a "lock" file so other processes will
52
# not try to write to it. Using custom code as flock is automagically
54
# not try to write to it. Using custom code as flock is automagically
Line 124... Line 126...
124
      $output .= sprintf( $format, @{$data->[$row]} );
126
      $output .= sprintf( $format, @{$data->[$row]} );
125
   } # for row
127
   } # for row
126
   return $output;
128
   return $output;
127
}
129
}
128
 
130
 
-
 
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