Subversion Repositories havirt

Rev

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

Rev 14 Rev 15
Line 109... Line 109...
109
   }
109
   }
110
   
110
   
111
   return &main::report( \@header, \@data );
111
   return &main::report( \@header, \@data );
112
}
112
}
113
 
113
 
-
 
114
# reread the domain definition file and freshen the database
114
sub update {
115
sub update {
115
   &main::readDB(1); # loading it for write, so lock
116
   &main::readDB(1); # loading it for write, so lock
116
   unless ( @_ ) {
117
   unless ( @_ ) {
117
      # they didn't pass in anything, so do everything
118
      # they didn't pass in anything, so do everything
118
      @_ = keys %{ $main::statusDB->{'virt'} }
119
      @_ = keys %{ $main::statusDB->{'virt'} }
Line 123... Line 124...
123
   } # while
124
   } # while
124
   &main::writeDB( $main::domainDBName, $main::statusDB->{'virt'} );
125
   &main::writeDB( $main::domainDBName, $main::statusDB->{'virt'} );
125
   return "Updated\n";
126
   return "Updated\n";
126
}
127
}
127
 
128
 
-
 
129
 
-
 
130
# finds one xml value in file.
-
 
131
# since libvirt does not use good xml which can be parsed by
-
 
132
# several libraries, we must do it manually with regex's
128
sub getXMLValue {
133
sub getXMLValue {
129
   my ( $key, $string ) = @_;
134
   my ( $key, $string ) = @_;
130
   print "getXMLValue: looking for [$key] $string\n" if $main::DEBUG > 2;
135
   print "getXMLValue: looking for [$key] $string\n" if $main::DEBUG > 2;
131
   my $start = "<$key";
136
   my $start = "<$key";
132
   my $end = "</$key>";
137
   my $end = "</$key>";
133
   $string =~ m/$start([^>]*)>([^<]+)$end/;
138
   $string =~ m/$start([^>]*)>([^<]+)$end/;
134
   return ($1,$2);
139
   return ($1,$2);
135
}
140
}
136
 
141
 
-
 
142
 
-
 
143
# parse an xml file to find the values we want to save
-
 
144
# if config file exists in conf, just read it. If it does not
-
 
145
# exist, or if force is set, do a dumpxml on the running
-
 
146
# domain, put it into conf/ and load it.
137
sub parseDomain {
147
sub parseDomain {
138
   my ($virt, $nodePopulations ) = @_;
148
   my ($virt, $nodePopulations ) = @_;
139
 
149
 
140
   my @keysToSave = ( 'uuid', 'memory', 'vcpu','vnc' );
150
   my @keysToSave = ( 'uuid', 'memory', 'vcpu','vnc' );
141
   my $filename = "$main::confDir/$virt.xml";
151
   my $filename = "$main::confDir/$virt.xml";
Line 149... Line 159...
149
 
159
 
150
   $xml =~ m/type='vnc' port='(\d+)'/;
160
   $xml =~ m/type='vnc' port='(\d+)'/;
151
   $main::statusDB->{'virt'}->{$virt}->{'vnc'} = $1;
161
   $main::statusDB->{'virt'}->{$virt}->{'vnc'} = $1;
152
}
162
}
153
 
163
 
-
 
164
# returns xml definition of a domain
-
 
165
# if the definition exists in conf/, simply open and read
154
# get the XML definition file of a running domain off of whatever
166
# if the definition file does not exist, or if $force is set, find the 
155
# node it is running on, and save it to disk
167
# running domain, perform an xml dump of it, save it to conf/, then 
-
 
168
# return it.
156
sub getVirtConfig {
169
sub getVirtConfig {
157
   my ($virt,$filename) = @_;
170
   my ($virt,$filename) = @_;
158
   my $return;
171
   my $return;
159
   print "In getVirtConfig looking for $virt with file $filename\n" if $main::DEBUG;
172
   print "In getVirtConfig looking for $virt with file $filename, force is $main::force\n" if $main::DEBUG;
160
   if ( -f $filename ) {
173
   if ( -f $filename && ! $main::force) {
161
      open XML, "<$filename" or die "Could not read from $filename: $!\n";
174
      open XML, "<$filename" or die "Could not read from $filename: $!\n";
162
      $return = join( '', <XML> );
175
      $return = join( '', <XML> );
163
      close XML;
176
      close XML;
164
   } else {
177
   } else {
165
      &main::readDB();
178
      &main::readDB();
Line 181... Line 194...
181
# start a domain
194
# start a domain
182
sub start {
195
sub start {
183
   my ( $virt, $node ) = @_;
196
   my ( $virt, $node ) = @_;
184
   $node = `hostname` unless $node;
197
   $node = `hostname` unless $node;
185
   chomp $node;
198
   chomp $node;
-
 
199
   &main::forceScan();
186
   if ( my $alreadyRunning = &findDomain( $node ) ) {
200
   #if ( my $alreadyRunning = &findDomain( $node ) ) {
187
      die "$virt already running on $alreadyRunning; not starting\n";
201
   #   die "$virt already running on $alreadyRunning; not starting\n";
188
   }
202
   #}
189
   &main::readDB();
203
   &main::readDB();
190
   for my $myNode ( keys %{$main::statusDB->{'nodePopulation'} } ) {
204
   for my $myNode ( keys %{$main::statusDB->{'nodePopulation'} } ) {
191
      die "$virt already running on $myNode, not starting\n" if ( $main::statusDB->{'nodePopulation'}->{$myNode}->{'running'}->{$virt} );
205
      die "$virt already running on $myNode, not starting\n" if ( $main::statusDB->{'nodePopulation'}->{$myNode}->{'running'}->{$virt} );
192
   }
206
   }
193
   die "I do not have a definition for $virt\n" unless exists( $main::statusDB->{'virt'}->{$virt} );
207
   die "I do not have a definition for $virt\n" unless exists( $main::statusDB->{'virt'}->{$virt} );
Line 197... Line 211...
197
}
211
}
198
 
212
 
199
sub shutdown {
213
sub shutdown {
200
   my $virt = shift;
214
   my $virt = shift;
201
   my $node = '';
215
   my $node = '';
-
 
216
   my $return;
-
 
217
   &main::forceScan();
202
   &main::readDB();
218
   &main::readDB();
203
   for my $myNode ( keys %{$main::statusDB->{'nodePopulation'} } ) {
219
   for my $myNode ( keys %{$main::statusDB->{'nodePopulation'} } ) {
204
      if ( $main::statusDB->{'nodePopulation'}->{$myNode}->{'running'}->{$virt} ) {
220
      if ( $main::statusDB->{'nodePopulation'}->{$myNode}->{'running'}->{$virt} ) {
205
         $node = $myNode;
221
         $node = $myNode;
206
         last;
222
         last;
207
      }
223
      }
208
   }
224
   }
209
   print Dumper( $main::statusDB->{'nodePopulation'} ) if $main::DEBUG > 2;
225
   print Dumper( $main::statusDB->{'nodePopulation'} ) if $main::DEBUG > 2;
210
   die "I can not find $virt on any node\n" unless $node;
226
   die "I can not find $virt on any node\n" unless $node;
211
   return "ssh $node 'virsh shutdown $virt'\n";
227
   my $command = &main::makeCommand( $node, "virsh shutdown $virt" );
-
 
228
   if ( $main::force ) { # they want us to actually do it
-
 
229
      $return = ( &main::executeAndWait( $command, $node, $virt, 0 ) ? 'Success' : 'Time Out waiting for shutdown');
-
 
230
      &main::forceScan();
-
 
231
   } else {
-
 
232
      $return = $command;
-
 
233
   }
-
 
234
   return "$return\n";
212
}
235
}
213
 
236
 
214
sub migrate {
237
sub migrate {
215
   my ( $virt, $target ) = @_;
238
   my ( $virt, $target ) = @_;
216
   return "I don't know how to migrate yet\n";
239
   return "I don't know how to migrate yet\n";