Subversion Repositories havirt

Rev

Rev 5 | Rev 8 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5 Rev 7
Line 39... Line 39...
39
 
39
 
40
use Exporter;
40
use Exporter;
41
 
41
 
42
our @ISA = qw( Exporter );
42
our @ISA = qw( Exporter );
43
our @EXPORT = qw( 
43
our @EXPORT = qw( 
44
                  &list &update &scan &add
44
                  &list
45
                );
45
                );
46
 
46
 
-
 
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