UNPKG

@axway/axway-central-cli

Version:

Manage APIs, services and publish to the Amplify Marketplace

258 lines (252 loc) 13.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.testables = exports.gatewayConnectivity = exports.completeInstall = exports.askTraceableRegion = exports.askConfigType = exports.askBundleType = exports.amplifyAgentsNs = exports.TraceableInstallMethods = exports.ConfigFiles = void 0; var _chalk = _interopRequireDefault(require("chalk")); var _snooplogg = _interopRequireDefault(require("snooplogg")); var _Kubectl = require("../../common/Kubectl"); var _basicPrompts = require("../../common/basicPrompts"); var _inputs = require("./helpers/inputs"); var _types = require("../../common/types"); var _utils = require("../../common/utils"); var _agents = require("./agents"); var helpers = _interopRequireWildcard(require("./helpers")); var _traceableTemplates = require("./helpers/templates/traceableTemplates"); function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); } function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } // @ts-ignore const { log } = (0, _snooplogg.default)('central: install: agents: Traceable'); const caImage = `${_types.PublicDockerRepoBaseUrl}${_types.BasePaths.DockerAgentPublicRepo}/${_types.AgentNames.TRACEABLE_CA}`; const amplifyAgentsNs = exports.amplifyAgentsNs = 'amplify-agents'; // ConfigFiles - all the config file that are used in the setup const ConfigFiles = exports.ConfigFiles = { helmOverride: 'agent-overrides.yaml', agentEnvVars: `${helpers.configFiles.AGENT_ENV_VARS}` }; // TraceablePrompts - prompts for user inputs const prompts = { configTypeMsg: 'Select the mode of installation', agentNamespace: 'Enter the namespace to use for the Amplify Traceable Agents', enterToken: 'Enter the token that the agent will use', enterRegion: 'Enter the region that the agent will use', enterEnvironments: 'Enter a Traceable environment', enterMoreEnvironments: 'Do you want to enter another mapping?', selectCentralMappingEnvironment: 'Select an Engage environment to map to the provided Traceable environment', environmentsDescription: 'Configure a mapping of Traceable environment to Engage environment that the agent will use' }; const askBundleType = async () => { return _types.BundleType.TRACEABILITY; }; exports.askBundleType = askBundleType; const askConfigType = async () => { return await (0, _basicPrompts.askList)({ msg: prompts.configTypeMsg, choices: [_types.AgentConfigTypes.DOCKERIZED, _types.AgentConfigTypes.HELM] }); }; // // Questions for the configuration of Traceable agent // exports.askConfigType = askConfigType; const askToken = async () => await (0, _basicPrompts.askInput)({ msg: prompts.enterToken, allowEmptyInput: false }); const askTraceableRegion = async () => { return await (0, _basicPrompts.askList)({ msg: prompts.enterRegion, choices: Object.entries(_types.TraceableRegionType).reduce((accumulator, curr) => { return accumulator.concat({ name: curr[0], value: curr[1] }); }, []), default: _types.TraceableRegionType.US }); }; exports.askTraceableRegion = askTraceableRegion; const askEnvironments = async (centralEnvs, traceableAgentValues, excludeEnvironment) => { // Filter out the already-selected agent installation environment if (excludeEnvironment) { centralEnvs = centralEnvs.filter(env => env.name !== excludeEnvironment); } // If no central environments are available, exit the installation if (centralEnvs.length === 0) { console.log(_chalk.default.red('Installation cannot proceed: No Engage environments are available for mapping.')); console.log(_chalk.default.yellow('Please create at least one Engage environment before installing the Traceable agent.')); console.log(_chalk.default.gray('You can create an environment using: axway engage create environment')); process.exit(1); } let askEnvs = true; let envs = []; let mappedCentralEnvs = []; console.log(_chalk.default.gray(prompts.environmentsDescription)); while (askEnvs) { const env = await (0, _basicPrompts.askInput)({ msg: prompts.enterEnvironments, allowEmptyInput: true }); if (envs.length === 0 && (!env || env.toString().trim() === "")) { break; } if (env && env.toString().trim() !== "") { envs.push(env); } const centralMappingEnv = await (0, _basicPrompts.askList)({ msg: prompts.selectCentralMappingEnvironment, choices: centralEnvs.map(e => e.name) }); if (centralMappingEnv && centralMappingEnv.toString().trim() !== "") { mappedCentralEnvs.push(centralMappingEnv); } // Remove the selected environment from available choices for next iteration centralEnvs = centralEnvs.filter(env => env.name !== centralMappingEnv); // Only ask to continue if there are remaining central environments if (centralEnvs.length > 0) { askEnvs = (await (0, _basicPrompts.askList)({ msg: prompts.enterMoreEnvironments, default: _types.YesNo.No, choices: _types.YesNoChoices })) === _types.YesNo.Yes; } else { askEnvs = false; // Auto-stop when no environments remain } } traceableAgentValues.environments = envs; traceableAgentValues.centralEnvironments = mappedCentralEnvs; }; const gatewayConnectivity = async installConfig => { let traceableAgentValues = new _traceableTemplates.TraceableAgentValues(); if (installConfig.switches.isHelmInstall) { console.log(_chalk.default.gray(`The Amplify Traceable Agent needs to be deployed to your Kubernetes cluster to discover APIs for publishing to Amplify Central.`)); const { error } = await _Kubectl.kubectl.isInstalled(); if (error) { throw new Error(`Kubectl is required to fill out the following prompts. It appears to be missing or misconfigured.\n${error}`); } traceableAgentValues.namespace = await (0, _inputs.askNamespace)(prompts.agentNamespace, amplifyAgentsNs); } if (installConfig.switches.isDockerInstall) { console.log('\nCONNECTION TO TRACEABLE API GATEWAY:'); console.log(_chalk.default.gray("The Discovery Agent needs to connect to the Traceable API Gateway to discover API's for publishing to Amplify Central.")); } traceableAgentValues.traceableToken = await askToken(); traceableAgentValues.traceableRegion = await askTraceableRegion(); await helpers.getCentralEnvironments(installConfig.centralConfig.apiServerClient, installConfig.centralConfig.definitionManager).then(async envs => { if (envs) { var _installConfig$centra; // Pass the already-selected agent installation environment to exclude it from mapping choices const agentInstallEnv = (_installConfig$centra = installConfig.centralConfig.ampcEnvInfo) === null || _installConfig$centra === void 0 ? void 0 : _installConfig$centra.name; await askEnvironments(envs, traceableAgentValues, agentInstallEnv); } }); return traceableAgentValues; }; exports.gatewayConnectivity = gatewayConnectivity; const dockerSuccessMsg = installConfig => { let dockerInfo; const runAgentLinuxMsg = `docker run -it --env-file ${helpers.pwd}/${helpers.configFiles.AGENT_ENV_VARS} -v ${helpers.pwd}:/keys ${helpers.eolChar}`; const runAgentWinMsg = `docker run -it --env-file ${helpers.pwdWin}/${helpers.configFiles.AGENT_ENV_VARS} -v ${helpers.pwdWin}:/keys ${helpers.eolCharWin}`; const startAgentLinuxMsg = `\nStart the Traceable Agent on a Linux based machine`; const startAgentWinMsg = `\nStart the Traceable Agent on a Windows machine`; if (installConfig.switches.isTaEnabled) { dockerInfo = `To utilize the agent, pull the latest Docker image and run it using the appropriate supplied environment file, (${helpers.configFiles.AGENT_ENV_VARS}):`; console.log(_chalk.default.whiteBright(dockerInfo), '\n'); const caImageVersion = `${caImage}:${installConfig.taVersion}`; console.log(_chalk.default.white('Pull the latest image of the Agent:')); console.log(_chalk.default.cyan(`docker pull ${caImageVersion}`)); console.log(_chalk.default.white(_utils.isWindows ? startAgentWinMsg : startAgentLinuxMsg)); console.log(_chalk.default.cyan(_utils.isWindows ? runAgentWinMsg : runAgentLinuxMsg)); console.log('\t', _chalk.default.cyan(`-v /data ${caImageVersion}`), '\n'); } }; const helmSuccessMsg = namespace => { console.log(`Traceable Agent override file has been placed at ${process.cwd()}/${ConfigFiles.helmOverride}`); helpers.helmImageSecretInfo(namespace); let agentHelmInfo = new Set(); agentHelmInfo.add({ helmReleaseName: 'traceable-agent', helmChartName: ' axway/traceable-agent', overrideFileName: ConfigFiles.helmOverride, imageSecretOverrides: `--set image.pullSecret=<image-pull-secret-name>` }); helpers.helmInstallInfo('Traceable', namespace, agentHelmInfo); }; const generateSuccessHelpMsg = installConfig => { const traceableAgentValues = installConfig.gatewayConfig; const configType = installConfig.deploymentType; if (installConfig.centralConfig.ampcDosaInfo.isNew && !installConfig.switches.isHelmInstall) { console.log(_chalk.default.yellow(_agents.svcAccMsg)); } if (configType === _types.AgentConfigTypes.DOCKERIZED) { dockerSuccessMsg(installConfig); } else if (configType === _types.AgentConfigTypes.HELM) { helmSuccessMsg(traceableAgentValues.namespace.name); } console.log('Configuration file(s) have been successfully created.\n'); console.log(_chalk.default.gray(`\nAdditional information about agent features can be found here:\n${helpers.agentsDocsUrl.TRACEABLE}`)); }; const completeInstall = async installConfig => { // Add final settings to TraceableAgentValues const traceableAgentValues = installConfig.gatewayConfig; traceableAgentValues.centralConfig = installConfig.centralConfig; traceableAgentValues.traceabilityConfig = installConfig.traceabilityConfig; if (installConfig.switches.isHelmInstall) { traceableAgentValues.traceableSecret = helpers.amplifyAgentsCredsSecret; traceableAgentValues.agentKeysSecret = helpers.amplifyAgentsKeysSecret; if (traceableAgentValues.namespace.isNew) { await helpers.createNamespace(traceableAgentValues.namespace.name); } await helpers.createSecret(traceableAgentValues.namespace.name, helpers.amplifyAgentsKeysSecret, async () => { if (installConfig.centralConfig.ampcDosaInfo.isNew) { console.log(_chalk.default.yellow(`The secret '${helpers.amplifyAgentsKeysSecret}' will be created with the same "private_key.pem" and "public_key.pem" that was auto generated to create the Service Account.`)); } await helpers.createAmplifyAgentKeysSecret(traceableAgentValues.namespace.name, helpers.amplifyAgentsKeysSecret, 'publicKey', traceableAgentValues.centralConfig.dosaAccount.publicKey, 'privateKey', traceableAgentValues.centralConfig.dosaAccount.privateKey); }); await helpers.createSecret(traceableAgentValues.namespace.name, helpers.amplifyAgentsCredsSecret, async () => { await createTraceableCredsSecret(traceableAgentValues.namespace.name, helpers.amplifyAgentsCredsSecret, traceableAgentValues.traceableToken); }); } console.log('Generating the configuration file(s)...'); if (installConfig.switches.isDockerInstall) { if (installConfig.switches.isTaEnabled) { (0, _utils.writeTemplates)(ConfigFiles.agentEnvVars, traceableAgentValues, helpers.traceableEnvVarTemplate); } } else if (installConfig.switches.isHelmInstall) { (0, _utils.writeTemplates)(ConfigFiles.helmOverride, traceableAgentValues, helpers.traceableHelmOverrideTemplate); } generateSuccessHelpMsg(installConfig); }; exports.completeInstall = completeInstall; const createTraceableCredsSecret = async (namespace, secretName, token) => { const { error } = await _Kubectl.kubectl.create('secret', `-n ${namespace} generic ${secretName} \ --from-literal=token=${token} `); if (error) { throw Error(error); } console.log(`Created ${secretName} in the ${namespace} namespace.`); }; const TraceableInstallMethods = exports.TraceableInstallMethods = { GetBundleType: askBundleType, GetDeploymentType: askConfigType, AskGatewayQuestions: gatewayConnectivity, FinalizeGatewayInstall: completeInstall, ConfigFiles: Object.values(ConfigFiles), AgentNameMap: { [_types.AgentTypes.ca]: _types.AgentNames.TRACEABLE_CA }, GatewayDisplay: _types.GatewayTypes.TRACEABLE }; const testables = exports.testables = { prompts, ConfigFiles, askEnvironments, gatewayConnectivity, createTraceableCredsSecret };