UNPKG

@catladder/cli

Version:

Panter cli tool for cloud CI/CD and DevOps

122 lines (121 loc) 5.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getSecretVarNameForContext = exports.getSecretVarName = exports.getEnvironmentVariables = void 0; const deploy_1 = require("../deploy"); const BashExpression_1 = require("../bash/BashExpression"); const types_1 = require("../build/types"); const VariableValueContainingReferences_1 = require("../variables/VariableValueContainingReferences"); const resolveAllReferences_1 = require("../variables/resolveAllReferences"); const getBuildInfoVariables_1 = require("./getBuildInfoVariables"); const getEnvironmentContext_1 = require("./getEnvironmentContext"); const transformJobOnlyVars_1 = require("./transformJobOnlyVars"); const envVars_1 = require("./utils/envVars"); const getBasePredefinedVariables = ctx => { return { ENV_SHORT: ctx.env, APP_DIR: ctx.envConfigRaw.dir, ENV_TYPE: ctx.envType, ...(ctx.envType !== "local" ? (0, getBuildInfoVariables_1.getBuildInfoVariables)(ctx) : {}) }; }; const getEnvironmentVariables = async (ctx, options = {}) => { var _a, _b, _c, _d, _e, _f, _g, _h; const environmentContext = (0, getEnvironmentContext_1.getEnvironmentContext)(ctx); const { config, env, componentName } = ctx; const { envConfigRaw, deployConfigRaw, buildConfigRaw, envType } = environmentContext; const basePredefinedVariables = getBasePredefinedVariables(environmentContext); let predefinedVariables; let host; let url; if (envType === "local") { const devLocalConfig = envConfigRaw; const port = devLocalConfig.port !== false ? (_a = devLocalConfig.port) !== null && _a !== void 0 ? _a : 3000 : null; host = port ? "localhost:" + port.toString() : null; url = host ? "http://" + host : null; predefinedVariables = { ...basePredefinedVariables, ENV_SHORT: "local", ...(url ? { ROOT_URL: url } : {}), ...(host ? { HOSTNAME: host } : {}), ...(host ? { HOSTNAME_INTERNAL: host } : {}), ...(host ? { ROOT_URL_INTERNAL: "http://" + host } : {}), ...(port ? { PORT: port.toString() } : {}) }; } else { const additionalEnvVars = deployConfigRaw ? deploy_1.DEPLOY_TYPES[deployConfigRaw.type].getAdditionalEnvVars(environmentContext) : {}; const HOSTNAME_INTERNAL = (_b = additionalEnvVars.HOSTNAME_INTERNAL) !== null && _b !== void 0 ? _b : "unknown-host.example.com"; host = (_c = envConfigRaw === null || envConfigRaw === void 0 ? void 0 : envConfigRaw.host) !== null && _c !== void 0 ? _c : HOSTNAME_INTERNAL; url = (0, BashExpression_1.joinBashExpressions)(["https://", host]); predefinedVariables = { ...basePredefinedVariables, HOSTNAME: host, ROOT_URL: url, HOSTNAME_INTERNAL, ROOT_URL_INTERNAL: (0, BashExpression_1.joinBashExpressions)(["https://", HOSTNAME_INTERNAL]), ...additionalEnvVars }; } const publicEnvVarsRaw = (_e = (_d = envConfigRaw.vars) === null || _d === void 0 ? void 0 : _d.public) !== null && _e !== void 0 ? _e : {}; const additionalSecretKeys = deployConfigRaw ? deploy_1.DEPLOY_TYPES[deployConfigRaw.type].additionalSecretKeys(environmentContext) : []; const secretEnvVarKeys = [...(0, envVars_1.stringListToSecreteEnvVarList)((_g = (_f = envConfigRaw.vars) === null || _f === void 0 ? void 0 : _f.secret) !== null && _g !== void 0 ? _g : []), ...additionalSecretKeys]; const secretEnvVars = (0, envVars_1.makeSecretEnvVarMapping)(env, componentName, secretEnvVarKeys); const publicEnvVarsRawSanitized = (0, envVars_1.stringifyValues)(publicEnvVarsRaw); const publicEnvVarsUnresolved = Object.fromEntries(Object.entries(publicEnvVarsRawSanitized).map(([key, value]) => [key, (0, VariableValueContainingReferences_1.createVariableValueContainingReferencesFromString)(value, { componentName: ctx.componentName })])); const publicEnvVars = ((_h = options.shouldResolveReferences) !== null && _h !== void 0 ? _h : true) ? await (0, resolveAllReferences_1.resolveAllReferences)(publicEnvVarsUnresolved, async otherComponentName => { const { envVars: otherEnvVars } = await (0, exports.getEnvironmentVariables)({ ...ctx, componentName: otherComponentName }, { shouldResolveReferences: false // we already do this here with replaceAllReferences recursivly until re replaced all }); return otherEnvVars; }) : publicEnvVarsUnresolved; const envVars = addIndexVar({ ...predefinedVariables, ...secretEnvVars, ...publicEnvVars }); return { envVars, secretEnvVarKeys, jobOnlyVars: { build: await (0, transformJobOnlyVars_1.transformJobOnlyVars)(env, componentName, buildConfigRaw && (0, types_1.isStandaloneBuildConfig)(buildConfigRaw) && buildConfigRaw.jobVars || null), deploy: await (0, transformJobOnlyVars_1.transformJobOnlyVars)(env, componentName, deployConfigRaw && deployConfigRaw.jobVars || null) } }; }; exports.getEnvironmentVariables = getEnvironmentVariables; const sanitizeForEnVar = s => s.replace(/-/g, "_"); const getSecretVarName = (env, componentName, key) => `CL_${sanitizeForEnVar(env)}_${sanitizeForEnVar(componentName)}_${key}`; // remove dash from component name exports.getSecretVarName = getSecretVarName; const addIndexVar = vars => ({ ...vars, _ALL_ENV_VAR_KEYS: JSON.stringify(Object.keys(vars)) }); const getSecretVarNameForContext = (context, key) => (0, exports.getSecretVarName)(context.env, context.name, key); exports.getSecretVarNameForContext = getSecretVarNameForContext;