#!/usr/bin/perl # # Manages VNC ports for server VMs that have VNC enabled. # use strict; use warnings; use Anvil::Tools; use Data::Dumper; 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 get_vnc_pipe_info { my $parameters = shift; my $server_uuid = $parameters->{server_uuid}; my $host_uuid = $parameters->{host_uuid}; my $vnc_pipe_info = { protocol => "ws" }; my $query = " SELECT ssh_tunnel_forward_port FROM public.vnc_pipes WHERE server_uuid = ".$anvil->Database->quote($server_uuid)." AND ssh_tunnel_host_uuid = ".$anvil->Database->quote($host_uuid)." ;"; my $forward_port = $anvil->Database->query({ query => $query, source => $THIS_FILE, line => __LINE__ })->[0]->[0]; $vnc_pipe_info->{forward_port} = $forward_port; return $vnc_pipe_info; } 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; } $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 }); } # Read in any CGI variables, if needed. $anvil->Get->cgi(); $anvil->Database->get_hosts(); $anvil->Database->get_anvils(); print $anvil->Template->get({ file => "shared.html", name => "json_headers", show_name => 0 })."\n"; my $response_body = {}; my $request_body; if (defined $anvil->data->{cgi}{PUTDATA}{value}) { my $is_decode_json_success = eval { $request_body = decode_json($anvil->data->{cgi}{PUTDATA}{value}); }; if (not $is_decode_json_success) { $anvil->Log->entry({ source => $THIS_FILE, line => __LINE__, level => 0, 'print' => 1, priority => "err", key => "error_0304", variables => { request_body_string => $anvil->data->{cgi}{PUTDATA}{value}, json_decode_error => $@ } }); } } my $server_uuid = exists $request_body->{server_uuid} ? $request_body->{server_uuid} : $anvil->data->{switches}{'server-uuid'}; my $is_open = exists $request_body->{is_open} ? $request_body->{is_open} : $anvil->data->{switches}{'is-open'}; $anvil->Log->variables({ source => $THIS_FILE, line => __LINE__, level => 2, list => { server_uuid => $server_uuid, is_open => $is_open } }); if ($server_uuid) { my $host_uuid = $anvil->Get->host_uuid(); my $operation_string = defined $is_open ? "open" : "close"; my $open_string = defined $is_open ? $is_open : ""; my $job_uuid = $anvil->Database->insert_or_update_jobs({ job_command => $anvil->data->{path}{exe}{'striker-manage-vnc-pipes'}, job_data => "server-uuid=".$server_uuid."\nopen=".$open_string, job_host_uuid => $host_uuid, job_description => "job_0351,!!operation!".$operation_string."!!,!!server_uuid!".$server_uuid."!!,!!host_uuid!".$host_uuid."!!", job_name => "cgi-bin::manage_vnc_pipes::".$server_uuid."::".$operation_string, job_progress => 0, job_title => "job_0350" }); # Wait until the job is complete before continuing. while(is_job_incomplete({ job_uuid => $job_uuid })) { sleep(2); } if ($is_open) { $response_body = get_vnc_pipe_info({ server_uuid => $server_uuid, host_uuid => $host_uuid }); } } print JSON->new->utf8->encode($response_body)."\n";