diff --git a/striker-ui-api/src/lib/accessModule.ts b/striker-ui-api/src/lib/accessModule.ts index a0c5de24..7e0fa0be 100644 --- a/striker-ui-api/src/lib/accessModule.ts +++ b/striker-ui-api/src/lib/accessModule.ts @@ -384,41 +384,22 @@ const getUpsSpec = async () => { return getData('ups_data'); }; -const vncpipe = async (serverUuid: string, open?: boolean) => { - const [output, rReturnCode]: [string, string] = await subroutine('call', { - params: [ - { - shell_call: `${ - SERVER_PATHS.usr.sbin['anvil-manage-vnc-pipe'].self - } --server-uuid ${serverUuid} --component st${open ? ' --open' : ''}`, - }, - ], - pre: ['System'], - root: true, - }); - - const rcode = Number.parseInt(rReturnCode); +const getVncinfo = async (serverUuid: string): Promise => { + const rows: [[string]] = await query( + `SELECT variable_value FROM variables WHERE variable_name = 'server::${serverUuid}::vncinfo';`, + ); - if (rcode !== 0) { - throw new Error(`VNC pipe call failed with code ${rcode}`); + if (rows.length) { + throw new Error('No record found'); } - const lines = output.split('\n'); - const lastLine = lines[lines.length - 1]; - const rVncPipeProps = lastLine - .split(',') - .reduce>((previous, pair) => { - const [key, value] = pair.trim().split(/\s*:\s*/, 2); - - previous[key] = value; - - return previous; - }, {}); + const [[vncinfo]] = rows; + const [domain, port] = vncinfo.split(':'); - const forwardPort = Number.parseInt(rVncPipeProps.forward_port); - const protocol = rVncPipeProps.protocol; + const forwardPort = Number.parseInt(port); + const protocol = 'ws'; - return { forwardPort, protocol }; + return { domain, forwardPort, protocol }; }; export { @@ -438,8 +419,8 @@ export { getNetworkData, getPeerData, getUpsSpec, + getVncinfo, query, subroutine as sub, - vncpipe, write, }; diff --git a/striker-ui-api/src/lib/consts/SERVER_PATHS.ts b/striker-ui-api/src/lib/consts/SERVER_PATHS.ts index 91576d46..209ce32d 100644 --- a/striker-ui-api/src/lib/consts/SERVER_PATHS.ts +++ b/striker-ui-api/src/lib/consts/SERVER_PATHS.ts @@ -31,7 +31,6 @@ const EMPTY_SERVER_PATHS: ServerPath = { 'anvil-join-anvil': {}, 'anvil-manage-keys': {}, 'anvil-manage-power': {}, - 'anvil-manage-vnc-pipe': {}, 'anvil-provision-server': {}, 'anvil-safe-start': {}, 'anvil-safe-stop': {}, diff --git a/striker-ui-api/src/lib/request_handlers/command/index.ts b/striker-ui-api/src/lib/request_handlers/command/index.ts index 45c134c0..208c8982 100644 --- a/striker-ui-api/src/lib/request_handlers/command/index.ts +++ b/striker-ui-api/src/lib/request_handlers/command/index.ts @@ -1,7 +1,6 @@ export * from './getHostSSH'; export * from './joinAn'; export * from './leaveAn'; -export * from './manageVncSshTunnel'; export * from './poweroffStriker'; export * from './rebootStriker'; export * from './runManifest'; diff --git a/striker-ui-api/src/lib/request_handlers/command/manageVncSshTunnel.ts b/striker-ui-api/src/lib/request_handlers/command/manageVncSshTunnel.ts deleted file mode 100644 index 5f30705c..00000000 --- a/striker-ui-api/src/lib/request_handlers/command/manageVncSshTunnel.ts +++ /dev/null @@ -1,52 +0,0 @@ -import assert from 'assert'; -import { RequestHandler } from 'express'; - -import { REP_UUID } from '../../consts'; - -import { vncpipe } from '../../accessModule'; -import { sanitize } from '../../sanitize'; -import { stderr, stdoutVar } from '../../shell'; - -export const manageVncSshTunnel: RequestHandler< - unknown, - { forwardPort: number; protocol: string }, - { open: boolean; serverUuid: string } -> = async (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(); - } - - stdoutVar({ isOpen, serverUuid }, 'Manage VNC SSH tunnel params: '); - - let operation = 'close'; - - if (isOpen) { - operation = 'open'; - } - - let rsbody: { forwardPort: number; protocol: string }; - - try { - rsbody = await vncpipe(serverUuid, isOpen); - } catch (error) { - stderr( - `Failed to ${operation} VNC SSH tunnel to server ${serverUuid}; CAUSE: ${error}`, - ); - - return response.status(500).send(); - } - - return response.json(rsbody); -}; diff --git a/striker-ui-api/src/lib/request_handlers/server/getServerDetail.ts b/striker-ui-api/src/lib/request_handlers/server/getServerDetail.ts index af938029..662b0e8b 100644 --- a/striker-ui-api/src/lib/request_handlers/server/getServerDetail.ts +++ b/striker-ui-api/src/lib/request_handlers/server/getServerDetail.ts @@ -1,11 +1,12 @@ import assert from 'assert'; +import { execSync } from 'child_process'; import { RequestHandler } from 'express'; import { REP_UUID, SERVER_PATHS } from '../../consts'; +import { getVncinfo } from '../../accessModule'; import { sanitize } from '../../sanitize'; import { stderr, stdout } from '../../shell'; -import { execSync } from 'child_process'; export const getServerDetail: RequestHandler< ServerDetailParamsDictionary, @@ -15,12 +16,13 @@ export const getServerDetail: RequestHandler< > = async (request, response) => { const { params: { serverUUID: serverUuid }, - query: { ss }, + query: { ss: rSs, vnc: rVnc }, } = request; - const isScreenshot = sanitize(ss, 'boolean'); + const ss = sanitize(rSs, 'boolean'); + const vnc = sanitize(rVnc, 'boolean'); - stdout(`serverUUID=[${serverUuid}],isScreenshot=[${isScreenshot}]`); + stdout(`serverUUID=[${serverUuid}],isScreenshot=[${ss}]`); try { assert( @@ -35,8 +37,8 @@ export const getServerDetail: RequestHandler< return response.status(500).send(); } - if (isScreenshot) { - const rsbody = { screenshot: '' }; + if (ss) { + const rsbody: ServerDetailScreenshot = { screenshot: '' }; try { rsbody.screenshot = execSync( @@ -49,6 +51,18 @@ export const getServerDetail: RequestHandler< return response.status(500).send(); } + return response.send(rsbody); + } else if (vnc) { + let rsbody: ServerDetailVncInfo; + + try { + rsbody = await getVncinfo(serverUuid); + } catch (error) { + stderr(`Failed to get server ${serverUuid} VNC info; CAUSE: ${error}`); + + return response.status(500).send(); + } + return response.send(rsbody); } else { // For getting sever detail data. diff --git a/striker-ui-api/src/routes/command.ts b/striker-ui-api/src/routes/command.ts index e451a73b..a6f211e1 100644 --- a/striker-ui-api/src/routes/command.ts +++ b/striker-ui-api/src/routes/command.ts @@ -4,7 +4,6 @@ import { getHostSSH, joinAn, leaveAn, - manageVncSshTunnel, poweroffStriker, rebootStriker, runManifest, @@ -24,7 +23,6 @@ router .put('/inquire-host', getHostSSH) .put('/join-an/:uuid', joinAn) .put('/leave-an/:uuid', leaveAn) - .put('/vnc-pipe', manageVncSshTunnel) .put('/poweroff-host', poweroffStriker) .put('/reboot-host', rebootStriker) .put('/run-manifest/:manifestUuid', runManifest) diff --git a/striker-ui-api/src/types/ApiServer.d.ts b/striker-ui-api/src/types/ApiServer.d.ts index 49f4c4e7..7b0c789a 100644 --- a/striker-ui-api/src/types/ApiServer.d.ts +++ b/striker-ui-api/src/types/ApiServer.d.ts @@ -12,6 +12,17 @@ type ServerDetailParamsDictionary = { }; type ServerDetailParsedQs = { - ss: boolean | number | string; resize: string; + ss: boolean | number | string; + vnc: boolean | number | string; +}; + +type ServerDetailScreenshot = { + screenshot: string; +}; + +type ServerDetailVncInfo = { + domain: string; + forwardPort: number; + protocol: string; };