Subversion Repositories havirt

Rev

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

Rev 13 Rev 14
Line 55... Line 55...
55
   push @return, "domain shutdown domainname";
55
   push @return, "domain shutdown domainname";
56
   push @return, "\tInitiates a shutdown on a running domain and puts it in to manual mode";
56
   push @return, "\tInitiates a shutdown on a running domain and puts it in to manual mode";
57
   push @return, "domain migrate domainname [node]";
57
   push @return, "domain migrate domainname [node]";
58
   push @return, "\tmigrates domain from current node to target. If target node not specified";
58
   push @return, "\tmigrates domain from current node to target. If target node not specified";
59
   push @return, "\twill be automatically selected";
59
   push @return, "\twill be automatically selected";
-
 
60
   push @return, "domain new [domainname]";
-
 
61
   push @return, "\tgenerates a virt-install command that will fill in several blanks";
-
 
62
   push @return, "\tNOTE: MAC address not guaranteed to be unique";
60
   return join( "\n", @return ) . "\n";
63
   return join( "\n", @return ) . "\n";
61
}
64
}
62
 
65
 
63
 
66
 
-
 
67
# find node a domain is on
-
 
68
# first parameter is the domain name
-
 
69
# rest of @_ is list of nodes to search
-
 
70
# if no nodes passed in, will search all known nodes
-
 
71
# returns first node found with the domain, or an empty string if not found
-
 
72
sub findDomain {
-
 
73
   my $domainName = shift;
-
 
74
   my @node = @_;
-
 
75
   my $foundNode = '';
-
 
76
   unless ( @node ) {
-
 
77
      @node = keys %{$main::statusDB->{'node'} };
-
 
78
   }
-
 
79
   foreach my $thisNode ( @node ) {
-
 
80
      my $output = `ssh $thisNode 'virsh list'`;
-
 
81
      return $thisNode if ( $output =~ m/domainName/ );
-
 
82
   }
-
 
83
   return '';
-
 
84
}
-
 
85
 
64
sub list {
86
sub list {
65
   &main::readDB();
87
   &main::readDB();
66
   print Dumper( $main::statusDB->{'nodePopulation'} ) if $main::DEBUG > 2;
88
   print Dumper( $main::statusDB->{'nodePopulation'} ) if $main::DEBUG > 2;
67
 
89
 
68
   my @header;
90
   my @header;
69
   my @data;
91
   my @data;
70
   
92
   
71
   foreach my $node ( keys %{$main::statusDB->{'nodePopulation'}} ) {
93
   foreach my $node ( sort keys %{$main::statusDB->{'nodePopulation'}} ) {
72
      foreach my $virt (keys %{$main::statusDB->{'nodePopulation'}->{$node}->{'running'}} ) {
94
      foreach my $virt (sort keys %{$main::statusDB->{'nodePopulation'}->{$node}->{'running'}} ) {
73
         unless ( @header ) {
95
         unless ( @header ) {
74
            # if we don't have a header yet, create it from the keys in this one. Assumes every entry has same keys
96
            # if we don't have a header yet, create it from the keys in this one. Assumes every entry has same keys
75
            @header = sort keys %{ $main::statusDB->{'virt'}->{$virt} };
97
            @header = sort keys %{ $main::statusDB->{'virt'}->{$virt} };
76
            unshift @header, 'Domain';
98
            unshift @header, 'Domain';
77
            unshift @header, 'Node';
99
            unshift @header, 'Node';
Line 159... Line 181...
159
# start a domain
181
# start a domain
160
sub start {
182
sub start {
161
   my ( $virt, $node ) = @_;
183
   my ( $virt, $node ) = @_;
162
   $node = `hostname` unless $node;
184
   $node = `hostname` unless $node;
163
   chomp $node;
185
   chomp $node;
-
 
186
   if ( my $alreadyRunning = &findDomain( $node ) ) {
-
 
187
      die "$virt already running on $alreadyRunning; not starting\n";
-
 
188
   }
164
   &main::readDB();
189
   &main::readDB();
165
   for my $myNode ( keys %{$main::statusDB->{'nodePopulation'} } ) {
190
   for my $myNode ( keys %{$main::statusDB->{'nodePopulation'} } ) {
166
      die "$virt already running on $myNode, not starting\n" if ( $main::statusDB->{'nodePopulation'}->{$myNode}->{'running'}->{$virt} );
191
      die "$virt already running on $myNode, not starting\n" if ( $main::statusDB->{'nodePopulation'}->{$myNode}->{'running'}->{$virt} );
167
   }
192
   }
168
   die "I do not have a definition for $virt\n" unless exists( $main::statusDB->{'virt'}->{$virt} );
193
   die "I do not have a definition for $virt\n" unless exists( $main::statusDB->{'virt'}->{$virt} );
Line 171... Line 196...
171
   return "ssh $node 'virsh create $filename'\n";
196
   return "ssh $node 'virsh create $filename'\n";
172
}
197
}
173
 
198
 
174
sub shutdown {
199
sub shutdown {
175
   my $virt = shift;
200
   my $virt = shift;
-
 
201
   my $node = '';
-
 
202
   &main::readDB();
-
 
203
   for my $myNode ( keys %{$main::statusDB->{'nodePopulation'} } ) {
-
 
204
      if ( $main::statusDB->{'nodePopulation'}->{$myNode}->{'running'}->{$virt} ) {
-
 
205
         $node = $myNode;
-
 
206
         last;
-
 
207
      }
-
 
208
   }
-
 
209
   print Dumper( $main::statusDB->{'nodePopulation'} ) if $main::DEBUG > 2;
-
 
210
   die "I can not find $virt on any node\n" unless $node;
176
   return "This code not written yet\n";
211
   return "ssh $node 'virsh shutdown $virt'\n";
177
}
212
}
178
 
213
 
179
sub migrate {
214
sub migrate {
180
   my ( $virt, $target ) = @_;
215
   my ( $virt, $target ) = @_;
181
   return "I don't know how to migrate yet\n";
216
   return "I don't know how to migrate yet\n";
182
}
-
 
183
 
217
}
-
 
218
 
-
 
219
# find an unused VNC port
-
 
220
sub findVNCPort {
-
 
221
   my $currentPorts = shift;
-
 
222
   my $return = 5900;
-
 
223
   while ( $currentPorts->{$return} ) {
-
 
224
      $return++;
-
 
225
   }
-
 
226
   return $return;
-
 
227
}
-
 
228
 
-
 
229
 
-
 
230
sub printMac {
-
 
231
   my $mac = shift;
-
 
232
   my @return;
-
 
233
   my $separator = ':';
-
 
234
   for ( my $i = 0; $i < length( $mac ); $i += 2 ) {
-
 
235
      push @return, substr( $mac, $i, 2 );
-
 
236
   }
-
 
237
   return join( $separator, @return );
-
 
238
}
-
 
239
      
-
 
240
      
-
 
241
sub makeMac {
-
 
242
   my $numDigits = 12; # 12 hex digits in a mac address
-
 
243
   my $macBaseXen = '00163e'; # Xen has 00:16:3E* assigned to it.
-
 
244
   my $hexDigits = '0123456789abcdef';
-
 
245
 
-
 
246
   my $baseMac = $macBaseXen;
-
 
247
 
-
 
248
   while (length( $baseMac ) < $numDigits ) {
-
 
249
      $baseMac .= substr($hexDigits, int(rand(length($hexDigits))),1 );
-
 
250
   }
-
 
251
 
-
 
252
   return  &printMac( $baseMac );
-
 
253
}
-
 
254
 
-
 
255
# generate a virt-install statement, verifying things are not already being used
-
 
256
sub new {
-
 
257
   my %config;
-
 
258
   $config{'name'} = shift;
-
 
259
   my $return;
-
 
260
   my $template = $main::scriptDir . '/virt-install.template';
-
 
261
   &main::readDB();
-
 
262
   my %current;
-
 
263
   foreach my $virt ( keys %{$main::statusDB->{'virt'} } ) {
-
 
264
      $current{'vnc'}{$main::statusDB->{'virt'}->{$virt}->{'vnc'}} = 1;
-
 
265
      #$current{'mac'}{'null'} = 1;
-
 
266
   }
-
 
267
   $config{'vnc'} = &findVNCPort( \%{$current{'vnc'}} );
-
 
268
   $config{'mac'} = &makeMac();
-
 
269
   $config{'uuid'} = `uuidgen`;
-
 
270
   chomp $config{'uuid'};
-
 
271
   if ( open TEMPLATE, "<$template" ) {
-
 
272
      $return = join( '', <TEMPLATE> );
-
 
273
      close TEMPLATE;
-
 
274
      foreach my $key ( keys %config ) {
-
 
275
         $return =~ s/<$key>/$config{$key}/gi if $config{$key};
-
 
276
      }
-
 
277
   } else {
-
 
278
      $return = "Template $template not found, the following values should be used\n";
-
 
279
      foreach my $key ( keys %config ) {
-
 
280
         $return .= "$key: $config{$key}\n";
-
 
281
      }
-
 
282
   }
-
 
283
   return $return;
-
 
284
}
-
 
285