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.

189 lines
5.1 KiB

#!/usr/bin/perl
#
#
#
use strict;
use warnings;
use Anvil::Tools;
$| = 1;
my $THIS_FILE = ($0 =~ /^.*\/(.*)$/)[0];
my $running_directory = ($0 =~ /^(.*?)\/$THIS_FILE$/)[0];
if (($running_directory =~ /^\./) && ($ENV{PWD}))
{
$running_directory =~ s/^\./$ENV{PWD}/;
}
my $anvil = Anvil::Tools->new();
sub system_call
{
my $parameters = shift;
my $shell_call = $parameters->{shell_call};
my ($shell_output, $shell_return_code) = $anvil->System->call({ shell_call => $shell_call });
$anvil->Log->variables({ source => $THIS_FILE, line => __LINE__, level => 2, list => {
shell_call => $shell_call,
shell_output => $shell_output,
shell_return_code => $shell_return_code
} });
return ($shell_output, $shell_return_code);
}
sub is_existing_server_screenshot_outdated
{
my $parameters = shift;
my $server_uuid = $parameters->{server_uuid};
my ($encoded_image, $variable_uuid, $variable_mtime) = $anvil->Database->read_variable({ variable_name => "server_screenshot::".$server_uuid });
my $time_difference = time - $variable_mtime;
return $time_difference > 120 ? 1 : 0;
}
sub get_server_screenshot
{
my $parameters = shift;
my $server_uuid = $parameters->{server_uuid};
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};
if ($resize_args =~ /^\d+x\d+$/)
{
my ($resize_x, $resize_y) = split(/x/ , $resize_args);
$shell_call .= " | ".$anvil->data->{path}{exe}{pamscale}." -quiet -xyfit ".$resize_x." ".$resize_y;
}
$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 });
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({
state_name => "server_screenshot::".$server_uuid,
state_note => $encoded_image
});
}
$anvil->Get->switches;
$anvil->Database->connect;
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, secure => 0, key => "log_0132"});
if (not $anvil->data->{sys}{database}{connections})
{
# No databases, exit.
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, 'print' => 1, priority => "err", key => "error_0003"});
$anvil->nice_exit({exit_code => 1});
}
# Try to get a job UUID if not given.
if (not $anvil->data->{switches}{'job-uuid'})
{
$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'}
} });
}
# Handle this script as a job when job UUID is provided.
if ($anvil->data->{switches}{'job-uuid'})
{
$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_0263"
});
foreach my $line (split/\n/, $anvil->data->{jobs}{job_data})
{
if ($line =~ /server-uuid=(.*?)$/)
{
$anvil->data->{switches}{'server-uuid'} = $1;
}
if ($line =~ /resize=(.*?)$/)
{
$anvil->data->{switches}{'resize'} = $1;
}
if ($line =~ /stdout=(.*?)$/)
{
$anvil->data->{switches}{'stdout'} = $1;
}
}
}
my $server_uuid = $anvil->data->{switches}{'server-uuid'};
my $resize_args = $anvil->data->{switches}{'resize'};
# Restrict output to stdout and keep the DB insert logic hidden for now.
my $is_stdout = 1;
my $job_uuid = $anvil->data->{switches}{'job-uuid'};
$anvil->Log->variables({ source => $THIS_FILE, line => __LINE__, level => 2, list => {
server_uuid => $server_uuid,
resize_args => $resize_args,
is_stdout => $is_stdout,
job_uuid => $job_uuid
} });
if ($server_uuid)
{
my $encoded_image;
if ($is_stdout)
{
$encoded_image = get_server_screenshot({ server_uuid => $server_uuid, resize_args => $resize_args });
if (defined $encoded_image)
{
print($encoded_image);
$anvil->Job->update_progress({ progress => 100, message => "message_0264" });
}
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)
{
insert_server_screenshot({ server_uuid => $server_uuid, encoded_image => $encoded_image });
$anvil->Job->update_progress({ progress => 100, message => "message_0264" });
}
else
{
$anvil->Job->update_progress({ progress => 100, message => "message_0265" });
}
}
else
{
$anvil->Job->update_progress({ progress => 100, message => "message_0266" });
}
}
else
{
$anvil->Job->update_progress({ progress => 100, message => "message_0266" });
}