Subversion Repositories havirt

Rev

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

Rev 25 Rev 26
Line 21... Line 21...
21
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
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.
22
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
 
23
 
24
# v0.0.1 20240602 RWR
24
# v0.0.1 20240602 RWR
25
# Initial setup
25
# Initial setup
-
 
26
#
-
 
27
# v1.2.0 20240826 RWR
-
 
28
# Added some code to migrate domains if node placed in maintenance mode
-
 
29
# Added a lot of 'verbose' print lines, and modified for new flag structure
-
 
30
#
26
 
31
 
27
package node;
32
package node;
28
 
33
 
29
use warnings;
34
use warnings;
30
use strict;  
35
use strict;  
31
 
36
 
32
# define the version number
37
# define the version number
33
# see https://metacpan.org/pod/release/JPEACOCK/version-0.97/lib/version.pod
38
# see https://metacpan.org/pod/release/JPEACOCK/version-0.97/lib/version.pod
34
use version;
39
use version;
35
our $VERSION = version->declare("1.0.0");
40
our $VERSION = version->declare("1.2.0");
36
 
41
 
37
 
42
 
38
use Data::Dumper;
43
use Data::Dumper;
39
 
44
 
40
use Exporter;
45
use Exporter;
Line 101... Line 106...
101
# first defined, or if there is a hardware upgrade
106
# first defined, or if there is a hardware upgrade
102
# reads information off of the stack (@_), but will add to that if --target
107
# reads information off of the stack (@_), but will add to that if --target
103
# was defined
108
# was defined
104
sub update {
109
sub update {
105
   &main::readDB( 1 ); # open and lock so we can write to it later
110
   &main::readDB( 1 ); # open and lock so we can write to it later
106
   my $return;
111
   my @return;
107
   my @requiredFields = ( 'maintenance' );
112
   my @requiredFields = ( 'maintenance' );
108
   my @targets;
113
   my @targets;
109
   if ( $main::config->{'flags'}->{'target'} ) {
114
   if ( $main::config->{'flags'}->{'target'} ) {
110
      push @_, $main::config->{'flags'}->{'target'};
115
      push @_, $main::config->{'flags'}->{'target'};
111
   }
116
   }
112
   @_ = keys %{$main::statusDB->{'node'}} unless @_;
117
   @_ = keys %{$main::statusDB->{'node'}} unless @_;
113
   while ( my $nodename = shift  ) {
118
   while ( my $nodename = shift  ) {
114
      print "Updating $nodename\n" if $main::config->{'flags'}->{'debug'};
119
      print "Updating $nodename\n" if $main::config->{'flags'}->{'debug'} || $main::config->{'flags'}->{'verbose'};
115
      my $command = &main::makeCommand($nodename, "virsh nodeinfo" );
120
      my $command = &main::makeCommand($nodename, "virsh nodeinfo" );
-
 
121
      if ( $main::config->{'flags'}->{'dryrun'} ) {
-
 
122
         push @return, $command;
-
 
123
      } else {  
116
      $return = `$command`;
124
         my $return, `$command`;
117
      print "Output of [$command] is\n" . $return if $main::config->{'flags'}->{'debug'};
125
         print "Output of [$command] is\n" . $return if $main::config->{'flags'}->{'debug'};
118
      my @nodeinfo = split( "\n", $return );
126
         my @nodeinfo = split( "\n", $return );
119
      for ( my $i = 0; $i < @nodeinfo; $i++ ) {
127
         for ( my $i = 0; $i < @nodeinfo; $i++ ) {
120
         my ($key, $value) = split( /:\s+/, $nodeinfo[$i] );
128
            my ($key, $value) = split( /:\s+/, $nodeinfo[$i] );
121
         if ( $value =~ m/^(\d+)\s+[a-z]+$/i ) {
129
            if ( $value =~ m/^(\d+)\s+[a-z]+$/i ) {
122
            $value = $1;
130
               $value = $1;
123
         }
131
            }
124
         $key = $conversion{$key} if exists( $conversion{$key} );
132
            $key = $conversion{$key} if exists( $conversion{$key} );
125
         $main::statusDB->{'node'}->{$nodename}->{$key} = $value;
133
            $main::statusDB->{'node'}->{$nodename}->{$key} = $value;
126
      } # for
134
         } # for
127
      foreach my $field ( @requiredFields ) {
135
         foreach my $field ( @requiredFields ) {
128
         $main::statusDB->{'node'}->{$nodename}->{$field} = '' 
136
            $main::statusDB->{'node'}->{$nodename}->{$field} = '' 
129
            unless defined ( $main::statusDB->{'node'}->{$nodename}->{$field} );
137
               unless defined ( $main::statusDB->{'node'}->{$nodename}->{$field} );
130
      } # foreach
138
         } # foreach
-
 
139
      }
131
   } # while
140
   } # while
132
   print "main::statusDB->{'node'} state after update\n" . Dumper( $main::statusDB->{'node'} ) if $main::config->{'flags'}->{'debug'};
141
   print "main::statusDB->{'node'} state after update\n" . Dumper( $main::statusDB->{'node'} ) if $main::config->{'flags'}->{'debug'};
133
   &main::writeDB();
142
   &main::writeDB();
134
   return "Node has been updated\n";
143
   return "Node has been updated\n" . join( "\n", @return ) . "\n";
135
}      
144
}      
136
 
145
 
137
 
146
 
138
# check one or more nodes and determine which domains are running on them.
147
# check one or more nodes and determine which domains are running on them.
139
# defaults to everything in the node database, but the -t can have it run on only one
148
# defaults to everything in the node database, but the -t can have it run on only one
Line 143... Line 152...
143
}
152
}
144
 
153
 
145
 
154
 
146
# add a new node. This is the same as doing an update on a node that doesn't exist.
155
# add a new node. This is the same as doing an update on a node that doesn't exist.
147
sub add {
156
sub add {
148
#   &main::readDB();
-
 
149
#   print "Adding $main::config->{'flags'}->{'target'} as new node\n" if $main::config->{'flags'}->{'debug'};
-
 
150
#   $main::statusDB->{'node'}->{$main::config->{'flags'}->{'target'}} = '';
-
 
151
#   print "Calling update for new node\n" if $main::config->{'flags'}->{'debug'};
-
 
152
   &update( @_ );
157
   &update( @_ );
153
}
158
}
154
 
159
 
155
# put node in maintenance mode
160
# put node in maintenance mode
156
# if there are running domains on it, migrate them off first
161
# if there are running domains on it, migrate them off first
Line 160... Line 165...
160
   my @return;
165
   my @return;
161
   if ( $action ) {
166
   if ( $action ) {
162
      if ( lc ( $action ) eq 'on' ) {
167
      if ( lc ( $action ) eq 'on' ) {
163
         if ( $main::statusDB->{'nodePopulation'}->{$node}->{'running'} ) {
168
         if ( $main::statusDB->{'nodePopulation'}->{$node}->{'running'} ) {
164
            # we've requested maintenance mode, but there are domains running on the node
169
            # we've requested maintenance mode, but there are domains running on the node
-
 
170
            print "Trying to migrate domains off of $node before doing maintenance\n" if $main::config->{'flags'}->{'verbose'};
165
            push @return, &migrateAllDomains( $node, $main::config->{'flags'}->{'target'}, keys %{$main::statusDB->{'nodePopulation'}->{$node}->{'running'}} );
171
            push @return, &migrateAllDomains( $node, $main::config->{'flags'}->{'target'}, keys %{$main::statusDB->{'nodePopulation'}->{$node}->{'running'}} );
166
         }
172
         }
167
         if ( $main::statusDB->{'nodePopulation'}->{$node}->{'running'} ) {
173
         if ( $main::statusDB->{'nodePopulation'}->{$node}->{'running'} ) {
168
            push @return,  "Can not mark $node in maintenance mode with running domains";
174
            push @return,  "Can not mark $node in maintenance mode with running domains";
169
         } else {
175
         } else {
-
 
176
            print "Marking $node as under maintenance\n" if $main::config->{'flags'}->{'verbose'};
170
            $main::statusDB->{'node'}->{$node}->{'maintenance'} = 1;
177
            $main::statusDB->{'node'}->{$node}->{'maintenance'} = 1;
171
         }
178
         }
172
      } else {
179
      } else {
-
 
180
         print "Marking $node as Online\n" if $main::config->{'flags'}->{'verbose'};
173
         $main::statusDB->{'node'}->{$node}->{'maintenance'} = 1;
181
         $main::statusDB->{'node'}->{$node}->{'maintenance'} = 0;
174
      }
182
      }
175
   }
183
   }
176
   &main::writeDB();
184
   &main::writeDB();
177
   return "Maintenance set to " . ( $main::statusDB->{'node'}->{$node}->{'maintenance'} ? 'On' : 'Off' ) . "\n" .
185
   return "Maintenance set to " . ( $main::statusDB->{'node'}->{$node}->{'maintenance'} ? 'On' : 'Off' ) . "\n" .
178
          ( @return ? join( "\n", @return ) . "\n" : '');
186
          ( @return ? join( "\n", @return ) . "\n" : '');
Line 185... Line 193...
185
   my $from = shift;
193
   my $from = shift;
186
   my $to = shift;
194
   my $to = shift;
187
   print "In node.pm:migrateAllDomains, migrating\n" . join( "\n", @_ ) . "\nto $to\n"
195
   print "In node.pm:migrateAllDomains, migrating\n" . join( "\n", @_ ) . "\nto $to\n"
188
      if ( $main::config->{'flags'}->{'debug'} );
196
      if ( $main::config->{'flags'}->{'debug'} );
189
   my @commands;
197
   my @commands;
-
 
198
   print "Checking for available resources on $to before migrating\n"  if $main::config->{'flags'}->{'verbose'};
190
   if ( my $error = &main::validateResources( $to, @_ ) ) {
199
   if ( my $error = &main::validateResources( $to, @_ ) ) {
191
      return "We can not migrate all of the domains on $from to $to\n$error\n";
200
      return "We can not migrate all of the domains on $from to $to\n$error\n";
192
   }
201
   }
193
   while ( my $domain = shift ) {
202
   while ( my $domain = shift ) {
194
      push @commands, &main::migrate( $domain, $to );
203
      push @commands, &main::migrate( $domain, $to );