File size: 18,806 Bytes
4e83772 fc50dd8 4e83772 446bc13 4e83772 c6260d1 4e83772 fc50dd8 4e83772 446bc13 4e83772 fc50dd8 4e83772 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 | import { Types } from "mongoose";
import { RunTaskCommand } from "@aws-sdk/client-ecs";
import { _Object, DeleteObjectsCommand, ListObjectsV2Command } from "@aws-sdk/client-s3";
import { spawn } from "child_process";
import { generateSlug } from "random-word-slugs";
import { IDeploymentService } from "@/interfaces/service/IDeploymentService.js";
import { IDeploymentRepository } from "@/interfaces/repository/IDeploymentRepository.js";
import { IProjectRepository } from "@/interfaces/repository/IProjectRepository.js";
import { IUserSerivce } from "@/interfaces/service/IUserService.js";
import { IRedisCache } from "@/interfaces/cache/IRedisCache.js";
import { ILogsService } from "@/interfaces/service/ILogsService.js";
import { DeploymentStatus, DeploymentTriggers, IDeployment } from "@/models/Deployment.js";
import AppError from "@/utils/AppError.js";
import { IProject, ProjectProvider, ProjectStatus } from "@/models/Projects.js";
import { nanoid } from "@/utils/generateNanoid.js";
import { DEPLOYMENT_ID_LENGTH } from "@/constants/subdomain.js";
import { QueryDeploymentDTO } from "@/dtos/deployment.dto.js";
import { deploymentBasicFields } from "@/constants/populates/deployment.populate.js";
import getNessesaryEnvs from "@/utils/getNessesaryEnvs.js";
import dispatchRequestService from "@/utils/dispatchRequest.js";
import { config, ecsClient, s3Client } from "@/config/cloud.config.js";
import { ENVS } from "@/config/env.config.js";
import { S3_OUTPUTS_DIR } from "@/constants/paths.js";
import { STATUS_CODES } from "@/utils/statusCodes.js";
import { DEPLOYMENT_ERRORS, PROJECT_ERRORS, USER_ERRORS } from "@/constants/errors.js";
import { PLANS } from "@/constants/plan.js";
import { IUser } from "@/models/User.js";
import { IGithubService } from "@/interfaces/service/IGithubService.js";
class DeploymentService implements IDeploymentService {
private deploymentRepository: IDeploymentRepository;
private projectRepository: IProjectRepository;
private userService: IUserSerivce;
private MAX_CONCURRENT_RUNNABLE_DPYMNTS = 20;
private DEPLOYMENTS_SET_KEY = "deployments:running";
private redisCache: IRedisCache;
private logsService: ILogsService;
private githubSvcs: IGithubService;
private USER_CONCURRENCY_KEY_PREFIX = "build:cncrncy";
private USER_CONCURRENCY_TTL_SECONDS = 60 * 20;
constructor(
deploymentRepo: IDeploymentRepository,
projectRepo: IProjectRepository,
userService: IUserSerivce,
logsService: ILogsService,
redisCache: IRedisCache,
githubSvcs: IGithubService,
) {
this.deploymentRepository = deploymentRepo;
this.projectRepository = projectRepo;
this.userService = userService;
this.logsService = logsService;
this.redisCache = redisCache;
this.githubSvcs = githubSvcs;
}
async newDeployment(deploymentData: Partial<IDeployment>, userId: string, projectId: string, isRedeploy: boolean): Promise<IDeployment | null> {
const [canDeploy, problems] = await Promise.all([
this.userService.userCanDeploy(userId),
this.redisCache.get<{ affectedModules: string[] }>("server-notifications"),
]);
if (problems?.affectedModules?.includes("deployments")) {
throw new AppError(DEPLOYMENT_ERRORS.DEPLOY_FAILED + "; Deployments are currently disabled", STATUS_CODES.BAD_REQUEST);
}
if (!canDeploy.user) {
throw new AppError(USER_ERRORS.NOT_FOUND, STATUS_CODES.NOT_FOUND);
}
if (!canDeploy.allowed) {
throw new AppError(DEPLOYMENT_ERRORS.DAILY_DEPLOYMENT_LIMIT, STATUS_CODES.TOO_MANY_REQUESTS);
}
const correspondindProject = await this.projectRepository.findProject(projectId, userId);
if (!correspondindProject) {
throw new AppError(PROJECT_ERRORS.NOT_FOUND, STATUS_CODES.NOT_FOUND);
}
if (correspondindProject.status === ProjectStatus.BUILDING) {
throw new AppError(PROJECT_ERRORS.PROJECT_IN_PROGRESS, STATUS_CODES.CONFLICT);
}
if (correspondindProject.status === ProjectStatus.QUEUED) {
throw new AppError(PROJECT_ERRORS.PROJECT_IN_PROGRESS, STATUS_CODES.CONFLICT);
}
if (correspondindProject.tempDeployment !== null || correspondindProject.tempDeployment) {
throw new AppError(PROJECT_ERRORS.PROJECT_IN_PROGRESS, STATUS_CODES.CONFLICT);
}
if (correspondindProject.isDeleted) {
throw new AppError(PROJECT_ERRORS.NOT_FOUND, 404);
}
if (correspondindProject.isDisabled) {
throw new AppError(PROJECT_ERRORS.DISABLED, 400);
}
deploymentData.status = DeploymentStatus.QUEUED;
deploymentData.overWrite = false;
deploymentData.commit_hash = "----||----";
deploymentData.publicId = nanoid(DEPLOYMENT_ID_LENGTH);
deploymentData.identifierSlug = generateSlug(3);
deploymentData.project = new Types.ObjectId(correspondindProject._id);
deploymentData.user = correspondindProject.user;
deploymentData.branch = correspondindProject.branch;
deploymentData.triggerEvent = isRedeploy ? DeploymentTriggers.REDEPLOY : DeploymentTriggers.MANUAL;
await this.incrementRunningDeplymnts(correspondindProject._id, canDeploy.user._id, canDeploy.user.plan);
try {
const deployment = await this.deploymentRepository.createDeployment(deploymentData);
await Promise.all([
this.projectRepository.pushToDeployments(correspondindProject.id, userId, deployment?.id),
this.userService.incrementDeployment(correspondindProject.user.toString()),
]);
let token = undefined;
if (correspondindProject.isPrivateGhRepo || correspondindProject.provider === ProjectProvider.GITHUB) {
token = await this.githubSvcs.createOrGetInstallationAcsTokn(canDeploy.user.githubInstallationId as number);
}
if (deployment?._id) {
await this.deployLocal(deployment._id, projectId, token);
}
return deployment;
} catch (error) {
await this.decrementRunningDeplymnts(projectId, canDeploy.user._id);
throw error;
}
}
async createNewFailedDeployment(deployData: Partial<IDeployment>, project: IProject, reason: string): Promise<IDeployment | null> {
deployData.timings = { build_ms: 0, upload_ms: 0, duration_ms: 1000, install_ms: 0 };
deployData.complete_at = new Date(Date.now() + 2000);
deployData.error_message = reason;
deployData.status = DeploymentStatus.CANCELED;
const deployment = await this.deploymentRepository.createDeployment(deployData);
await this.projectRepository.pushToDeployments(deployData.id, project.user.toString(), deployment?.id);
return deployment;
}
async newPushDeployment(
deploymentData: Partial<IDeployment>,
project: IProject,
installationId?: number,
): Promise<{ status: string; reason: string }> {
const [canDeploy, problems] = await Promise.all([
this.userService.userCanDeploy(project.user.toString()),
this.redisCache.get<{ affectedModules: string[] }>("server-notifications"),
]);
const correspondindProject = project;
if (installationId && canDeploy.user.githubInstallationId && canDeploy.user.githubInstallationId !== installationId) {
return { status: "ignored", reason: "installation mismatch" };
}
deploymentData.status = DeploymentStatus.QUEUED;
deploymentData.overWrite = false;
deploymentData.publicId = nanoid(DEPLOYMENT_ID_LENGTH);
deploymentData.identifierSlug = generateSlug(3);
deploymentData.project = new Types.ObjectId(correspondindProject._id);
deploymentData.user = correspondindProject.user;
if (problems?.affectedModules?.includes("github") || problems?.affectedModules?.includes("deployments")) {
await this.createNewFailedDeployment(deploymentData, project, DEPLOYMENT_ERRORS.DEPLOY_FAILED + "; Deployments are currently disabled");
return { status: "cancelled", reason: DEPLOYMENT_ERRORS.CREATE_FAILED };
}
if (!canDeploy.allowed) {
await this.createNewFailedDeployment(deploymentData, project, "User daily deploy limit reached.");
return { status: "cancelled", reason: DEPLOYMENT_ERRORS.DAILY_DEPLOYMENT_LIMIT };
}
if (correspondindProject.status === ProjectStatus.BUILDING || correspondindProject.status === ProjectStatus.QUEUED) {
await this.createNewFailedDeployment(deploymentData, project, "Deployment cancelled; Project already in progress state.");
return { status: "cancelled", reason: PROJECT_ERRORS.PROJECT_IN_PROGRESS };
}
if (correspondindProject.tempDeployment !== null || correspondindProject.tempDeployment) {
await this.createNewFailedDeployment(deploymentData, project, "Deployment cancelled; Project already in progress state.");
return { status: "cancelled", reason: PROJECT_ERRORS.PROJECT_IN_PROGRESS };
}
try {
await this.incrementRunningDeplymnts(correspondindProject._id, canDeploy.user._id, canDeploy.user.plan);
} catch (error) {
if (error instanceof AppError) {
await this.createNewFailedDeployment(deploymentData, project, error.message);
return { status: "cancelled", reason: error.message };
}
}
try {
const deployment = await this.deploymentRepository.createDeployment(deploymentData);
await Promise.all([
this.projectRepository.pushToDeployments(correspondindProject.id, correspondindProject.user.toString(), deployment?.id),
this.userService.incrementDeployment(correspondindProject.user.toString()),
]);
let token = undefined;
if (project.isPrivateGhRepo || project.provider === ProjectProvider.GITHUB) {
token = await this.githubSvcs.createOrGetInstallationAcsTokn(canDeploy.user.githubInstallationId as number);
}
if (deployment?._id) {
await this.deployLocal(deployment._id, project._id.toString(), token);
}
return { status: "deploy started", reason: "" };
} catch (error) {
await this.decrementRunningDeplymnts(project._id.toString(), canDeploy.user._id);
return { status: "deploy error", reason: (error as any).message };
}
}
async getDeploymentById(id: string, userId: string, includes?: string): Promise<IDeployment | null> {
return await this.deploymentRepository.findDeploymentById(id, userId, { includes, exclude: ["file_structure"] });
}
async getDeploymentFiles(id: string, userId: string, includes?: string): Promise<IDeployment | null> {
return await this.deploymentRepository.findDeploymentById(id, userId, {
fields: ["file_structure"],
});
}
async getAllDeployments(userId: string, query: QueryDeploymentDTO): Promise<{ deployments: IDeployment[]; total: number }> {
return await this.deploymentRepository.findAllDeployments(userId, query, { fields: !query.full ? deploymentBasicFields : [] });
}
async getProjectDeployments(
userId: string,
projectId: string,
query: QueryDeploymentDTO,
): Promise<{ deployments: IDeployment[]; total: number }> {
const correspondindProject = await this.projectRepository.findProject(projectId, userId);
if (!correspondindProject) {
throw new AppError(PROJECT_ERRORS.NOT_FOUND, STATUS_CODES.NOT_FOUND);
}
return await this.deploymentRepository.findProjectDeployments(userId, projectId, query, { fields: !query.full ? deploymentBasicFields : [] });
}
async deleteDeployment(projectId: string, deploymentId: string, userId: string): Promise<number> {
const project = await this.projectRepository.findProject(projectId, userId, { deletedToo: true });
if (!project) throw new AppError(PROJECT_ERRORS.NOT_FOUND, STATUS_CODES.NOT_FOUND);
if (project.status === ProjectStatus.BUILDING || project.status === ProjectStatus.QUEUED) {
throw new AppError(PROJECT_ERRORS.PROJECT_IN_PROGRESS, STATUS_CODES.CONFLICT);
}
if (!project.deployments?.includes(deploymentId as any)) {
const deployment = await this.deploymentRepository.__findDeployment(deploymentId);
if (!deployment || deployment.project.toString() !== project._id.toString()) {
throw new AppError(DEPLOYMENT_ERRORS.NOT_FOUND, STATUS_CODES.NOT_FOUND);
}
}
let newCurrentDeployment = null;
if (project.currentDeployment === deploymentId) {
const allDeployments = await this.deploymentRepository.__findAllProjectDeployment(projectId, "createdAt");
const currentIndex = allDeployments.findIndex((d) => d._id.toString() === deploymentId);
// console.log(allDeployments, "<<<<", currentIndex);
if (currentIndex > 0) {
newCurrentDeployment = allDeployments[currentIndex - 1];
}
}
const nextStatus = newCurrentDeployment
? newCurrentDeployment.status
: deploymentId === project.currentDeployment
? ProjectStatus.NOT_STARTED
: project.status;
const [_, deleteResult] = await Promise.all([
this.projectRepository.pullDeployments(
projectId,
userId,
deploymentId,
newCurrentDeployment ? newCurrentDeployment._id : deploymentId === project.currentDeployment ? null : project.currentDeployment,
),
this.deploymentRepository.deleteDeployment(projectId, deploymentId, userId),
this.deleteCloud(deploymentId, project._id),
this.logsService.deleteDeploymentLogs(deploymentId),
...(project.status !== nextStatus ? [this.projectRepository.__updateProject(project._id, { status: nextStatus as ProjectStatus })] : []),
]);
return deleteResult;
}
async deployLocal(deploymentId: string, projectId: string, token?: string): Promise<void> {
try {
// const envs = getNessesaryEnvs();
// const command = spawn("node", ["script.js"], {
// cwd: "../build-server/",
// env: {
// ...process.env,
// DEPLOYMENT_ID: deploymentId,
// PROJECT_ID: projectId,
// },
// })
// command.stdout?.on("data", (data) => {
// console.log(`[stdout]: -----data-----from----deployLocal`);
// });
// command.stderr?.on("data", (data) => {
// console.error(`[stderr]: ${data.toString().trim()}`);
// });
// command.on("exit", (code) => {
// console.log(`Process exited with code ${code}`);
// });
// command.on("error", (err) => {
// console.error("Failed to start process:", err);
// });
// return
const result = await dispatchRequestService.dispatchBuild(deploymentId, projectId, token);
console.log(result, " - - - ");
} catch (error: any) {
console.log("Error on build");
await Promise.all([
this.__updateDeployment(projectId, deploymentId, {
status: DeploymentStatus.FAILED,
error_message: DEPLOYMENT_ERRORS.DEPLOY_FAILED,
}),
this.projectRepository.__updateProject(projectId, {
status: ProjectStatus.FAILED,
tempDeployment: null,
}),
]);
throw error;
}
}
async deployCloud(project: IProject, deployment: IDeployment, token?: string): Promise<void> {
try {
// currently using deploy local without aws ecs; incomplete code
const command = new RunTaskCommand({
cluster: config.CLUSTER,
taskDefinition: config.TASK,
launchType: "FARGATE",
count: 1,
networkConfiguration: {
awsvpcConfiguration: {
subnets: ENVS.SUBNETS_STRING?.split(","),
securityGroups: ENVS.SECURITY_GROUPS?.split(","),
assignPublicIp: "ENABLED",
},
},
overrides: {
containerOverrides: [
{
name: ENVS.CONTAINER_NAME, // docker image after build,
environment: [
...getNessesaryEnvs(),
{ name: "SERVER_PUSHED_D_ID", value: deployment._id },
{ name: "SERVER_PUSHED_P_ID", value: project.id },
],
},
],
},
});
await ecsClient.send(command);
} catch (error: any) {
await Promise.all([
this.__updateDeployment(project._id, deployment._id, {
status: DeploymentStatus.FAILED,
error_message: DEPLOYMENT_ERRORS.DEPLOY_FAILED,
}),
this.projectRepository.__updateProject(project._id, {
status: ProjectStatus.FAILED,
tempDeployment: null,
}),
]);
throw error;
}
}
async deleteLocal(deploymentId: string, projectId: string): Promise<void> {
// Make a delete files api call to the mock s3 server.
}
async deleteCloud(deploymentId: string, projectId: string): Promise<void> {
const prefix = `${S3_OUTPUTS_DIR}/${projectId}/${deploymentId}/`;
const APP_FILES_BUCKET = ENVS.CLOUD_BUCKET;
const listCommand = new ListObjectsV2Command({
Bucket: APP_FILES_BUCKET,
Prefix: prefix,
});
const listed = await s3Client.send(listCommand);
if (!listed.Contents || listed.Contents.length === 0) {
return;
}
const deleteCommand = new DeleteObjectsCommand({
Bucket: APP_FILES_BUCKET,
Delete: {
Objects: listed.Contents.map((obj: _Object) => ({ Key: obj.Key })),
},
});
await s3Client.send(deleteCommand);
}
async deleteCloudDeploysMultiple(projectId: string): Promise<void> {
const prefix = `${S3_OUTPUTS_DIR}/${projectId}/`;
const APP_FILES_BUCKET = ENVS.CLOUD_BUCKET;
const listCommand = new ListObjectsV2Command({
Bucket: APP_FILES_BUCKET,
Prefix: prefix,
});
const listed = await s3Client.send(listCommand);
if (!listed.Contents || listed.Contents.length === 0) {
return;
}
const deleteCommand = new DeleteObjectsCommand({
Bucket: APP_FILES_BUCKET,
Delete: {
Objects: listed.Contents.map((obj: _Object) => ({ Key: obj.Key })),
},
});
await s3Client.send(deleteCommand);
}
async incrementRunningDeplymnts(projectId: string, userId: string, userPlan: string): Promise<void> {
await this.redisCache.setAdd(this.DEPLOYMENTS_SET_KEY, projectId);
const currentRunningDpymnts = await this.redisCache.getSetLength(this.DEPLOYMENTS_SET_KEY);
if (currentRunningDpymnts > this.MAX_CONCURRENT_RUNNABLE_DPYMNTS) {
await this.redisCache.setRemove(this.DEPLOYMENTS_SET_KEY, projectId);
throw new AppError(DEPLOYMENT_ERRORS.BUSY_RUNNERS, STATUS_CODES.SERVICE_UNAVAILABLE);
}
const key = `${this.USER_CONCURRENCY_KEY_PREFIX}:${userId}`;
const current = await this.redisCache.incrementKey(key);
if (current === 1) {
await this.redisCache.setKeyExpiry(key, this.USER_CONCURRENCY_TTL_SECONDS);
}
if (Number(current) > PLANS[userPlan as IUser["plan"]].concurrentBuilds) {
await this.redisCache.decrementKey(key);
throw new AppError(DEPLOYMENT_ERRORS.CONCURRENT_LIMIT, STATUS_CODES.SERVICE_UNAVAILABLE);
}
}
async decrementRunningDeplymnts(projectId: string, userId?: string): Promise<void> {
let project = null;
if (!userId) {
project = await this.projectRepository.__findProject(projectId);
}
const fetchedUserId = project?.user.toString();
await Promise.all([
this.redisCache.setRemove(this.DEPLOYMENTS_SET_KEY, projectId),
...(userId
? [this.redisCache.decrementKey(`${this.USER_CONCURRENCY_KEY_PREFIX}:${userId}`)]
: [this.redisCache.decrementKey(`${this.USER_CONCURRENCY_KEY_PREFIX}:${fetchedUserId}`)]),
]);
}
async __getDeploymentById(id: string): Promise<IDeployment | null> {
//container
return this.deploymentRepository.__findDeployment(id);
}
async __updateDeployment(projectId: string, deploymentId: string, updateData: Partial<IDeployment>): Promise<IDeployment | null> {
return await this.deploymentRepository.__updateDeployment(projectId, deploymentId, updateData);
}
}
export default DeploymentService;
|