parent
54b78e9f25
commit
5e9c38b008
8 changed files with 117 additions and 84 deletions
@ -0,0 +1,34 @@ |
||||
import { RequestHandler } from 'express'; |
||||
|
||||
import { sanitize } from '../sanitize'; |
||||
import { stderr, stdout } from '../shell'; |
||||
|
||||
export const buildBranchRequestHandler: (map: { |
||||
[handler: string]: RequestHandler | undefined; |
||||
}) => RequestHandler = |
||||
(map) => |
||||
(...args) => { |
||||
const [ |
||||
{ |
||||
query: { handler: rawHandler }, |
||||
}, |
||||
response, |
||||
] = args; |
||||
|
||||
const handlerKey = sanitize(rawHandler, 'string'); |
||||
|
||||
stdout(`Create host handler: ${handlerKey}`); |
||||
|
||||
// Ensure each handler sends a response at the end of any branch.
|
||||
const handler = map[handlerKey]; |
||||
|
||||
if (handler) { |
||||
handler(...args); |
||||
} else { |
||||
stderr(`Handler is not registered; got [${handlerKey}]`); |
||||
|
||||
response.status(400).send(); |
||||
|
||||
return; |
||||
} |
||||
}; |
@ -1,24 +1,8 @@ |
||||
import { RequestHandler } from 'express'; |
||||
|
||||
import { buildBranchRequestHandler } from '../buildBranchRequestHandler'; |
||||
import { configStriker } from './configStriker'; |
||||
import { sanitize } from '../../sanitize'; |
||||
import { stdout } from '../../shell'; |
||||
|
||||
// Ensure each create handler sends a response at the end of any branch.
|
||||
const MAP_TO_CREATE_HANDLER: Record<string, RequestHandler | undefined> = { |
||||
export const createHost: RequestHandler = buildBranchRequestHandler({ |
||||
striker: configStriker, |
||||
}; |
||||
|
||||
export const createHost: RequestHandler = (...args) => { |
||||
const [ |
||||
{ |
||||
query: { hostType: rawHostType }, |
||||
}, |
||||
] = args; |
||||
|
||||
const hostType = sanitize(rawHostType, 'string'); |
||||
|
||||
stdout(`hostType=[${hostType}]`); |
||||
|
||||
MAP_TO_CREATE_HANDLER[hostType]?.call(null, ...args); |
||||
}; |
||||
}); |
||||
|
@ -0,0 +1,39 @@ |
||||
import { RequestHandler } from 'express'; |
||||
|
||||
import { LOCAL } from '../../consts/LOCAL'; |
||||
import SERVER_PATHS from '../../consts/SERVER_PATHS'; |
||||
|
||||
import { job } from '../../accessModule'; |
||||
import { stderr, stdout } from '../../shell'; |
||||
|
||||
export const setHostInstallTarget: RequestHandler = (request, response) => { |
||||
stdout( |
||||
`Begin set host install target.\n${JSON.stringify(request.body, null, 2)}`, |
||||
); |
||||
|
||||
const { isEnableInstallTarget } = |
||||
request.body as SetHostInstallTargetRequestBody; |
||||
const { hostUUID: rawHostUUID } = request.params as UpdateHostParams; |
||||
const hostUUID: string | undefined = |
||||
rawHostUUID === LOCAL ? undefined : rawHostUUID; |
||||
const task = isEnableInstallTarget ? 'enable' : 'disable'; |
||||
|
||||
try { |
||||
job({ |
||||
file: __filename, |
||||
job_command: `${SERVER_PATHS.usr.sbin['striker-manage-install-target'].self} --${task}`, |
||||
job_description: 'job_0016', |
||||
job_host_uuid: hostUUID, |
||||
job_name: `install-target::${task}`, |
||||
job_title: 'job_0015', |
||||
}); |
||||
} catch (subError) { |
||||
stderr(`Failed to ${task} install target; CAUSE: ${subError}`); |
||||
|
||||
response.status(500).send(); |
||||
|
||||
return; |
||||
} |
||||
|
||||
response.status(200).send(); |
||||
}; |
@ -1,47 +1,8 @@ |
||||
import { RequestHandler } from 'express'; |
||||
|
||||
import { LOCAL } from '../../consts/LOCAL'; |
||||
import SERVER_PATHS from '../../consts/SERVER_PATHS'; |
||||
import { buildBranchRequestHandler } from '../buildBranchRequestHandler'; |
||||
import { setHostInstallTarget } from './setHostInstallTarget'; |
||||
|
||||
import { job } from '../../accessModule'; |
||||
import { stderr, stdout } from '../../shell'; |
||||
|
||||
export const updateHost: RequestHandler< |
||||
{ hostUUID: string }, |
||||
undefined, |
||||
{ isEnableInstallTarget?: boolean } |
||||
> = (request, response) => { |
||||
stdout( |
||||
`Begin edit host properties.\n${JSON.stringify(request.body, null, 2)}`, |
||||
); |
||||
|
||||
const { |
||||
body: { isEnableInstallTarget }, |
||||
params: { hostUUID: rawHostUUID }, |
||||
} = request; |
||||
const hostUUID: string | undefined = |
||||
rawHostUUID === LOCAL ? undefined : rawHostUUID; |
||||
|
||||
if (isEnableInstallTarget !== undefined) { |
||||
const task = isEnableInstallTarget ? 'enable' : 'disable'; |
||||
|
||||
try { |
||||
job({ |
||||
file: __filename, |
||||
job_command: `${SERVER_PATHS.usr.sbin['striker-manage-install-target'].self} --${task}`, |
||||
job_description: 'job_0016', |
||||
job_host_uuid: hostUUID, |
||||
job_name: `install-target::${task}`, |
||||
job_title: 'job_0015', |
||||
}); |
||||
} catch (subError) { |
||||
stderr(`Failed to ${task} install target; CAUSE: ${subError}`); |
||||
|
||||
response.status(500).send(); |
||||
|
||||
return; |
||||
} |
||||
} |
||||
|
||||
response.status(200).send(); |
||||
}; |
||||
export const updateHost: RequestHandler = buildBranchRequestHandler({ |
||||
'install-target': setHostInstallTarget, |
||||
}); |
||||
|
@ -0,0 +1,19 @@ |
||||
type SetHostInstallTargetRequestBody = { |
||||
isEnableInstallTarget: boolean; |
||||
}; |
||||
|
||||
type PrepareHostRequestBody = { |
||||
hostIPAddress: string; |
||||
hostName: string; |
||||
hostPassword: string; |
||||
hostSSHPort?: number; |
||||
hostType: string; |
||||
hostUser?: string; |
||||
hostUUID?: string; |
||||
redhatPassword: string; |
||||
redhatUser: string; |
||||
}; |
||||
|
||||
type UpdateHostParams = { |
||||
hostUUID: string; |
||||
}; |
Loading…
Reference in new issue