Subversion Repositories havirt

Rev

Rev 25 | Rev 29 | Go to most recent revision | 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
#
3 rodolico 31
 
32
package node;
33
 
34
use warnings;
35
use strict;  
36
 
4 rodolico 37
# define the version number
38
# see https://metacpan.org/pod/release/JPEACOCK/version-0.97/lib/version.pod
39
use version;
26 rodolico 40
our $VERSION = version->declare("1.2.0");
3 rodolico 41
 
4 rodolico 42
 
43
use Data::Dumper;
44
 
3 rodolico 45
use Exporter;
46
 
47
our @ISA = qw( Exporter );
48
our @EXPORT = qw( 
5 rodolico 49
                  &node &list &update &scan &add
3 rodolico 50
                );
51
 
4 rodolico 52
# Converts from output of node info to a key we want to use
53
my %conversion = ( 
54
  'CPU frequency' => 'clock',
55
  'CPU model' => 'cpu_model',
56
  'CPU socket(s)' => 'cpu_socket',
57
  'CPU(s)' => 'cpu_count',
58
  'Core(s) per socket' => 'cpu_cores',
59
  'Memory size' => 'memory',
60
  'NUMA cell(s)' => 'numa_cells',
61
  'Thread(s) per core' => 'threads_per_core'
62
);
3 rodolico 63
 
9 rodolico 64
# show a help screen
65
sub help {
66
   my @return;
67
   push @return, "node update [nodename|-t nodename]";
68
   push @return, "\tUpdates capabilities on one or more nodes, default is all nodes";
69
   push @return, "node list [--format|-f screen|tsv]";
70
   push @return, "\tLists all nodes with some statistics about them as screen or tsv (default screen)";
71
   push @return, "node scan [nodename|-t nodename]";
72
   push @return, "\tUpdates list of domains on one or more existing nodes, default is all nodes";
17 rodolico 73
   push @return, "node maintenance nodename [on|off]";
74
   push @return, "\ton - set maintenance flag; no domains can be started/migrated to node";
75
   push @return, "\toff - Allows domains to be migrated/started on node";
76
   push @return, "\tnothing - displays current maintenance flag";
77
   push @return, "\tNote: a node with any domains running can not have maintenance mode turned on";
9 rodolico 78
   return join( "\n", @return ) . "\n";
79
}
80
 
81
 
5 rodolico 82
# lists hardware capabilities of all nodes (virsh nodeinfo)
4 rodolico 83
sub list {
84
   my @header;
85
   my @data;
86
   my $return;
12 rodolico 87
   &main::readDB();
88
   foreach my $node ( sort keys %{$main::statusDB->{'node'}} ) {
4 rodolico 89
      unless ( @header ) {
90
         # just grab the keys for headers
12 rodolico 91
         @header = sort keys %{ $main::statusDB->{'node'}->{$node} };
4 rodolico 92
         # put Node at the beginning
93
         unshift ( @header, 'Node' );
94
      }
95
      my @line;
96
      push @line, $node;
12 rodolico 97
      foreach my $column (sort keys %{ $main::statusDB->{'node'}->{$node} }) {
98
         push @line, $main::statusDB->{'node'}->{$node}->{$column};
4 rodolico 99
      }
100
      push (@data, \@line );
101
   }
7 rodolico 102
   return &main::report( \@header, \@data );
4 rodolico 103
}
104
 
5 rodolico 105
# Get information about a node. Really only needs to be done when a node is
106
# first defined, or if there is a hardware upgrade
9 rodolico 107
# reads information off of the stack (@_), but will add to that if --target
108
# was defined
4 rodolico 109
sub update {
12 rodolico 110
   &main::readDB( 1 ); # open and lock so we can write to it later
26 rodolico 111
   my @return;
18 rodolico 112
   my @requiredFields = ( 'maintenance' );
4 rodolico 113
   my @targets;
25 rodolico 114
   if ( $main::config->{'flags'}->{'target'} ) {
115
      push @_, $main::config->{'flags'}->{'target'};
4 rodolico 116
   }
12 rodolico 117
   @_ = keys %{$main::statusDB->{'node'}} unless @_;
9 rodolico 118
   while ( my $nodename = shift  ) {
26 rodolico 119
      print "Updating $nodename\n" if $main::config->{'flags'}->{'debug'} || $main::config->{'flags'}->{'verbose'};
25 rodolico 120
      my $command = &main::makeCommand($nodename, "virsh nodeinfo" );
26 rodolico 121
      if ( $main::config->{'flags'}->{'dryrun'} ) {
122
         push @return, $command;
123
      } else {  
124
         my $return, `$command`;
125
         print "Output of [$command] is\n" . $return if $main::config->{'flags'}->{'debug'};
126
         my @nodeinfo = split( "\n", $return );
127
         for ( my $i = 0; $i < @nodeinfo; $i++ ) {
128
            my ($key, $value) = split( /:\s+/, $nodeinfo[$i] );
129
            if ( $value =~ m/^(\d+)\s+[a-z]+$/i ) {
130
               $value = $1;
131
            }
132
            $key = $conversion{$key} if exists( $conversion{$key} );
133
            $main::statusDB->{'node'}->{$nodename}->{$key} = $value;
134
         } # for
135
         foreach my $field ( @requiredFields ) {
136
            $main::statusDB->{'node'}->{$nodename}->{$field} = '' 
137
               unless defined ( $main::statusDB->{'node'}->{$nodename}->{$field} );
138
         } # foreach
139
      }
4 rodolico 140
   } # while
25 rodolico 141
   print "main::statusDB->{'node'} state after update\n" . Dumper( $main::statusDB->{'node'} ) if $main::config->{'flags'}->{'debug'};
12 rodolico 142
   &main::writeDB();
26 rodolico 143
   return "Node has been updated\n" . join( "\n", @return ) . "\n";
4 rodolico 144
}      
145
 
5 rodolico 146
 
147
# check one or more nodes and determine which domains are running on them.
148
# defaults to everything in the node database, but the -t can have it run on only one
149
# this is the function that should be run every few minutes on one of the servers
4 rodolico 150
sub scan {
15 rodolico 151
   return &main::scan(@_);
5 rodolico 152
}
4 rodolico 153
 
3 rodolico 154
 
5 rodolico 155
# add a new node. This is the same as doing an update on a node that doesn't exist.
156
sub add {
12 rodolico 157
   &update( @_ );
3 rodolico 158
}
159
 
25 rodolico 160
# put node in maintenance mode
161
# if there are running domains on it, migrate them off first
17 rodolico 162
sub maintenance {
163
   my ( $node, $action ) = @_;
164
   &main::readDB(1);
25 rodolico 165
   my @return;
17 rodolico 166
   if ( $action ) {
25 rodolico 167
      if ( lc ( $action ) eq 'on' ) {
168
         if ( $main::statusDB->{'nodePopulation'}->{$node}->{'running'} ) {
169
            # we've requested maintenance mode, but there are domains running on the node
26 rodolico 170
            print "Trying to migrate domains off of $node before doing maintenance\n" if $main::config->{'flags'}->{'verbose'};
25 rodolico 171
            push @return, &migrateAllDomains( $node, $main::config->{'flags'}->{'target'}, keys %{$main::statusDB->{'nodePopulation'}->{$node}->{'running'}} );
172
         }
173
         if ( $main::statusDB->{'nodePopulation'}->{$node}->{'running'} ) {
174
            push @return,  "Can not mark $node in maintenance mode with running domains";
175
         } else {
26 rodolico 176
            print "Marking $node as under maintenance\n" if $main::config->{'flags'}->{'verbose'};
25 rodolico 177
            $main::statusDB->{'node'}->{$node}->{'maintenance'} = 1;
178
         }
179
      } else {
26 rodolico 180
         print "Marking $node as Online\n" if $main::config->{'flags'}->{'verbose'};
181
         $main::statusDB->{'node'}->{$node}->{'maintenance'} = 0;
25 rodolico 182
      }
17 rodolico 183
   }
184
   &main::writeDB();
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