Subversion Repositories havirt

Rev

Rev 15 | Rev 17 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5 rodolico 1
#!/usr/bin/env perl
2
 
3
# All functions related to maniplating domains
4
# part of havirt.
5
 
6
# Copyright 2024 Daily Data, Inc.
7
# 
8
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following 
9
# conditions are met:
10
#
11
#   Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
12
#   Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer 
13
#   in the documentation and/or other materials provided with the distribution.
14
#   Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived
15
#   from this software without specific prior written permission.
16
# 
17
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
18
# NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
19
# THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
22
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
 
24
# v0.0.1 20240602 RWR
25
# Initial setup
26
 
27
package domain;
28
 
29
use warnings;
30
use strict;  
31
 
32
# define the version number
33
# see https://metacpan.org/pod/release/JPEACOCK/version-0.97/lib/version.pod
34
use version;
35
our $VERSION = version->declare("0.0.1");
36
 
37
 
38
use Data::Dumper;
39
 
40
use Exporter;
41
 
42
our @ISA = qw( Exporter );
43
our @EXPORT = qw( 
7 rodolico 44
                  &list
5 rodolico 45
                );
46
 
9 rodolico 47
sub help {
48
   my @return;
49
   push @return, "domain update [domainname|-t domainname]";
50
   push @return, "\tUpdates capabilities on one or more domains, default is all domains";
51
   push @return, "domain list [--format|-f screen|tsv]";
52
   push @return, "\tLists all domains with some statistics about them as screen or tsv (default screen)";
12 rodolico 53
   push @return, "domain start domainname [node]";
54
   push @return, "\tstarts domainname on node. If node not set, will pick a node.";
13 rodolico 55
   push @return, "domain shutdown domainname";
56
   push @return, "\tInitiates a shutdown on a running domain and puts it in to manual mode";
57
   push @return, "domain migrate domainname [node]";
58
   push @return, "\tmigrates domain from current node to target. If target node not specified";
59
   push @return, "\twill be automatically selected";
14 rodolico 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";
9 rodolico 63
   return join( "\n", @return ) . "\n";
64
}
65
 
7 rodolico 66
 
14 rodolico 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
16 rodolico 72
# possibly not being used??
14 rodolico 73
sub findDomain {
74
   my $domainName = shift;
75
   my @node = @_;
76
   my $foundNode = '';
77
   unless ( @node ) {
78
      @node = keys %{$main::statusDB->{'node'} };
79
   }
80
   foreach my $thisNode ( @node ) {
81
      my $output = `ssh $thisNode 'virsh list'`;
82
      return $thisNode if ( $output =~ m/domainName/ );
83
   }
84
   return '';
85
}
86
 
7 rodolico 87
sub list {
12 rodolico 88
   &main::readDB();
89
   print Dumper( $main::statusDB->{'nodePopulation'} ) if $main::DEBUG > 2;
7 rodolico 90
 
91
   my @header;
92
   my @data;
93
 
14 rodolico 94
   foreach my $node ( sort keys %{$main::statusDB->{'nodePopulation'}} ) {
95
      foreach my $virt (sort keys %{$main::statusDB->{'nodePopulation'}->{$node}->{'running'}} ) {
7 rodolico 96
         unless ( @header ) {
97
            # if we don't have a header yet, create it from the keys in this one. Assumes every entry has same keys
12 rodolico 98
            @header = sort keys %{ $main::statusDB->{'virt'}->{$virt} };
99
            unshift @header, 'Domain';
7 rodolico 100
            unshift @header, 'Node';
101
         } # unless
102
         my @line;
103
         push @line, $node;
104
         push @line, $virt;
12 rodolico 105
         foreach my $column ( sort keys %{ $main::statusDB->{'virt'}->{$virt} } ) {
106
            push @line, $main::statusDB->{'virt'}->{$virt}->{$column};
7 rodolico 107
         }
108
         push @data, \@line;
109
      }
110
   }
111
 
112
   return &main::report( \@header, \@data );
113
}
114
 
15 rodolico 115
# reread the domain definition file and freshen the database
7 rodolico 116
sub update {
12 rodolico 117
   &main::readDB(1); # loading it for write, so lock
8 rodolico 118
   unless ( @_ ) {
119
      # they didn't pass in anything, so do everything
12 rodolico 120
      @_ = keys %{ $main::statusDB->{'virt'} }
8 rodolico 121
   } # unless
122
   print "Preparing to update " . join( "\n", @_ ) . "\n" if $main::DEBUG > 1;
7 rodolico 123
   while ( my $virt = shift ) {
124
      &parseDomain( $virt );
125
   } # while
12 rodolico 126
   &main::writeDB( $main::domainDBName, $main::statusDB->{'virt'} );
9 rodolico 127
   return "Updated\n";
7 rodolico 128
}
129
 
15 rodolico 130
 
131
# finds one xml value in file.
132
# since libvirt does not use good xml which can be parsed by
133
# several libraries, we must do it manually with regex's
7 rodolico 134
sub getXMLValue {
135
   my ( $key, $string ) = @_;
9 rodolico 136
   print "getXMLValue: looking for [$key] $string\n" if $main::DEBUG > 2;
7 rodolico 137
   my $start = "<$key";
138
   my $end = "</$key>";
139
   $string =~ m/$start([^>]*)>([^<]+)$end/;
140
   return ($1,$2);
141
}
142
 
15 rodolico 143
 
144
# parse an xml file to find the values we want to save
145
# if config file exists in conf, just read it. If it does not
146
# exist, or if force is set, do a dumpxml on the running
147
# domain, put it into conf/ and load it.
7 rodolico 148
sub parseDomain {
149
   my ($virt, $nodePopulations ) = @_;
150
 
151
   my @keysToSave = ( 'uuid', 'memory', 'vcpu','vnc' );
152
   my $filename = "$main::confDir/$virt.xml";
153
   my $xml = &getVirtConfig( $virt, $filename );
154
   my ($param,$value) = &getXMLValue( 'uuid', $xml );
12 rodolico 155
   $main::statusDB->{'virt'}->{$virt}->{'uuid'} = $value;
7 rodolico 156
   ($param,$value) = &getXMLValue( 'memory', $xml );
12 rodolico 157
   $main::statusDB->{'virt'}->{$virt}->{'memory'} = $value;
7 rodolico 158
   ($param,$value) = &getXMLValue( 'vcpu', $xml );
12 rodolico 159
   $main::statusDB->{'virt'}->{$virt}->{'vcpu'} = $value;
7 rodolico 160
 
161
   $xml =~ m/type='vnc' port='(\d+)'/;
12 rodolico 162
   $main::statusDB->{'virt'}->{$virt}->{'vnc'} = $1;
7 rodolico 163
}
164
 
15 rodolico 165
# returns xml definition of a domain
166
# if the definition exists in conf/, simply open and read
167
# if the definition file does not exist, or if $force is set, find the 
168
# running domain, perform an xml dump of it, save it to conf/, then 
169
# return it.
7 rodolico 170
sub getVirtConfig {
171
   my ($virt,$filename) = @_;
172
   my $return;
15 rodolico 173
   print "In getVirtConfig looking for $virt with file $filename, force is $main::force\n" if $main::DEBUG;
174
   if ( -f $filename && ! $main::force) {
7 rodolico 175
      open XML, "<$filename" or die "Could not read from $filename: $!\n";
176
      $return = join( '', <XML> );
177
      close XML;
178
   } else {
12 rodolico 179
      &main::readDB();
180
      foreach my $node ( keys %{$main::statusDB->{'nodePopulation'}} ) {
9 rodolico 181
         print "getVirtConfig Looking on $node for $virt\n" if $main::DEBUG > 1;;
12 rodolico 182
         if ( exists( $main::statusDB->{'nodePopulation'}->{$node}->{'running'}->{$virt} ) ) { # we found it
9 rodolico 183
            print "Found $virt on node $node\n" if $main::DEBUG;;
7 rodolico 184
            $return = `ssh $node 'virsh dumpxml $virt'`;
9 rodolico 185
            print "Writing config for $virt from $node into $filename\n" if $main::DEBUG;
7 rodolico 186
            open XML,">$filename" or die "Could not write to $filename: $!\n";
187
            print XML $return;
188
            close XML;
189
         } # if
190
      } # foreach
191
   } # if..else
192
   return $return;
193
} # sub getVirtConfig
12 rodolico 194
 
195
# start a domain
196
sub start {
197
   my ( $virt, $node ) = @_;
16 rodolico 198
   my $return;
12 rodolico 199
   $node = `hostname` unless $node;
200
   chomp $node;
15 rodolico 201
   &main::forceScan();
12 rodolico 202
   &main::readDB();
203
   for my $myNode ( keys %{$main::statusDB->{'nodePopulation'} } ) {
204
      die "$virt already running on $myNode, not starting\n" if ( $main::statusDB->{'nodePopulation'}->{$myNode}->{'running'}->{$virt} );
205
   }
206
   die "I do not have a definition for $virt\n" unless exists( $main::statusDB->{'virt'}->{$virt} );
207
   print Dumper( $main::statusDB->{'nodePopulation'} ) if $main::DEBUG > 2;
208
   my $filename = "$main::confDir/$virt.xml";
16 rodolico 209
   my $command = &main::makeCommand( $node, "virsh create $filename" );
210
   if ( $main::force ) { # we'll actually do it
211
      $return = ( &main::executeAndWait( $command, $node, $virt, 1 ) ? 'Success' : 'Can not start');
212
      &main::forceScan();
213
   } else {
214
      $return =  $command;;
215
   }
216
   return "$return\n";
12 rodolico 217
}
13 rodolico 218
 
219
sub shutdown {
220
   my $virt = shift;
14 rodolico 221
   my $node = '';
15 rodolico 222
   my $return;
223
   &main::forceScan();
14 rodolico 224
   &main::readDB();
225
   for my $myNode ( keys %{$main::statusDB->{'nodePopulation'} } ) {
226
      if ( $main::statusDB->{'nodePopulation'}->{$myNode}->{'running'}->{$virt} ) {
227
         $node = $myNode;
228
         last;
229
      }
230
   }
231
   print Dumper( $main::statusDB->{'nodePopulation'} ) if $main::DEBUG > 2;
232
   die "I can not find $virt on any node\n" unless $node;
15 rodolico 233
   my $command = &main::makeCommand( $node, "virsh shutdown $virt" );
234
   if ( $main::force ) { # they want us to actually do it
235
      $return = ( &main::executeAndWait( $command, $node, $virt, 0 ) ? 'Success' : 'Time Out waiting for shutdown');
236
      &main::forceScan();
237
   } else {
238
      $return = $command;
239
   }
240
   return "$return\n";
13 rodolico 241
}
242
 
243
sub migrate {
244
   my ( $virt, $target ) = @_;
245
   return "I don't know how to migrate yet\n";
14 rodolico 246
}
247
 
248
# find an unused VNC port
249
sub findVNCPort {
250
   my $currentPorts = shift;
251
   my $return = 5900;
252
   while ( $currentPorts->{$return} ) {
253
      $return++;
254
   }
255
   return $return;
256
}
257
 
258
 
259
sub printMac {
260
   my $mac = shift;
261
   my @return;
262
   my $separator = ':';
263
   for ( my $i = 0; $i < length( $mac ); $i += 2 ) {
264
      push @return, substr( $mac, $i, 2 );
265
   }
266
   return join( $separator, @return );
267
}
268
 
269
 
270
sub makeMac {
271
   my $numDigits = 12; # 12 hex digits in a mac address
272
   my $macBaseXen = '00163e'; # Xen has 00:16:3E* assigned to it.
273
   my $hexDigits = '0123456789abcdef';
274
 
275
   my $baseMac = $macBaseXen;
276
 
277
   while (length( $baseMac ) < $numDigits ) {
278
      $baseMac .= substr($hexDigits, int(rand(length($hexDigits))),1 );
279
   }
280
 
281
   return  &printMac( $baseMac );
282
}
283
 
284
# generate a virt-install statement, verifying things are not already being used
285
sub new {
286
   my %config;
287
   $config{'name'} = shift;
288
   my $return;
289
   my $template = $main::scriptDir . '/virt-install.template';
290
   &main::readDB();
291
   my %current;
292
   foreach my $virt ( keys %{$main::statusDB->{'virt'} } ) {
293
      $current{'vnc'}{$main::statusDB->{'virt'}->{$virt}->{'vnc'}} = 1;
294
      #$current{'mac'}{'null'} = 1;
295
   }
296
   $config{'vnc'} = &findVNCPort( \%{$current{'vnc'}} );
297
   $config{'mac'} = &makeMac();
298
   $config{'uuid'} = `uuidgen`;
299
   chomp $config{'uuid'};
300
   if ( open TEMPLATE, "<$template" ) {
301
      $return = join( '', <TEMPLATE> );
302
      close TEMPLATE;
303
      foreach my $key ( keys %config ) {
304
         $return =~ s/<$key>/$config{$key}/gi if $config{$key};
305
      }
306
   } else {
307
      $return = "Template $template not found, the following values should be used\n";
308
      foreach my $key ( keys %config ) {
309
         $return .= "$key: $config{$key}\n";
310
      }
311
   }
312
   return $return;
313
}