From d5ee937a9eea02feb2744747496075d19b06551f Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Tue, 18 Oct 2022 15:27:29 -0400 Subject: [PATCH] fix(striker-ui-api): add commands to manage host power actions --- striker-ui-api/src/lib/accessModule.ts | 2 +- striker-ui-api/src/lib/consts/SERVER_PATHS.ts | 1 + .../command/buildHostPowerHandler.ts | 56 +++++++++++++++++++ .../src/lib/request_handlers/command/index.ts | 2 + .../request_handlers/command/poweroffHost.ts | 3 + .../request_handlers/command/rebootHost.ts | 3 + striker-ui-api/src/routes/command.ts | 6 +- .../types/DBInsertOrUpdateJobFunction.d.ts | 18 ++++++ 8 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 striker-ui-api/src/lib/request_handlers/command/buildHostPowerHandler.ts create mode 100644 striker-ui-api/src/lib/request_handlers/command/index.ts create mode 100644 striker-ui-api/src/lib/request_handlers/command/poweroffHost.ts create mode 100644 striker-ui-api/src/lib/request_handlers/command/rebootHost.ts create mode 100644 striker-ui-api/src/types/DBInsertOrUpdateJobFunction.d.ts diff --git a/striker-ui-api/src/lib/accessModule.ts b/striker-ui-api/src/lib/accessModule.ts index bdd3d4fa..bb9c3138 100644 --- a/striker-ui-api/src/lib/accessModule.ts +++ b/striker-ui-api/src/lib/accessModule.ts @@ -74,7 +74,7 @@ const execModuleSubroutine = ( }; }; -const dbInsertOrUpdateJob = (options?: ExecModuleSubroutineOptions) => +const dbInsertOrUpdateJob = (options?: DBInsertOrUpdateJobOptions) => execModuleSubroutine('insert_or_update_jobs', options).stdout; const dbJobAnvilSyncShared = ( diff --git a/striker-ui-api/src/lib/consts/SERVER_PATHS.ts b/striker-ui-api/src/lib/consts/SERVER_PATHS.ts index f5cb357f..37c06a2f 100644 --- a/striker-ui-api/src/lib/consts/SERVER_PATHS.ts +++ b/striker-ui-api/src/lib/consts/SERVER_PATHS.ts @@ -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': {}, diff --git a/striker-ui-api/src/lib/request_handlers/command/buildHostPowerHandler.ts b/striker-ui-api/src/lib/request_handlers/command/buildHostPowerHandler.ts new file mode 100644 index 00000000..e809085a --- /dev/null +++ b/striker-ui-api/src/lib/request_handlers/command/buildHostPowerHandler.ts @@ -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(); + }; diff --git a/striker-ui-api/src/lib/request_handlers/command/index.ts b/striker-ui-api/src/lib/request_handlers/command/index.ts new file mode 100644 index 00000000..490dfe5c --- /dev/null +++ b/striker-ui-api/src/lib/request_handlers/command/index.ts @@ -0,0 +1,2 @@ +export * from './poweroffHost'; +export * from './rebootHost'; diff --git a/striker-ui-api/src/lib/request_handlers/command/poweroffHost.ts b/striker-ui-api/src/lib/request_handlers/command/poweroffHost.ts new file mode 100644 index 00000000..47dc21cb --- /dev/null +++ b/striker-ui-api/src/lib/request_handlers/command/poweroffHost.ts @@ -0,0 +1,3 @@ +import { buildHostPowerHandler } from './buildHostPowerHandler'; + +export const poweroffHost = buildHostPowerHandler('poweroff'); diff --git a/striker-ui-api/src/lib/request_handlers/command/rebootHost.ts b/striker-ui-api/src/lib/request_handlers/command/rebootHost.ts new file mode 100644 index 00000000..0ee5321b --- /dev/null +++ b/striker-ui-api/src/lib/request_handlers/command/rebootHost.ts @@ -0,0 +1,3 @@ +import { buildHostPowerHandler } from './buildHostPowerHandler'; + +export const rebootHost = buildHostPowerHandler('reboot'); diff --git a/striker-ui-api/src/routes/command.ts b/striker-ui-api/src/routes/command.ts index b8196ffa..61060418 100644 --- a/striker-ui-api/src/routes/command.ts +++ b/striker-ui-api/src/routes/command.ts @@ -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; diff --git a/striker-ui-api/src/types/DBInsertOrUpdateJobFunction.d.ts b/striker-ui-api/src/types/DBInsertOrUpdateJobFunction.d.ts new file mode 100644 index 00000000..26424ca5 --- /dev/null +++ b/striker-ui-api/src/types/DBInsertOrUpdateJobFunction.d.ts @@ -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; +};