fix(tools): fetch and send server screenshot from node to striker that made the request

main
Tsu-ba-me 3 years ago
parent e14b1fc93e
commit e5fc75f306
  1. 1
      Anvil/Tools.pm
  2. 140
      tools/anvil-get-server-screenshot

@ -1117,6 +1117,7 @@ sub _set_paths
syslinux => "/usr/share/syslinux", syslinux => "/usr/share/syslinux",
tftpboot => "/var/lib/tftpboot", tftpboot => "/var/lib/tftpboot",
temp => "/tmp/anvil", temp => "/tmp/anvil",
tmp => "/tmp",
tools => "/usr/sbin", tools => "/usr/sbin",
units => "/usr/lib/systemd/system", units => "/usr/lib/systemd/system",
}, },

@ -18,40 +18,71 @@ if (($running_directory =~ /^\./) && ($ENV{PWD}))
my $anvil = Anvil::Tools->new(); my $anvil = Anvil::Tools->new();
sub system_call sub remote_call
{ {
my $parameters = shift; my $parameters = shift;
my $shell_call = $parameters->{shell_call}; my $debug = $parameters->{debug};
my ($shell_output, $shell_return_code) = $anvil->System->call({ shell_call => $shell_call }); my @call_result = $anvil->Remote->call($parameters);
$anvil->Log->variables({ source => $THIS_FILE, line => __LINE__, level => 2, list => { my ($output, $error, $return_code) = @call_result;
shell_call => $shell_call, $anvil->Log->variables({ source => $THIS_FILE, line => __LINE__, level => $debug, list => {
shell_output => $shell_output, %$parameters,
shell_return_code => $shell_return_code output => $output,
error => $error,
return_code => $return_code,
} }); } });
return ($shell_output, $shell_return_code); return @call_result;
} }
sub is_existing_server_screenshot_outdated sub system_call
{ {
my $parameters = shift; my $parameters = shift;
my $server_uuid = $parameters->{server_uuid}; my $debug = $parameters->{debug};
my @call_result = $anvil->System->call($parameters);
my ($output, $return_code) = @call_result;
$anvil->Log->variables({ source => $THIS_FILE, line => __LINE__, level => $debug, list => {
%$parameters,
output => $output,
return_code => $return_code,
} });
my ($encoded_image, $variable_uuid, $variable_mtime) = $anvil->Database->read_variable({ variable_name => "server_screenshot::".$server_uuid }); return @call_result;
}
my $time_difference = time - $variable_mtime; sub get_remote_call_requirements
{
my $parameters = shift;
my $host_uuid = $parameters->{host_uuid};
my $debug = $parameters->{debug};
my $query = "
SELECT ip_address_address
FROM ip_addresses
WHERE ip_address_gateway <> ''
AND ip_address_note <> 'DELETED'
AND ip_address_host_uuid = ".$anvil->Database->quote($host_uuid)."
;";
my $host_ip_address = $anvil->Database->query({ query => $query, source => $THIS_FILE, line => __LINE__, debug => $debug })->[0]->[0];
$anvil->Log->variables({ source => $THIS_FILE, line => __LINE__, level => $debug, list => {
host_uuid => $host_uuid,
host_ip_address => $host_ip_address,
} });
return $time_difference > 120 ? 1 : 0; return $host_ip_address;
} }
sub get_server_screenshot sub get_server_screenshot
{ {
my $parameters = shift; my $parameters = shift;
my $server_uuid = $parameters->{server_uuid}; my $server_uuid = $parameters->{server_uuid};
my $debug = $parameters->{debug};
my $resize_args = defined $parameters->{resize_args} ? $parameters->{resize_args} : ''; my $resize_args = defined $parameters->{resize_args} ? $parameters->{resize_args} : '';
my $shell_call = $anvil->data->{path}{exe}{virsh}" --quiet screenshot --domain ".$server_uuid." --file ".$anvil->data->{path}{devices}{stdout}; my $shell_call = $anvil->data->{path}{exe}{virsh}." --quiet screenshot --domain ".$server_uuid." --file ".$anvil->data->{path}{devices}{stdout};
if ($resize_args =~ /^\d+x\d+$/) if ($resize_args =~ /^\d+x\d+$/)
{ {
@ -62,39 +93,28 @@ sub get_server_screenshot
$shell_call .= " | ".$anvil->data->{path}{exe}{pamtopng}." -quiet | ".$anvil->data->{path}{exe}{base64}." --wrap 0"; $shell_call .= " | ".$anvil->data->{path}{exe}{pamtopng}." -quiet | ".$anvil->data->{path}{exe}{base64}." --wrap 0";
my ($shell_output, $shell_return_code) = system_call({ shell_call => $shell_call }); my ($output, $return_code) = system_call({ debug => $debug, shell_call => $shell_call });
return $shell_return_code == 0 ? $shell_output : undef;
}
sub insert_server_screenshot
{
my $parameters = shift;
my $server_uuid = $parameters->{server_uuid};
my $encoded_image = $parameters->{encoded_image};
$anvil->Database->insert_or_update_states({ return $return_code == 0 ? $output : undef;
state_name => "server_screenshot::".$server_uuid,
state_note => $encoded_image
});
} }
$anvil->Get->switches; $anvil->Get->switches;
my $debug = $anvil->data->{switches}{debug};
$anvil->Database->connect; $anvil->Database->connect;
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, secure => 0, key => "log_0132"}); $anvil->Log->entry({ source => $THIS_FILE, line => __LINE__, level => $debug, secure => 0, key => "log_0132" });
if (not $anvil->data->{sys}{database}{connections}) if (not $anvil->data->{sys}{database}{connections})
{ {
# No databases, exit. # No databases, exit.
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, 'print' => 1, priority => "err", key => "error_0003"}); $anvil->Log->entry({ source => $THIS_FILE, line => __LINE__, level => 0, 'print' => 1, priority => "err", key => "error_0003" });
$anvil->nice_exit({exit_code => 1}); $anvil->nice_exit({ exit_code => 1 });
} }
# Try to get a job UUID if not given. # Try to get a job UUID if not given.
if (not $anvil->data->{switches}{'job-uuid'}) if (not $anvil->data->{switches}{'job-uuid'})
{ {
$anvil->data->{switches}{'job-uuid'} = $anvil->Job->get_job_uuid({ program => $THIS_FILE }); $anvil->data->{switches}{'job-uuid'} = $anvil->Job->get_job_uuid({ program => $THIS_FILE });
$anvil->Log->variables({ source => $THIS_FILE, line => __LINE__, level => 2, list => { $anvil->Log->variables({ source => $THIS_FILE, line => __LINE__, level => $debug, list => {
"switches::job-uuid" => $anvil->data->{switches}{'job-uuid'} "switches::job-uuid" => $anvil->data->{switches}{'job-uuid'}
} }); } });
} }
@ -123,52 +143,40 @@ if ($anvil->data->{switches}{'job-uuid'})
$anvil->data->{switches}{'resize'} = $1; $anvil->data->{switches}{'resize'} = $1;
} }
if ($line =~ /stdout=(.*?)$/) if ($line =~ /request-host-uuid=(.*?)$/)
{ {
$anvil->data->{switches}{'stdout'} = $1; $anvil->data->{switches}{'request-host-uuid'} = $1;
} }
} }
} }
my $server_uuid = $anvil->data->{switches}{'server-uuid'}; my $server_uuid = $anvil->data->{switches}{'server-uuid'};
my $resize_args = $anvil->data->{switches}{'resize'}; my $resize_args = $anvil->data->{switches}{resize};
# Restrict output to stdout and keep the DB insert logic hidden for now. my $request_host_uuid = $anvil->data->{switches}{'request-host-uuid'};
my $is_stdout = 1; my $job_uuid = $anvil->data->{switches}{'job-uuid'};
my $job_uuid = $anvil->data->{switches}{'job-uuid'};
$anvil->Log->variables({ source => $THIS_FILE, line => __LINE__, level => $debug, list => {
$anvil->Log->variables({ source => $THIS_FILE, line => __LINE__, level => 2, list => { debug => $debug,
server_uuid => $server_uuid, job_uuid => $job_uuid,
resize_args => $resize_args, request_host_uuid => $request_host_uuid,
is_stdout => $is_stdout, resize_args => $resize_args,
job_uuid => $job_uuid server_uuid => $server_uuid,
} }); } });
if ($server_uuid) if ($server_uuid)
{ {
my $encoded_image; my $encoded_image = get_server_screenshot({ debug => $debug, server_uuid => $server_uuid, resize_args => $resize_args });
if ($is_stdout) if ($request_host_uuid)
{ {
$encoded_image = get_server_screenshot({ server_uuid => $server_uuid, resize_args => $resize_args });
if (defined $encoded_image) if (defined $encoded_image)
{ {
print($encoded_image); my $request_host_ip_address = get_remote_call_requirements({ debug => $debug, host_uuid => $request_host_uuid });
my $remote_shell_call = $anvil->data->{path}{exe}{echo}." '".$encoded_image."' >".$anvil->data->{path}{directories}{tmp}."/".$server_uuid."_screenshot";
$anvil->Job->update_progress({ progress => 100, message => "message_0264" }); $anvil->Log->variables({ source => $THIS_FILE, line => __LINE__, level => $debug, list => { request_host_ip_address => $request_host_ip_address } });
}
else
{
$anvil->Job->update_progress({ progress => 100, message => "message_0265" });
}
}
elsif (is_existing_server_screenshot_outdated({ server_uuid => $server_uuid }))
{
$encoded_image = get_server_screenshot({ server_uuid => $server_uuid, resize_args => $resize_args });
if (defined $encoded_image) remote_call({ debug => $debug, shell_call => $remote_shell_call, target => $request_host_ip_address });
{
insert_server_screenshot({ server_uuid => $server_uuid, encoded_image => $encoded_image });
$anvil->Job->update_progress({ progress => 100, message => "message_0264" }); $anvil->Job->update_progress({ progress => 100, message => "message_0264" });
} }
@ -179,7 +187,7 @@ if ($server_uuid)
} }
else else
{ {
$anvil->Job->update_progress({ progress => 100, message => "message_0266" }); print($encoded_image);
} }
} }
else else

Loading…
Cancel
Save