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

@ -82,6 +82,14 @@ sub get_server_screenshot
my $resize_args = defined $parameters->{resize_args} ? $parameters->{resize_args} : ''; 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}; 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+$/) 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 }); 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; $anvil->Get->switches;
@ -175,20 +183,22 @@ if ($server_uuid)
if ($request_host_uuid) 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 $out_file_path = $anvil->data->{path}{directories}{tmp}."/".$server_uuid."_screenshot_".$out_file_id;
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."' 1<>'".$out_file_path."' >'".$out_file_path."'";
my $remote_shell_call = $anvil->data->{path}{exe}{echo}." '".$encoded_image."' >".$anvil->data->{path}{directories}{tmp}."/".$server_uuid."_screenshot_".$out_file_id;
$anvil->Log->variables({ source => $THIS_FILE, line => __LINE__, level => $debug, list => { request_host_ip_address => $request_host_ip_address } }); $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 }); 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" }); $anvil->Job->update_progress({ progress => 100, message => "message_0264" });
} }
else else
{ {
$anvil->Job->update_progress({ progress => 100, message => "message_0265" }); $anvil->Job->update_progress({ progress => 100, message => "message_0265" });
$anvil->nice_exit({ exit_code => 1 });
} }
} }
else else
@ -199,4 +209,7 @@ if ($server_uuid)
else else
{ {
$anvil->Job->update_progress({ progress => 100, message => "message_0266" }); $anvil->Job->update_progress({ progress => 100, message => "message_0266" });
$anvil->nice_exit({ exit_code => 1 });
} }
$anvil->nice_exit({ exit_code => 0 });

Loading…
Cancel
Save