fix(striker-ui-api): add commands to manage host power actions

main
Tsu-ba-me 2 years ago
parent 28596e15dd
commit d5ee937a9e
  1. 2
      striker-ui-api/src/lib/accessModule.ts
  2. 1
      striker-ui-api/src/lib/consts/SERVER_PATHS.ts
  3. 56
      striker-ui-api/src/lib/request_handlers/command/buildHostPowerHandler.ts
  4. 2
      striker-ui-api/src/lib/request_handlers/command/index.ts
  5. 3
      striker-ui-api/src/lib/request_handlers/command/poweroffHost.ts
  6. 3
      striker-ui-api/src/lib/request_handlers/command/rebootHost.ts
  7. 6
      striker-ui-api/src/routes/command.ts
  8. 18
      striker-ui-api/src/types/DBInsertOrUpdateJobFunction.d.ts

@ -74,7 +74,7 @@ const execModuleSubroutine = (
};
};
const dbInsertOrUpdateJob = (options?: ExecModuleSubroutineOptions) =>
const dbInsertOrUpdateJob = (options?: DBInsertOrUpdateJobOptions) =>
execModuleSubroutine('insert_or_update_jobs', options).stdout;
const dbJobAnvilSyncShared = (

@ -18,6 +18,7 @@ const EMPTY_SERVER_PATHS: ServerPath = {
'anvil-access-module': {},
'anvil-configure-host': {},
'anvil-get-server-screenshot': {},
'anvil-manage-power': {},
'anvil-provision-server': {},
'anvil-sync-shared': {},
'striker-parse-os-list': {},

@ -0,0 +1,56 @@
import { RequestHandler } from 'express';
import SERVER_PATHS from '../../consts/SERVER_PATHS';
import { job } from '../../accessModule';
import { stderr } from '../../shell';
type DistinctDBJobParams = Omit<
DBJobParams,
'file' | 'line' | 'job_data' | 'job_progress'
>;
const MANAGE_HOST_POWER_JOB_PARAMS: {
poweroff: DistinctDBJobParams;
reboot: DistinctDBJobParams;
} = {
poweroff: {
job_command: `${SERVER_PATHS.usr.sbin['anvil-manage-power'].self} --poweroff -y`,
job_name: 'poweroff::system',
job_title: 'job_0010',
job_description: 'job_0008',
},
reboot: {
job_command: `${SERVER_PATHS.usr.sbin['anvil-manage-power'].self} --reboot -y`,
job_name: 'reboot::system',
job_title: 'job_0009',
job_description: 'job_0006',
},
};
export const buildHostPowerHandler: (
task?: 'poweroff' | 'reboot',
) => RequestHandler =
(task = 'reboot') =>
(request, response) => {
const subParams: DBJobParams = {
file: __filename,
line: 0,
job_data: '',
job_progress: 100,
...MANAGE_HOST_POWER_JOB_PARAMS[task],
};
try {
job({ subParams });
} catch (subError) {
stderr(`Failed to ${task} host; CAUSE: ${subError}`);
response.status(500).send();
return;
}
response.status(204).send();
};

@ -0,0 +1,2 @@
export * from './poweroffHost';
export * from './rebootHost';

@ -0,0 +1,3 @@
import { buildHostPowerHandler } from './buildHostPowerHandler';
export const poweroffHost = buildHostPowerHandler('poweroff');

@ -0,0 +1,3 @@
import { buildHostPowerHandler } from './buildHostPowerHandler';
export const rebootHost = buildHostPowerHandler('reboot');

@ -1,9 +1,13 @@
import express from 'express';
import { initializeStriker } from '../lib/request_handlers';
import { poweroffHost, rebootHost } from '../lib/request_handlers/command';
const router = express.Router();
router.put('/initialize-striker', initializeStriker);
router
.put('/initialize-striker', initializeStriker)
.put('/poweroff-host', poweroffHost)
.put('/reboot-host', rebootHost);
export default router;

@ -0,0 +1,18 @@
type DBJobParams = {
file: string;
line: number;
job_command: string;
job_data: string;
job_name: string;
job_title: string;
job_description: string;
job_host_uuid?: string;
job_progress: number;
};
type DBInsertOrUpdateJobOptions = Omit<
ExecModuleSubroutineOptions,
'subParams' | 'subModuleName'
> & {
subParams?: DBJobParams;
};
Loading…
Cancel
Save