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.";
9 rodolico 55
   return join( "\n", @return ) . "\n";
56
}
57
 
7 rodolico 58
 
59
sub list {
12 rodolico 60
   &main::readDB();
61
   print Dumper( $main::statusDB->{'nodePopulation'} ) if $main::DEBUG > 2;
7 rodolico 62
 
63
   my @header;
64
   my @data;
65
 
12 rodolico 66
   foreach my $node ( keys %{$main::statusDB->{'nodePopulation'}} ) {
67
      foreach my $virt (keys %{$main::statusDB->{'nodePopulation'}->{$node}->{'running'}} ) {
7 rodolico 68
         unless ( @header ) {
69
            # if we don't have a header yet, create it from the keys in this one. Assumes every entry has same keys
12 rodolico 70
            @header = sort keys %{ $main::statusDB->{'virt'}->{$virt} };
71
            unshift @header, 'Domain';
7 rodolico 72
            unshift @header, 'Node';
73
         } # unless
74
         my @line;
75
         push @line, $node;
76
         push @line, $virt;
12 rodolico 77
         foreach my $column ( sort keys %{ $main::statusDB->{'virt'}->{$virt} } ) {
78
            push @line, $main::statusDB->{'virt'}->{$virt}->{$column};
7 rodolico 79
         }
80
         push @data, \@line;
81
      }
82
   }
83
 
84
   return &main::report( \@header, \@data );
85
}
86
 
87
sub update {
12 rodolico 88
   &main::readDB(1); # loading it for write, so lock
8 rodolico 89
   unless ( @_ ) {
90
      # they didn't pass in anything, so do everything
12 rodolico 91
      @_ = keys %{ $main::statusDB->{'virt'} }
8 rodolico 92
   } # unless
93
   print "Preparing to update " . join( "\n", @_ ) . "\n" if $main::DEBUG > 1;
7 rodolico 94
   while ( my $virt = shift ) {
95
      &parseDomain( $virt );
96
   } # while
12 rodolico 97
   &main::writeDB( $main::domainDBName, $main::statusDB->{'virt'} );
9 rodolico 98
   return "Updated\n";
7 rodolico 99
}
100
 
101
sub getXMLValue {
102
   my ( $key, $string ) = @_;
9 rodolico 103
   print "getXMLValue: looking for [$key] $string\n" if $main::DEBUG > 2;
7 rodolico 104
   my $start = "<$key";
105
   my $end = "</$key>";
106
   $string =~ m/$start([^>]*)>([^<]+)$end/;
107
   return ($1,$2);
108
}
109
 
110
sub parseDomain {
111
   my ($virt, $nodePopulations ) = @_;
112
 
113
   my @keysToSave = ( 'uuid', 'memory', 'vcpu','vnc' );
114
   my $filename = "$main::confDir/$virt.xml";
115
   my $xml = &getVirtConfig( $virt, $filename );
116
   my ($param,$value) = &getXMLValue( 'uuid', $xml );
12 rodolico 117
   $main::statusDB->{'virt'}->{$virt}->{'uuid'} = $value;
7 rodolico 118
   ($param,$value) = &getXMLValue( 'memory', $xml );
12 rodolico 119
   $main::statusDB->{'virt'}->{$virt}->{'memory'} = $value;
7 rodolico 120
   ($param,$value) = &getXMLValue( 'vcpu', $xml );
12 rodolico 121
   $main::statusDB->{'virt'}->{$virt}->{'vcpu'} = $value;
7 rodolico 122
 
123
   $xml =~ m/type='vnc' port='(\d+)'/;
12 rodolico 124
   $main::statusDB->{'virt'}->{$virt}->{'vnc'} = $1;
7 rodolico 125
}
126
 
127
# get the XML definition file of a running domain off of whatever
128
# node it is running on, and save it to disk
129
sub getVirtConfig {
130
   my ($virt,$filename) = @_;
131
   my $return;
8 rodolico 132
   print "In getVirtConfig looking for $virt with file $filename\n" if $main::DEBUG;
7 rodolico 133
   if ( -f $filename ) {
134
      open XML, "<$filename" or die "Could not read from $filename: $!\n";
135
      $return = join( '', <XML> );
136
      close XML;
137
   } else {
12 rodolico 138
      &main::readDB();
139
      foreach my $node ( keys %{$main::statusDB->{'nodePopulation'}} ) {
9 rodolico 140
         print "getVirtConfig Looking on $node for $virt\n" if $main::DEBUG > 1;;
12 rodolico 141
         if ( exists( $main::statusDB->{'nodePopulation'}->{$node}->{'running'}->{$virt} ) ) { # we found it
9 rodolico 142
            print "Found $virt on node $node\n" if $main::DEBUG;;
7 rodolico 143
            $return = `ssh $node 'virsh dumpxml $virt'`;
9 rodolico 144
            print "Writing config for $virt from $node into $filename\n" if $main::DEBUG;
7 rodolico 145
            open XML,">$filename" or die "Could not write to $filename: $!\n";
146
            print XML $return;
147
            close XML;
148
         } # if
149
      } # foreach
150
   } # if..else
151
   return $return;
152
} # sub getVirtConfig
12 rodolico 153
 
154
# start a domain
155
sub start {
156
   my ( $virt, $node ) = @_;
157
   $node = `hostname` unless $node;
158
   chomp $node;
159
   &main::readDB();
160
   for my $myNode ( keys %{$main::statusDB->{'nodePopulation'} } ) {
161
      die "$virt already running on $myNode, not starting\n" if ( $main::statusDB->{'nodePopulation'}->{$myNode}->{'running'}->{$virt} );
162
   }
163
   die "I do not have a definition for $virt\n" unless exists( $main::statusDB->{'virt'}->{$virt} );
164
   print Dumper( $main::statusDB->{'nodePopulation'} ) if $main::DEBUG > 2;
165
   my $filename = "$main::confDir/$virt.xml";
166
   return "ssh $node 'virsh create $filename'\n";
167
}