@axway/axway-central-cli
Version:
Manage APIs, services and publish to the Amplify Marketplace
235 lines (231 loc) • 12.1 kB
JavaScript
;
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 helpers = _interopRequireWildcard(require("./helpers"));
var _traceableTemplates = require("./helpers/templates/traceableTemplates");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
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 an environment that the agent will use',
enterMoreEnviroments: 'Do you want to enter another environment ?',
selectCentralMappingEnvironment: 'Select an Engage environment to map to the provided Traceable Agent Environement'
};
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) => {
let askEnvs = true;
let envs = [];
let mappedCentralEnvs = [];
console.log(_chalk.default.gray("An array of environments that the agent will use"));
while (askEnvs) {
const env = await (0, _basicPrompts.askInput)({
msg: prompts.enterEnvironments,
allowEmptyInput: true
});
if (envs.length === 0 && 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);
}
centralEnvs = centralEnvs.filter(env => {
var _centralEnvs$find;
return ((_centralEnvs$find = centralEnvs.find(env => env.name === centralMappingEnv)) === null || _centralEnvs$find === void 0 ? void 0 : _centralEnvs$find.name) !== env.name;
});
askEnvs = (await (0, _basicPrompts.askList)({
msg: prompts.enterMoreEnviroments,
default: _types.YesNo.No,
choices: _types.YesNoChoices
})) === _types.YesNo.Yes;
}
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) {
await askEnvironments(envs, traceableAgentValues);
}
});
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('\nPlease make sure to copy the "private_key.pem" and "public_key.pem" files for the existing service account you selected.'));
}
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
};