@axway/axway-central-cli
Version:
Manage APIs, services and publish to the Amplify Marketplace
243 lines (235 loc) • 13.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.testables = exports.gatewayConnectivity = exports.defaultLogFiles = exports.completeInstall = exports.askConfigType = exports.askBundleType = exports.ConfigFiles = exports.AzureInstallMethods = void 0;
var _chalk = _interopRequireDefault(require("chalk"));
var _snooplogg = _interopRequireDefault(require("snooplogg"));
var _basicPrompts = require("../../common/basicPrompts");
var _types = require("../../common/types");
var _utils = require("../../common/utils");
var _agents = require("./agents");
var helpers = _interopRequireWildcard(require("./helpers"));
var _azureTemplates = require("./helpers/templates/azureTemplates");
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)('engage: install: agents: azure');
const daImage = `${_types.PublicDockerRepoBaseUrl}${_types.BasePaths.DockerAgentPublicRepo}/${_types.AgentNames.AZURE_DA}`;
const taImage = `${_types.PublicDockerRepoBaseUrl}${_types.BasePaths.DockerAgentPublicRepo}/${_types.AgentNames.AZURE_TA}`;
const InvalidMessages = {
enterApiManagementServiceName: `The API Management Service Name can contain only letters, numbers and hyphens. The first character must be a letter and last character must be a letter or a number.`
};
const defaultLogFiles = exports.defaultLogFiles = '/group-*_instance-*.log';
// ConfigFiles - all the config file that are used in the setup
const ConfigFiles = exports.ConfigFiles = {
DAEnvVars: `${helpers.configFiles.DA_ENV_VARS}`,
TAEnvVars: `${helpers.configFiles.TA_ENV_VARS}`,
AzureDABinaryFile: 'discovery_agent',
AzureDAYaml: 'discovery_agent.yml',
AzureTABinaryFile: 'traceability_agent',
AzureTAYaml: 'traceability_agent.yml'
};
// AzurePrompts - prompts for user inputs
const AzurePrompts = {
configTypeMsg: 'Select the mode of installation',
enterEventHubName: 'Enter the Azure Event Hub Name',
enterEventHubNamespace: 'Enter the Azure Event Hub Namespace',
enterPolicyKey: 'Enter the Azure Policy Key',
enterPolicyName: 'Enter the Azure Policy Name',
enterTenantId: 'Enter the Azure Tenant ID',
enterSubscriptionId: 'Enter the Azure Subscription ID',
enterServicePrincipalClientId: 'Enter the Azure Service Principal Client ID',
enterServicePrincipalClientSecret: 'Enter the Azure Service Principal Client Secret',
enterResourceGroupName: 'Enter the Azure Resource Group Name',
enterApiManagementServiceName: 'Enter the Azure API Management Service Name'
};
const askBundleType = async gateway => {
console.log(gateway);
if (gateway === _types.GatewayTypes.AZURE_GATEWAY) {
return await (0, _basicPrompts.askList)({
msg: helpers.agentMessages.selectAgentType,
choices: [_types.BundleType.ALL_AGENTS, _types.BundleType.DISCOVERY, _types.BundleType.TRACEABILITY]
});
} else {
return _types.BundleType.DISCOVERY;
}
};
exports.askBundleType = askBundleType;
const askConfigType = async () => {
return _types.AgentConfigTypes.DOCKERIZED;
};
//
// Questions for the configuration of Azure agents
//
exports.askConfigType = askConfigType;
const askAzureTenantId = async () => await (0, _basicPrompts.askInput)({
msg: AzurePrompts.enterTenantId
});
const askAzureSubscriptionId = async () => await (0, _basicPrompts.askInput)({
msg: AzurePrompts.enterSubscriptionId
});
const askAzureServicePrincipalClientId = async () => await (0, _basicPrompts.askInput)({
msg: AzurePrompts.enterServicePrincipalClientId
});
const askAzureServicePrincipalClientSecret = async () => await (0, _basicPrompts.askInput)({
msg: AzurePrompts.enterServicePrincipalClientSecret
});
const askAzureResourceGroupName = async () => await (0, _basicPrompts.askInput)({
msg: AzurePrompts.enterResourceGroupName
});
const askAzureapiManagementServiceName = async () => await (0, _basicPrompts.askInput)({
msg: AzurePrompts.enterApiManagementServiceName,
validate: (0, _basicPrompts.validateRegex)(helpers.AzureRegexPatterns.azureApiManagementServiceNameRegex, InvalidMessages.enterApiManagementServiceName)
});
const askAzureEventHubNamespace = async () => await (0, _basicPrompts.askInput)({
msg: AzurePrompts.enterEventHubNamespace
});
const askAzureEventHubName = async () => await (0, _basicPrompts.askInput)({
msg: AzurePrompts.enterEventHubName
});
const askAzurePolicyName = async () => await (0, _basicPrompts.askInput)({
msg: AzurePrompts.enterPolicyName,
defaultValue: 'RootManageSharedAccessKey'
});
const askAzurePolicyKey = async () => await (0, _basicPrompts.askInput)({
msg: AzurePrompts.enterPolicyKey
});
const gatewayConnectivity = async installConfig => {
const azureAgentValues = new _azureTemplates.AzureAgentValues();
if (installConfig.gatewayType === _types.GatewayTypes.AZURE_EVENTHUB) {
azureAgentValues.isAzureEventHub = true;
console.log('\nCONNECTION TO AZURE EVENTHUB:');
console.log(_chalk.default.gray(`The discovery agent needs to connect to the Azure EventHub to discover API's for publishing to Amplify.\n`));
} else {
console.log('\nCONNECTION TO AZURE:');
console.log(_chalk.default.gray(`The discovery agent needs to connect to the Azure API Gateway to discover API's for publishing to Amplify.\nThe traceability agent needs to connect to an Azure Event Hub for collecting APIs transactions. These will be forwarded to the Business Insights.\n`));
}
// Azure Discovery Agent Prompts
if (installConfig.switches.isDaEnabled) {
console.log(_chalk.default.gray(`\nDiscovery Agent Configuration\nThe discovery agent needs to connect to Azure using a service principal with password based authentication. Refer to https://docs.microsoft.com/en-us/cli/azure/create-an-azure-service-principal-azure-cli for creating such service principal using Azure CLI.`));
await askDiscoveryPrompts(azureAgentValues, installConfig.gatewayType);
}
// Azure Traceability Agent Prompts
if (installConfig.switches.isTaEnabled) {
console.log(_chalk.default.gray(`\nTraceability Agent Configuration\nThe traceability agent needs to connect to Azure Event Hub using a Policy. Refer to https://docs.microsoft.com/en-us/azure/event-hubs/authorize-access-shared-access-signature.`));
await askTraceabilityPrompts(azureAgentValues);
// ask discovery prompts without asking for an agent name, or displaying the Discovery Configuration message
if (installConfig.switches.isTaEnabled && !installConfig.switches.isDaEnabled) {
await askDiscoveryPrompts(azureAgentValues, installConfig.gatewayType);
}
}
return azureAgentValues;
};
exports.gatewayConnectivity = gatewayConnectivity;
const generateSuccessHelpMsg = installConfig => {
if (installConfig.centralConfig.ampcDosaInfo.isNew && !installConfig.switches.isHelmInstall) {
console.log(_chalk.default.yellow(_agents.svcAccMsg));
}
dockerSuccessMsg(installConfig);
console.log(_chalk.default.gray(`\nAdditional information about agent features can be found here:\n${helpers.agentsDocsUrl.AZURE}`));
};
const dockerSuccessMsg = installConfig => {
let dockerInfo;
const runDaLinuxMsg = `docker run -it --env-file ${helpers.pwd}/${helpers.configFiles.DA_ENV_VARS} -v ${helpers.pwd}:/keys ${helpers.eolChar}`;
const runDaWinMsg = `docker run -it --env-file ${helpers.pwdWin}/${helpers.configFiles.DA_ENV_VARS} -v ${helpers.pwdWin}:/keys ${helpers.eolCharWin}`;
const runTaLinuxMsg = `docker run -it --env-file ${helpers.pwd}/${helpers.configFiles.TA_ENV_VARS} -v ${helpers.pwd}:/keys ${helpers.eolChar}`;
const runTaWinMsg = `docker run -it --env-file ${helpers.pwdWin}/${helpers.configFiles.TA_ENV_VARS} -v ${helpers.pwdWin}:/keys ${helpers.eolCharWin}`;
const startDaLinuxMsg = `\nStart the Discovery Agent on a Linux based machine`;
const startDaWinMsg = `\nStart the Discovery Agent on a Windows machine`;
const startTaLinuxMsg = `\nStart the Traceability Agent on a Linux based machine`;
const startTaWinMsg = `\nStart the Traceability Agent on a Windows machine`;
if (installConfig.switches.isDaEnabled && installConfig.switches.isTaEnabled) {
dockerInfo = `To utilize the agents, pull the latest Docker images and run them using the appropriate supplied environment files, (${helpers.configFiles.DA_ENV_VARS} & ${helpers.configFiles.TA_ENV_VARS}):`;
} else if (installConfig.switches.isDaEnabled) {
dockerInfo = `To utilize the discovery agent, pull the latest Docker image and run it using the supplied environment file, (${helpers.configFiles.DA_ENV_VARS}):`;
} else {
dockerInfo = `To utilize the traceability agent, pull the latest Docker image and run it using the supplied environment file, (${helpers.configFiles.TA_ENV_VARS}):`;
}
console.log(_chalk.default.whiteBright(dockerInfo), '\n');
if (installConfig.switches.isDaEnabled) {
const daImageVersion = `${daImage}:${installConfig.daVersion}`;
console.log(_chalk.default.white('Pull the latest image of the Discovery Agent:'));
console.log(_chalk.default.cyan(`docker pull ${daImageVersion}`));
console.log(_chalk.default.white(_utils.isWindows ? startDaWinMsg : startDaLinuxMsg));
console.log(_chalk.default.cyan(_utils.isWindows ? runDaWinMsg : runDaLinuxMsg));
console.log('\t', _chalk.default.cyan(`-v /data ${daImageVersion}`), '\n');
}
if (installConfig.switches.isTaEnabled) {
const taImageVersion = `${taImage}:${installConfig.taVersion}`;
console.log(_chalk.default.white('Pull the latest image of the Traceability Agent:'));
console.log(_chalk.default.cyan(`docker pull ${taImageVersion}`));
console.log(_chalk.default.white(_utils.isWindows ? startTaWinMsg : startTaLinuxMsg));
console.log(_chalk.default.cyan(_utils.isWindows ? runTaWinMsg : runTaLinuxMsg));
console.log('\t', _chalk.default.cyan(`-v /data ${taImageVersion}`), '\n');
}
};
// Azure DA prompts
async function askDiscoveryPrompts(azureAgentValues, gatewayType) {
// Azure Tenant Id
azureAgentValues.tenantId = await askAzureTenantId();
// Azure Subscription Id
azureAgentValues.subscriptionId = await askAzureSubscriptionId();
// Azure Service Principal Client Id
azureAgentValues.servicePrincipalClientId = await askAzureServicePrincipalClientId();
// Azure Service Principal Client Secret
azureAgentValues.servicePrincipalClientSecret = await askAzureServicePrincipalClientSecret();
// Azure Resource Group Name
azureAgentValues.resourceGroupName = await askAzureResourceGroupName();
if (gatewayType === _types.GatewayTypes.AZURE_GATEWAY) {
// Azure API Management Service Name
azureAgentValues.apiManagementServiceName = await askAzureapiManagementServiceName();
}
if (gatewayType === _types.GatewayTypes.AZURE_EVENTHUB) {
// Azure Event Hub Namespace
azureAgentValues.eventHubNamespace = await askAzureEventHubNamespace();
}
}
async function askTraceabilityPrompts(azureAgentValues) {
// Azure Event Hub Namespace
azureAgentValues.eventHubNamespace = await askAzureEventHubNamespace();
// Azure Event Hub Name
azureAgentValues.eventHubName = await askAzureEventHubName();
// Azure Policy Name
azureAgentValues.policyName = await askAzurePolicyName();
// Azure Policy Key
azureAgentValues.policyKey = await askAzurePolicyKey();
}
const completeInstall = async installConfig => {
/**
* Create agent resources
*/
const azureAgentValues = installConfig.gatewayConfig;
// Add final settings to azureAgentsValues
azureAgentValues.centralConfig = installConfig.centralConfig;
azureAgentValues.traceabilityConfig = installConfig.traceabilityConfig;
console.log('Generating the configuration file(s)...');
if (installConfig.switches.isDaEnabled) {
(0, _utils.writeTemplates)(ConfigFiles.DAEnvVars, azureAgentValues, helpers.azureDAEnvVarTemplate);
}
if (installConfig.switches.isTaEnabled) {
(0, _utils.writeTemplates)(ConfigFiles.TAEnvVars, azureAgentValues, helpers.azureTAEnvVarTemplate);
}
console.log('Configuration file(s) have been successfully created.\n');
generateSuccessHelpMsg(installConfig);
};
exports.completeInstall = completeInstall;
const AzureInstallMethods = exports.AzureInstallMethods = {
GetBundleType: askBundleType,
GetDeploymentType: askConfigType,
AskGatewayQuestions: gatewayConnectivity,
FinalizeGatewayInstall: completeInstall,
ConfigFiles: Object.values(ConfigFiles),
AgentNameMap: {
[_types.AgentTypes.da]: _types.AgentNames.AZURE_DA,
[_types.AgentTypes.ta]: _types.AgentNames.AZURE_TA
},
GatewayDisplay: _types.GatewayTypes.AZURE_GATEWAY
};
const testables = exports.testables = {
AzurePrompts,
ConfigFiles,
defaultLogFiles
};