Subversion Repositories havirt

Rev

Go to most recent revision | Details | 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
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
 
7 rodolico 86
sub list {
12 rodolico 87
   &main::readDB();
88
   print Dumper( $main::statusDB->{'nodePopulation'} ) if $main::DEBUG > 2;
7 rodolico 89
 
90
   my @header;
91
   my @data;
92
 
14 rodolico 93
   foreach my $node ( sort keys %{$main::statusDB->{'nodePopulation'}} ) {
94
      foreach my $virt (sort keys %{$main::statusDB->{'nodePopulation'}->{$node}->{'running'}} ) {
7 rodolico 95
         unless ( @header ) {
96
            # if we don't have a header yet, create it from the keys in this one. Assumes every entry has same keys
12 rodolico 97
            @header = sort keys %{ $main::statusDB->{'virt'}->{$virt} };
98
            unshift @header, 'Domain';
7 rodolico 99
            unshift @header, 'Node';
100
         } # unless
101
         my @line;
102
         push @line, $node;
103
         push @line, $virt;
12 rodolico 104
         foreach my $column ( sort keys %{ $main::statusDB->{'virt'}->{$virt} } ) {
105
            push @line, $main::statusDB->{'virt'}->{$virt}->{$column};
7 rodolico 106
         }
107
         push @data, \@line;
108
      }
109
   }
110
 
111
   return &main::report( \@header, \@data );
112
}
113
 
114
sub update {
12 rodolico 115
   &main::readDB(1); # loading it for write, so lock
8 rodolico 116
   unless ( @_ ) {
117
      # they didn't pass in anything, so do everything
12 rodolico 118
      @_ = keys %{ $main::statusDB->{'virt'} }
8 rodolico 119
   } # unless
120
   print "Preparing to update " . join( "\n", @_ ) . "\n" if $main::DEBUG > 1;
7 rodolico 121
   while ( my $virt = shift ) {
122
      &parseDomain( $virt );
123
   } # while
12 rodolico 124
   &main::writeDB( $main::domainDBName, $main::statusDB->{'virt'} );
9 rodolico 125
   return "Updated\n";
7 rodolico 126
}
127
 
128
sub getXMLValue {
129
   my ( $key, $string ) = @_;
9 rodolico 130
   print "getXMLValue: looking for [$key] $string\n" if $main::DEBUG > 2;
7 rodolico 131
   my $start = "<$key";
132
   my $end = "</$key>";
133
   $string =~ m/$start([^>]*)>([^<]+)$end/;
134
   return ($1,$2);
135
}
136
 
137
sub parseDomain {
138
   my ($virt, $nodePopulations ) = @_;
139
 
140
   my @keysToSave = ( 'uuid', 'memory', 'vcpu','vnc' );
141
   my $filename = "$main::confDir/$virt.xml";
142
   my $xml = &getVirtConfig( $virt, $filename );
143
   my ($param,$value) = &getXMLValue( 'uuid', $xml );
12 rodolico 144
   $main::statusDB->{'virt'}->{$virt}->{'uuid'} = $value;
7 rodolico 145
   ($param,$value) = &getXMLValue( 'memory', $xml );
12 rodolico 146
   $main::statusDB->{'virt'}->{$virt}->{'memory'} = $value;
7 rodolico 147
   ($param,$value) = &getXMLValue( 'vcpu', $xml );
12 rodolico 148
   $main::statusDB->{'virt'}->{$virt}->{'vcpu'} = $value;
7 rodolico 149
 
150
   $xml =~ m/type='vnc' port='(\d+)'/;
12 rodolico 151
   $main::statusDB->{'virt'}->{$virt}->{'vnc'} = $1;
7 rodolico 152
}
153
 
154
# get the XML definition file of a running domain off of whatever
155
# node it is running on, and save it to disk
156
sub getVirtConfig {
157
   my ($virt,$filename) = @_;
158
   my $return;
8 rodolico 159
   print "In getVirtConfig looking for $virt with file $filename\n" if $main::DEBUG;
7 rodolico 160
   if ( -f $filename ) {
161
      open XML, "<$filename" or die "Could not read from $filename: $!\n";
162
      $return = join( '', <XML> );
163
      close XML;
164
   } else {
12 rodolico 165
      &main::readDB();
166
      foreach my $node ( keys %{$main::statusDB->{'nodePopulation'}} ) {
9 rodolico 167
         print "getVirtConfig Looking on $node for $virt\n" if $main::DEBUG > 1;;
12 rodolico 168
         if ( exists( $main::statusDB->{'nodePopulation'}->{$node}->{'running'}->{$virt} ) ) { # we found it
9 rodolico 169
            print "Found $virt on node $node\n" if $main::DEBUG;;
7 rodolico 170
            $return = `ssh $node 'virsh dumpxml $virt'`;
9 rodolico 171
            print "Writing config for $virt from $node into $filename\n" if $main::DEBUG;
7 rodolico 172
            open XML,">$filename" or die "Could not write to $filename: $!\n";
173
            print XML $return;
174
            close XML;
175
         } # if
176
      } # foreach
177
   } # if..else
178
   return $return;
179
} # sub getVirtConfig
12 rodolico 180
 
181
# start a domain
182
sub start {
183
   my ( $virt, $node ) = @_;
184
   $node = `hostname` unless $node;
185
   chomp $node;
14 rodolico 186
   if ( my $alreadyRunning = &findDomain( $node ) ) {
187
      die "$virt already running on $alreadyRunning; not starting\n";
188
   }
12 rodolico 189
   &main::readDB();
190
   for my $myNode ( keys %{$main::statusDB->{'nodePopulation'} } ) {
191
      die "$virt already running on $myNode, not starting\n" if ( $main::statusDB->{'nodePopulation'}->{$myNode}->{'running'}->{$virt} );
192
   }
193
   die "I do not have a definition for $virt\n" unless exists( $main::statusDB->{'virt'}->{$virt} );
194
   print Dumper( $main::statusDB->{'nodePopulation'} ) if $main::DEBUG > 2;
195
   my $filename = "$main::confDir/$virt.xml";
196
   return "ssh $node 'virsh create $filename'\n";
197
}
13 rodolico 198
 
199
sub shutdown {
200
   my $virt = shift;
14 rodolico 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;
211
   return "ssh $node 'virsh shutdown $virt'\n";
13 rodolico 212
}
213
 
214
sub migrate {
215
   my ( $virt, $target ) = @_;
216
   return "I don't know how to migrate yet\n";
14 rodolico 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
}