@topgroup/diginext
Version:
A BUILD SERVER & CLI to deploy apps to any Kubernetes clusters.
157 lines (156 loc) • 7.94 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BuildService = void 0;
const path_1 = __importDefault(require("path"));
const app_config_1 = require("../app.config");
const const_1 = require("../config/const");
const Build_1 = require("../entities/Build");
const generate_build_tag_1 = require("../modules/build/generate-build-tag");
const check_quota_1 = require("../modules/workspace/check-quota");
const mongodb_1 = require("../plugins/mongodb");
const AppService_1 = require("./AppService");
const BaseService_1 = __importDefault(require("./BaseService"));
const ContainerRegistryService_1 = require("./ContainerRegistryService");
const WorkspaceService_1 = require("./WorkspaceService");
class BuildService extends BaseService_1.default {
constructor(ownership) {
super(Build_1.buildSchema, ownership);
this.regSvc = new ContainerRegistryService_1.ContainerRegistryService(ownership);
}
async find(filter, options, pagination) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
if (((_c = (_b = (_a = this.user) === null || _a === void 0 ? void 0 : _a.allowAccess) === null || _b === void 0 ? void 0 : _b.projects) === null || _c === void 0 ? void 0 : _c.length) > 0) {
if (filter.$or) {
filter.$or.push({ project: { $in: (_e = (_d = this.user) === null || _d === void 0 ? void 0 : _d.allowAccess) === null || _e === void 0 ? void 0 : _e.projects } });
}
else {
filter = { $or: [filter, { project: { $in: (_g = (_f = this.user) === null || _f === void 0 ? void 0 : _f.allowAccess) === null || _g === void 0 ? void 0 : _g.projects } }] };
}
}
if (((_k = (_j = (_h = this.user) === null || _h === void 0 ? void 0 : _h.allowAccess) === null || _j === void 0 ? void 0 : _j.apps) === null || _k === void 0 ? void 0 : _k.length) > 0) {
if (filter.$or) {
filter.$or.push({ app: { $in: (_m = (_l = this.user) === null || _l === void 0 ? void 0 : _l.allowAccess) === null || _m === void 0 ? void 0 : _m.apps } });
}
else {
filter = { $or: [filter, { project: { $in: (_p = (_o = this.user) === null || _o === void 0 ? void 0 : _o.allowAccess) === null || _p === void 0 ? void 0 : _p.apps } }] };
}
}
// options.isDebugging = true;
return super.find(filter, options, pagination);
}
async findOne(filter, options) {
const [item] = await this.find(filter, options, { limit: 1 });
if (!item)
throw new Error(`Item not found.`);
return item;
}
async startBuild(data, ownership) {
// validates
const { appSlug, user, userId, gitBranch, registrySlug } = data;
if (!appSlug)
throw new Error(`App slug is required.`);
if (!user && !userId)
throw new Error(`User or UserID is required.`);
if (!gitBranch)
throw new Error(`Git branch is required.`);
if (!registrySlug)
throw new Error(`Container registry slug is required.`);
if (ownership.owner)
data.user = ownership.owner;
// get workspace & check quotas
const appSvc = new AppService_1.AppService(ownership || this.ownership);
const app = await appSvc.findOne({ slug: data.appSlug });
if (!app)
throw new Error(`App not found.`);
const wsId = app.workspace ? (mongodb_1.MongoDB.isValidObjectId(app.workspace) ? app.workspace : app.workspace._id) : undefined;
if (!wsId)
throw new Error(`Workspace ID is not valid.`);
const wsSvc = new WorkspaceService_1.WorkspaceService();
const workspace = this.workspace || ownership.workspace || (await wsSvc.findOne({ _id: wsId }));
if (!workspace)
throw new Error(`Workspace not found.`);
// check dx quota
const quotaRes = await (0, check_quota_1.checkQuota)(workspace);
if (!quotaRes.status)
throw new Error(quotaRes.messages.join(". "));
if (quotaRes.data && quotaRes.data.isExceed)
throw new Error(`You've exceeded the limit amount of concurrent builds.`);
// default values
// app build directory
if (!data.buildTag) {
const { projectSlug } = app;
const SOURCE_CODE_DIR = `cache/${projectSlug}/${appSlug}/${gitBranch}`;
const buildDir = path_1.default.resolve(const_1.CLI_CONFIG_DIR, SOURCE_CODE_DIR);
const tagInfo = await (0, generate_build_tag_1.generateBuildTagBySourceDir)(buildDir, { branch: gitBranch });
data.buildTag = tagInfo.tag;
}
// start the build
const buildModule = await Promise.resolve().then(() => __importStar(require("../modules/build")));
const buildInfo = await buildModule.startBuild(data);
const logURL = `${app_config_1.Config.BASE_URL}/build/logs?build_slug=${buildInfo.SOCKET_ROOM}`;
return { logURL, ...buildInfo };
}
async stopBuild(slug, buildStatus, deployStatus = "pending") {
// const { slug } = data;
// console.log("slug :>> ", slug);
// return ApiResponse.failed(res, `${slug}`);
if (!slug)
throw new Error(`Build "slug" is required.`);
let build = await this.findOne({ slug });
if (!build)
throw new Error(`Build "${slug}" not found.`);
const buildModule = await Promise.resolve().then(() => __importStar(require("../modules/build")));
const stoppedBuild = await buildModule.stopBuild(build.projectSlug, build.appSlug, slug.toString(), buildStatus, deployStatus);
if (stoppedBuild === null || stoppedBuild === void 0 ? void 0 : stoppedBuild.error)
throw new Error(stoppedBuild.error);
return stoppedBuild;
}
async rerunBuild(build, options, ownership) {
// validate
const { appSlug, tag: prevBuildNumber, branch: gitBranch, registry: registryID, status } = build;
if (!appSlug)
throw new Error(`App slug is required.`);
if (!gitBranch)
throw new Error(`Git branch is required.`);
if (!registryID)
throw new Error(`Container registry ID is required.`);
// find registry
const registry = await this.regSvc.findOne({ _id: registryID });
if (!registry)
throw new Error(`Container registry not found.`);
// build params
const buildParams = {
appSlug,
gitBranch,
registrySlug: registry.slug,
};
return this.startBuild(buildParams, ownership);
}
}
exports.BuildService = BuildService;