@topgroup/diginext
Version:
A BUILD SERVER & CLI to deploy apps to any Kubernetes clusters.
134 lines (133 loc) • 6.78 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseOptionsToAppConfig = void 0;
const log_1 = require("diginext-utils/dist/xconsole/log");
const lodash_1 = require("lodash");
const plugins_1 = require("../../plugins");
const slug_1 = require("../../plugins/slug");
const app_helper_1 = require("../apps/app-helper");
const ask_project_and_app_1 = require("../apps/ask-project-and-app");
const update_config_1 = require("../apps/update-config");
const build_1 = require("../build");
const ask_for_cluster_1 = require("../cluster/ask-for-cluster");
const ask_for_registry_1 = require("../registry/ask-for-registry");
const ask_deploy_environment_info_1 = require("./ask-deploy-environment-info");
const parseOptionsToAppConfig = async (options) => {
const { author, env, targetDirectory, slug, projectSlug, namespace, cluster, domain, port, shouldEnableCDN: cdn, shouldInherit, redirect, replicas, healthz, size, ssl, providerProject, region, zone, registry, isDebugging = false, } = options;
const { app, project } = await (0, ask_project_and_app_1.askForProjectAndApp)(options.targetDirectory, options);
// console.log("app :>> ", app);
// console.log("project :>> ", project);
// get current config
let appConfig = await (0, app_helper_1.getAppConfigFromApp)(app);
if ((0, lodash_1.isEmpty)(appConfig)) {
(0, log_1.logError)(`No app configurations found, please initialize first: $ dx init`);
return;
}
appConfig.project = project.slug;
// get remote SSH
const gitRepoData = await (0, plugins_1.getCurrentGitRepoData)(targetDirectory);
if (!gitRepoData) {
(0, log_1.logError)(`This directory doesn't have any initialized git.`);
return;
}
const { repoSSH, repoURL, provider: gitProvider, branch } = gitRepoData;
if (!appConfig.git)
appConfig.git = {};
appConfig.git.provider = gitProvider;
appConfig.git.repoSSH = repoSSH;
appConfig.git.repoURL = repoURL;
options.repoSSH = repoSSH;
options.repoURL = repoURL;
options.gitProvider = gitProvider;
options.gitBranch = branch;
// validate deploy environment
if (!appConfig.deployEnvironment)
appConfig.deployEnvironment = {};
if (!appConfig.deployEnvironment[env])
appConfig.deployEnvironment[env] = {};
const deployEnvironment = appConfig.deployEnvironment[env];
// Google Cloud Info
if (typeof providerProject !== "undefined")
deployEnvironment.project = providerProject;
if (typeof region !== "undefined")
deployEnvironment.region = region;
if (typeof zone !== "undefined")
deployEnvironment.zone = zone;
// Container Registry
if (typeof registry !== "undefined")
deployEnvironment.registry = typeof registry !== "boolean" ? registry : deployEnvironment.registry;
if (typeof deployEnvironment.registry === "undefined")
deployEnvironment.registry = (await (0, ask_for_registry_1.askForRegistry)()).slug;
// Domains
if (typeof domain !== "undefined" && typeof domain !== "boolean")
deployEnvironment.domains = [domain];
// Kubernetes Info
if (typeof namespace !== "undefined")
deployEnvironment.namespace = namespace;
if (typeof cluster !== "undefined")
deployEnvironment.cluster = typeof cluster !== "boolean" ? cluster : (await (0, ask_for_cluster_1.askForCluster)()).slug;
if (typeof port !== "undefined")
deployEnvironment.port = port;
if (typeof cdn !== "undefined")
deployEnvironment.cdn = cdn;
if (typeof shouldInherit !== "undefined")
deployEnvironment.shouldInherit = shouldInherit;
if (typeof redirect !== "undefined")
deployEnvironment.redirect = redirect;
if (typeof replicas !== "undefined")
deployEnvironment.replicas = replicas;
if (typeof size !== "undefined")
deployEnvironment.size = size;
if (typeof healthz !== "undefined")
deployEnvironment.healthzPath = healthz === "" ? null : healthz;
if (typeof ssl !== "undefined") {
if (ssl) {
if (typeof deployEnvironment.ssl === "undefined" || deployEnvironment.ssl === "none") {
if ((0, lodash_1.isEmpty)(deployEnvironment.domains)) {
// generate one or ask to generate one!
const domains = await (0, build_1.askForDomain)(env, project.slug, app.slug, deployEnvironment, {
user: author,
shouldGenerate: domain == true,
});
if ((0, lodash_1.isEmpty)(domains)) {
(0, log_1.logError)(`No domains to issue SSL certificate.`);
return;
}
deployEnvironment.domains = domains;
}
const primaryDomain = deployEnvironment.domains[0];
deployEnvironment.ssl = domain == true ? "letsencrypt" : await (0, ask_deploy_environment_info_1.askForCertIssuer)();
deployEnvironment.tlsSecret = `tls-secret-${deployEnvironment.ssl}-${(0, slug_1.makeSlug)(primaryDomain)}`;
}
else {
// if current ssl is "letsencrypt" or "custom"...
if ((0, lodash_1.isEmpty)(deployEnvironment.domains)) {
if (domain == true) {
deployEnvironment.domains = await (0, build_1.askForDomain)(env, project.slug, app.slug, deployEnvironment, {
user: author,
shouldGenerate: true,
});
deployEnvironment.ssl = "letsencrypt";
}
else {
(0, log_1.logError)(`There is domains in deploy environment config (${env}) but the SSL was enabled, deploy again without "--ssl" flag or delete "ssl" in deploy environment config on Diginext workspace.`);
return;
}
}
const primaryDomain = deployEnvironment.domains[0];
deployEnvironment.tlsSecret = `tls-secret-${deployEnvironment.ssl}-${(0, slug_1.makeSlug)(primaryDomain)}`;
}
}
else {
deployEnvironment.ssl = "none";
deployEnvironment.tlsSecret = "";
}
}
if (isDebugging)
console.log("parseOptionsToAppConfig() > deployEnvironment :>> ", deployEnvironment);
appConfig.deployEnvironment[env] = deployEnvironment;
// save to app config on server
appConfig = await (0, update_config_1.updateAppConfig)(app, env, deployEnvironment);
return appConfig;
};
exports.parseOptionsToAppConfig = parseOptionsToAppConfig;