Subversion Repositories havirt

Rev

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

Rev 19 Rev 25
Line 69... Line 69...
69
}
69
}
70
 
70
 
71
 
71
 
72
sub list {
72
sub list {
73
   &main::readDB();
73
   &main::readDB();
74
   print Dumper( $main::statusDB->{'nodePopulation'} ) if $main::DEBUG > 2;
74
   print Dumper( $main::statusDB->{'nodePopulation'} ) if $main::config->{'flags'}->{'debug'} > 2;
75
 
75
 
76
   my @header;
76
   my @header;
77
   my @data;
77
   my @data;
78
   my @found;
78
   my @found;
79
   
79
   
Line 118... Line 118...
118
   &main::readDB(1); # loading it for write, so lock
118
   &main::readDB(1); # loading it for write, so lock
119
   unless ( @_ ) {
119
   unless ( @_ ) {
120
      # they didn't pass in anything, so do everything
120
      # they didn't pass in anything, so do everything
121
      @_ = keys %{ $main::statusDB->{'virt'} }
121
      @_ = keys %{ $main::statusDB->{'virt'} }
122
   } # unless
122
   } # unless
123
   print "Preparing to update " . join( "\n", @_ ) . "\n" if $main::DEBUG > 1;
123
   print "Preparing to update " . join( "\n", @_ ) . "\n" if $main::config->{'flags'}->{'debug'} > 1;
124
   while ( my $virt = shift ) {
124
   while ( my $virt = shift ) {
125
      &parseDomain( $virt );
125
      &parseDomain( $virt );
126
      foreach my $field ( @requiredFields ) {
126
      foreach my $field ( @requiredFields ) {
127
         $main::statusDB->{'virt'}->{$virt}->{$field} = '' 
127
         $main::statusDB->{'virt'}->{$virt}->{$field} = '' 
128
            unless defined ( $main::statusDB->{'virt'}->{$virt}->{$field} );
128
            unless defined ( $main::statusDB->{'virt'}->{$virt}->{$field} );
Line 136... Line 136...
136
# finds one xml value in file.
136
# finds one xml value in file.
137
# since libvirt does not use good xml which can be parsed by
137
# since libvirt does not use good xml which can be parsed by
138
# several libraries, we must do it manually with regex's
138
# several libraries, we must do it manually with regex's
139
sub getXMLValue {
139
sub getXMLValue {
140
   my ( $key, $string ) = @_;
140
   my ( $key, $string ) = @_;
141
   print "getXMLValue: looking for [$key] $string\n" if $main::DEBUG > 2;
141
   print "getXMLValue: looking for [$key] $string\n" if $main::config->{'flags'}->{'debug'} > 2;
142
   my $start = "<$key";
142
   my $start = "<$key";
143
   my $end = "</$key>";
143
   my $end = "</$key>";
144
   $string =~ m/$start([^>]*)>([^<]+)$end/;
144
   $string =~ m/$start([^>]*)>([^<]+)$end/;
145
   return ($1,$2);
145
   return ($1,$2);
146
}
146
}
Line 150... Line 150...
150
# if config file exists in conf, just read it. If it does not
150
# if config file exists in conf, just read it. If it does not
151
# exist, or if force is set, do a dumpxml on the running
151
# exist, or if force is set, do a dumpxml on the running
152
# domain, put it into conf/ and load it.
152
# domain, put it into conf/ and load it.
153
sub parseDomain {
153
sub parseDomain {
154
   my ($virt, $nodePopulations ) = @_;
154
   my ($virt, $nodePopulations ) = @_;
155
 
-
 
-
 
155
   print "Parsing domain $virt in domain.pm:parseDomain\n" if $main::config->{'flags'}->{'debug'};
156
   my @keysToSave = ( 'uuid', 'memory', 'vcpu','vnc' );
156
   my @keysToSave = ( 'uuid', 'memory', 'vcpu','vnc' );
157
   my $filename = "$main::confDir/$virt.xml";
157
   my $filename = "$main::config->{'conf dir'}/$virt.xml";
158
   my $xml = &getVirtConfig( $virt, $filename );
158
   my $xml = &getVirtConfig( $virt, $filename );
159
   my ($param,$value) = &getXMLValue( 'uuid', $xml );
159
   my ($param,$value) = &getXMLValue( 'uuid', $xml );
160
   $main::statusDB->{'virt'}->{$virt}->{'uuid'} = $value;
160
   $main::statusDB->{'virt'}->{$virt}->{'uuid'} = $value;
161
   ($param,$value) = &getXMLValue( 'memory', $xml );
161
   ($param,$value) = &getXMLValue( 'memory', $xml );
162
   $main::statusDB->{'virt'}->{$virt}->{'memory'} = $value;
162
   $main::statusDB->{'virt'}->{$virt}->{'memory'} = $value;
163
   ($param,$value) = &getXMLValue( 'vcpu', $xml );
163
   ($param,$value) = &getXMLValue( 'vcpu', $xml );
164
   $main::statusDB->{'virt'}->{$virt}->{'vcpu'} = $value;
164
   $main::statusDB->{'virt'}->{$virt}->{'vcpu'} = $value;
165
 
165
 
166
   $xml =~ m/type='vnc' port='(\d+)'/;
166
   $xml =~ m/type='vnc' port='(\d+)'/;
167
   $main::statusDB->{'virt'}->{$virt}->{'vnc'} = $1;
167
   $main::statusDB->{'virt'}->{$virt}->{'vnc'} = $1;
-
 
168
   print "After Parsing $virt in domain.pm:parseDomain\n" . Dumper($main::statusDB->{'virt'}->{$virt}) . "\n"  if $main::config->{'flags'}->{'debug'} > 2;
168
}
169
}
169
 
170
 
170
# returns xml definition of a domain
171
# returns xml definition of a domain
171
# if the definition exists in conf/, simply open and read
172
# if the definition exists in conf/, simply open and read
172
# if the definition file does not exist, or if $force is set, find the 
173
# if the definition file does not exist, or if $force is set, find the 
173
# running domain, perform an xml dump of it, save it to conf/, then 
174
# running domain, perform an xml dump of it, save it to conf/, then 
174
# return it.
175
# return it.
175
sub getVirtConfig {
176
sub getVirtConfig {
176
   my ($virt,$filename) = @_;
177
   my ($virt,$filename) = @_;
177
   my $return;
178
   my $return;
178
   print "In getVirtConfig looking for $virt with file $filename, force is $main::force\n" if $main::DEBUG;
179
   print "In getVirtConfig looking for $virt with file $filename, force is $main::config->{'flags'}->{'yes'}\n" if $main::config->{'flags'}->{'debug'};
179
   if ( -f $filename && ! $main::force) {
180
   if ( -f $filename && ! $main::config->{'flags'}->{'yes'}) {
180
      open XML, "<$filename" or die "Could not read from $filename: $!\n";
181
      open XML, "<$filename" or die "Could not read from $filename: $!\n";
181
      $return = join( '', <XML> );
182
      $return = join( '', <XML> );
182
      close XML;
183
      close XML;
183
   } else {
184
   } else {
184
      &main::readDB();
185
      &main::readDB();
185
      foreach my $node ( keys %{$main::statusDB->{'nodePopulation'}} ) {
186
      foreach my $node ( keys %{$main::statusDB->{'nodePopulation'}} ) {
186
         print "getVirtConfig Looking on $node for $virt\n" if $main::DEBUG > 1;;
187
         print "getVirtConfig Looking on $node for $virt\n" if $main::config->{'flags'}->{'debug'} > 1;;
187
         if ( exists( $main::statusDB->{'nodePopulation'}->{$node}->{'running'}->{$virt} ) ) { # we found it
188
         if ( exists( $main::statusDB->{'nodePopulation'}->{$node}->{'running'}->{$virt} ) ) { # we found it
188
            print "Found $virt on node $node\n" if $main::DEBUG;;
189
            print "Found $virt on node $node\n" if $main::config->{'flags'}->{'debug'};;
189
            $return = `ssh $node 'virsh dumpxml $virt'`;
190
            my $command = &main::makeCommand($node, "virsh dumpxml $virt");
-
 
191
            $return = `$command`;
190
            print "Writing config for $virt from $node into $filename\n" if $main::DEBUG;
192
            print "Writing config for $virt from $node into $filename\n" if $main::config->{'flags'}->{'debug'};
191
            open XML,">$filename" or die "Could not write to $filename: $!\n";
193
            open XML,">$filename" or die "Could not write to $filename: $!\n";
192
            print XML $return;
194
            print XML $return;
193
            close XML;
195
            close XML;
194
         } # if
196
         } # if
195
      } # foreach
197
      } # foreach
Line 210... Line 212...
210
   #&main::readDB();
212
   #&main::readDB();
211
   if ( my $foundNode = &main::findDomain( $virt ) ) {
213
   if ( my $foundNode = &main::findDomain( $virt ) ) {
212
      die "$virt already running on $foundNode, not starting\n";
214
      die "$virt already running on $foundNode, not starting\n";
213
   }
215
   }
214
   die "I do not have a definition for $virt\n" unless exists( $main::statusDB->{'virt'}->{$virt} );
216
   die "I do not have a definition for $virt\n" unless exists( $main::statusDB->{'virt'}->{$virt} );
215
   print Dumper( $main::statusDB->{'nodePopulation'} ) if $main::DEBUG > 2;
217
   print Dumper( $main::statusDB->{'nodePopulation'} ) if $main::config->{'flags'}->{'debug'} > 2;
-
 
218
   if ( my $error = &main::validateResources( $node, $virt ) ) {
-
 
219
      die $error;
-
 
220
   }
216
   my $filename = "$main::confDir/$virt.xml";
221
   my $filename = "$main::config->{'conf dir'}/$virt.xml";
217
   my $command = &main::makeCommand( $node, "virsh create $filename" );
222
   my $command = &main::makeCommand( $node, "virsh create $filename" );
218
   if ( $main::force ) { # we'll actually do it
223
   if ( $main::config->{'flags'}->{'yes'} ) { # we'll actually do it
219
      $return = ( &main::executeAndWait( $command, $node, $virt, 1 ) ? 'Success' : 'Can not start');
224
      $return = ( &main::executeAndWait( $command, $node, $virt, 1 ) ? 'Success' : 'Can not start');
220
      &main::forceScan();
225
      &main::forceScan();
221
   } else {
226
   } else {
222
      $return =  $command;;
227
      $return =  $command;;
223
   }
228
   }
Line 231... Line 236...
231
   # these are replaced by the safer findDomain
236
   # these are replaced by the safer findDomain
232
   #&main::forceScan();
237
   #&main::forceScan();
233
   #&main::readDB();
238
   #&main::readDB();
234
   $node = &main::findDomain( $virt );
239
   $node = &main::findDomain( $virt );
235
   die "I could not find the domain $virt running\n" unless $node;
240
   die "I could not find the domain $virt running\n" unless $node;
236
   print Dumper( $main::statusDB->{'nodePopulation'} ) if $main::DEBUG > 2;
241
   print Dumper( $main::statusDB->{'nodePopulation'} ) if $main::config->{'flags'}->{'debug'} > 2;
237
   die "I can not find $virt on any node\n" unless $node;
242
   die "I can not find $virt on any node\n" unless $node;
238
   my $command = &main::makeCommand( $node, "virsh shutdown $virt" );
243
   my $command = &main::makeCommand( $node, "virsh shutdown $virt" );
239
   if ( $main::force ) { # they want us to actually do it
244
   if ( $main::config->{'flags'}->{'yes'} ) { # they want us to actually do it
240
      $return = ( &main::executeAndWait( $command, $node, $virt, 0 ) ? 'Success' : 'Time Out waiting for shutdown');
245
      $return = ( &main::executeAndWait( $command, $node, $virt, 0 ) ? 'Success' : 'Time Out waiting for shutdown');
241
      &main::forceScan();
246
      &main::forceScan();
242
   } else {
247
   } else {
243
      $return = $command;
248
      $return = $command;
244
   }
249
   }
245
   return "$return\n";
250
   return "$return\n";
246
}
251
}
247
 
252
 
248
sub migrate {
253
sub migrate {
249
   my ( $virt, $target ) = @_;
254
   my ( $domain, $target ) = @_;
250
   my $return;
-
 
251
   my $node;
-
 
252
   # these are replaced by the safer findDomain
-
 
253
   #&main::forceScan();
-
 
254
   #&main::readDB();
-
 
255
   $node = &main::findDomain( $virt );
-
 
256
   print Dumper( $main::statusDB->{'nodePopulation'} ) if $main::DEBUG > 2;
-
 
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;
255
   if ( my $error = &main::validateResources( $target, $domain ) ) {
261
   my $command = &main::makeCommand( $node, "virsh migrate --live --persistent --verbose  $virt qemu+ssh://$target/system" );
-
 
262
   if ( $main::force ) { # they want us to actually do it
-
 
263
      $return = ( &main::executeAndWait( $command, $node, $virt, 0 ) ? 'Success' : 'Time Out waiting for shutdown');
-
 
264
      &main::forceScan();
-
 
265
   } else {
-
 
266
      $return = $command;
256
      die $error;
267
   }
257
   }
268
   return "$return\n";
258
   return &main::migrate( $domain, $target );
269
}
259
}
270
 
260
 
271
# find an unused VNC port
261
# find an unused VNC port
272
sub findVNCPort {
262
sub findVNCPort {
273
   my $currentPorts = shift;
263
   my $currentPorts = shift;
Line 307... Line 297...
307
# generate a virt-install statement, verifying things are not already being used
297
# generate a virt-install statement, verifying things are not already being used
308
sub new {
298
sub new {
309
   my %config;
299
   my %config;
310
   $config{'name'} = shift;
300
   $config{'name'} = shift;
311
   my $return;
301
   my $return;
312
   my $template = $main::scriptDir . '/virt-install.template';
302
   my $template = $main::config->{'script dir'} . '/virt-install.template';
313
   &main::readDB();
303
   &main::readDB();
314
   my %current;
304
   my %current;
315
   foreach my $virt ( keys %{$main::statusDB->{'virt'} } ) {
305
   foreach my $virt ( keys %{$main::statusDB->{'virt'} } ) {
316
      $current{'vnc'}{$main::statusDB->{'virt'}->{$virt}->{'vnc'}} = 1;
306
      $current{'vnc'}{$main::statusDB->{'virt'}->{$virt}->{'vnc'}} = 1;
317
      #$current{'mac'}{'null'} = 1;
307
      #$current{'mac'}{'null'} = 1;