@catladder/cli
Version:
Panter cli tool for cloud CI/CD and DevOps
104 lines • 5.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createMongodbBaseConfig = void 0;
const lodash_1 = require("lodash");
const getEnvironmentVariables_1 = require("../../context/getEnvironmentVariables");
const types_1 = require("../types");
const getCredentialString = (context) => `root:$${(0, getEnvironmentVariables_1.getSecretVarNameForContext)(context, "MONGODB_ROOT_PASSWORD")}@`;
const getMongodbHost = (context, name) => {
const namespace = context.environment.envVars.KUBE_NAMESPACE;
return `${name}.${namespace}.svc.cluster.local:27017`;
};
const getMongodbStandaloneHost = (context) => {
const fullAppname = context.environment.envVars.KUBE_APP_NAME;
return getMongodbHost(context, `${fullAppname}-mongodb`);
};
const getMongodbReplicasetHost = (context, index) => {
const fullAppname = context.environment.envVars.KUBE_APP_NAME;
return getMongodbHost(context, `${fullAppname}-mongodb-${index}.${fullAppname}-mongodb-headless`);
};
const createMongodbUrl = (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");
}
const mongodbConfig = (_c = (_b = context.deploy) === null || _b === void 0 ? void 0 : _b.config.values) === null || _c === void 0 ? void 0 : _c.mongodb;
let queryParams = undefined;
let 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((i) => getMongodbReplicasetHost(context, i))
.join(",");
queryParams = "replicaSet=rs0&authSource=admin";
}
else {
hosts = getMongodbStandaloneHost(context);
queryParams = "authSource=admin";
}
return `mongodb://${getCredentialString(context)}${hosts}/${dbName}${queryParams ? `?${queryParams}` : ""}`;
};
const createMongoBackupDefaultConfig = (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");
}
const mongodbConfig = (_c = (_b = context.deploy) === null || _b === void 0 ? void 0 : _b.config.values) === null || _c === void 0 ? void 0 : _c.mongodb;
const fullAppName = context.environment.envVars.KUBE_APP_NAME;
const backupEnabled = ["prod", "stage"].includes(context.environment.envType);
let hostToBackup;
let 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)
const backupHostIndex = (mongodbConfig === null || mongodbConfig === void 0 ? void 0 : mongodbConfig.architecture) === "replicaset" &&
mongodbConfig.replicaCount &&
mongodbConfig.replicaCount > 1
? 1
: 0;
hostToBackup = getMongodbReplicasetHost(context, backupHostIndex);
pvcToBackup = `datadir-${fullAppName}-mongodb-${backupHostIndex}`;
}
else {
hostToBackup = getMongodbStandaloneHost(context);
pvcToBackup = `${fullAppName}-mongodb`;
}
return {
enabled: backupEnabled,
hostToBackup,
pvcToBackup,
image: "mrelite/kubectlmongoshell:v1.0",
schedule: "0 4 * * *",
volumeSnapshotClass: "snapshotclass",
};
};
const createMongodbBaseConfig = (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");
}
const 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",
},
backup: createMongoBackupDefaultConfig(context),
},
env: {
secret: {
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;
//# sourceMappingURL=mongodb.js.map