Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: deployed application delete #36

Merged
merged 1 commit into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -253,6 +263,11 @@ export const showLogs = (req: Request, res: Response): Response => {
return res.send('Demo Logs...');
};

export const deployDelete = (
req: Omit<Request, 'body'> & { body: deleteBody },
res: Response
): Response => deployDeleteController(req, res);

export const validateAndDeployEnabled = (
req: Request,
res: Response
Expand All @@ -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
*
*/
2 changes: 2 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
6 changes: 6 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown>;

export interface IApp {
Expand Down
46 changes: 46 additions & 0 deletions src/controller/delete.ts
Original file line number Diff line number Diff line change
@@ -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 (

Check warning on line 11 in src/controller/delete.ts

View workflow job for this annotation

GitHub Actions / ci

Missing return type on function

Check warning on line 11 in src/controller/delete.ts

View workflow job for this annotation

GitHub Actions / ci

Missing return type on function

Check warning on line 11 in src/controller/delete.ts

View workflow job for this annotation

GitHub Actions / ci

Missing return type on function

Check warning on line 11 in src/controller/delete.ts

View workflow job for this annotation

GitHub Actions / ci

Missing return type on function
req: Omit<Request, 'body'> & { 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']
);
};
9 changes: 9 additions & 0 deletions src/utils/resposeTexts.ts
Original file line number Diff line number Diff line change
@@ -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.`
});
Loading