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