From 8e64a9a1851bfe5fe2d1daea83ad1a6b6274c39d Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Thu, 12 May 2022 16:55:43 -0400 Subject: [PATCH] feat(striker-ui-api): add provision server endpoint --- .../request_handlers/server/createServer.ts | 60 +++++++++++++++++++ .../lib/request_handlers/server/getServer.ts | 4 +- .../src/lib/request_handlers/server/index.ts | 2 + striker-ui-api/src/routes/server.ts | 4 +- 4 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 striker-ui-api/src/lib/request_handlers/server/createServer.ts create mode 100644 striker-ui-api/src/lib/request_handlers/server/index.ts diff --git a/striker-ui-api/src/lib/request_handlers/server/createServer.ts b/striker-ui-api/src/lib/request_handlers/server/createServer.ts new file mode 100644 index 00000000..6ee9a196 --- /dev/null +++ b/striker-ui-api/src/lib/request_handlers/server/createServer.ts @@ -0,0 +1,60 @@ +import { RequestHandler } from 'express'; + +// import SERVER_PATHS from '../../consts/SERVER_PATHS'; + +import { sub } from '../../accessModule'; + +export const createServer: RequestHandler = ({ body }, response) => { + console.log('Creating server.'); + + if (body) { + const { + serverName, + cpuCores, + memory, + virtualDisks: [{ storageSize, storageGroupUUID }], + installISOFileUUID, + driverISOFileUUIDs, + anvilUUID, + optimizeForOS, + } = body; + + const provisionServerJobData = ` +server_name=${serverName} +os=${optimizeForOS} +cpu_cores=${cpuCores} +ram=${memory} +storage_group_uuid=${storageGroupUUID} +storage_size=${storageSize} +install_iso=${installISOFileUUID} +driver_iso=${driverISOFileUUIDs}`; + + console.log(`provisionServerJobData: ${provisionServerJobData}`); + + const { stdout: provisionServerJobHostUUID } = sub( + 'get_primary_host_uuid', + { + subModuleName: 'Cluster', + subParams: { anvil_uuid: anvilUUID }, + }, + ); + + console.log(`provisionServerJobHostUUID: [${provisionServerJobHostUUID}]`); + + // sub('insert_or_update_jobs', { + // subParams: { + // file: __filename, + // line: 0, + // job_command: SERVER_PATHS.usr.sbin['anvil-provision-server'].self, + // job_data: provisionServerJobData, + // job_name: 'server:provision', + // job_title: 'job_0147', + // job_description: 'job_0148', + // job_progress: 0, + // job_host_uuid: provisionServerJobHostUUID, + // }, + // }); + } + + response.status(202).send(); +}; diff --git a/striker-ui-api/src/lib/request_handlers/server/getServer.ts b/striker-ui-api/src/lib/request_handlers/server/getServer.ts index cdd70e5f..97daa881 100644 --- a/striker-ui-api/src/lib/request_handlers/server/getServer.ts +++ b/striker-ui-api/src/lib/request_handlers/server/getServer.ts @@ -1,7 +1,7 @@ import buildGetRequestHandler from '../buildGetRequestHandler'; import join from '../../join'; -const getServer = buildGetRequestHandler((request) => { +export const getServer = buildGetRequestHandler((request) => { const { anvilsUUID } = request.body; const condAnvilsUUID = join(anvilsUUID, { @@ -23,5 +23,3 @@ const getServer = buildGetRequestHandler((request) => { WHERE server_state != 'DELETED' ${condAnvilsUUID};`; }); - -export default getServer; diff --git a/striker-ui-api/src/lib/request_handlers/server/index.ts b/striker-ui-api/src/lib/request_handlers/server/index.ts new file mode 100644 index 00000000..28aabe98 --- /dev/null +++ b/striker-ui-api/src/lib/request_handlers/server/index.ts @@ -0,0 +1,2 @@ +export { createServer } from './createServer'; +export { getServer } from './getServer'; diff --git a/striker-ui-api/src/routes/server.ts b/striker-ui-api/src/routes/server.ts index 460cd9ec..45c12243 100644 --- a/striker-ui-api/src/routes/server.ts +++ b/striker-ui-api/src/routes/server.ts @@ -1,9 +1,9 @@ import express from 'express'; -import getServer from '../lib/request_handlers/server/getServer'; +import { createServer, getServer } from '../lib/request_handlers/server'; const router = express.Router(); -router.get('/', getServer); +router.get('/', getServer).post('/', createServer); export default router;