@topgroup/diginext
Version:
A BUILD SERVER & CLI to deploy apps to any Kubernetes clusters.
136 lines (135 loc) • 6.75 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.ProjectService = void 0;
const log_1 = require("diginext-utils/dist/xconsole/log");
const lodash_1 = require("lodash");
const Project_1 = require("../entities/Project");
const k8s_1 = __importDefault(require("../modules/k8s"));
const mongodb_1 = require("../plugins/mongodb");
const string_1 = require("../plugins/string");
const user_utils_1 = require("../plugins/user-utils");
const AppService_1 = require("./AppService");
const BaseService_1 = __importDefault(require("./BaseService"));
class ProjectService extends BaseService_1.default {
constructor(ownership) {
super(Project_1.projectSchema, ownership);
}
async find(filter, options, pagination) {
var _a, _b, _c, _d, _e;
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)
filter = { $or: [filter, { _id: { $in: (_e = (_d = this.user) === null || _d === void 0 ? void 0 : _d.allowAccess) === null || _e === void 0 ? void 0 : _e.projects } }] };
return super.find(filter, options, pagination);
}
async create(data, options) {
// validate
if ((0, string_1.containsSpecialCharacters)(data.name))
throw new Error(`Project name should not contain special characters.`);
// process
return super.create(data, options);
}
async update(filter, data, options) {
// check access permissions
await (0, user_utils_1.checkProjectPermissionsByFilter)(this, filter, this.user);
return super.update(filter, data, options);
}
async updateOne(filter, data, options) {
// check permissions
await (0, user_utils_1.checkProjectPermissionsByFilter)(this, filter, this.user);
return super.updateOne(filter, data, options);
}
async delete(filter, options) {
// check permissions
await (0, user_utils_1.checkProjectPermissionsByFilter)(this, filter, this.user);
// Delete all apps & deploy environments!
const projects = await this.find(filter);
if (projects.length > 0) {
for (const project of projects) {
const appSvc = new AppService_1.AppService(this.ownership);
const appFilter = { project: project._id };
try {
const apps = await appSvc.find(appFilter);
if (apps.length > 0) {
await Promise.all(apps.map((app) => appSvc.delete({ _id: app._id })));
}
}
catch (e) {
(0, log_1.logWarn)(`ProjectService > delete > delete apps :>>`, e);
}
}
}
return super.delete(filter, options);
}
async softDelete(filter) {
var _a, _b;
const { DB } = await Promise.resolve().then(() => __importStar(require("../modules/api/DB")));
// find the project:
const project = await this.findOne(filter);
if (!project)
return { ok: false, affected: 0 };
// check access permissions
(0, user_utils_1.checkProjectPermissions)(project);
// check access permissions
if (this.user && ((_a = this.user.allowAccess) === null || _a === void 0 ? void 0 : _a.projects)) {
if (!((_b = this.user.allowAccess) === null || _b === void 0 ? void 0 : _b.projects.map((p) => mongodb_1.MongoDB.toString(p)).includes(mongodb_1.MongoDB.toString(project._id)))) {
throw new Error(`Permission denied.`);
}
}
// find all apps & environments, then take down all namespaces:
const appSvc = new AppService_1.AppService();
const appFilter = { project: project._id };
const apps = await appSvc.find(appFilter);
// delete all workloads of each deploy environment in an app:
if (!(0, lodash_1.isEmpty)(apps)) {
for (const app of apps) {
if (!app.deployEnvironment)
break;
for (const [env, deployEnvironment] of Object.entries(app.deployEnvironment)) {
if (!(0, lodash_1.isEmpty)(deployEnvironment)) {
const { cluster: clusterSlug, namespace } = deployEnvironment;
const cluster = await DB.findOne("cluster", { slug: clusterSlug }, { subpath: "/all" });
if (cluster) {
const { contextName: context } = cluster;
// delete namespace
k8s_1.default.deleteNamespace(namespace, { context })
.then(() => (0, log_1.logSuccess)(`[PROJECT_DELETE] ${app.slug} > Deleted "${namespace}" namespace on "${cluster.name}" cluster.`))
.catch((e) => (0, log_1.logWarn)(`[PROJECT_DELETE] ${app.slug} > Can't delete "${namespace}" namespace on "${cluster.name}" cluster:`, e));
}
}
}
}
// find all related apps and delete them all:
const deletedApps = await appSvc.delete(appFilter);
(0, log_1.logWarn)(`[ProjectService] Deleted ${deletedApps.affected} apps.`);
}
// delete the project in the database:
const result = await super.delete(filter);
return result;
}
}
exports.ProjectService = ProjectService;