fix(striker-ui-api): set job host uuid for start/stop server jobs

main
Tsu-ba-me 9 months ago
parent 8dd8a28bb3
commit 91dd5d37a1
  1. 73
      striker-ui-api/src/lib/request_handlers/command/buildPowerHandler.ts
  2. 4
      striker-ui-api/src/lib/request_handlers/command/startServer.ts
  3. 4
      striker-ui-api/src/lib/request_handlers/command/stopServer.ts
  4. 1
      striker-ui-api/src/types/BuildPowerHandlerFunction.d.ts

@ -1,7 +1,7 @@
import assert from 'assert'; import assert from 'assert';
import { RequestHandler } from 'express'; import { RequestHandler } from 'express';
import { LOCAL, REP_UUID, SERVER_PATHS } from '../../consts'; import { DELETED, LOCAL, REP_UUID, SERVER_PATHS } from '../../consts';
import { job, query } from '../../accessModule'; import { job, query } from '../../accessModule';
import { sanitize } from '../../sanitize'; import { sanitize } from '../../sanitize';
@ -34,29 +34,39 @@ const MAP_TO_POWER_JOB_PARAMS_BUILDER: Record<
job_name: `set_power::on`, job_name: `set_power::on`,
job_title: 'job_0334', job_title: 'job_0334',
}), }),
startserver: ({ uuid } = {}) => ({ startserver: ({ runOn, uuid } = {}) => ({
job_command: `${SERVER_PATHS.usr.sbin['anvil-boot-server'].self} --server-uuid '${uuid}'`, job_command: `${SERVER_PATHS.usr.sbin['anvil-boot-server'].self}`,
job_data: `server-uuid=${uuid}`,
job_description: 'job_0341', job_description: 'job_0341',
job_host_uuid: runOn,
job_name: 'set_power::server::on', job_name: 'set_power::server::on',
job_title: 'job_0340', job_title: 'job_0340',
}), }),
stop: ({ isStopServers, uuid } = {}) => ({ stop: ({ isStopServers, runOn } = {}) => ({
job_command: `${SERVER_PATHS.usr.sbin['anvil-safe-stop'].self} --power-off${ job_command: `${SERVER_PATHS.usr.sbin['anvil-safe-stop'].self} --power-off${
isStopServers ? ' --stop-servers' : '' isStopServers ? ' --stop-servers' : ''
}`, }`,
job_description: 'job_0333', job_description: 'job_0333',
job_host_uuid: uuid, job_host_uuid: runOn,
job_name: 'set_power::off', job_name: 'set_power::off',
job_title: 'job_0332', job_title: 'job_0332',
}), }),
stopserver: ({ force, uuid } = {}) => ({ stopserver: ({ force, runOn, uuid } = {}) => {
job_command: `${ let command = SERVER_PATHS.usr.sbin['anvil-shutdown-server'].self;
SERVER_PATHS.usr.sbin['anvil-shutdown-server'].self
} --server-uuid '${uuid}'${force ? ' --immediate' : ''}`, if (force) {
command += ' --immediate';
}
return {
job_command: command,
job_data: `server-uuid=${uuid}`,
job_description: 'job_0343', job_description: 'job_0343',
job_host_uuid: runOn,
job_name: 'set_power::server::off', job_name: 'set_power::server::off',
job_title: 'job_0342', job_title: 'job_0342',
}), };
},
}; };
const queuePowerJob = async ( const queuePowerJob = async (
@ -74,8 +84,10 @@ const queuePowerJob = async (
export const buildPowerHandler: ( export const buildPowerHandler: (
task: PowerTask, task: PowerTask,
options?: { getJobHostUuid?: (uuid?: string) => Promise<string | undefined> },
) => RequestHandler<{ uuid?: string }> = ) => RequestHandler<{ uuid?: string }> =
(task) => async (request, response) => { (task, { getJobHostUuid } = {}) =>
async (request, response) => {
const { const {
params: { uuid }, params: { uuid },
query: { force: rForce }, query: { force: rForce },
@ -97,7 +109,9 @@ export const buildPowerHandler: (
} }
try { try {
await queuePowerJob(task, { force, uuid }); const runOn = await getJobHostUuid?.call(null, uuid);
await queuePowerJob(task, { force, runOn, uuid });
} catch (error) { } catch (error) {
stderr(`Failed to ${task} ${uuid ?? LOCAL}; CAUSE: ${error}`); stderr(`Failed to ${task} ${uuid ?? LOCAL}; CAUSE: ${error}`);
@ -144,7 +158,11 @@ export const buildAnPowerHandler: (
for (const hostUuid of rows[0]) { for (const hostUuid of rows[0]) {
try { try {
await queuePowerJob(task, { isStopServers: true, uuid: hostUuid }); await queuePowerJob(task, {
isStopServers: true,
runOn: hostUuid,
uuid: hostUuid,
});
} catch (error) { } catch (error) {
stderr(`Failed to ${task} host ${hostUuid}; CAUSE: ${error}`); stderr(`Failed to ${task} host ${hostUuid}; CAUSE: ${error}`);
@ -154,3 +172,32 @@ export const buildAnPowerHandler: (
return response.status(204).send(); return response.status(204).send();
}; };
export const buildServerPowerHandler: (
task: Extract<PowerTask, 'startserver' | 'stopserver'>,
) => RequestHandler<{ uuid: string }> = (task) =>
buildPowerHandler(task, {
getJobHostUuid: async (uuid) => {
if (!uuid) return;
let serverHostUuid: string | undefined;
try {
const rows = await query<[[null | string]]>(
`SELECT server_host_uuid FROM servers WHERE server_uuid = '${uuid}' server_state != '${DELETED}';`,
);
assert.ok(rows.length, `No entry found`);
const [[hostUuid]] = rows;
if (hostUuid) {
serverHostUuid = hostUuid;
}
} catch (error) {
throw new Error(`Failed to get server host; CAUSE: ${error}`);
}
return serverHostUuid;
},
});

@ -1,3 +1,3 @@
import { buildPowerHandler } from './buildPowerHandler'; import { buildServerPowerHandler } from './buildPowerHandler';
export const startServer = buildPowerHandler('startserver'); export const startServer = buildServerPowerHandler('startserver');

@ -1,3 +1,3 @@
import { buildPowerHandler } from './buildPowerHandler'; import { buildServerPowerHandler } from './buildPowerHandler';
export const stopServer = buildPowerHandler('stopserver'); export const stopServer = buildServerPowerHandler('stopserver');

@ -11,6 +11,7 @@ type PowerJobParams = Omit<JobParams, 'file' | 'line'>;
type BuildPowerJobParamsOptions = { type BuildPowerJobParamsOptions = {
force?: boolean; force?: boolean;
isStopServers?: boolean; isStopServers?: boolean;
runOn?: string;
uuid?: string; uuid?: string;
}; };

Loading…
Cancel
Save