Local modifications to ClusterLabs/Anvil by Alteeve
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

412 lines
16 KiB

#!/usr/bin/perl
#
# This program will manage servers; Changing RAM, CPU cores, Growing virtual disks, adding virtual disks,
# inserting and ejecting ISO images into virtual optical media.
#
# Exit codes;
# 0 = Normal exit.
# 1 = No database connection.
#
# TODO:
#
use strict;
use warnings;
use Anvil::Tools;
require POSIX;
use Term::Cap;
my $THIS_FILE = ($0 =~ /^.*\/(.*)$/)[0];
my $running_directory = ($0 =~ /^(.*?)\/$THIS_FILE$/)[0];
if (($running_directory =~ /^\./) && ($ENV{PWD}))
{
$running_directory =~ s/^\./$ENV{PWD}/;
}
# Turn off buffering so that the pinwheel will display while waiting for the SSH call(s) to complete.
$| = 1;
my $anvil = Anvil::Tools->new();
$anvil->data->{switches}{anvil} = ""; # This is the Anvil! Name or UUID being worked on.
$anvil->data->{switches}{boot} = ""; # This is a comma-separated list of ordered boot devices
$anvil->data->{switches}{cores} = ""; # This sets the server to use this number of CPU cores.
$anvil->data->{switches}{drive} = ""; # drive being modified (insert/eject ISO, growing drive)
$anvil->data->{switches}{eject} = ""; # This will eject whatever ISO (if any) in the '--drive'.
$anvil->data->{switches}{'expand-to'} = ""; # When the drive is a disk (backed by a DRBD resource), this is the new desired size to grow to.
$anvil->data->{switches}{insert} = ""; # This is the ISO to insert into the --drive
$anvil->data->{switches}{'job-uuid'} = "";
$anvil->data->{switches}{ram} = ""; # This is the amount of RAM to set the server to use.
$anvil->data->{switches}{server} = ""; # server name or uuid
$anvil->data->{switches}{y} = ""; # Don't prompt for confirmation. Only useful when there isn't a job UUID.
$anvil->Get->switches;
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, 'print' => 1, level => 1, secure => 0, key => "log_0115", variables => { program => $THIS_FILE }});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
'switches::boot' => $anvil->data->{switches}{boot},
'switches::cores' => $anvil->data->{switches}{cores},
'switches::drive' => $anvil->data->{switches}{drive},
'switches::eject' => $anvil->data->{switches}{eject},
'switches::expand-to' => $anvil->data->{switches}{'expand-to'},
'switches::insert' => $anvil->data->{switches}{insert},
'switches::job-uuid' => $anvil->data->{switches}{'job-uuid'},
'switches::ram' => $anvil->data->{switches}{ram},
'switches::server' => $anvil->data->{switches}{server},
'switches::y' => $anvil->data->{switches}{y},
}});
# Connect to the database(s). If we have no connections, we'll proceed anyway as one of the 'run_once' tasks
# is to setup the database server.
$anvil->Database->connect();
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 3, secure => 0, key => "log_0132"});
if (not $anvil->data->{sys}{database}{connections})
{
# No databases, update the job, sleep for a bit and then exit. The daemon will pick it up and try
# again after we exit.
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, 'print' => 1, level => 0, priority => "err", key => "error_0305"});
sleep 10;
$anvil->nice_exit({exit_code => 1});
}
# If we don't have a job UUID, try to find one.
if (not $anvil->data->{switches}{'job-uuid'})
{
# Load the job data.
$anvil->data->{switches}{'job-uuid'} = $anvil->Job->get_job_uuid({program => $THIS_FILE});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { "switches::job-uuid" => $anvil->data->{switches}{'job-uuid'} }});
}
# If we still don't have a job-uuit, go into interactive mode.
if ($anvil->data->{switches}{'job-uuid'})
{
# Load the job data.
$anvil->Job->clear();
$anvil->Job->get_job_details();
$anvil->Job->update_progress({
progress => 1,
job_picked_up_by => $$,
job_picked_up_at => time,
message => "message_0251",
});
# Job data will be in $anvil->data->{jobs}{job_data}
run_jobs($anvil);
}
else
{
# Interactive!
interactive_question($anvil);
}
$anvil->nice_exit({exit_code => 0});
# Make sure we're in an Anvil!
$anvil->data->{sys}{anvil_uuid} = $anvil->Cluster->get_anvil_uuid();
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
'sys::anvil_uuid' => $anvil->data->{sys}{anvil_uuid},
}});
if (not $anvil->data->{sys}{anvil_uuid})
{
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, 'print' => 1, level => 0, priority => "err", key => "error_0260"});
$anvil->Job->update_progress({progress => 100, message => "error_0260"});
$anvil->nice_exit({exit_code => 1});
}
# Load servers and resources.
$anvil->Database->get_servers();
$anvil->DRBD->gather_data({debug => 2});
$anvil->Get->available_resources({
debug => 2,
anvil_uuid => $anvil->data->{sys}{anvil_uuid},
});
# Do we have a server (name or UUID)?
if ($anvil->Validate->uuid({uuid => $anvil->data->{switches}{server}}))
{
$anvil->data->{sys}{server_uuid} = $anvil->data->{switches}{server};
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
"sys::server_uuid" => $anvil->data->{sys}{server_uuid},
}});
# Find the server name
my $server_uuid = $anvil->data->{sys}{server_uuid};
if (exists $anvil->data->{servers}{server_uuid}{$server_uuid})
{
$anvil->data->{sys}{server_name} = $anvil->data->{servers}{server_uuid}{$server_uuid}{server_name};
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
"sys::server_name" => $anvil->data->{sys}{server_name},
}});
}
}
elsif ($anvil->data->{switches}{server})
{
$anvil->data->{sys}{server_name} = $anvil->data->{switches}{server};
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
"sys::server_name" => $anvil->data->{sys}{server_name},
}});
# Get the server UUID.
my $anvil_uuid = $anvil->data->{sys}{anvil_uuid};
my $server_name = $anvil->data->{sys}{server_name};
if (exists $anvil->data->{servers}{anvil_uuid}{$anvil_uuid}{server_name}{$server_name})
{
$anvil->data->{sys}{server_uuid} = $anvil->data->{servers}{anvil_uuid}{$anvil_uuid}{server_name}{$server_name}{server_uuid};
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
"sys::server_uuid" => $anvil->data->{sys}{server_uuid},
}});
}
}
# Do we have a valid server?
if ((not $anvil->data->{sys}{server_name}) or (not $anvil->data->{sys}{server_uuid}))
{
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, 'print' => 1, level => 0, key => "message_0247"});
my $anvil_uuid = $anvil->data->{sys}{anvil_uuid};
foreach my $server_name (sort {$a cmp $b} keys %{$anvil->data->{servers}{anvil_uuid}{$anvil_uuid}{server_name}})
{
my $server_uuid = $anvil->data->{servers}{anvil_uuid}{$anvil_uuid}{server_name}{$server_name}{server_uuid};
print "- ".$server_name." (".$server_uuid.")\n";
}
}
# Show the server's existing stats.
show_stats($anvil);
$anvil->nice_exit({exit_code => 0});
#############################################################################################################
# Functions #
#############################################################################################################
sub run_jobs
{
my ($anvil) = @_;
return(0);
}
sub interactive_question
{
my ($anvil) = @_;
$anvil->Database->get_anvils();
$anvil->Database->get_servers();
### Server
# First, has the user specified a server? If so, and if it's by name, make sure it's unique. If the
# name exists on two or more Anvil! systems, we'll need an Anvil! name as well. If it's unique, we
# can devine the Anvil! UUID.
if ($anvil->data->{switches}{server})
{
if ($anvil->Validate->uuid({uuid => $anvil->data->{switches}{server}}))
{
$anvil->data->{target_server}{server_uuid} = $anvil->data->{switches}{server};
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
"target_server::server_uuid" => $anvil->data->{target_server}{server_uuid},
}});
}
else
{
$anvil->data->{target_server}{server_name} = $anvil->data->{switches}{server};
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
"target_server::server_name" => $anvil->data->{target_server}{server_name},
}});
}
}
# Do we know or can we find the Anvil! UUID?
$anvil->data->{target_server}{server_uuid} = $anvil->data->{switches}{'server-uuid'} ? $anvil->data->{switches}{'server-uuid'} : "";
$anvil->data->{target_server}{server_name} = $anvil->data->{switches}{'server-name'} ? $anvil->data->{switches}{'server-name'} : "";
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
"target_server::server_uuid" => $anvil->data->{target_server}{server_uuid},
"target_server::server_name" => $anvil->data->{target_server}{server_name},
}});
# If we have a server UUID, make sure it's valid.
if $anvil->data->{target_server}{server_uuid})
{
# Pull up the server data.
my $server_uuid = $anvil->data->{target_server}{server_uuid};
if (exists $anvil->data->{servers}{server_uuid}{$server_uuid})
{
# We can divine everthing from this.
$anvil->data->{target_server}{server_name} = $anvil->data->{servers}{server_uuid}{$server_uuid}{server_name};
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
"target_server::server_name" => $anvil->data->{target_server}{server_name},
}});
}
}
if (not $anvil->data->{target_server}{server_name})
{
$anvil->data->{target_server}{server_name} = $anvil->Cluster->get_server_name({server_uuid => $anvil->data->{target_server}{server_uuid}});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { "target_server::server_name" => $anvil->data->{target_server}{server_name} }});
}
elsif (not $anvil->data->{target_server}{server_uuid})
{
$anvil->data->{target_server}{server_uuid} = $anvil->Cluster->get_server_uuid({server_name => $anvil->data->{target_server}{server_name}});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { "target_server::server_uuid" => $anvil->data->{target_server}{server_uuid} }});
}
### Anvil
# If 'switches::anvil' is set, see if it's a UUID and then set either 'anvil-uuid' or 'anvil-name'.
if ($anvil->data->{switches}{anvil})
{
if ($anvil->Validate->uuid({uuid => $anvil->data->{switches}{anvil}}))
{
$anvil->data->{target_server}{anvil_uuid} = $anvil->data->{switches}{anvil};
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
"target_server::anvil_uuid" => $anvil->data->{target_server}{anvil_uuid},
}});
}
else
{
$anvil->data->{target_server}{anvil_name} = $anvil->data->{switches}{anvil};
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
"target_server::anvil_name" => $anvil->data->{target_server}{anvil_name},
}});
}
}
# Do we know or can we find the Anvil! UUID?
$anvil->data->{target_server}{anvil_uuid} = $anvil->data->{switches}{'anvil-uuid'} ? $anvil->data->{switches}{'anvil-uuid'} : "";
$anvil->data->{target_server}{anvil_name} = $anvil->data->{switches}{'anvil-name'} ? $anvil->data->{switches}{'anvil-name'} : "";
if ((not $anvil->data->{target_server}{anvil_uuid}) && (not $anvil->data->{target_server}{anvil_name}))
{
# Nothing given. Is this host a node, perhaps?
my $anvil_uuid = $anvil->Cluster->get_anvil_uuid();
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { anvil_uuid => $anvil_uuid }});
if ($anvil_uuid)
{
$anvil->data->{target_server}{anvil_uuid} = $anvil_uuid;
$anvil->data->{target_server}{anvil_name} = $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_name};
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
"target_server::anvil_name" => $anvil->data->{target_server}{anvil_name},
"target_server::anvil_uuid" => $anvil->data->{target_server}{anvil_uuid},
}});
}
}
elsif (not $anvil->data->{target_server}{anvil_uuid})
{
$anvil->data->{target_server}{anvil_uuid} = $anvil->Cluster->get_anvil_uuid({anvil_name => $anvil->data->{target_server}{anvil_name}});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { "target_server::anvil_uuid" => $anvil->data->{target_server}{anvil_uuid} }});
}
elsif (not $anvil->data->{target_server}{anvil_name})
{
$anvil->data->{target_server}{anvil_name} = $anvil->Cluster->get_anvil_name({anvil_uuid => $anvil->data->{target_server}{anvil_uuid}});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { "target_server::anvil_name" => $anvil->data->{target_server}{anvil_name} }});
}
# If this is a node, load the anvil_uuid automatically.
my $termios = new POSIX::Termios;
$termios->getattr;
my $ospeed = $termios->getospeed;
my $terminal = Tgetent Term::Cap { TERM => undef, OSPEED => $ospeed };
$terminal->Trequire(qw/ce ku kd/);
return(0);
}
sub show_stats
{
my ($anvil) = @_;
# Load the server's details.
my $server_xml = $anvil->Server->get_definition({server_uuid => $anvil->data->{sys}{server_uuid}});
my $server_name = $anvil->data->{sys}{server_name};
my $short_host_name = $anvil->Get->short_host_name();
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
's1:short_host_name' => $short_host_name,
's2:server_name' => $server_name,
's3:server_xml' => $server_xml,
}});
$anvil->Server->parse_definition({
debug => 2,
source => "from_db",
definition => $server_xml,
server => $server_name,
});
my $cpu_cores = $anvil->data->{server}{$short_host_name}{$server_name}{from_db}{cpu}{total_cores};
my $ram_bytes = $anvil->data->{server}{$short_host_name}{$server_name}{from_db}{memory};
my $say_ram = $anvil->Convert->bytes_to_human_readable({'bytes' => $ram_bytes});
print "Cores: [".$cpu_cores."], RAM: [".$say_ram."] (".$ram_bytes." bytes)\n";
# Show disks
foreach my $device ("disk", "cdrom")
{
print "- Device: [".$device."]\n";
foreach my $device_target (sort {$a cmp $b} keys %{$anvil->data->{server}{$short_host_name}{$server_name}{from_db}{device}{$device}{target}})
{
my $boot_order = $anvil->data->{server}{$short_host_name}{$server_name}{from_db}{device}{$device}{target}{$device_target}{boot_order};
my $type = $anvil->data->{server}{$short_host_name}{$server_name}{from_db}{device}{$device}{target}{$device_target}{type};
my $device_bus = $anvil->data->{server}{$short_host_name}{$server_name}{from_db}{device}{$device}{target}{$device_target}{device_bus};
my $path = $anvil->data->{server}{$short_host_name}{$server_name}{from_db}{device}{$device}{target}{$device_target}{path};
print " - Target: [".$device_target."], type: [".$type."], boot order: [".$boot_order."], bus: [".$device_bus."]\n";
print " - Path: [".$path."]\n";
if ($device eq "disk")
{
# Pull the size
my $volume = ($path =~ /\/(\d+)$/)[0];
print " - Volume: [".$volume."]\n";
foreach my $host_name (sort {$a cmp $b} keys %{$anvil->data->{new}{resource}{$server_name}{host}})
{
my $host_uuid = $anvil->Get->host_uuid_from_name({host_name => $host_name});
my $device_path = $anvil->data->{new}{resource}{$server_name}{host}{$host_name}{volume}{$volume}{device_path};
my $backing_disk = $anvil->data->{new}{resource}{$server_name}{host}{$host_name}{volume}{$volume}{backing_disk};
print " - Host: [".$host_name."] (".$host_uuid."), path: [".$device_path."], backing disk: [".$backing_disk."]\n";
}
}
else
{
}
}
}
return(0);
}