#!/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; my $debug = $anvil->data->{switches}{debug}; $anvil->Database->connect; $anvil->Log->entry({ source => $THIS_FILE, line => __LINE__, level => $debug, 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 => $debug, 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 =~ /request-host-uuid=(.*?)$/) { $anvil->data->{switches}{'request-host-uuid'} = $1; } if ($line =~ /out-file-id=(.*?)$/) { $anvil->data->{switches}{'out-file-id'} = $1; } } } my $is_convert = $anvil->data->{switches}{'convert'}; my $job_uuid = $anvil->data->{switches}{'job-uuid'}; my $out_file_id = $anvil->data->{switches}{'out-file-id'}; my $resize_args = $anvil->data->{switches}{'resize'}; my $request_host_name = $anvil->data->{switches}{'request-host-name'}; my $server_uuid = $anvil->data->{switches}{'server-uuid'}; $out_file_id = ( (defined $out_file_id) && ($out_file_id ne "#!SET!#") ) ? "_${out_file_id}" : ""; $anvil->Log->variables({ source => $THIS_FILE, line => __LINE__, level => $debug, list => { debug => $debug, job_uuid => $job_uuid, request_host_name => $request_host_name, resize_args => $resize_args, server_uuid => $server_uuid, } }); if ($server_uuid) { my $out_file_path = $anvil->data->{path}{directories}{tmp}."/${server_uuid}_screenshot${out_file_id}"; if ($is_convert) { my ($rcode, $encoded_image) = convert_server_screenshot({ debug => $debug, resize_args => $resize_args, source_file => $out_file_path, }); print($encoded_image) if ($rcode == 0); $anvil->nice_exit({ exit_code => $rcode }); } my ($rcode) = get_server_screenshot({ debug => $debug, output_file => $out_file_path, server_uuid => $server_uuid, }); if ($rcode > 0) { $anvil->Job->update_progress({ progress => 100, message => "message_0265" }); $anvil->nice_exit({ exit_code => 1 }); } if ($request_host_name) { chomp $request_host_name; my $rsync = $anvil->data->{path}{exe}{rsync}; foreach my $host_name ( split(/,/, $request_host_name) ) { my $shell_call = "$rsync -e \"ssh -o BatchMode=yes\" -ac '$out_file_path' '$host_name':'$out_file_path'"; $anvil->Log->variables({ source => $THIS_FILE, line => __LINE__, level => $debug, list => { host_name => $host_name, out_file_path => $out_file_path, shell_call => $shell_call, } }); system_call({ debug => $debug, shell_call => $shell_call }); } } $anvil->Job->update_progress({ progress => 100, message => "message_0264" }); } else { $anvil->Job->update_progress({ progress => 100, message => "message_0266" }); $anvil->nice_exit({ exit_code => 1 }); } $anvil->nice_exit({ exit_code => 0 }); # # Functions # sub convert_server_screenshot { my $parameters = shift; my $resize_args = $parameters->{resize_args}; my $source_file = $parameters->{source_file}; my $debug = $parameters->{debug}; my $host_type = $anvil->Get->host_type(); return (1) if ( ($host_type ne "striker") || (not -e $source_file) ); my $base64 = $anvil->data->{path}{exe}{base64}; my $pamscale = $anvil->data->{path}{exe}{pamscale}; my $pamtopng = $anvil->data->{path}{exe}{pamtopng}; my $shell_call = "cat $source_file"; if ( (defined $resize_args) && ($resize_args =~ /^\d+x\d+$/) ) { my ($resize_x, $resize_y) = split(/x/ , $resize_args); $shell_call .= " | $pamscale -quiet -xyfit $resize_x $resize_y"; } $shell_call .= " | $pamtopng -quiet | $base64 --wrap 0"; my ($output, $return_code) = system_call({ debug => $debug, shell_call => $shell_call }); return ($return_code, $output); } sub get_server_screenshot { my $parameters = shift; my $output_file = $parameters->{output_file}; my $server_uuid = $parameters->{server_uuid}; my $debug = $parameters->{debug}; return (1) if ( (not $server_uuid) || (not $output_file) ); my $setsid = $anvil->data->{path}{exe}{setsid}; my $virsh = $anvil->data->{path}{exe}{virsh}; my $shell_call = "$setsid --wait $virsh --quiet screenshot --domain $server_uuid --file $output_file"; my ($output, $return_code) = system_call({ debug => $debug, shell_call => $shell_call }); return ($return_code, $output); } sub system_call { my $parameters = shift; 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, } }); return @call_result; }