Merge branch 'main' into anvil-tools-dev

main
Digimer 2 years ago committed by GitHub
commit d59034a488
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      striker-ui-api/out/index.js
  2. 66
      striker-ui-api/src/lib/request_handlers/server/getServerDetail.ts
  3. 21
      tools/anvil-get-server-screenshot

File diff suppressed because one or more lines are too long

@ -8,7 +8,15 @@ import SERVER_PATHS from '../../consts/SERVER_PATHS';
import { dbQuery, getLocalHostUUID, job } from '../../accessModule';
import { sanitize } from '../../sanitize';
import { mkfifo, rm } from '../../shell';
import { mkfifo, rm, stderr, stdout } from '../../shell';
const rmfifo = (path: string) => {
try {
rm(path);
} catch (rmfifoError) {
stderr(`Failed to clean up named pipe; CAUSE: ${rmfifoError}`);
}
};
export const getServerDetail: RequestHandler = (request, response) => {
const { serverUUID } = request.params;
@ -17,7 +25,7 @@ export const getServerDetail: RequestHandler = (request, response) => {
const epoch = Date.now();
const isScreenshot = sanitize(ss, 'boolean');
console.log(
stdout(
`serverUUID=[${serverUUID}],epoch=[${epoch}],isScreenshot=[${isScreenshot}]`,
);
@ -27,7 +35,7 @@ export const getServerDetail: RequestHandler = (request, response) => {
`Server UUID must be a valid UUID; got [${serverUUID}]`,
);
} catch (assertError) {
console.log(
stderr(
`Failed to assert value when trying to get server detail; CAUSE: ${assertError}.`,
);
@ -42,14 +50,14 @@ export const getServerDetail: RequestHandler = (request, response) => {
try {
requestHostUUID = getLocalHostUUID();
} catch (subError) {
console.log(subError);
stderr(String(subError));
response.status(500).send();
return;
}
console.log(`requestHostUUID=[${requestHostUUID}]`);
stdout(`requestHostUUID=[${requestHostUUID}]`);
try {
[[serverHostUUID]] = dbQuery(`
@ -57,14 +65,14 @@ export const getServerDetail: RequestHandler = (request, response) => {
FROM servers
WHERE server_uuid = '${serverUUID}';`).stdout;
} catch (queryError) {
console.log(`Failed to get server host UUID; CAUSE: ${queryError}`);
stderr(`Failed to get server host UUID; CAUSE: ${queryError}`);
response.status(500).send();
return;
}
console.log(`serverHostUUID=[${serverHostUUID}]`);
stdout(`serverHostUUID=[${serverHostUUID}]`);
const imageFileName = `${serverUUID}_screenshot_${epoch}`;
const imageFilePath = path.join(SERVER_PATHS.tmp.self, imageFileName);
@ -79,40 +87,46 @@ export const getServerDetail: RequestHandler = (request, response) => {
let imageData = '';
namedPipeReadStream.once('close', () => {
console.log(`On close; removing named pipe at ${imageFilePath}.`);
try {
rm(imageFilePath);
} catch (cleanPipeError) {
console.log(
`Failed to clean up named pipe; CAUSE: ${cleanPipeError}`,
);
}
namedPipeReadStream.once('error', (readError) => {
stderr(`Failed to read from named pipe; CAUSE: ${readError}`);
});
namedPipeReadStream.once('end', () => {
namedPipeReadStream.once('close', () => {
stdout(`On close; removing named pipe at ${imageFilePath}.`);
response.status(200).send({ screenshot: imageData });
rmfifo(imageFilePath);
});
namedPipeReadStream.on('data', (data) => {
const imageChunk = data.toString().trim();
const chunkLogLength = 10;
const peekLength = 10;
console.log(
`${serverUUID} image chunk: ${imageChunk.substring(
stdout(
`${serverUUID} image chunk: ${
imageChunk.length > 0
? `${imageChunk.substring(
0,
chunkLogLength,
)}...${imageChunk.substring(imageChunk.length - chunkLogLength - 1)}`,
peekLength,
)}...${imageChunk.substring(
imageChunk.length - peekLength - 1,
)}`
: 'empty'
}`,
);
imageData += imageChunk;
});
} catch (prepPipeError) {
console.log(`Failed to prepare named pipe; CAUSE: ${prepPipeError}`);
stderr(
`Failed to prepare named pipe and/or receive image data; CAUSE: ${prepPipeError}`,
);
response.status(500).send();
rmfifo(imageFilePath);
return;
}
@ -136,9 +150,7 @@ out-file-id=${epoch}`,
job_host_uuid: serverHostUUID,
});
} catch (subError) {
console.log(
`Failed to queue fetch server screenshot job; CAUSE: ${subError}`,
);
stderr(`Failed to queue fetch server screenshot job; CAUSE: ${subError}`);
response.status(500).send();

@ -82,6 +82,14 @@ sub get_server_screenshot
my $resize_args = defined $parameters->{resize_args} ? $parameters->{resize_args} : '';
my $sh_domstate = $anvil->data->{path}{exe}{virsh}." domstate --domain ".$server_uuid;
my ($sh_domstate_output, $sh_domstate_return_code) = system_call({ debug => $debug, shell_call => $sh_domstate });
if ( ($sh_domstate_return_code != 0) || ($sh_domstate_output ne "running") )
{
return "";
}
my $shell_call = $anvil->data->{path}{exe}{virsh}." --quiet screenshot --domain ".$server_uuid." --file ".$anvil->data->{path}{devices}{stdout};
if ($resize_args =~ /^\d+x\d+$/)
@ -95,7 +103,7 @@ sub get_server_screenshot
my ($output, $return_code) = system_call({ debug => $debug, shell_call => $shell_call });
return $return_code == 0 ? $output : undef;
return $return_code == 0 ? $output : "";
}
$anvil->Get->switches;
@ -174,21 +182,23 @@ if ($server_uuid)
my $encoded_image = get_server_screenshot({ debug => $debug, server_uuid => $server_uuid, resize_args => $resize_args });
if ($request_host_uuid)
{
if (defined $encoded_image)
{
my $request_host_ip_address = get_remote_call_requirements({ debug => $debug, host_uuid => $request_host_uuid });
my $remote_shell_call = $anvil->data->{path}{exe}{echo}." '".$encoded_image."' >".$anvil->data->{path}{directories}{tmp}."/".$server_uuid."_screenshot_".$out_file_id;
my $out_file_path = $anvil->data->{path}{directories}{tmp}."/".$server_uuid."_screenshot_".$out_file_id;
my $remote_shell_call = $anvil->data->{path}{exe}{echo}." '".$encoded_image."' 1<>'".$out_file_path."' >'".$out_file_path."'";
$anvil->Log->variables({ source => $THIS_FILE, line => __LINE__, level => $debug, list => { request_host_ip_address => $request_host_ip_address } });
remote_call({ debug => $debug, shell_call => $remote_shell_call, target => $request_host_ip_address });
if (length($encoded_image))
{
$anvil->Job->update_progress({ progress => 100, message => "message_0264" });
}
else
{
$anvil->Job->update_progress({ progress => 100, message => "message_0265" });
$anvil->nice_exit({ exit_code => 1 });
}
}
else
@ -199,4 +209,7 @@ if ($server_uuid)
else
{
$anvil->Job->update_progress({ progress => 100, message => "message_0266" });
$anvil->nice_exit({ exit_code => 1 });
}
$anvil->nice_exit({ exit_code => 0 });

Loading…
Cancel
Save