diff --git a/tools/anvil-get-server-screenshot b/tools/anvil-get-server-screenshot new file mode 100755 index 00000000..d7f5e1e9 --- /dev/null +++ b/tools/anvil-get-server-screenshot @@ -0,0 +1,88 @@ +#!/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(); + +$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}); +} + +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 get_server_screenshot +{ + my $parameters = shift; + my $server_uuid = $parameters->{server_uuid}; + + my $shell_call = "virsh screenshot --domain ".$server_uuid." --file /dev/stdout | sed 's/Screenshot.*//' | base64"; + 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_variables({ + variable_name => "server_screenshot::".$server_uuid, + variable_value => $encoded_image + }); +} + +my $server_uuid = $anvil->data->{switches}{'server-uuid'}; +my $is_stdout = $anvil->data->{switches}{'stdout'}; + +if ($server_uuid) +{ + my $encoded_image = get_server_screenshot({ server_uuid => $server_uuid }); + + if ($is_stdout) + { + print($encoded_image); + } + else + { + insert_server_screenshot({ + server_uuid => $server_uuid, + encoded_image => $encoded_image + }); + } +}