Skip to content

Commit

Permalink
Add multiple improvements, refactor busboy, needs more improvement in…
Browse files Browse the repository at this point in the history
… the deploy create and worker.
  • Loading branch information
viferga committed Apr 10, 2024
1 parent 72802cb commit b071085
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 144 deletions.
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@
"rules": {
"tsdoc/syntax": "warn",
"no-unused-vars": "off",
"@typescript-eslint/naming-convention": [
"error",
{
"selector": "property",
"format": [
"camelCase"
]
}
],
"@typescript-eslint/no-unused-vars": [
"error",
{
Expand Down Expand Up @@ -101,4 +110,4 @@
"prettier": "^2.1.2",
"typescript": "^4.3.2"
}
}
}
73 changes: 30 additions & 43 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ import deployDeleteController from './controller/delete';
import uploadController from './controller/upload';

import {
CurrentUploadedFile,
DeleteBody,
DeployBody,
Deployment,
FetchBranchListBody,
FetchFilesFromRepoBody,
ProtocolMessageType,
WorkerMessage,
WorkerMessageUnknown,
allApplications,
childProcesses,
deleteBody,
deployBody,
fetchBranchListBody,
fetchFilesFromRepoBody
deploymentMap
} from './constants';

import AppError from './utils/appError';
Expand Down Expand Up @@ -132,7 +133,7 @@ export const fetchFiles = (

export const fetchFilesFromRepo = catchAsync(
async (
req: Omit<Request, 'body'> & { body: fetchFilesFromRepoBody },
req: Omit<Request, 'body'> & { body: FetchFilesFromRepoBody },
res: Response,
next: NextFunction
) => {
Expand All @@ -158,16 +159,16 @@ export const fetchFilesFromRepo = catchAsync(
const id = dirName(req.body.url);

// TODO: This method is wrong
// currentFile['id'] = id;
// currentFile.path = `${appsDir}/${id}`;
// deployment.id = id;
// deployment.path = `${appsDir}/${id}`;

return res.status(201).send({ id });
}
);

export const fetchBranchList = catchAsync(
async (
req: Omit<Request, 'body'> & { body: fetchBranchListBody },
req: Omit<Request, 'body'> & { body: FetchBranchListBody },
res: Response
) => {
const { stdout } = await execPromise(
Expand All @@ -190,7 +191,7 @@ export const fetchBranchList = catchAsync(

export const fetchFileList = catchAsync(
async (
req: Omit<Request, 'body'> & { body: fetchFilesFromRepoBody },
req: Omit<Request, 'body'> & { body: FetchFilesFromRepoBody },
res: Response,
next: NextFunction
) => {
Expand Down Expand Up @@ -224,44 +225,45 @@ export const fetchFileList = catchAsync(

export const deploy = catchAsync(
async (
req: Omit<Request, 'body'> & { body: deployBody },
req: Omit<Request, 'body'> & { body: DeployBody },
res: Response,
next: NextFunction
) => {
try {
// TODO Currently Deploy function will only work for workdir, we will add the addRepo
// TODO: Implement repository
// req.body.resourceType == 'Repository' &&
// (await calculatePackages(next));

console.log(req.body);
const deployment = deploymentMap[req.body.suffix];

const currentFile: CurrentUploadedFile = {
id: '',
type: '',
path: '',
jsons: []
};
if (deployment === undefined) {
return next(
new AppError(
`Invalid deployment id: ${req.body.suffix}`,
400
)
);
}

await installDependencies(currentFile);
await installDependencies(deployment);

const desiredPath = path.join(__dirname, '/worker/index.js');
const desiredPath = path.join(__dirname, 'worker', 'index.js');

const proc = spawn('metacall', [desiredPath], {
stdio: ['pipe', 'pipe', 'pipe', 'ipc']
});

proc.send({
type: ProtocolMessageType.Load,
data: currentFile
data: deployment
});

logProcessOutput(proc.stdout, proc.pid, currentFile.id);
logProcessOutput(proc.stderr, proc.pid, currentFile.id);
logProcessOutput(proc.stdout, proc.pid, deployment.id);
logProcessOutput(proc.stderr, proc.pid, deployment.id);

proc.on('message', (payload: WorkerMessageUnknown) => {
if (payload.type === ProtocolMessageType.MetaData) {
const message =
payload as WorkerMessage<CurrentUploadedFile>;
const message = payload as WorkerMessage<Deployment>;
if (isIAllApps(message.data)) {
const appName = Object.keys(message.data)[0];
childProcesses[appName] = proc;
Expand All @@ -272,7 +274,7 @@ export const deploy = catchAsync(

return res.status(200).json({
suffix: hostname(),
prefix: currentFile.id,
prefix: deployment.id,
version: 'v1'
});
} catch (err) {
Expand All @@ -290,7 +292,7 @@ export const showLogs = (req: Request, res: Response): Response => {
};

export const deployDelete = (
req: Omit<Request, 'body'> & { body: deleteBody },
req: Omit<Request, 'body'> & { body: DeleteBody },
res: Response,
next: NextFunction
): void => deployDeleteController(req, res, next);
Expand All @@ -303,18 +305,3 @@ export const validateAndDeployEnabled = (
status: 'success',
data: true
});

/**
* deploy
* Provide a mesage that repo has been deployed, use --inspect to know more about deployment
* We can add the type of url in the --inspect
* If there is already metacall.json present then, log found metacall.json and reading it, reading done
* We must an option to go back in the fileselection wizard so that, user dont have to close the connection
* At the end of deployment through deploy cli, we should run the --inspect command so that current deployed file is shown and show only the current deployed app
*
*
* 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 we are uploading
*
*/
24 changes: 12 additions & 12 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { DeployStatus, MetaCallJSON } from '@metacall/protocol/deployment';
import { ChildProcess } from 'child_process';

export interface CurrentUploadedFile {
export interface Deployment {
id: string;
type?: string;
jsons: MetaCallJSON[];
runners?: string[];
path: string;
blob?: string;
}

export const deploymentMap: Record<string, Deployment> = {};

export const createInstallDependenciesScript = (
runner: string,
path: string
Expand All @@ -22,28 +25,25 @@ export const createInstallDependenciesScript = (
return installDependenciesScript[runner];
};

export type namearg = 'id' | 'type' | 'jsons' | 'runners' | 'path';
export type valueArg = string;

export type fetchFilesFromRepoBody = {
export type FetchFilesFromRepoBody = {
branch: 'string';
url: 'string';
};
export type fetchBranchListBody = {
export type FetchBranchListBody = {
url: 'string';
};

export type deployBody = {
suffix: string; //name of deployment
export type DeployBody = {
suffix: string; // name of deployment
resourceType: 'Package' | 'Repository';
release: string; //release date
release: string; // release date
env: string[];
plan: string;
version: string;
};

export type deleteBody = {
suffix: string; //name of deployment
export type DeleteBody = {
suffix: string; // name of deployment
prefix: string;
version: string;
};
Expand Down Expand Up @@ -118,7 +118,7 @@ export interface LogMessage {
message: string;
}

export const asniCode: number[] = [
export const ANSICode: number[] = [
166, 154, 142, 118, 203, 202, 190, 215, 214, 32, 6, 4, 220, 208, 184, 172
];

Expand Down
4 changes: 2 additions & 2 deletions src/controller/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { join } from 'path';

import { NextFunction, Request, Response } from 'express';

import { allApplications, childProcesses, deleteBody } from '../constants';
import { allApplications, childProcesses, DeleteBody } from '../constants';
import { appsDirectory } from '../utils/config';
import { deleteStatusMessage } from '../utils/responseTexts';
import { catchAsync, ensureFolderExists } from '../utils/utils';

export default catchAsync(
async (
req: Omit<Request, 'body'> & { body: deleteBody },
req: Omit<Request, 'body'> & { body: DeleteBody },
res: Response,
_next: NextFunction
): Promise<Response> => {
Expand Down
Loading

0 comments on commit b071085

Please sign in to comment.