feat(cgi-bin): add endpoint for fetching server screenshot

main
Tsu-ba-me 3 years ago
parent 1014299d38
commit d195a53ba2
  1. 1
      Anvil/Tools.pm
  2. 124
      cgi-bin/get_server_screenshot
  3. 2
      share/words.xml

@ -1106,6 +1106,7 @@ sub _set_paths
'anvil-delete-server' => "/usr/sbin/anvil-delete-server", 'anvil-delete-server' => "/usr/sbin/anvil-delete-server",
'anvil-download-file' => "/usr/sbin/anvil-download-file", 'anvil-download-file' => "/usr/sbin/anvil-download-file",
'anvil-file-details' => "/usr/sbin/anvil-file-details", 'anvil-file-details' => "/usr/sbin/anvil-file-details",
'anvil-get-server-screenshot' => "/usr/sbin/anvil-get-server-screenshot",
'anvil-join-anvil' => "/usr/sbin/anvil-join-anvil", 'anvil-join-anvil' => "/usr/sbin/anvil-join-anvil",
'anvil-maintenance-mode' => "/usr/sbin/anvil-maintenance-mode", 'anvil-maintenance-mode' => "/usr/sbin/anvil-maintenance-mode",
'anvil-manage-firewall' => "/usr/sbin/anvil-manage-firewall", 'anvil-manage-firewall' => "/usr/sbin/anvil-manage-firewall",

@ -0,0 +1,124 @@
#!/usr/bin/perl
#
# Gets a server VM's screenshot and convert it to a Base64 string.
#
use strict;
use warnings;
use Anvil::Tools;
use JSON;
$| = 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 is_job_incomplete
{
my $parameters = shift;
my $job_uuid = $parameters->{job_uuid};
my $query = "
SELECT
job_progress
FROM
public.jobs
WHERE
job_uuid = ".$anvil->Database->quote($job_uuid)."
;";
my $job_progress = $anvil->Database->query({ query => $query, source => $THIS_FILE, line => __LINE__ })->[0]->[0];
return $job_progress == 100 ? 0 : 1;
}
sub get_server_host_uuid
{
my $parameters = shift;
my $server_uuid = $parameters->{server_uuid};
my $query = "
SELECT
server_host_uuid
FROM
public.servers
WHERE
server_uuid = ".$anvil->Database->quote($server_uuid)."
;";
return $anvil->Database->query({ query => $query, source => $THIS_FILE, line => __LINE__ })->[0]->[0];
}
sub get_screenshot
{
my $parameters = shift;
my $server_uuid = $parameters->{server_uuid};
my $server_host_uuid = $parameters->{server_host_uuid};
my ($job_uuid) = $anvil->Database->insert_or_update_jobs({
job_command => $anvil->data->{path}{exe}{'anvil-get-server-screenshot'},
job_data => "server-uuid=".$server_uuid,
job_host_uuid => $server_host_uuid,
job_description => "job_0357",
job_name => "cgi-bin::get_server_screenshot::".$server_uuid,
job_progress => 0,
job_title => "job_0356"
});
# Wait until the job is complete before continuing.
while(is_job_incomplete({ job_uuid => $job_uuid }))
{
sleep(2);
}
my ($encoded_image) = $anvil->Database->read_variable({ variable_name => "server_screenshot::".$server_uuid });
return $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});
}
my $cookie_problem = $anvil->Account->read_cookies();
# Don't do anything data-related if the user is not logged in.
if ($cookie_problem)
{
$anvil->Log->entry({ source => $THIS_FILE, line => __LINE__, level => 0, 'print' => 1, priority => "err", key => "error_0307" });
$anvil->nice_exit({ exit_code => 1 });
}
# Read in any CGI variables, if needed.
$anvil->Get->cgi();
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 $response_body = {};
if ($server_uuid)
{
my $encoded_image = get_screenshot({ server_uuid => $server_uuid, server_host_uuid => get_server_host_uuid({ server_uuid => $server_uuid }) });
if (defined $encoded_image)
{
$response_body->{image} = $encoded_image;
}
}
print JSON->new->utf8->encode($response_body)."\n";

@ -1148,6 +1148,8 @@ It should be provisioned in the next minute or two.</key>
<key name="job_0353">* Please enter the name of the server you want to manage</key> <key name="job_0353">* Please enter the name of the server you want to manage</key>
<key name="job_0354">-=] Servers available to manage on the Anvil! [#!variable!anvil_name!#] [=-</key> <key name="job_0354">-=] Servers available to manage on the Anvil! [#!variable!anvil_name!#] [=-</key>
<key name="job_0355">-=] Managing the server: [#!variable!server_name!#] on the Anvil!: [#!variable!anvil_name!#]</key> <key name="job_0355">-=] Managing the server: [#!variable!server_name!#] on the Anvil!: [#!variable!anvil_name!#]</key>
<key name="job_0356">Get Server VM Screenshot</key>
<key name="job_0357">Fetch a screenshot of the specified server VM and represent it as a Base64 string.</key>
<!-- Log entries --> <!-- Log entries -->
<key name="log_0001">Starting: [#!variable!program!#].</key> <key name="log_0001">Starting: [#!variable!program!#].</key>

Loading…
Cancel
Save