Subversion Repositories havirt

Rev

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

Rev 17 Rev 18
Line 67... Line 67...
67
   push @return, "\tIf maintenance flag is set, no havirt will refuse any actions";
67
   push @return, "\tIf maintenance flag is set, no havirt will refuse any actions";
68
   return join( "\n", @return ) . "\n";
68
   return join( "\n", @return ) . "\n";
69
}
69
}
70
 
70
 
71
 
71
 
72
# find node a domain is on
-
 
73
# first parameter is the domain name
-
 
74
# rest of @_ is list of nodes to search
-
 
75
# if no nodes passed in, will search all known nodes
-
 
76
# returns first node found with the domain, or an empty string if not found
-
 
77
# possibly not being used??
-
 
78
sub findDomain {
-
 
79
   my $domainName = shift;
-
 
80
   my @node = @_;
-
 
81
   my $foundNode = '';
-
 
82
   unless ( @node ) {
-
 
83
      @node = keys %{$main::statusDB->{'node'} };
-
 
84
   }
-
 
85
   foreach my $thisNode ( @node ) {
-
 
86
      my $output = `ssh $thisNode 'virsh list'`;
-
 
87
      return $thisNode if ( $output =~ m/domainName/ );
-
 
88
   }
-
 
89
   return '';
-
 
90
}
-
 
91
 
-
 
92
sub list {
72
sub list {
93
   &main::readDB();
73
   &main::readDB();
94
   print Dumper( $main::statusDB->{'nodePopulation'} ) if $main::DEBUG > 2;
74
   print Dumper( $main::statusDB->{'nodePopulation'} ) if $main::DEBUG > 2;
95
 
75
 
96
   my @header;
76
   my @header;
97
   my @data;
77
   my @data;
-
 
78
   my @found;
98
   
79
   
99
   foreach my $node ( sort keys %{$main::statusDB->{'nodePopulation'}} ) {
80
   foreach my $node ( sort keys %{$main::statusDB->{'nodePopulation'}} ) {
100
      foreach my $virt (sort keys %{$main::statusDB->{'nodePopulation'}->{$node}->{'running'}} ) {
81
      foreach my $virt (sort keys %{$main::statusDB->{'nodePopulation'}->{$node}->{'running'}} ) {
101
         unless ( @header ) {
82
         unless ( @header ) {
102
            # if we don't have a header yet, create it from the keys in this one. Assumes every entry has same keys
83
            # if we don't have a header yet, create it from the keys in this one. Assumes every entry has same keys
103
            @header = sort keys %{ $main::statusDB->{'virt'}->{$virt} };
84
            @header = sort keys %{ $main::statusDB->{'virt'}->{$virt} };
104
            unshift @header, 'Domain';
85
            unshift @header, 'Domain';
105
            unshift @header, 'Node';
86
            unshift @header, 'Node';
106
         } # unless
87
         } # unless
-
 
88
         push @found, $virt;
107
         my @line;
89
         my @line;
108
         push @line, $node;
90
         push @line, $node;
109
         push @line, $virt;
91
         push @line, $virt;
110
         foreach my $column ( sort keys %{ $main::statusDB->{'virt'}->{$virt} } ) {
92
         foreach my $column ( sort keys %{ $main::statusDB->{'virt'}->{$virt} } ) {
111
            push @line, $main::statusDB->{'virt'}->{$virt}->{$column};
93
            push @line, $main::statusDB->{'virt'}->{$virt}->{$column};
112
         }
94
         }
113
         push @data, \@line;
95
         push @data, \@line;
114
      }
96
      }
115
   }
97
   }
-
 
98
   my @allDomains = keys %{$main::statusDB->{'virt'} };
-
 
99
   my $notRunning = &main::diffArray( \@allDomains, \@found );
-
 
100
   my @padding;
-
 
101
   while ( @padding < @header - 2 ) {
-
 
102
      push @padding, '';
116
   
103
   }
-
 
104
   for ( my $i = 0; $i < @$notRunning; $i++ ) {
-
 
105
      my @line;
-
 
106
      push @line, 'Down';
-
 
107
      push @line, $notRunning->[$i];
-
 
108
      push @line, @padding;
-
 
109
      push @data, \@line;
-
 
110
   }
-
 
111
 
117
   return &main::report( \@header, \@data );
112
   return &main::report( \@header, \@data );
118
}
113
}
119
 
114
 
120
# reread the domain definition file and freshen the database
115
# reread the domain definition file and freshen the database
121
sub update {
116
sub update {
-
 
117
   my @requiredFields = ( 'maintenance' );
122
   &main::readDB(1); # loading it for write, so lock
118
   &main::readDB(1); # loading it for write, so lock
123
   unless ( @_ ) {
119
   unless ( @_ ) {
124
      # they didn't pass in anything, so do everything
120
      # they didn't pass in anything, so do everything
125
      @_ = keys %{ $main::statusDB->{'virt'} }
121
      @_ = keys %{ $main::statusDB->{'virt'} }
126
   } # unless
122
   } # unless
127
   print "Preparing to update " . join( "\n", @_ ) . "\n" if $main::DEBUG > 1;
123
   print "Preparing to update " . join( "\n", @_ ) . "\n" if $main::DEBUG > 1;
128
   while ( my $virt = shift ) {
124
   while ( my $virt = shift ) {
129
      &parseDomain( $virt );
125
      &parseDomain( $virt );
-
 
126
      foreach my $field ( @requiredFields ) {
-
 
127
         $main::statusDB->{'virt'}->{$virt}->{$field} = '' 
-
 
128
            unless defined ( $main::statusDB->{'virt'}->{$virt}->{$field} );
-
 
129
      } # foreach
130
   } # while
130
   } # while
131
   &main::writeDB( $main::domainDBName, $main::statusDB->{'virt'} );
131
   &main::writeDB( $main::domainDBName, $main::statusDB->{'virt'} );
132
   return "Updated\n";
132
   return "Updated\n";
133
}
133
}
134
 
134
 
Line 201... Line 201...
201
sub start {
201
sub start {
202
   my ( $virt, $node ) = @_;
202
   my ( $virt, $node ) = @_;
203
   my $return;
203
   my $return;
204
   $node = `hostname` unless $node;
204
   $node = `hostname` unless $node;
205
   chomp $node;
205
   chomp $node;
-
 
206
   return "Domain $virt in maintenance mode, can not start\n" if $main::statusDB->{'virt'}->{$virt}->{'maintenance'};
-
 
207
   return "Node $node in maintenance mode, can not start\n" if $main::statusDB->{'node'}->{$node}->{'maintenance'};
-
 
208
   # these are replaced by the safer findDomain
206
   &main::forceScan();
209
   #&main::forceScan();
207
   &main::readDB();
210
   #&main::readDB();
208
   for my $myNode ( keys %{$main::statusDB->{'nodePopulation'} } ) {
211
   if ( my $foundNode = &main::findDomain( $virt ) ) {
209
      die "$virt already running on $myNode, not starting\n" if ( $main::statusDB->{'nodePopulation'}->{$myNode}->{'running'}->{$virt} );
212
      die "$virt already running on $foundNode, not starting\n";
210
   }
213
   }
211
   die "I do not have a definition for $virt\n" unless exists( $main::statusDB->{'virt'}->{$virt} );
214
   die "I do not have a definition for $virt\n" unless exists( $main::statusDB->{'virt'}->{$virt} );
212
   print Dumper( $main::statusDB->{'nodePopulation'} ) if $main::DEBUG > 2;
215
   print Dumper( $main::statusDB->{'nodePopulation'} ) if $main::DEBUG > 2;
213
   my $filename = "$main::confDir/$virt.xml";
216
   my $filename = "$main::confDir/$virt.xml";
214
   my $command = &main::makeCommand( $node, "virsh create $filename" );
217
   my $command = &main::makeCommand( $node, "virsh create $filename" );
Line 223... Line 226...
223
 
226
 
224
sub shutdown {
227
sub shutdown {
225
   my $virt = shift;
228
   my $virt = shift;
226
   my $node = '';
229
   my $node = '';
227
   my $return;
230
   my $return;
-
 
231
   # these are replaced by the safer findDomain
228
   &main::forceScan();
232
   #&main::forceScan();
229
   &main::readDB();
233
   #&main::readDB();
230
   for my $myNode ( keys %{$main::statusDB->{'nodePopulation'} } ) {
234
   $node = &main::findDomain( $virt );
231
      if ( $main::statusDB->{'nodePopulation'}->{$myNode}->{'running'}->{$virt} ) {
235
   die "I could not find the domain $virt running\n" unless $node;
232
         $node = $myNode;
-
 
233
         last;
-
 
234
      }
-
 
235
   }
-
 
236
   print Dumper( $main::statusDB->{'nodePopulation'} ) if $main::DEBUG > 2;
236
   print Dumper( $main::statusDB->{'nodePopulation'} ) if $main::DEBUG > 2;
237
   die "I can not find $virt on any node\n" unless $node;
237
   die "I can not find $virt on any node\n" unless $node;
238
   my $command = &main::makeCommand( $node, "virsh shutdown $virt" );
238
   my $command = &main::makeCommand( $node, "virsh shutdown $virt" );
239
   if ( $main::force ) { # they want us to actually do it
239
   if ( $main::force ) { # they want us to actually do it
240
      $return = ( &main::executeAndWait( $command, $node, $virt, 0 ) ? 'Success' : 'Time Out waiting for shutdown');
240
      $return = ( &main::executeAndWait( $command, $node, $virt, 0 ) ? 'Success' : 'Time Out waiting for shutdown');
Line 247... Line 247...
247
 
247
 
248
sub migrate {
248
sub migrate {
249
   my ( $virt, $target ) = @_;
249
   my ( $virt, $target ) = @_;
250
   my $return;
250
   my $return;
251
   my $node;
251
   my $node;
-
 
252
   # these are replaced by the safer findDomain
252
   &main::forceScan();
253
   #&main::forceScan();
253
   &main::readDB();
254
   #&main::readDB();
254
   for my $myNode ( keys %{$main::statusDB->{'nodePopulation'} } ) {
-
 
255
      if ( $main::statusDB->{'nodePopulation'}->{$myNode}->{'running'}->{$virt} ) {
-
 
256
         $node = $myNode;
255
   $node = &main::findDomain( $virt );
257
         last;
-
 
258
      }
-
 
259
   }
-
 
260
   print Dumper( $main::statusDB->{'nodePopulation'} ) if $main::DEBUG > 2;
256
   print Dumper( $main::statusDB->{'nodePopulation'} ) if $main::DEBUG > 2;
261
   die "I can not find $virt on any node\n" unless $node;
257
   die "I can not find $virt on any node\n" unless $node;
-
 
258
   die "Domain $virt in maintenance mode, can not migrate it\n" if $main::statusDB->{'virt'}->{$virt}->{'maintenance'};
-
 
259
   die "Node $target in maintenance mode, can not migrate anything to it\n" if $main::statusDB->{'node'}->{$target}->{'maintenance'};
-
 
260
   die "$virt already on $target\n" if $target eq $node;
262
   my $command = &main::makeCommand( $node, "virsh migrate --live --persistent --verbose  $virt qemu+ssh://$target/system" );
261
   my $command = &main::makeCommand( $node, "virsh migrate --live --persistent --verbose  $virt qemu+ssh://$target/system" );
263
   if ( $main::force ) { # they want us to actually do it
262
   if ( $main::force ) { # they want us to actually do it
264
      $return = ( &main::executeAndWait( $command, $node, $virt, 0 ) ? 'Success' : 'Time Out waiting for shutdown');
263
      $return = ( &main::executeAndWait( $command, $node, $virt, 0 ) ? 'Success' : 'Time Out waiting for shutdown');
265
      &main::forceScan();
264
      &main::forceScan();
266
   } else {
265
   } else {