#!/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}); =cut # 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); =cut $anvil->nice_exit({exit_code => 0}); ############################################################################################################# # Functions # ############################################################################################################# sub run_jobs { my ($anvil) = @_; return(0); } sub interactive_question { my ($anvil) = @_; $anvil->Database->get_hosts(); $anvil->Database->get_anvils(); $anvil->Database->get_servers(); $anvil->data->{target_server}{server_uuid} = "" if not defined $anvil->data->{target_server}{server_uuid}; $anvil->data->{target_server}{server_name} = "" if not defined $anvil->data->{target_server}{server_name}; $anvil->data->{target_server}{server_state} = "" if not defined $anvil->data->{target_server}{server_state}; $anvil->data->{target_server}{anvil_uuid} = "" if not defined $anvil->data->{target_server}{anvil_uuid}; $anvil->data->{target_server}{anvil_name} = "" if not defined $anvil->data->{target_server}{anvil_name}; $anvil->data->{target_server}{anvil_description} = "" if not defined $anvil->data->{target_server}{anvil_description}; $anvil->data->{target_server}{anvil_node1_host_uuid} = "" if not defined $anvil->data->{target_server}{anvil_node1_host_uuid}; $anvil->data->{target_server}{anvil_node1_host_name} = "" if not defined $anvil->data->{target_server}{anvil_node1_host_name}; $anvil->data->{target_server}{anvil_node2_host_uuid} = "" if not defined $anvil->data->{target_server}{anvil_node2_host_uuid}; $anvil->data->{target_server}{anvil_node2_host_name} = "" if not defined $anvil->data->{target_server}{anvil_node2_host_name}; $anvil->data->{target_server}{anvil_dr1_host_uuid} = "" if not defined $anvil->data->{target_server}{anvil_dr1_host_uuid}; $anvil->data->{target_server}{anvil_dr1_host_name} = "" if not defined $anvil->data->{target_server}{anvil_dr1_host_name}; ### 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}, }}); } } # 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. my $anvil_uuid = $anvil->data->{servers}{server_uuid}{$server_uuid}{server_anvil_uuid}; my $server_host_uuid = $anvil->data->{servers}{server_uuid}{$server_uuid}{server_host_uuid}; my $node1_host_uuid = $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_node1_host_uuid}; my $node2_host_uuid = $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_node1_host_uuid}; my $dr1_host_uuid = $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_dr1_host_uuid}; $anvil->data->{target_server}{server_name} = $anvil->data->{servers}{server_uuid}{$server_uuid}{server_name}; $anvil->data->{target_server}{server_state} = $anvil->data->{servers}{server_uuid}{$server_uuid}{server_state}; $anvil->data->{target_server}{anvil_uuid} = $anvil_uuid; $anvil->data->{target_server}{anvil_name} = $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_name}; $anvil->data->{target_server}{anvil_description} = $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_description}; $anvil->data->{target_server}{anvil_node1_host_uuid} = $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_node1_host_uuid}; $anvil->data->{target_server}{anvil_node1_host_name} = $anvil->data->{hosts}{host_uuid}{$node1_host_uuid}{host_name}; $anvil->data->{target_server}{anvil_node2_host_uuid} = $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_node2_host_uuid}; $anvil->data->{target_server}{anvil_node2_host_name} = $anvil->data->{hosts}{host_uuid}{$node2_host_uuid}{host_name}; $anvil->data->{target_server}{anvil_dr1_host_uuid} = $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_node1_host_uuid}; $anvil->data->{target_server}{anvil_dr1_host_name} = $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_node1_host_uuid}; if ($dr1_host_uuid) { $anvil->data->{target_server}{anvil_dr1_host_uuid} = $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_dr1_host_uuid}; $anvil->data->{target_server}{anvil_dr1_host_name} = $anvil->data->{hosts}{host_uuid}{$dr1_host_uuid}{host_name};; } ### Pull out details of the server. # How much RAM are we using and how much is configured? $anvil->data->{target_server}{server_ram_in_use} = $anvil->data->{servers}{server_uuid}{$server_uuid}{server_ram_in_use}; $anvil->data->{target_server}{server_host_uuid} = $server_host_uuid; $anvil->data->{target_server}{server_host_name} = $anvil->data->{hosts}{host_uuid}{$server_host_uuid}{host_name}; if ($anvil->data->{target_server}{server_state} eq "shut off") { $anvil->data->{target_server}{server_ram_in_use} = 0; $anvil->data->{target_server}{server_host_uuid} = ""; $anvil->data->{target_server}{server_host_name} = ""; } $anvil->data->{target_server}{server_configured_ram} = $anvil->data->{servers}{server_uuid}{$server_uuid}{server_configured_ram}; $anvil->data->{target_server}{server_start_after_server_uuid} = $anvil->data->{servers}{server_uuid}{$server_uuid}{server_start_after_server_uuid}; $anvil->data->{target_server}{server_start_after_server_name} = ""; $anvil->data->{target_server}{server_start_delay} = 0; if ($anvil->data->{target_server}{server_start_after_server_uuid}) { my $server_start_after_server_uuid = $anvil->data->{target_server}{server_start_after_server_uuid}; $anvil->data->{target_server}{server_start_after_server_name} = $anvil->data->{servers}{server_uuid}{$server_start_after_server_uuid}{server_name}; $anvil->data->{target_server}{server_start_delay} = $anvil->data->{servers}{server_uuid}{$server_uuid}{server_start_delay}; } # Get a list of files on this Anvil! foreach my $file_uuid (keys %{$anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{file_uuid}}) { my $file_name = $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{file_uuid}{$file_uuid}{file_name}; my $file_directory = $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{file_uuid}{$file_uuid}{file_directory}; my $file_size = $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{file_uuid}{$file_uuid}{file_size}; my $file_md5sum = $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{file_uuid}{$file_uuid}{file_md5sum}; my $file_type = $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{file_uuid}{$file_uuid}{file_type}; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { file_name => $file_name, file_directory => $file_directory, file_size => $anvil->Convert->bytes_to_human_readable({'bytes' => $file_size})." (".$anvil->Convert->add_commas({number => $file_size}).")", file_md5sum => $file_md5sum, file_type => $file_type, }}); if ($file_type eq "iso") { # ISO image } else { # (Potential) script } } # $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { # "anvils::anvil_uuid::${anvil_uuid}::file_uuid::${file_uuid}::file_name" => $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{file_uuid}{$file_uuid}{file_name}, # "anvils::anvil_uuid::${anvil_uuid}::file_uuid::${file_uuid}::file_directory" => $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{file_uuid}{$file_uuid}{file_directory}, # "anvils::anvil_uuid::${anvil_uuid}::file_uuid::${file_uuid}::file_size" => $anvil->Convert->bytes_to_human_readable({'bytes' => $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{file_uuid}{$file_uuid}{file_size}})." (".$anvil->Convert->add_commas({number => $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{file_uuid}{$file_uuid}{file_size}}).")", # "anvils::anvil_uuid::${anvil_uuid}::file_uuid::${file_uuid}::file_md5sum" => $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{file_uuid}{$file_uuid}{file_md5sum}, # "anvils::anvil_uuid::${anvil_uuid}::file_uuid::${file_uuid}::file_type" => $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{file_uuid}{$file_uuid}{file_type}, # }}); # # "servers::server_uuid::${server_uuid}::server_pre_migration_file_uuid" => $anvil->data->{servers}{server_uuid}{$server_uuid}{server_pre_migration_file_uuid}, # "servers::server_uuid::${server_uuid}::server_pre_migration_arguments" => $anvil->data->{servers}{server_uuid}{$server_uuid}{server_pre_migration_arguments}, # "servers::server_uuid::${server_uuid}::server_post_migration_file_uuid" => $anvil->data->{servers}{server_uuid}{$server_uuid}{server_post_migration_file_uuid}, # "servers::server_uuid::${server_uuid}::server_post_migration_arguments" => $anvil->data->{servers}{server_uuid}{$server_uuid}{server_post_migration_arguments}, # # # $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { # "hosts::host_uuid::${host_uuid}::host_name" => $anvil->data->{hosts}{host_uuid}{$host_uuid}{host_name}, # "hosts::host_uuid::${host_uuid}::short_host_name" => $anvil->data->{hosts}{host_uuid}{$host_uuid}{short_host_name}, # "hosts::host_uuid::${host_uuid}::host_type" => $anvil->data->{hosts}{host_uuid}{$host_uuid}{host_type}, # "hosts::host_uuid::${host_uuid}::host_key" => $anvil->data->{hosts}{host_uuid}{$host_uuid}{host_key}, # "hosts::host_uuid::${host_uuid}::host_ipmi" => $host_ipmi =~ /passw/ ? $anvil->Log->is_secure($anvil->data->{hosts}{host_uuid}{$host_uuid}{host_ipmi}) : $anvil->data->{hosts}{host_uuid}{$host_uuid}{host_ipmi}, # "hosts::host_uuid::${host_uuid}::host_status" => $anvil->data->{hosts}{host_uuid}{$host_uuid}{host_status}, # "hosts::host_uuid::${host_uuid}::anvil_name" => $anvil->data->{hosts}{host_uuid}{$host_uuid}{anvil_name}, # "hosts::host_uuid::${host_uuid}::anvil_uuid" => $anvil->data->{hosts}{host_uuid}{$host_uuid}{anvil_uuid}, # }}); # $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { # "anvils::anvil_uuid::${anvil_uuid}::anvil_name" => $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_name}, # "anvils::anvil_uuid::${anvil_uuid}::anvil_description" => $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_description}, # "anvils::anvil_uuid::${anvil_uuid}::anvil_password" => $anvil->Log->is_secure($anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_password}), # "anvils::anvil_uuid::${anvil_uuid}::anvil_node1_host_uuid" => $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_node1_host_uuid}, # "anvils::anvil_uuid::${anvil_uuid}::anvil_node2_host_uuid" => $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_node2_host_uuid}, # "anvils::anvil_uuid::${anvil_uuid}::anvil_dr1_host_uuid" => $anvil->data->{anvils}{anvil_uuid}{$anvil_uuid}{anvil_dr1_host_uuid}, # }}); # $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => $debug, list => { # "servers::server_uuid::${server_uuid}::server_anvil_uuid" => $anvil->data->{servers}{server_uuid}{$server_uuid}{server_anvil_uuid}, # "servers::server_uuid::${server_uuid}::server_user_stop" => $anvil->data->{servers}{server_uuid}{$server_uuid}{server_user_stop}, # "servers::server_uuid::${server_uuid}::server_start_after_server_uuid" => $anvil->data->{servers}{server_uuid}{$server_uuid}{server_start_after_server_uuid}, # "servers::server_uuid::${server_uuid}::server_start_delay" => $anvil->data->{servers}{server_uuid}{$server_uuid}{server_start_delay}, # "servers::server_uuid::${server_uuid}::server_host_uuid" => $anvil->data->{servers}{server_uuid}{$server_uuid}{server_host_uuid}, # "servers::server_uuid::${server_uuid}::server_state" => $anvil->data->{servers}{server_uuid}{$server_uuid}{server_state}, # "servers::server_uuid::${server_uuid}::server_live_migration" => $anvil->data->{servers}{server_uuid}{$server_uuid}{server_live_migration}, # "servers::server_uuid::${server_uuid}::server_pre_migration_file_uuid" => $anvil->data->{servers}{server_uuid}{$server_uuid}{server_pre_migration_file_uuid}, # "servers::server_uuid::${server_uuid}::server_pre_migration_arguments" => $anvil->data->{servers}{server_uuid}{$server_uuid}{server_pre_migration_arguments}, # "servers::server_uuid::${server_uuid}::server_post_migration_file_uuid" => $anvil->data->{servers}{server_uuid}{$server_uuid}{server_post_migration_file_uuid}, # "servers::server_uuid::${server_uuid}::server_post_migration_arguments" => $anvil->data->{servers}{server_uuid}{$server_uuid}{server_post_migration_arguments}, # "servers::server_uuid::${server_uuid}::server_ram_in_use" => $anvil->data->{servers}{server_uuid}{$server_uuid}{server_ram_in_use}, # "servers::server_uuid::${server_uuid}::server_configured_ram" => $anvil->data->{servers}{server_uuid}{$server_uuid}{server_configured_ram}, # "servers::server_uuid::${server_uuid}::server_updated_by_user" => $anvil->data->{servers}{server_uuid}{$server_uuid}{server_updated_by_user}, # "servers::server_uuid::${server_uuid}::server_boot_time" => $anvil->data->{servers}{server_uuid}{$server_uuid}{server_boot_time}, # }}); $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { "target_server::server_name" => $anvil->data->{target_server}{server_name}, }}); } else { # Bad server UUID. If it's a job, abort. Otherwise, clear and go back to my $variables = { server_uuid => $anvil->data->{target_server}{server_uuid} }; $anvil->Jobs->update_progress({ progress => 100, message => "error_0318", variables => $variables, job_status => "failed", log_level => 1, priority => "err", }); if ($anvil->data->{switches}{'job-uuid'}) { $anvil->nice_exit({exit_code => 1}); } $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}, }}); } else { # If there is only one Anvil!, choose it my $known_anvils = keys %{$anvil->data->{anvils}{anvil_name}}; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { known_anvils => $known_anvils }}); if (not $known_anvils) my $variables = { server_uuid => $anvil->data->{target_server}{server_uuid} }; $anvil->Jobs->update_progress({ progress => 100, message => "error_0321", job_status => "failed", log_level => 1, priority => "err", }); $anvil->nice_exit({exit_code => 1}); } elsif ($known_anvils == 1) { foreach my $anvil_name (keys %{$anvil->data->{anvils}{anvil_name}}) { $anvil->data->{target_server}{anvil_name} = $anvil_name; $anvil->data->{target_server}{anvil_uuid} = $anvil->data->{anvils}{anvil_name}{$anvil_name}{anvil_uuid}; $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}) { # We have a name, get the 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} }}); # If the name is bad, error out if not interactive if (not $anvil->data->{target_server}{anvil_uuid}) { # Bad server UUID. If it's a job, abort. Otherwise, clear and go back to my $variables = { anvil_name => $anvil->data->{target_server}{anvil_name} }; $anvil->Jobs->update_progress({ progress => 100, message => "error_0319", variables => $variables, job_status => "failed", log_level => 1, priority => "err", }); if ($anvil->data->{switches}{'job-uuid'}) { $anvil->nice_exit({exit_code => 1}); } $anvil->data->{target_server}{anvil_name} = ""; } } 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 the name is bad, error out if not interactive if (not $anvil->data->{target_server}{anvil_name}) { # Bad server UUID. If it's a job, abort. Otherwise, clear and go back to my $variables = { anvil_uuid => $anvil->data->{target_server}{anvil_uuid} }; $anvil->Jobs->update_progress({ progress => 100, message => "error_0320", variables => $variables, job_status => "failed", log_level => 1, priority => "err", }); if ($anvil->data->{switches}{'job-uuid'}) { $anvil->nice_exit({exit_code => 1}); } $anvil->data->{target_server}{anvil_uuid} = ""; } } # If we don't have an Anvil! UUID at this point, we need to ask the user. 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/); if (not $anvil->data->{target_server}{anvil_uuid}) { interactive_ask_anvil_name($anvil, $terminal); } return(0); } sub interactive_ask_anvil_name { my ($anvil, $terminal) = @_; my $retry = 0; while(1) { print $terminal->Tputs('cl'); print $anvil->Words->string({key => "job_0352"})."\n"; print $anvil->Words->string({key => "job_0151", variables => { anvil_name => $anvil->data->{target_server}{anvil_name} }})."\n\n\n"; # Show all the current server names. if ($retry) { print $anvil->Words->string({key => "job_0152"})."\n\n"; } print $anvil->Words->string({key => "job_0153"})."\n"; foreach my $anvil_name (sort {$a cmp $b} keys %{$anvil->data->{anvils}{anvil_name}}) { print "- ".$anvil_name.": - ".$anvil->data->{anvils}{anvil_name}{$anvil_name}{anvil_description}."\n"; } print $terminal->Tgoto('cm', 0, 2)."? "; my $answer = ; chomp $answer; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { answer => $answer }}); if ((not $answer) && ($default_anvil)) { $answer = $default_anvil; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { answer => $answer }}); } # Reload in case a new anvil! was saved while we waited. $anvil->Database->get_anvils(); if (exists $anvil->data->{anvils}{anvil_name}{$answer}) { # Valid. $anvil->data->{target_server}{anvil_name} = $answer; $anvil->data->{target_server}{anvil_uuid} = $anvil->Cluster->get_anvil_uuid({anvil_name => $answer}); $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}, }}); last; } else { $retry = 1; } } interactive_question($anvil); return(0); } sub interactive_ask_server_name { my ($anvil, $terminal) = @_; $anvil->Database->get_servers({debug => 2}); ### TODO: Figure out how many rows we have and break the server list into columns if too long. my $retry = 0; my $duplicate = ""; while(1) { my $default = ""; if ($anvil->data->{switches}{name}) { $default = $anvil->data->{switches}{name}; } print $terminal->Tputs('cl'); print $anvil->Words->string({key => "job_0150"})."\n"; print $anvil->Words->string({key => "job_0151", variables => { anvil_name => $anvil->data->{target_server}{anvil_name} }})."\n"; print $anvil->Words->string({key => "job_0157", variables => { server_name => $anvil->data->{target_server}{name} }})."\n\n\n"; # Show all the current server names. if ($retry) { if ($duplicate) { print $anvil->Words->string({key => "job_0219", variables => { server_name => $duplicate }})."\n\n"; $duplicate = ""; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { duplicate => $duplicate }}); } else { print $anvil->Words->string({key => "job_0159"})."\n\n"; } } my $anvil_uuid = $anvil->data->{target_server}{anvil_uuid}; print $anvil->Words->string({key => "job_0160", variables => { anvil_name => $anvil->data->{target_server}{anvil_name} }})."\n"; 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}; my $server_state = $anvil->data->{servers}{server_uuid}{$server_uuid}{server_state}; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { server_uuid => $server_uuid, server_state => $server_state, }}); if ($server_state eq "DELETED") { ### NOTE: This could get cluttered, so for now we'll not show them. #print $anvil->Words->string({key => "message_0220", variables => { server_name => $server_name }})."\n"; } else { print $anvil->Words->string({key => "message_0219", variables => { server_name => $server_name, server_state => $server_state, }})."\n"; } } print $terminal->Tgoto('cm', 0, 3)."? "; my $answer = ; chomp $answer; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { answer => $answer }}); if ((not $answer) && ($default)) { $answer = $default; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { answer => $answer }}); } # Reload in case a new anvil! was saved while we waited. $anvil->Database->get_servers(); if ($answer) { # Duplicate? $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { answer => $answer }}); if (exists $anvil->data->{servers}{anvil_uuid}{$anvil_uuid}{server_name}{$answer}) { my $server_uuid = $anvil->data->{servers}{anvil_uuid}{$anvil_uuid}{server_name}{$answer}{server_uuid}; my $server_state = $anvil->data->{servers}{server_uuid}{$server_uuid}{server_state}; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { server_uuid => $server_uuid, server_state => $server_state, }}); if ($server_state eq "DELETED") { # Valid, we can re-use deleted server names. We'll also re-use the # UUID, if the user didn't specifically specify a UUID. $anvil->data->{target_server}{name} = $answer; $anvil->data->{target_server}{uuid} = $server_uuid if not $anvil->data->{target_server}{uuid}; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { "target_server::name" => $anvil->data->{target_server}{name}, "target_server::uuid" => $anvil->data->{target_server}{uuid}, }}); } else { # Invalid, duplicate. $duplicate = $answer; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { duplicate => $duplicate }}); } } else { # Valid. $anvil->data->{target_server}{name} = $answer; $anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { "target_server::name" => $anvil->data->{target_server}{name}, }}); } last; } else { $retry = 1; } } 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); }