fix(tools): add option to resize server screenshot

main
Tsu-ba-me 3 years ago
parent 7467036054
commit 65613f501b
  1. 10
      cgi-bin/get_server_screenshot
  2. 28
      tools/anvil-get-server-screenshot

@ -60,10 +60,11 @@ sub get_screenshot
my $parameters = shift; my $parameters = shift;
my $server_uuid = $parameters->{server_uuid}; my $server_uuid = $parameters->{server_uuid};
my $server_host_uuid = $parameters->{server_host_uuid}; my $server_host_uuid = $parameters->{server_host_uuid};
my $resize_args = defined $parameters->{resize_args} ? $parameters->{resize_args} : "512x512";
my ($job_uuid) = $anvil->Database->insert_or_update_jobs({ my ($job_uuid) = $anvil->Database->insert_or_update_jobs({
job_command => $anvil->data->{path}{exe}{'anvil-get-server-screenshot'}, job_command => $anvil->data->{path}{exe}{'anvil-get-server-screenshot'},
job_data => "server-uuid=".$server_uuid, job_data => "server-uuid=".$server_uuid."\nresize=".$resize_args,
job_host_uuid => $server_host_uuid, job_host_uuid => $server_host_uuid,
job_description => "job_0357", job_description => "job_0357",
job_name => "cgi-bin::get_server_screenshot::".$server_uuid, job_name => "cgi-bin::get_server_screenshot::".$server_uuid,
@ -108,12 +109,17 @@ $anvil->Get->cgi();
print $anvil->Template->get({ file => "shared.html", name => "json_headers", show_name => 0 })."\n"; print $anvil->Template->get({ file => "shared.html", name => "json_headers", show_name => 0 })."\n";
my $server_uuid = defined $anvil->data->{cgi}{server_uuid}{value} ? $anvil->data->{cgi}{server_uuid}{value} : $anvil->data->{switches}{'server-uuid'}; my $server_uuid = defined $anvil->data->{cgi}{server_uuid}{value} ? $anvil->data->{cgi}{server_uuid}{value} : $anvil->data->{switches}{'server-uuid'};
my $resize_args = defined $anvil->data->{cgi}{resize}{value} ? $anvil->data->{cgi}{resize}{value} : $anvil->data->{switches}{'resize'};
my $response_body = {}; my $response_body = {};
if ($server_uuid) if ($server_uuid)
{ {
my $encoded_image = get_screenshot({ server_uuid => $server_uuid, server_host_uuid => get_server_host_uuid({ server_uuid => $server_uuid }) }); my $encoded_image = get_screenshot({
server_uuid => $server_uuid,
server_host_uuid => get_server_host_uuid({ server_uuid => $server_uuid }),
resize_args => $resize_args
});
if (defined $encoded_image) if (defined $encoded_image)
{ {

@ -49,8 +49,17 @@ sub get_server_screenshot
{ {
my $parameters = shift; my $parameters = shift;
my $server_uuid = $parameters->{server_uuid}; my $server_uuid = $parameters->{server_uuid};
my $resize_args = $parameters->{resize_args};
my $shell_call = "virsh screenshot --domain ".$server_uuid." --file /dev/stdout | sed 's/Screenshot.*//'";
if (defined $resize_args)
{
$shell_call .= " | convert - -resize ".$resize_args." png:-";
}
$shell_call .= " | base64 --wrap 0";
my $shell_call = "virsh screenshot --domain ".$server_uuid." --file /dev/stdout | sed 's/Screenshot.*//' | base64 --wrap 0";
my ($shell_output, $shell_return_code) = system_call({ shell_call => $shell_call }); my ($shell_output, $shell_return_code) = system_call({ shell_call => $shell_call });
return $shell_return_code == 0 ? $shell_output : undef; return $shell_return_code == 0 ? $shell_output : undef;
@ -105,27 +114,28 @@ if ($anvil->data->{switches}{'job-uuid'})
if ($line =~ /server-uuid=(.*?)$/) if ($line =~ /server-uuid=(.*?)$/)
{ {
$anvil->data->{switches}{'server-uuid'} = $1; $anvil->data->{switches}{'server-uuid'} = $1;
$anvil->Log->variables({ source => $THIS_FILE, line => __LINE__, level => 2, list => { }
'switches::server-uuid' => $anvil->data->{switches}{'server-uuid'}
} }); if ($line =~ /resize=(.*?)$/)
{
$anvil->data->{switches}{'resize'} = $1;
} }
if ($line =~ /stdout=(.*?)$/) if ($line =~ /stdout=(.*?)$/)
{ {
$anvil->data->{switches}{'stdout'} = $1; $anvil->data->{switches}{'stdout'} = $1;
$anvil->Log->variables({ source => $THIS_FILE, line => __LINE__, level => 2, list => {
'switches::stdout' => $anvil->data->{switches}{'stdout'}
} });
} }
} }
} }
my $server_uuid = $anvil->data->{switches}{'server-uuid'}; my $server_uuid = $anvil->data->{switches}{'server-uuid'};
my $resize_args = $anvil->data->{switches}{'resize'};
my $is_stdout = $anvil->data->{switches}{'stdout'}; my $is_stdout = $anvil->data->{switches}{'stdout'};
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 => 2, list => { $anvil->Log->variables({ source => $THIS_FILE, line => __LINE__, level => 2, list => {
server_uuid => $server_uuid, server_uuid => $server_uuid,
resize_args => $resize_args,
is_stdout => $is_stdout, is_stdout => $is_stdout,
job_uuid => $job_uuid job_uuid => $job_uuid
} }); } });
@ -136,7 +146,7 @@ if ($server_uuid)
if ($is_stdout) if ($is_stdout)
{ {
$encoded_image = get_server_screenshot({ server_uuid => $server_uuid }); $encoded_image = get_server_screenshot({ server_uuid => $server_uuid, resize_args => $resize_args });
if (defined $encoded_image) if (defined $encoded_image)
{ {
@ -151,7 +161,7 @@ if ($server_uuid)
} }
elsif (is_existing_server_screenshot_outdated({ server_uuid => $server_uuid })) elsif (is_existing_server_screenshot_outdated({ server_uuid => $server_uuid }))
{ {
$encoded_image = get_server_screenshot({ server_uuid => $server_uuid }); $encoded_image = get_server_screenshot({ server_uuid => $server_uuid, resize_args => $resize_args });
if (defined $encoded_image) if (defined $encoded_image)
{ {

Loading…
Cancel
Save