From f9d7449b7def33bece91be3d214def8911d6555b Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Thu, 27 Apr 2023 00:57:58 -0400 Subject: [PATCH] fix(striker-ui-api): migrate get anvil data to access interact --- striker-ui-api/src/lib/accessModule.ts | 67 +++++++++++++------ .../request_handlers/command/runManifest.ts | 36 +++------- .../fence/getFenceTemplate.ts | 10 ++- .../host/createHostConnection.ts | 8 +-- .../host/getHostConnection.ts | 8 +-- .../manifest/getManifestDetail.ts | 19 ++---- .../request_handlers/ups/getUPSTemplate.ts | 9 +-- .../src/types/GetAnvilDataFunction.d.ts | 2 +- 8 files changed, 77 insertions(+), 82 deletions(-) diff --git a/striker-ui-api/src/lib/accessModule.ts b/striker-ui-api/src/lib/accessModule.ts index 0259e19d..6a92d3be 100644 --- a/striker-ui-api/src/lib/accessModule.ts +++ b/striker-ui-api/src/lib/accessModule.ts @@ -139,9 +139,6 @@ class Access extends EventEmitter { const access = new Access(); -const data = async (...keys: string[]) => - access.interact('x', `data->${keys.join('->')}`); - const subroutine = async ( subroutine: string, { @@ -150,7 +147,7 @@ const subroutine = async ( }: { params?: unknown[]; pre?: string[]; - }, + } = {}, ) => { const chain = `${pre.join('->')}->${subroutine}`; @@ -319,19 +316,29 @@ const dbSubRefreshTimestamp = () => { return result; }; -const getAnvilData = ( - dataStruct: AnvilDataStruct, - { predata, ...spawnSyncOptions }: GetAnvilDataOptions = {}, -): HashType => - execAnvilAccessModule( - [ - '--predata', - JSON.stringify(predata), - '--data', - JSON.stringify(dataStruct), - ], - spawnSyncOptions, - ).stdout; +const getData = async (...keys: string[]) => { + const chain = `data->${keys.join('->')}`; + + const { + sub_results: [data], + } = await access.interact<{ sub_results: [T] }>('x', chain); + + shvar(data, `${chain} data: `); + + return data; +}; + +const getFenceSpec = async () => { + await subroutine('get_fence_data', { pre: ['Striker'] }); + + return getData('fence_data'); +}; + +const getHostData = async () => { + await subroutine('get_hosts'); + + return getData('hosts'); +}; const getLocalHostName = () => { let result: string; @@ -349,7 +356,7 @@ const getLocalHostName = () => { return result; }; -const getLocalHostUUID = () => { +const getLocalHostUuid = () => { let result: string; try { @@ -365,6 +372,15 @@ const getLocalHostUUID = () => { return result; }; +const getManifestData = async (manifestUuid?: string) => { + await subroutine('load_manifest', { + params: [{ manifest_uuid: manifestUuid }], + pre: ['Striker'], + }); + + return getData('manifests'); +}; + const getPeerData: GetPeerDataFunction = async ( target, { password, port } = {}, @@ -396,17 +412,26 @@ const getPeerData: GetPeerDataFunction = async ( }; }; +const getUpsSpec = async () => { + await subroutine('get_ups_data', { pre: ['Striker'] }); + + return getData('ups_data'); +}; + export { dbInsertOrUpdateJob as job, dbInsertOrUpdateVariable as variable, dbJobAnvilSyncShared, dbSubRefreshTimestamp as timestamp, execModuleSubroutine as sub, - getAnvilData, + getData, + getFenceSpec, + getHostData, getLocalHostName, - getLocalHostUUID, + getLocalHostUuid as getLocalHostUUID, + getManifestData, getPeerData, - data, + getUpsSpec, query, subroutine, write, diff --git a/striker-ui-api/src/lib/request_handlers/command/runManifest.ts b/striker-ui-api/src/lib/request_handlers/command/runManifest.ts index 6f98296a..d3a6537c 100644 --- a/striker-ui-api/src/lib/request_handlers/command/runManifest.ts +++ b/striker-ui-api/src/lib/request_handlers/command/runManifest.ts @@ -4,7 +4,13 @@ import { RequestHandler } from 'express'; import { REP_PEACEFUL_STRING, REP_UUID } from '../../consts/REG_EXP_PATTERNS'; import SERVER_PATHS from '../../consts/SERVER_PATHS'; -import { getAnvilData, job, sub } from '../../accessModule'; +import { + getData, + getHostData, + getManifestData, + job, + sub, +} from '../../accessModule'; import { sanitize } from '../../sanitize'; import { stderr } from '../../shell'; @@ -12,7 +18,7 @@ export const runManifest: RequestHandler< { manifestUuid: string }, undefined, RunManifestRequestBody -> = (request, response) => { +> = async (request, response) => { const { params: { manifestUuid }, body: { @@ -85,29 +91,9 @@ export const runManifest: RequestHandler< let rawSysData: AnvilDataSysHash | undefined; try { - ({ - hosts: rawHostListData, - manifests: rawManifestListData, - sys: rawSysData, - } = getAnvilData<{ - hosts?: AnvilDataHostListHash; - manifests?: AnvilDataManifestListHash; - sys?: AnvilDataSysHash; - }>( - { hosts: true, manifests: true, sys: true }, - { - predata: [ - ['Database->get_hosts'], - [ - 'Striker->load_manifest', - { - debug, - manifest_uuid: manifestUuid, - }, - ], - ], - }, - )); + rawHostListData = await getHostData(); + rawManifestListData = await getManifestData(manifestUuid); + rawSysData = await getData('sys'); } catch (subError) { stderr( `Failed to get install manifest ${manifestUuid}; CAUSE: ${subError}`, diff --git a/striker-ui-api/src/lib/request_handlers/fence/getFenceTemplate.ts b/striker-ui-api/src/lib/request_handlers/fence/getFenceTemplate.ts index beec0a0e..7a723fcd 100644 --- a/striker-ui-api/src/lib/request_handlers/fence/getFenceTemplate.ts +++ b/striker-ui-api/src/lib/request_handlers/fence/getFenceTemplate.ts @@ -1,15 +1,13 @@ import { RequestHandler } from 'express'; -import { getAnvilData } from '../../accessModule'; + +import { getFenceSpec } from '../../accessModule'; import { stderr } from '../../shell'; -export const getFenceTemplate: RequestHandler = (request, response) => { +export const getFenceTemplate: RequestHandler = async (request, response) => { let rawFenceData; try { - ({ fence_data: rawFenceData } = getAnvilData<{ fence_data: unknown }>( - { fence_data: true }, - { predata: [['Striker->get_fence_data']] }, - )); + rawFenceData = await getFenceSpec(); } catch (subError) { stderr(`Failed to get fence device template; CAUSE: ${subError}`); diff --git a/striker-ui-api/src/lib/request_handlers/host/createHostConnection.ts b/striker-ui-api/src/lib/request_handlers/host/createHostConnection.ts index f4cfc5ca..4fe3b1a5 100644 --- a/striker-ui-api/src/lib/request_handlers/host/createHostConnection.ts +++ b/striker-ui-api/src/lib/request_handlers/host/createHostConnection.ts @@ -3,7 +3,7 @@ import { RequestHandler } from 'express'; import SERVER_PATHS from '../../consts/SERVER_PATHS'; import { - getAnvilData, + getData, getLocalHostUUID, getPeerData, job, @@ -140,10 +140,8 @@ export const createHostConnection: RequestHandler< try { const { - database: { - [localHostUUID]: { port: rawLocalDBPort }, - }, - } = getAnvilData<{ database: AnvilDataDatabaseHash }>({ database: true }); + [localHostUUID]: { port: rawLocalDBPort }, + } = await getData('database'); localDBPort = sanitize(rawLocalDBPort, 'number'); } catch (subError) { diff --git a/striker-ui-api/src/lib/request_handlers/host/getHostConnection.ts b/striker-ui-api/src/lib/request_handlers/host/getHostConnection.ts index 0bf8da69..0f06c9d5 100644 --- a/striker-ui-api/src/lib/request_handlers/host/getHostConnection.ts +++ b/striker-ui-api/src/lib/request_handlers/host/getHostConnection.ts @@ -1,4 +1,4 @@ -import { getAnvilData, getLocalHostUUID } from '../../accessModule'; +import { getData, getLocalHostUUID } from '../../accessModule'; import { buildUnknownIDCondition } from '../../buildCondition'; import buildGetRequestHandler from '../buildGetRequestHandler'; import { toLocal } from '../../convertHostUUID'; @@ -39,7 +39,7 @@ const buildHostConnections = ( ); export const getHostConnection = buildGetRequestHandler( - (request, buildQueryOptions) => { + async (request, buildQueryOptions) => { const { hostUUIDs: rawHostUUIDs } = request.query; let rawDatabaseData: AnvilDataDatabaseHash; @@ -59,9 +59,7 @@ export const getHostConnection = buildGetRequestHandler( stdout(`condHostUUIDs=[${condHostUUIDs}]`); try { - ({ database: rawDatabaseData } = getAnvilData<{ - database: AnvilDataDatabaseHash; - }>({ database: true })); + rawDatabaseData = await getData('database'); } catch (subError) { throw new Error(`Failed to get anvil data; CAUSE: ${subError}`); } diff --git a/striker-ui-api/src/lib/request_handlers/manifest/getManifestDetail.ts b/striker-ui-api/src/lib/request_handlers/manifest/getManifestDetail.ts index 0da929a9..fcabc918 100644 --- a/striker-ui-api/src/lib/request_handlers/manifest/getManifestDetail.ts +++ b/striker-ui-api/src/lib/request_handlers/manifest/getManifestDetail.ts @@ -1,6 +1,6 @@ import { RequestHandler } from 'express'; -import { getAnvilData } from '../../accessModule'; +import { getManifestData } from '../../accessModule'; import { getEntityParts } from '../../disassembleEntityId'; import { stderr, stdout } from '../../shell'; @@ -67,25 +67,18 @@ const handleSortNetworks = ( return result; }; -export const getManifestDetail: RequestHandler = (request, response) => { +export const getManifestDetail: RequestHandler = async (request, response) => { const { - params: { manifestUUID }, + params: { manifestUUID: manifestUuid }, } = request; let rawManifestListData: AnvilDataManifestListHash | undefined; try { - ({ manifests: rawManifestListData } = getAnvilData<{ - manifests?: AnvilDataManifestListHash; - }>( - { manifests: true }, - { - predata: [['Striker->load_manifest', { manifest_uuid: manifestUUID }]], - }, - )); + rawManifestListData = await getManifestData(manifestUuid); } catch (subError) { stderr( - `Failed to get install manifest ${manifestUUID}; CAUSE: ${subError}`, + `Failed to get install manifest ${manifestUuid}; CAUSE: ${subError}`, ); response.status(500).send(); @@ -109,7 +102,7 @@ export const getManifestDetail: RequestHandler = (request, response) => { const { manifest_uuid: { - [manifestUUID]: { + [manifestUuid]: { parsed: { domain, fences: fenceUuidList = {}, diff --git a/striker-ui-api/src/lib/request_handlers/ups/getUPSTemplate.ts b/striker-ui-api/src/lib/request_handlers/ups/getUPSTemplate.ts index 395e46b7..3c3f4f76 100644 --- a/striker-ui-api/src/lib/request_handlers/ups/getUPSTemplate.ts +++ b/striker-ui-api/src/lib/request_handlers/ups/getUPSTemplate.ts @@ -1,16 +1,13 @@ import { RequestHandler } from 'express'; -import { getAnvilData } from '../../accessModule'; +import { getUpsSpec } from '../../accessModule'; import { stderr } from '../../shell'; -export const getUPSTemplate: RequestHandler = (request, response) => { +export const getUPSTemplate: RequestHandler = async (request, response) => { let rawUPSData: AnvilDataUPSHash; try { - ({ ups_data: rawUPSData } = getAnvilData<{ ups_data: AnvilDataUPSHash }>( - { ups_data: true }, - { predata: [['Striker->get_ups_data']] }, - )); + rawUPSData = await getUpsSpec(); } catch (subError) { stderr(`Failed to get ups template; CAUSE: ${subError}`); diff --git a/striker-ui-api/src/types/GetAnvilDataFunction.d.ts b/striker-ui-api/src/types/GetAnvilDataFunction.d.ts index 0d8c2a0c..f4e4e775 100644 --- a/striker-ui-api/src/types/GetAnvilDataFunction.d.ts +++ b/striker-ui-api/src/types/GetAnvilDataFunction.d.ts @@ -111,6 +111,6 @@ type AnvilDataUPSHash = { }; }; -type GetAnvilDataOptions = import('child_process').SpawnSyncOptions & { +type GetAnvilDataOptions = { predata?: Array<[string, ...unknown[]]>; };