Subversion Repositories havirt

Rev

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