fix(striker-ui-api): add endpoint to manage VNC SSH tunnel

main
Tsu-ba-me 2 years ago
parent a48c6997fe
commit aa1ea8d796
  1. 1
      striker-ui-api/src/lib/consts/SERVER_PATHS.ts
  2. 1
      striker-ui-api/src/lib/request_handlers/command/index.ts
  3. 79
      striker-ui-api/src/lib/request_handlers/command/manageVncSshTunnel.ts
  4. 3
      striker-ui-api/src/lib/shell.ts
  5. 2
      striker-ui-api/src/routes/command.ts

@ -38,6 +38,7 @@ const EMPTY_SERVER_PATHS: ServerPath = {
'striker-initialize-host': {}, 'striker-initialize-host': {},
'striker-manage-install-target': {}, 'striker-manage-install-target': {},
'striker-manage-peers': {}, 'striker-manage-peers': {},
'striker-manage-vnc-pipes': {},
'striker-parse-os-list': {}, 'striker-parse-os-list': {},
}, },
}, },

@ -1,6 +1,7 @@
export * from './getHostSSH'; export * from './getHostSSH';
export * from './joinAn'; export * from './joinAn';
export * from './leaveAn'; export * from './leaveAn';
export * from './manageVncSshTunnel';
export * from './poweroffStriker'; export * from './poweroffStriker';
export * from './rebootStriker'; export * from './rebootStriker';
export * from './runManifest'; export * from './runManifest';

@ -0,0 +1,79 @@
import assert from 'assert';
import { RequestHandler } from 'express';
import { REP_UUID } from '../../consts';
import { sanitize } from '../../sanitize';
import { stderr, vncpipe } from '../../shell';
export const manageVncSshTunnel: RequestHandler<
unknown,
{ forwardPort: number; protocol: string },
{ open: boolean; serverUuid: string }
> = (request, response) => {
const { body: { open: rOpen, serverUuid: rServerUuid } = {} } = request;
const isOpen = sanitize(rOpen, 'boolean');
const serverUuid = sanitize(rServerUuid, 'string');
try {
assert(
REP_UUID.test(serverUuid),
`Server UUID must be a valid UUIDv4; got: [${serverUuid}]`,
);
} catch (error) {
stderr(`Assert input failed when manage VNC SSH tunnel; CAUSE: ${error}`);
return response.status(400).send();
}
let cstdout = '';
try {
cstdout = vncpipe(
'--server-uuid',
serverUuid,
'--component',
'st',
isOpen ? '--open' : '',
);
} catch (error) {
stderr(
`Failed to ${
isOpen ? 'open' : 'close'
} VNC SSH tunnel to server ${serverUuid}; CAUSE: ${error}`,
);
return response.status(500).send();
}
const coutput = cstdout
.split(/\s*,\s*/)
.reduce<Record<string, string>>((previous, pair: string) => {
const [key, value] = pair.split(/\s*:\s*/, 2);
previous[key] = value;
return previous;
}, {});
let forwardPort: number;
let protocol: string;
try {
assert('forwardPort' in coutput, 'Missing forward port in command output');
assert('protocol' in coutput, 'Missing protocol in command output');
forwardPort = Number.parseInt(coutput.forwardPort);
protocol = coutput.protocol;
} catch (error) {
stderr(`Failed to get VNC SSH tunnel connect info; CAUSE: ${error}`);
return response.status(500).send();
}
return response.json({
forwardPort,
protocol,
});
};

@ -47,6 +47,9 @@ export const rm = (...args: string[]) =>
export const uuidgen = (...args: string[]) => export const uuidgen = (...args: string[]) =>
systemCall(SERVER_PATHS.usr.bin.uuidgen.self, args); systemCall(SERVER_PATHS.usr.bin.uuidgen.self, args);
export const vncpipe = (...args: string[]) =>
systemCall(SERVER_PATHS.usr.sbin['striker-manage-vnc-pipes'].self, args);
export const resolveId = (id: number | string, database: string) => export const resolveId = (id: number | string, database: string) =>
Number.parseInt(getent(database, String(id)).split(':', 3)[2]); Number.parseInt(getent(database, String(id)).split(':', 3)[2]);

@ -4,6 +4,7 @@ import {
getHostSSH, getHostSSH,
joinAn, joinAn,
leaveAn, leaveAn,
manageVncSshTunnel,
poweroffStriker, poweroffStriker,
rebootStriker, rebootStriker,
runManifest, runManifest,
@ -21,6 +22,7 @@ router
.put('/inquire-host', getHostSSH) .put('/inquire-host', getHostSSH)
.put('/join-an/:uuid', joinAn) .put('/join-an/:uuid', joinAn)
.put('/leave-an/:uuid', leaveAn) .put('/leave-an/:uuid', leaveAn)
.put('/vnc-pipe', manageVncSshTunnel)
.put('/poweroff-host', poweroffStriker) .put('/poweroff-host', poweroffStriker)
.put('/reboot-host', rebootStriker) .put('/reboot-host', rebootStriker)
.put('/run-manifest/:manifestUuid', runManifest) .put('/run-manifest/:manifestUuid', runManifest)

Loading…
Cancel
Save