UNPKG

@catladder/pipeline

Version:

Panter workflow for cloud CI/CD and DevOps

116 lines (115 loc) 5.73 kB
"use strict"; var __assign = this && this.__assign || function () { __assign = Object.assign || function (t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createMongodbBaseConfig = void 0; var lodash_1 = require("lodash"); var getEnvironmentVariables_1 = require("../../context/getEnvironmentVariables"); var types_1 = require("../types"); var getCredentialString = function (context) { return "root:$".concat((0, getEnvironmentVariables_1.getSecretVarNameForContext)(context, "MONGODB_ROOT_PASSWORD"), "@"); }; var getMongodbHost = function (context, name) { var namespace = context.environment.envVars.KUBE_NAMESPACE; return "".concat(name, ".").concat(namespace, ".svc.cluster.local:27017"); }; var getMongodbStandaloneHost = function (context) { var fullAppname = context.environment.envVars.KUBE_APP_NAME; return getMongodbHost(context, "".concat(fullAppname, "-mongodb")); }; var getMongodbReplicasetHost = function (context, index) { var fullAppname = context.environment.envVars.KUBE_APP_NAME; return getMongodbHost(context, "".concat(fullAppname, "-mongodb-").concat(index, ".").concat(fullAppname, "-mongodb-headless")); }; var createMongodbUrl = function (context, dbName) { var _a, _b, _c, _d; if (!(0, types_1.isOfDeployType)((_a = context.deploy) === null || _a === void 0 ? void 0 : _a.config, "kubernetes")) { throw new Error("can only createMongodbUrl on supported deploys"); } var mongodbConfig = (_c = (_b = context.deploy) === null || _b === void 0 ? void 0 : _b.config.values) === null || _c === void 0 ? void 0 : _c.mongodb; var queryParams = undefined; var hosts = ""; if ((mongodbConfig === null || mongodbConfig === void 0 ? void 0 : mongodbConfig.architecture) === "replicaset") { hosts = (0, lodash_1.range)(0, (_d = mongodbConfig === null || mongodbConfig === void 0 ? void 0 : mongodbConfig.replicaCount) !== null && _d !== void 0 ? _d : 2).map(function (i) { return getMongodbReplicasetHost(context, i); }).join(","); queryParams = "replicaSet=rs0&authSource=admin"; } else { hosts = getMongodbStandaloneHost(context); queryParams = "authSource=admin"; } return "mongodb://".concat(getCredentialString(context)).concat(hosts, "/").concat(dbName).concat(queryParams ? "?".concat(queryParams) : ""); }; var createMongoBackupDefaultConfig = function (context) { var _a, _b, _c; if (!(0, types_1.isOfDeployType)((_a = context.deploy) === null || _a === void 0 ? void 0 : _a.config, "kubernetes")) { throw new Error("can only create mongodb base config on supported deploys"); } var mongodbConfig = (_c = (_b = context.deploy) === null || _b === void 0 ? void 0 : _b.config.values) === null || _c === void 0 ? void 0 : _c.mongodb; var fullAppName = context.environment.envVars.KUBE_APP_NAME; var backupEnabled = ["prod", "stage"].includes(context.environment.envType); var hostToBackup; var pvcToBackup; if ((mongodbConfig === null || mongodbConfig === void 0 ? void 0 : mongodbConfig.architecture) === "replicaset") { // bit quirky, we need to specify the host and its volume // on replicaset its probably best to not use the first one, whcih usually starts as master, so we take the second (last one would also be ok) var backupHostIndex = (mongodbConfig === null || mongodbConfig === void 0 ? void 0 : mongodbConfig.architecture) === "replicaset" && mongodbConfig.replicaCount && mongodbConfig.replicaCount > 1 ? 1 : 0; hostToBackup = getMongodbReplicasetHost(context, backupHostIndex); pvcToBackup = "datadir-".concat(fullAppName, "-mongodb-").concat(backupHostIndex); } else { hostToBackup = getMongodbStandaloneHost(context); pvcToBackup = "".concat(fullAppName, "-mongodb"); } return { enabled: backupEnabled, hostToBackup: hostToBackup, pvcToBackup: pvcToBackup, image: "mrelite/kubectlmongoshell:v1.0", schedule: "0 4 * * *", volumeSnapshotClass: "snapshotclass" }; }; var createMongodbBaseConfig = function (context) { var _a, _b, _c, _d; if (!(0, types_1.isOfDeployType)((_a = context.deploy) === null || _a === void 0 ? void 0 : _a.config, "kubernetes")) { throw new Error("can only create mongodb base config on supported deploys"); } var mongodbConfig = (_c = (_b = context.deploy) === null || _b === void 0 ? void 0 : _b.config.values) === null || _c === void 0 ? void 0 : _c.mongodb; return { mongodb: { enabled: true, auth: { enabled: true, rootPassword: "$" + (0, getEnvironmentVariables_1.getSecretVarNameForContext)(context, "MONGODB_ROOT_PASSWORD"), replicaSetKey: "$" + (0, getEnvironmentVariables_1.getSecretVarNameForContext)(context, "MONGODB_REPLICASET_KEY") }, persistence: { storageClass: "standard-rwo" }, image: { registry: "bitnamilegacy", repository: "mongodb" }, backup: createMongoBackupDefaultConfig(context) }, env: { secret: __assign({ MONGO_URL: createMongodbUrl(context, (_d = mongodbConfig === null || mongodbConfig === void 0 ? void 0 : mongodbConfig.dbName) !== null && _d !== void 0 ? _d : "app") }, (mongodbConfig === null || mongodbConfig === void 0 ? void 0 : mongodbConfig.architecture) === "replicaset" ? { MONGO_OPLOG_URL: createMongodbUrl(context, "local") } // oplog only works with replicasets : {}) } }; }; exports.createMongodbBaseConfig = createMongodbBaseConfig;