Subversion Repositories havirt

Rev

Rev 42 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 rodolico 1
#!/usr/bin/env perl
2
 
3
# All functions related to maniplating a specific node
4
# part of havirt.
5
 
4 rodolico 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
 
3 rodolico 24
# v0.0.1 20240602 RWR
25
# Initial setup
26 rodolico 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
#
38 rodolico 31
# v1.2.1 20240828 RWR
32
# Fixed node maintenance, adding scanning and fixing some bugs with
33
# the migration code
47 rodolico 34
#
35
# v1.2.2 20260102 RWR
36
# Refactored command execution in update() function to use centralized execute()
3 rodolico 37
 
38
package node;
39
 
40
use warnings;
41
use strict;  
42
 
4 rodolico 43
# define the version number
44
# see https://metacpan.org/pod/release/JPEACOCK/version-0.97/lib/version.pod
45
use version;
47 rodolico 46
our $VERSION = version->declare("1.2.2");
3 rodolico 47
 
4 rodolico 48
 
49
use Data::Dumper;
50
 
3 rodolico 51
use Exporter;
52
 
53
our @ISA = qw( Exporter );
54
our @EXPORT = qw( 
5 rodolico 55
                  &node &list &update &scan &add
3 rodolico 56
                );
57
 
4 rodolico 58
# Converts from output of node info to a key we want to use
59
my %conversion = ( 
60
  'CPU frequency' => 'clock',
61
  'CPU model' => 'cpu_model',
62
  'CPU socket(s)' => 'cpu_socket',
63
  'CPU(s)' => 'cpu_count',
64
  'Core(s) per socket' => 'cpu_cores',
65
  'Memory size' => 'memory',
66
  'NUMA cell(s)' => 'numa_cells',
67
  'Thread(s) per core' => 'threads_per_core'
68
);
3 rodolico 69
 
9 rodolico 70
# show a help screen
71
sub help {
72
   my @return;
73
   push @return, "node update [nodename|-t nodename]";
74
   push @return, "\tUpdates capabilities on one or more nodes, default is all nodes";
47 rodolico 75
   push @return, "node add nodename";
76
   push @return, "\tAdds a new node to the cluster. nodename must be reachable via ssh without password";
77
   push @return, "node list [--format screen|tsv]";
9 rodolico 78
   push @return, "\tLists all nodes with some statistics about them as screen or tsv (default screen)";
79
   push @return, "node scan [nodename|-t nodename]";
80
   push @return, "\tUpdates list of domains on one or more existing nodes, default is all nodes";
29 rodolico 81
   push @return, "node maintenance nodename [on|off --target=targetNode]";
17 rodolico 82
   push @return, "\ton - set maintenance flag; no domains can be started/migrated to node";
29 rodolico 83
   push @return, "\t     target must be set with the --target flag";
17 rodolico 84
   push @return, "\toff - Allows domains to be migrated/started on node";
85
   push @return, "\tnothing - displays current maintenance flag";
9 rodolico 86
   return join( "\n", @return ) . "\n";
87
}
88
 
89
 
5 rodolico 90
# lists hardware capabilities of all nodes (virsh nodeinfo)
4 rodolico 91
sub list {
92
   my @header;
93
   my @data;
94
   my $return;
12 rodolico 95
   &main::readDB();
96
   foreach my $node ( sort keys %{$main::statusDB->{'node'}} ) {
4 rodolico 97
      unless ( @header ) {
98
         # just grab the keys for headers
12 rodolico 99
         @header = sort keys %{ $main::statusDB->{'node'}->{$node} };
4 rodolico 100
         # put Node at the beginning
101
         unshift ( @header, 'Node' );
102
      }
103
      my @line;
104
      push @line, $node;
12 rodolico 105
      foreach my $column (sort keys %{ $main::statusDB->{'node'}->{$node} }) {
106
         push @line, $main::statusDB->{'node'}->{$node}->{$column};
4 rodolico 107
      }
108
      push (@data, \@line );
109
   }
7 rodolico 110
   return &main::report( \@header, \@data );
4 rodolico 111
}
112
 
5 rodolico 113
# Get information about a node. Really only needs to be done when a node is
114
# first defined, or if there is a hardware upgrade
9 rodolico 115
# reads information off of the stack (@_), but will add to that if --target
116
# was defined
4 rodolico 117
sub update {
12 rodolico 118
   &main::readDB( 1 ); # open and lock so we can write to it later
26 rodolico 119
   my @return;
18 rodolico 120
   my @requiredFields = ( 'maintenance' );
4 rodolico 121
   my @targets;
25 rodolico 122
   if ( $main::config->{'flags'}->{'target'} ) {
123
      push @_, $main::config->{'flags'}->{'target'};
4 rodolico 124
   }
12 rodolico 125
   @_ = keys %{$main::statusDB->{'node'}} unless @_;
9 rodolico 126
   while ( my $nodename = shift  ) {
26 rodolico 127
      print "Updating $nodename\n" if $main::config->{'flags'}->{'debug'} || $main::config->{'flags'}->{'verbose'};
25 rodolico 128
      my $command = &main::makeCommand($nodename, "virsh nodeinfo" );
26 rodolico 129
      if ( $main::config->{'flags'}->{'dryrun'} ) {
130
         push @return, $command;
131
      } else {  
47 rodolico 132
         my $return= &main::execute($command);
26 rodolico 133
         print "Output of [$command] is\n" . $return if $main::config->{'flags'}->{'debug'};
134
         my @nodeinfo = split( "\n", $return );
135
         for ( my $i = 0; $i < @nodeinfo; $i++ ) {
136
            my ($key, $value) = split( /:\s+/, $nodeinfo[$i] );
137
            if ( $value =~ m/^(\d+)\s+[a-z]+$/i ) {
138
               $value = $1;
139
            }
140
            $key = $conversion{$key} if exists( $conversion{$key} );
141
            $main::statusDB->{'node'}->{$nodename}->{$key} = $value;
142
         } # for
143
         foreach my $field ( @requiredFields ) {
144
            $main::statusDB->{'node'}->{$nodename}->{$field} = '' 
145
               unless defined ( $main::statusDB->{'node'}->{$nodename}->{$field} );
146
         } # foreach
147
      }
4 rodolico 148
   } # while
25 rodolico 149
   print "main::statusDB->{'node'} state after update\n" . Dumper( $main::statusDB->{'node'} ) if $main::config->{'flags'}->{'debug'};
12 rodolico 150
   &main::writeDB();
26 rodolico 151
   return "Node has been updated\n" . join( "\n", @return ) . "\n";
4 rodolico 152
}      
153
 
5 rodolico 154
 
155
# check one or more nodes and determine which domains are running on them.
156
# defaults to everything in the node database, but the -t can have it run on only one
157
# this is the function that should be run every few minutes on one of the servers
4 rodolico 158
sub scan {
15 rodolico 159
   return &main::scan(@_);
5 rodolico 160
}
4 rodolico 161
 
3 rodolico 162
 
5 rodolico 163
# add a new node. This is the same as doing an update on a node that doesn't exist.
164
sub add {
12 rodolico 165
   &update( @_ );
3 rodolico 166
}
167
 
25 rodolico 168
# put node in maintenance mode
47 rodolico 169
# no domains are allowed to be started or migrated to a node in maintenance mode
170
# if we are turning maintenance mode on, we must migrate all domains off
171
# maintenance does NOT remove existing domains, but prevents new ones from being added
172
# however, cluster::balance will try to migrate them off
17 rodolico 173
sub maintenance {
174
   my ( $node, $action ) = @_;
38 rodolico 175
   &main::readDB();
25 rodolico 176
   my @return;
17 rodolico 177
   if ( $action ) {
47 rodolico 178
      print "Found action [$action] in node.pm:maintenance\n" if $main::config->{'flags'}->{'debug'};
42 rodolico 179
      &main::readDB(1);
180
      $main::statusDB->{'node'}->{$node}->{'maintenance'} = lc $action eq 'on';
181
      &main::writeDB();
17 rodolico 182
   }
38 rodolico 183
   &main::readDB();
184
   print Dumper( $main::statusDB ) if $main::config->{'flags'}->{'debug'} > 2;
25 rodolico 185
   return "Maintenance set to " . ( $main::statusDB->{'node'}->{$node}->{'maintenance'} ? 'On' : 'Off' ) . "\n" .
186
          ( @return ? join( "\n", @return ) . "\n" : '');
17 rodolico 187
}
188
 
25 rodolico 189
 
190
# migrate domains from node $from to node $to
191
# the rest of the stack is a list of domains to migrate
192
sub migrateAllDomains {
193
   my $from = shift;
194
   my $to = shift;
195
   print "In node.pm:migrateAllDomains, migrating\n" . join( "\n", @_ ) . "\nto $to\n"
196
      if ( $main::config->{'flags'}->{'debug'} );
197
   my @commands;
26 rodolico 198
   print "Checking for available resources on $to before migrating\n"  if $main::config->{'flags'}->{'verbose'};
25 rodolico 199
   if ( my $error = &main::validateResources( $to, @_ ) ) {
200
      return "We can not migrate all of the domains on $from to $to\n$error\n";
201
   }
202
   while ( my $domain = shift ) {
203
      push @commands, &main::migrate( $domain, $to );
204
   }
205
   chomp @commands;
206
   return join( "\n", @commands ) . "\n";
207
}
208
 
209