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. 16
      striker-ui-api/src/types/GetPeerDataFunction.d.ts

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

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

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

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

Loading…
Cancel
Save