fix(striker-ui-api): migrate getPeerData to use access interact

main
Tsu-ba-me 2 years ago
parent cb6a699fc7
commit 1e84641157
  1. 30
      striker-ui-api/src/lib/accessModule.ts
  2. 10
      striker-ui-api/src/lib/request_handlers/command/getHostSSH.ts
  3. 43
      striker-ui-api/src/lib/request_handlers/host/createHostConnection.ts
  4. 12
      striker-ui-api/src/types/GetPeerDataFunction.d.ts

@ -142,7 +142,7 @@ const access = new Access();
const data = async <T>(...keys: string[]) => const data = async <T>(...keys: string[]) =>
access.interact<T>('x', `data->${keys.join('->')}`); access.interact<T>('x', `data->${keys.join('->')}`);
const subroutine = <T extends Record<string, unknown>>( const subroutine = async <T extends unknown[]>(
subroutine: string, subroutine: string,
{ {
params = [], params = [],
@ -163,10 +163,18 @@ const subroutine = <T extends Record<string, unknown>>(
result = String(p); result = String(p);
} }
return result; return `'${result}'`;
}); });
return access.interact<T>('x', chain, ...subParams); const { sub_results: results } = await access.interact<{ sub_results: T }>(
'x',
chain,
...subParams,
);
shvar(results, `${chain} results: `);
return results;
}; };
const query = <T extends (number | null | string)[][]>(script: string) => const query = <T extends (number | null | string)[][]>(script: string) =>
@ -357,9 +365,9 @@ const getLocalHostUUID = () => {
return result; return result;
}; };
const getPeerData: GetPeerDataFunction = ( const getPeerData: GetPeerDataFunction = async (
target, target,
{ password, port, ...restOptions } = {}, { password, port } = {},
) => { ) => {
const [ const [
rawIsConnected, rawIsConnected,
@ -370,11 +378,13 @@ const getPeerData: GetPeerDataFunction = (
internet: rawIsInetConnected, internet: rawIsInetConnected,
os_registered: rawIsOSRegistered, os_registered: rawIsOSRegistered,
}, },
] = execModuleSubroutine('get_peer_data', { ]: [connected: string, data: PeerDataHash] = await subroutine(
subModuleName: 'Striker', 'get_peer_data',
subParams: { password, port, target }, {
...restOptions, params: [{ password, port, target }],
}).stdout as [connected: string, data: PeerDataHash]; pre: ['Striker'],
},
);
return { return {
hostName, hostName,

@ -14,12 +14,12 @@ export const getHostSSH: RequestHandler<
GetHostSshRequestBody GetHostSshRequestBody
> = async (request, response) => { > = async (request, response) => {
const { const {
body: { password: rpassword, port: rport = 22, ipAddress: rtarget }, body: { password: rPassword, port: rPort = 22, ipAddress: rTarget } = {},
} = request; } = request;
const password = sanitize(rpassword, 'string'); const password = sanitize(rPassword, 'string');
const port = sanitize(rport, 'number'); const port = sanitize(rPort, 'number');
const target = sanitize(rtarget, 'string', { modifierType: 'sql' }); const target = sanitize(rTarget, 'string', { modifierType: 'sql' });
try { try {
assert( assert(
@ -47,7 +47,7 @@ export const getHostSSH: RequestHandler<
let rsbody: GetHostSshResponseBody; let rsbody: GetHostSshResponseBody;
try { try {
rsbody = getPeerData(target, { password, port }); rsbody = await getPeerData(target, { password, port });
} catch (subError) { } catch (subError) {
stderr(`Failed to get peer data; CAUSE: ${subError}`); stderr(`Failed to get peer data; CAUSE: ${subError}`);

@ -16,7 +16,7 @@ export const createHostConnection: RequestHandler<
unknown, unknown,
undefined, undefined,
CreateHostConnectionRequestBody CreateHostConnectionRequestBody
> = (request, response) => { > = async (request, response) => {
const { const {
body: { body: {
dbName = 'anvil', dbName = 'anvil',
@ -46,16 +46,15 @@ export const createHostConnection: RequestHandler<
let peerHostUUID: string; let peerHostUUID: string;
try { try {
({ hostUUID: peerHostUUID, isConnected: isPeerReachable } = getPeerData( ({ hostUUID: peerHostUUID, isConnected: isPeerReachable } =
peerIPAddress, await getPeerData(peerIPAddress, {
{ password: commonPassword, port: peerSSHPort }, password: commonPassword,
)); port: peerSSHPort,
}));
} catch (subError) { } catch (subError) {
stderr(`Failed to get peer data; CAUSE: ${subError}`); stderr(`Failed to get peer data; CAUSE: ${subError}`);
response.status(500).send(); return response.status(500).send();
return;
} }
stdoutVar({ peerHostUUID, isPeerReachable }); stdoutVar({ peerHostUUID, isPeerReachable });
@ -65,9 +64,7 @@ export const createHostConnection: RequestHandler<
`Cannot connect to peer; please verify credentials and SSH keys validity.`, `Cannot connect to peer; please verify credentials and SSH keys validity.`,
); );
response.status(400).send(); return response.status(400).send();
return;
} }
try { try {
@ -78,9 +75,7 @@ export const createHostConnection: RequestHandler<
} catch (subError) { } catch (subError) {
stderr(`Failed to get matching IP address; CAUSE: ${subError}`); stderr(`Failed to get matching IP address; CAUSE: ${subError}`);
response.status(500).send(); return response.status(500).send();
return;
} }
stdoutVar({ localIPAddress }); stdoutVar({ localIPAddress });
@ -107,9 +102,7 @@ export const createHostConnection: RequestHandler<
} catch (subError) { } catch (subError) {
stderr(`Failed to write ${pgpassFilePath}; CAUSE: ${subError}`); stderr(`Failed to write ${pgpassFilePath}; CAUSE: ${subError}`);
response.status(500).send(); return response.status(500).send();
return;
} }
try { try {
@ -130,9 +123,7 @@ export const createHostConnection: RequestHandler<
} catch (fsError) { } catch (fsError) {
stderr(`Failed to remove ${pgpassFilePath}; CAUSE: ${fsError}`); stderr(`Failed to remove ${pgpassFilePath}; CAUSE: ${fsError}`);
response.status(500).send(); return response.status(500).send();
return;
} }
stdoutVar({ isPeerDBReachable }); stdoutVar({ isPeerDBReachable });
@ -142,9 +133,7 @@ export const createHostConnection: RequestHandler<
`Cannot connect to peer database; please verify database credentials.`, `Cannot connect to peer database; please verify database credentials.`,
); );
response.status(400).send(); return response.status(400).send();
return;
} }
const localHostUUID = getLocalHostUUID(); const localHostUUID = getLocalHostUUID();
@ -160,9 +149,7 @@ export const createHostConnection: RequestHandler<
} catch (subError) { } catch (subError) {
stderr(`Failed to get local database data from hash; CAUSE: ${subError}`); stderr(`Failed to get local database data from hash; CAUSE: ${subError}`);
response.status(500).send(); return response.status(500).send();
return;
} }
const jobCommand = `${SERVER_PATHS.usr.sbin['striker-manage-peers'].self} --add --host-uuid ${peerHostUUID} --host ${peerIPAddress} --port ${commonDBPort} --ping ${commonPing}`; const jobCommand = `${SERVER_PATHS.usr.sbin['striker-manage-peers'].self} --add --host-uuid ${peerHostUUID} --host ${peerIPAddress} --port ${commonDBPort} --ping ${commonPing}`;
@ -181,9 +168,7 @@ peer_job_command=${peerJobCommand}`,
} catch (subError) { } catch (subError) {
stderr(`Failed to add peer ${peerHostUUID}; CAUSE: ${subError}`); stderr(`Failed to add peer ${peerHostUUID}; CAUSE: ${subError}`);
response.status(500).send(); return response.status(500).send();
return;
} }
response.status(201).send(); response.status(201).send();

@ -6,23 +6,19 @@ type PeerDataHash = {
os_registered: string; os_registered: string;
}; };
type GetPeerDataOptions = Omit< type GetPeerDataOptions = ModuleSubroutineCommonParams & {
ExecModuleSubroutineOptions,
'subModuleName' | 'subParams'
> &
ModuleSubroutineCommonParams & {
password?: string; password?: string;
port?: number; port?: number;
}; };
type GetPeerDataFunction = ( type GetPeerDataFunction = (
target: string, target: string,
options?: GetPeerDataOptions, options?: GetPeerDataOptions,
) => { ) => Promise<{
hostName: string; hostName: string;
hostOS: string; hostOS: string;
hostUUID: string; hostUUID: string;
isConnected: boolean; isConnected: boolean;
isInetConnected: boolean; isInetConnected: boolean;
isOSRegistered: boolean; isOSRegistered: boolean;
}; }>;

Loading…
Cancel
Save