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();
8 rodolico 84
   unless ( @_ ) {
85
      # they didn't pass in anything, so do everything
86
      @_ = keys %{ $main::virtDB }
87
   } # unless
88
   print "Preparing to update " . join( "\n", @_ ) . "\n" if $main::DEBUG > 1;
89
   die;
7 rodolico 90
   while ( my $virt = shift ) {
91
      &parseDomain( $virt );
92
   } # while
8 rodolico 93
   &writeDB( $main::domainDBName, $main::virtDB );
7 rodolico 94
}
95
 
96
sub getXMLValue {
97
   my ( $key, $string ) = @_;
98
   my $start = "<$key";
99
   my $end = "</$key>";
100
   $string =~ m/$start([^>]*)>([^<]+)$end/;
101
   return ($1,$2);
102
}
103
 
104
sub parseDomain {
105
   my ($virt, $nodePopulations ) = @_;
106
 
107
   my @keysToSave = ( 'uuid', 'memory', 'vcpu','vnc' );
108
   my $filename = "$main::confDir/$virt.xml";
109
   my $xml = &getVirtConfig( $virt, $filename );
110
   my ($param,$value) = &getXMLValue( 'uuid', $xml );
111
   $main::virtDB->{$virt}->{'uuid'} = $value;
112
   ($param,$value) = &getXMLValue( 'memory', $xml );
113
   $main::virtDB->{$virt}->{'memory'} = $value;
114
   ($param,$value) = &getXMLValue( 'vcpu', $xml );
115
   $main::virtDB->{$virt}->{'vcpu'} = $value;
116
 
117
   $xml =~ m/type='vnc' port='(\d+)'/;
118
   $main::virtDB->{$virt}->{'vnc'} = $1;
119
}
120
 
121
# get the XML definition file of a running domain off of whatever
122
# node it is running on, and save it to disk
123
sub getVirtConfig {
124
   my ($virt,$filename) = @_;
125
   my $return;
8 rodolico 126
   print "In getVirtConfig looking for $virt with file $filename\n" if $main::DEBUG;
7 rodolico 127
   if ( -f $filename ) {
128
      open XML, "<$filename" or die "Could not read from $filename: $!\n";
129
      $return = join( '', <XML> );
130
      close XML;
131
   } else {
132
      &main::loadNodePopulations();
133
      #die Dumper( $nodePopulations );
134
      foreach my $node ( keys %$main::nodePopulations ) {
135
         print "getVirtConfig Looking on $node for $virt\n";
136
         if ( exists( $main::nodePopulations->{$node}->{'running'}->{$virt} ) ) { # we found it
137
            print "Found $virt on node $node\n";
138
            $return = `ssh $node 'virsh dumpxml $virt'`;
139
            open XML,">$filename" or die "Could not write to $filename: $!\n";
140
            print XML $return;
141
            close XML;
142
         } # if
143
      } # foreach
144
   } # if..else
145
   return $return;
146
} # sub getVirtConfig