parent
28596e15dd
commit
d5ee937a9e
8 changed files with 89 additions and 2 deletions
@ -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…
Reference in new issue