fix(striker-ui-api): call psql directly to test peer database connection

main
Tsu-ba-me 1 year ago
parent 4182219e86
commit eeab34059b
  1. 32
      striker-ui-api/src/lib/request_handlers/host/createHostConnection.ts
  2. 2
      striker-ui-api/src/lib/shell.ts

@ -10,7 +10,7 @@ import {
sub,
} from '../../accessModule';
import { sanitize } from '../../sanitize';
import { rm, stderr, stdoutVar } from '../../shell';
import { rm, stderr, stdoutVar, systemCall } from '../../shell';
export const createHostConnection: RequestHandler<
unknown,
@ -100,17 +100,29 @@ export const createHostConnection: RequestHandler<
}
try {
const [rawIsPeerDBReachable]: [output: string, returnCode: number] =
await sub('call', {
params: [
{
shell_call: `PGPASSFILE="${pgpassFilePath}" ${SERVER_PATHS.usr.bin.psql.self} --host ${peerIPAddress} --port ${commonDBPort} --dbname ${commonDBName} --username ${commonDBUser} --no-password --tuples-only --no-align --command "SELECT 1"`,
},
const now = String(Date.now());
const rawIsPeerDBReachable = systemCall(
SERVER_PATHS.usr.bin.psql.self,
[
'--no-align',
'--no-password',
'--tuples-only',
'--command',
`SELECT ${now};`,
'--dbname',
commonDBName,
'--host',
peerIPAddress,
'--port',
String(commonDBPort),
'--username',
commonDBUser,
],
pre: ['System'],
});
{ env: { PGPASSFILE: pgpassFilePath } },
);
isPeerDBReachable = rawIsPeerDBReachable === '1';
isPeerDBReachable = rawIsPeerDBReachable === now;
} catch (subError) {
stderr(`Failed to test connection to peer database; CAUSE: ${subError}`);
}

@ -10,7 +10,7 @@ const print = (
}: { eol?: string; stream?: 'stderr' | 'stdout' } = {},
) => process[stream].write(`${message}${eol}`);
const systemCall = (
export const systemCall = (
...[command, args = [], options = {}]: Parameters<typeof spawnSync>
) => {
const { error, stderr, stdout } = spawnSync(command, args, {

Loading…
Cancel
Save