Subversion Repositories havirt

Rev

Rev 35 | 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";
29 rodolico 73
   push @return, "node maintenance nodename [on|off --target=targetNode]";
17 rodolico 74
   push @return, "\ton - set maintenance flag; no domains can be started/migrated to node";
29 rodolico 75
   push @return, "\t     target must be set with the --target flag";
17 rodolico 76
   push @return, "\toff - Allows domains to be migrated/started on node";
77
   push @return, "\tnothing - displays current maintenance flag";
78
   push @return, "\tNote: a node with any domains running can not have maintenance mode turned on";
9 rodolico 79
   return join( "\n", @return ) . "\n";
80
}
81
 
82
 
5 rodolico 83
# lists hardware capabilities of all nodes (virsh nodeinfo)
4 rodolico 84
sub list {
85
   my @header;
86
   my @data;
87
   my $return;
12 rodolico 88
   &main::readDB();
89
   foreach my $node ( sort keys %{$main::statusDB->{'node'}} ) {
4 rodolico 90
      unless ( @header ) {
91
         # just grab the keys for headers
12 rodolico 92
         @header = sort keys %{ $main::statusDB->{'node'}->{$node} };
4 rodolico 93
         # put Node at the beginning
94
         unshift ( @header, 'Node' );
95
      }
96
      my @line;
97
      push @line, $node;
12 rodolico 98
      foreach my $column (sort keys %{ $main::statusDB->{'node'}->{$node} }) {
99
         push @line, $main::statusDB->{'node'}->{$node}->{$column};
4 rodolico 100
      }
101
      push (@data, \@line );
102
   }
7 rodolico 103
   return &main::report( \@header, \@data );
4 rodolico 104
}
105
 
5 rodolico 106
# Get information about a node. Really only needs to be done when a node is
107
# first defined, or if there is a hardware upgrade
9 rodolico 108
# reads information off of the stack (@_), but will add to that if --target
109
# was defined
4 rodolico 110
sub update {
12 rodolico 111
   &main::readDB( 1 ); # open and lock so we can write to it later
26 rodolico 112
   my @return;
18 rodolico 113
   my @requiredFields = ( 'maintenance' );
4 rodolico 114
   my @targets;
25 rodolico 115
   if ( $main::config->{'flags'}->{'target'} ) {
116
      push @_, $main::config->{'flags'}->{'target'};
4 rodolico 117
   }
12 rodolico 118
   @_ = keys %{$main::statusDB->{'node'}} unless @_;
9 rodolico 119
   while ( my $nodename = shift  ) {
26 rodolico 120
      print "Updating $nodename\n" if $main::config->{'flags'}->{'debug'} || $main::config->{'flags'}->{'verbose'};
25 rodolico 121
      my $command = &main::makeCommand($nodename, "virsh nodeinfo" );
26 rodolico 122
      if ( $main::config->{'flags'}->{'dryrun'} ) {
123
         push @return, $command;
124
      } else {  
125
         my $return, `$command`;
126
         print "Output of [$command] is\n" . $return if $main::config->{'flags'}->{'debug'};
127
         my @nodeinfo = split( "\n", $return );
128
         for ( my $i = 0; $i < @nodeinfo; $i++ ) {
129
            my ($key, $value) = split( /:\s+/, $nodeinfo[$i] );
130
            if ( $value =~ m/^(\d+)\s+[a-z]+$/i ) {
131
               $value = $1;
132
            }
133
            $key = $conversion{$key} if exists( $conversion{$key} );
134
            $main::statusDB->{'node'}->{$nodename}->{$key} = $value;
135
         } # for
136
         foreach my $field ( @requiredFields ) {
137
            $main::statusDB->{'node'}->{$nodename}->{$field} = '' 
138
               unless defined ( $main::statusDB->{'node'}->{$nodename}->{$field} );
139
         } # foreach
140
      }
4 rodolico 141
   } # while
25 rodolico 142
   print "main::statusDB->{'node'} state after update\n" . Dumper( $main::statusDB->{'node'} ) if $main::config->{'flags'}->{'debug'};
12 rodolico 143
   &main::writeDB();
26 rodolico 144
   return "Node has been updated\n" . join( "\n", @return ) . "\n";
4 rodolico 145
}      
146
 
5 rodolico 147
 
148
# check one or more nodes and determine which domains are running on them.
149
# defaults to everything in the node database, but the -t can have it run on only one
150
# this is the function that should be run every few minutes on one of the servers
4 rodolico 151
sub scan {
15 rodolico 152
   return &main::scan(@_);
5 rodolico 153
}
4 rodolico 154
 
3 rodolico 155
 
5 rodolico 156
# add a new node. This is the same as doing an update on a node that doesn't exist.
157
sub add {
12 rodolico 158
   &update( @_ );
3 rodolico 159
}
160
 
25 rodolico 161
# put node in maintenance mode
162
# if there are running domains on it, migrate them off first
17 rodolico 163
sub maintenance {
164
   my ( $node, $action ) = @_;
165
   &main::readDB(1);
25 rodolico 166
   my @return;
17 rodolico 167
   if ( $action ) {
25 rodolico 168
      if ( lc ( $action ) eq 'on' ) {
169
         if ( $main::statusDB->{'nodePopulation'}->{$node}->{'running'} ) {
170
            # we've requested maintenance mode, but there are domains running on the node
26 rodolico 171
            print "Trying to migrate domains off of $node before doing maintenance\n" if $main::config->{'flags'}->{'verbose'};
25 rodolico 172
            push @return, &migrateAllDomains( $node, $main::config->{'flags'}->{'target'}, keys %{$main::statusDB->{'nodePopulation'}->{$node}->{'running'}} );
173
         }
174
         if ( $main::statusDB->{'nodePopulation'}->{$node}->{'running'} ) {
175
            push @return,  "Can not mark $node in maintenance mode with running domains";
176
         } else {
26 rodolico 177
            print "Marking $node as under maintenance\n" if $main::config->{'flags'}->{'verbose'};
25 rodolico 178
            $main::statusDB->{'node'}->{$node}->{'maintenance'} = 1;
179
         }
180
      } else {
26 rodolico 181
         print "Marking $node as Online\n" if $main::config->{'flags'}->{'verbose'};
182
         $main::statusDB->{'node'}->{$node}->{'maintenance'} = 0;
25 rodolico 183
      }
17 rodolico 184
   }
185
   &main::writeDB();
25 rodolico 186
   return "Maintenance set to " . ( $main::statusDB->{'node'}->{$node}->{'maintenance'} ? 'On' : 'Off' ) . "\n" .
187
          ( @return ? join( "\n", @return ) . "\n" : '');
17 rodolico 188
}
189
 
25 rodolico 190
 
191
# migrate domains from node $from to node $to
192
# the rest of the stack is a list of domains to migrate
193
sub migrateAllDomains {
194
   my $from = shift;
195
   my $to = shift;
196
   print "In node.pm:migrateAllDomains, migrating\n" . join( "\n", @_ ) . "\nto $to\n"
197
      if ( $main::config->{'flags'}->{'debug'} );
198
   my @commands;
26 rodolico 199
   print "Checking for available resources on $to before migrating\n"  if $main::config->{'flags'}->{'verbose'};
25 rodolico 200
   if ( my $error = &main::validateResources( $to, @_ ) ) {
201
      return "We can not migrate all of the domains on $from to $to\n$error\n";
202
   }
203
   while ( my $domain = shift ) {
204
      push @commands, &main::migrate( $domain, $to );
205
   }
206
   chomp @commands;
207
   return join( "\n", @commands ) . "\n";
208
}
209
 
210