parent
e638e2d388
commit
4e03e62dd4
4 changed files with 74 additions and 3 deletions
@ -0,0 +1,66 @@ |
||||
import assert from 'assert'; |
||||
import { RequestHandler } from 'express'; |
||||
|
||||
import { REP_UUID, SERVER_PATHS } from '../../consts'; |
||||
|
||||
import { job, query } from '../../accessModule'; |
||||
import { sanitize } from '../../sanitize'; |
||||
import { stderr, stdoutVar } from '../../shell'; |
||||
|
||||
export const deleteServer: RequestHandler< |
||||
{ serverUuid?: string }, |
||||
undefined, |
||||
{ serverUuids: string[] } |
||||
> = async (request, response) => { |
||||
const { |
||||
body: { serverUuids: rServerUuids } = {}, |
||||
params: { serverUuid: rServerUuid }, |
||||
} = request; |
||||
|
||||
const serverUuids = sanitize(rServerUuids, 'string[]', { |
||||
modifierType: 'sql', |
||||
}); |
||||
|
||||
if (rServerUuid) { |
||||
serverUuids.push( |
||||
sanitize(rServerUuid, 'string', { |
||||
modifierType: 'sql', |
||||
}), |
||||
); |
||||
} |
||||
|
||||
stdoutVar(serverUuids, `Delete servers with: `); |
||||
|
||||
for (const serverUuid of serverUuids) { |
||||
try { |
||||
assert( |
||||
REP_UUID.test(serverUuid), |
||||
`Server UUID must be a valid UUIDv4; got [${serverUuid}]`, |
||||
); |
||||
|
||||
const rows: [[string]] = await query( |
||||
`SELECT server_host_uuid FROM servers WHERE server_uuid = '${serverUuid}';`, |
||||
); |
||||
|
||||
assert.ok(rows.length, `Server ${serverUuid} not found`); |
||||
|
||||
const [[serverHostUuid]] = rows; |
||||
|
||||
job({ |
||||
file: __filename, |
||||
job_command: `${SERVER_PATHS.usr.sbin['anvil-delete-server'].self}`, |
||||
job_data: `server_uuid=${serverUuid}`, |
||||
job_description: 'job_0209', |
||||
job_host_uuid: serverHostUuid, |
||||
job_name: 'server::delete', |
||||
job_title: 'job_0208', |
||||
}); |
||||
} catch (error) { |
||||
stderr(`Failed to initiate delete server ${serverUuid}; CAUSE: ${error}`); |
||||
|
||||
return response.status(500).send(); |
||||
} |
||||
} |
||||
|
||||
return response.status(204).send(); |
||||
}; |
@ -1,3 +1,4 @@ |
||||
export { createServer } from './createServer'; |
||||
export { getServer } from './getServer'; |
||||
export { getServerDetail } from './getServerDetail'; |
||||
export * from './createServer'; |
||||
export * from './deleteServer'; |
||||
export * from './getServer'; |
||||
export * from './getServerDetail'; |
||||
|
Loading…
Reference in new issue