From 7ae85a2760501075a27d4aa7272b09fc66db4caf Mon Sep 17 00:00:00 2001 From: aakritiawasthi Date: Tue, 12 Mar 2024 02:49:13 +0530 Subject: [PATCH] deployed application delete --- src/api.ts | 21 +++++++++++++++--- src/app.ts | 2 ++ src/constants.ts | 6 +++++ src/controller/delete.ts | 46 +++++++++++++++++++++++++++++++++++++++ src/utils/resposeTexts.ts | 9 ++++++++ 5 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 src/controller/delete.ts create mode 100644 src/utils/resposeTexts.ts diff --git a/src/api.ts b/src/api.ts index f902d0d..08f9da8 100644 --- a/src/api.ts +++ b/src/api.ts @@ -4,13 +4,15 @@ import { NextFunction, Request, Response } from 'express'; import { hostname } from 'os'; import * as path from 'path'; -import upload from './controller/upload'; +import deployDeleteController from './controller/delete'; +import uploadController from './controller/upload'; import { allApplications, childProcessResponse, cps, currentFile, + deleteBody, deployBody, fetchBranchListBody, fetchFilesFromRepoBody, @@ -52,6 +54,14 @@ export const callFnByName = ( const { appName: app, name } = req.params; const args = Object.values(req.body); + if (!(app in cps)) { + return res + .status(404) + .send( + `Oops! It looks like the application (${app}) hasn't been deployed yet. Please deploy it before you can call its functions.` + ); + } + let responseSent = false; // Flag to track if response has been sent let errorCame = false; @@ -109,7 +119,7 @@ export const fetchFiles = ( req: Request, res: Response, next: NextFunction -): void => upload(req, res, next); +): void => uploadController(req, res, next); export const fetchFilesFromRepo = catchAsync( async ( @@ -253,6 +263,11 @@ export const showLogs = (req: Request, res: Response): Response => { return res.send('Demo Logs...'); }; +export const deployDelete = ( + req: Omit & { body: deleteBody }, + res: Response +): Response => deployDeleteController(req, res); + export const validateAndDeployEnabled = ( req: Request, res: Response @@ -273,6 +288,6 @@ export const validateAndDeployEnabled = ( * * FAAS * the apps are not getting detected once the server closes, do we need to again deploy them - * find a way to detect metacall.json in the files and dont deploy if there is not because json ke through hi hum upload krre hai + * find a way to detect metacall.json in the files and dont deploy if there is not because json ke through we are uploading * */ diff --git a/src/app.ts b/src/app.ts index 0166666..c40d83e 100644 --- a/src/app.ts +++ b/src/app.ts @@ -35,6 +35,8 @@ app.get('/api/inspect', (req, res) => { res.send(Object.values(allApplications)); }); +app.post('/api/deploy/delete', api.deployDelete); + // For all the additional unimplemented routes app.all('*', (req: Request, res: Response, next: NextFunction) => { next(new AppError(`Can't find ${req.originalUrl} on this server!`, 404)); diff --git a/src/constants.ts b/src/constants.ts index 5b8008a..02b0d0c 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -50,6 +50,12 @@ export type deployBody = { version: string; }; +export type deleteBody = { + suffix: string; //name of deployment + prefix: string; + version: string; +}; + export type tpackages = Record; export interface IApp { diff --git a/src/controller/delete.ts b/src/controller/delete.ts new file mode 100644 index 0000000..07ea895 --- /dev/null +++ b/src/controller/delete.ts @@ -0,0 +1,46 @@ +import { ChildProcess } from 'child_process'; +import { rmSync } from 'fs'; +import { join } from 'path'; + +import { Request, Response } from 'express'; + +import { allApplications, cps, deleteBody } from '../constants'; +import { appsDirectory } from '../utils/config'; +import { deleteStatusMessage } from '../utils/resposeTexts'; + +export default ( + req: Omit & { body: deleteBody }, + res: Response +) => { + // Extract the suffix (application name) of the application from the request body + const { suffix: app } = req.body; + + // Initialize isError flag + let isError = false; + + // Check if the application exists in cps and allApplications objects + if (!(app in cps && app in allApplications)) { + isError = true; + } + + // Retrieve the child process associated with the application and kill it + const appCP: ChildProcess = cps[app]; + appCP.kill(); + + // Remove the application from cps and allApplications objects + delete cps[app]; + delete allApplications[app]; + + // Determine the location of the application + const appLocation = join(appsDirectory(), app); + + // Delete the directory of the application + rmSync(appLocation, { recursive: true, force: true }); + + // Send response based on whether there was an error during deletion + return res.send( + isError + ? deleteStatusMessage(app)['error'] + : deleteStatusMessage(app)['success'] + ); +}; diff --git a/src/utils/resposeTexts.ts b/src/utils/resposeTexts.ts new file mode 100644 index 0000000..f68ad00 --- /dev/null +++ b/src/utils/resposeTexts.ts @@ -0,0 +1,9 @@ +export const deleteStatusMessage = ( + app: string +): { + success: string; + error: string; +} => ({ + success: 'Deploy Delete Succeed', + error: `Oops! It looks like the application (${app}) hasn't been deployed yet. Please deploy it before you delete it.` +});