From 556ceec0e28cdb8e0a94307b71f26b80b83420e7 Mon Sep 17 00:00:00 2001 From: roi-codefresh <60569147+roi-codefresh@users.noreply.github.com> Date: Tue, 19 May 2020 16:46:05 +0300 Subject: [PATCH] Saas-7214 added open issues link to all flows in runner init (#465) * added open issues link to all flows in runner init and delete * extracted pretty error --- .../cli/commands/hybrid/delete.cmd.js | 48 +++++++++++--- lib/interface/cli/commands/hybrid/init.cmd.js | 64 ++++++++----------- lib/logic/cli-config/errors/helpers.js | 23 +++++++ package.json | 2 +- test-setup.js | 15 ++--- 5 files changed, 94 insertions(+), 58 deletions(-) create mode 100644 lib/logic/cli-config/errors/helpers.js diff --git a/lib/interface/cli/commands/hybrid/delete.cmd.js b/lib/interface/cli/commands/hybrid/delete.cmd.js index 45410b815..693c6e00a 100644 --- a/lib/interface/cli/commands/hybrid/delete.cmd.js +++ b/lib/interface/cli/commands/hybrid/delete.cmd.js @@ -10,9 +10,21 @@ const colors = require('colors'); const DEFAULTS = require('../../defaults'); const sdk = require('../../../../logic/sdk'); const _ = require('lodash'); +const { to } = require('./../../../../logic/cli-config/errors/awaitTo'); +const { prettyError } = require('../../../../logic/cli-config/errors/helpers'); const defaultNamespace = 'codefresh'; +async function handleError(error, message) { + if (!error) { + return; + } + + console.log(`${colors.red('Error:')} ${message}: ${prettyError(error)}`); + console.log(colors.green(`If you had any issues with the uninstallation process please report them at: ${colors.blue('https://github.com/codefresh-io/cli/issues/new')}`)); + process.exit(1); +} + const deleteCmd = new Command({ root: false, parent: runnerRoot, @@ -78,7 +90,8 @@ const deleteCmd = new Command({ validate: value => (value !== undefined && value !== '') || 'Please enter namespace\'s name', }); } - const agents = await sdk.agents.list({}); + const [listAgentsErr, agents] = await to(sdk.agents.list({})); + await handleError(listAgentsErr, 'Failed to get agents'); if (!agentName) { questions.push({ type: 'list', @@ -95,16 +108,22 @@ const deleteCmd = new Command({ // check that agent exists const agent = _.find(agents, curr => curr.name === agentName); if (!agent) { - console.log(colors.red(`Agent with name ${agentName} doesn\'t exists`)); + console.log(colors.red(`Agent with name ${agentName} doesn't exists`)); return; } if (agent.runtimes && agent.runtimes > 1) { - console.log('Can\'t delete runner with more than one runtime , use runtime delete command'); + console.log('Can\'t delete runner with more than one runtime, use runtime delete command'); return; } - console.log(colors.green(`Uninstallation options summary : \n Context: ${colors.blue(kubeContextName)} \n Namespace: ${colors.blue(kubeNamespace)} \n Agent name: ${colors.blue(agentName)} `)); + + console.log(`\n${colors.green('Uninstallation options summary:')} +1. Kubernetes Context: ${colors.cyan(kubeContextName)} +2. Kubernetes Namespace: ${colors.cyan(kubeNamespace)} +3. Agent name: ${colors.cyan(agentName)} +`); + if (agent.runtimes.length === 1) { - await unInstallRuntime.handler({ + const uninstallRuntimeOptions = { 'agent-name': agentName, 'runtime-kube-namespace': kubeNamespace, 'runtime-kube-context-name': kubeContextName, @@ -112,21 +131,30 @@ const deleteCmd = new Command({ 'agent-kube-namespace': kubeNamespace, name: agent.runtimes[0], terminateProcess: false, - }); + }; + const [uninstallReErr] = await to(unInstallRuntime.handler(uninstallRuntimeOptions)); + handleError(uninstallReErr, 'Failed to uninstall runtime-environment'); } - await unInstallAgent.handler({ + + const uninstallAgentOptions = { 'kube-namespace': kubeNamespace, 'kube-context-name': kubeContextName, name: agentName, terminateProcess: false, - }); - await unInstallMonitor.handler({ + }; + const [uninstallAgentErr] = await to(unInstallAgent.handler(uninstallAgentOptions)); + handleError(uninstallAgentErr, 'Failed to uninstall agent'); + + const uninstallMonitorOptions = { 'kube-namespace': kubeNamespace, 'kube-context-name': kubeContextName, noExit: true, // to prevent if from calling: process.exit() + }; + const [uninstallMonitorErr] = await to(unInstallMonitor.handler(uninstallMonitorOptions)); + handleError(uninstallMonitorErr, 'Failed to uninstall monitor'); - }); console.log('Successfully uninstalled Codefresh Runner'); + console.log(colors.green(`\nIf you had any issues with the uninstallation process please report them at: ${colors.blue('https://github.com/codefresh-io/cli/issues/new')}`)); process.exit(); // TODO : This is not needed - needed to be fixed }, }); diff --git a/lib/interface/cli/commands/hybrid/init.cmd.js b/lib/interface/cli/commands/hybrid/init.cmd.js index ee14de80f..179be6462 100644 --- a/lib/interface/cli/commands/hybrid/init.cmd.js +++ b/lib/interface/cli/commands/hybrid/init.cmd.js @@ -15,6 +15,7 @@ const sdk = require('../../../../logic/sdk'); const _ = require('lodash'); const installationProgress = require('./installation-process'); const { to } = require('./../../../../logic/cli-config/errors/awaitTo'); +const { prettyError } = require('../../../../logic/cli-config/errors/helpers'); const INSTALLATION_DEFAULTS = { NAMESPACE: 'codefresh', @@ -24,24 +25,6 @@ const INSTALLATION_DEFAULTS = { CF_CONTEXT_NAME: 'cf-runner', }; -function prettyError(error) { - try { - const errMsg = _.get(error, 'message', error); - let errObj = JSON.parse(errMsg); - if (typeof errObj === 'string') { - errObj = JSON.parse(errObj); - } - - if (!errObj.message) { - return error; - } - - return errObj.code ? `${errObj.message} [code: ${errObj.code}]` : errObj.message; - } catch (e) { - return _.get(error, 'message', JSON.stringify(error)); - } -} - async function handleError(error, message, progressReporter, event) { if (!error) { return; @@ -49,7 +32,8 @@ async function handleError(error, message, progressReporter, event) { if (progressReporter) { await to(progressReporter.report(event, installationProgress.status.FAILURE)); } - console.log(`${colors.red('Error: ')} ${message}: ${prettyError(error)}`); + console.log(`${colors.red('Error:')} ${message}: ${prettyError(error)}`); + console.log(colors.green(`\nIf you had any issues with the installation please report them at: ${colors.blue('https://github.com/codefresh-io/cli/issues/new')}`)); process.exit(1); } @@ -112,10 +96,10 @@ async function createAndExecuteDemoPipeline(runtimeName, progressReporter) { async function getRecommendedKubeNamespace(kubeconfigPath, kubeContextName) { const defaultName = INSTALLATION_DEFAULTS.NAMESPACE; - const namespaces = await getAllNamespaces(kubeconfigPath, kubeContextName); + const [err, namespaces] = await to(getAllNamespaces(kubeconfigPath, kubeContextName)); let name; - if (!_.isArray(namespaces) || !_.find(namespaces, ns => ns === defaultName)) { + if (err || !_.isArray(namespaces) || !_.find(namespaces, ns => ns === defaultName)) { name = defaultName; // use the default name if there are no collisions } else { const namespacesSet = new Set(namespaces); // for fast lookup @@ -130,8 +114,8 @@ async function getRecommendedKubeNamespace(kubeconfigPath, kubeContextName) { } async function isNewAccount() { - const pipelines = await sdk.pipelines.list({ }); - if (_.isArray(_.get(pipelines, 'docs'))) { + const [pipelines, err] = await to(sdk.pipelines.list({ })); + if (!err && _.isArray(_.get(pipelines, 'docs'))) { return !pipelines.docs.length; } @@ -205,13 +189,13 @@ const initCmd = new Command({ yes: noQuestions, verbose, name, url, + token, } = argv; let { 'kube-context-name': kubeContextName, 'kube-namespace': kubeNamespace, 'set-default-runtime': shouldMakeDefaultRe, 'exec-demo-pipeline': shouldExecutePipeline, - token, } = argv; if (_.get(sdk, 'config.context.isNoAuth') && !token) { console.log('Not authenticated as a Codefresh account: '); @@ -229,7 +213,7 @@ const initCmd = new Command({ shouldExecutePipeline = INSTALLATION_DEFAULTS.RUN_DEMO_PIPELINE; } else { console.log(colors.green('This installer will guide you through the Codefresh Runner installation process')); - if (!kubeContextName && !noQuestions) { + if (!kubeContextName) { const contexts = getAllKubeContexts(kubeConfigPath); const currentKubeContext = getKubeContext(kubeConfigPath); @@ -240,10 +224,11 @@ const initCmd = new Command({ default: currentKubeContext, choices: contexts, }); - kubeContextName = answer.context; + kubeContextName = answer.context; // need this to set the default kube namespace in the next question } + const questions = []; - if (!kubeNamespace && !noQuestions) { + if (!kubeNamespace) { questions.push({ type: 'input', name: 'namespace', @@ -253,8 +238,9 @@ const initCmd = new Command({ }); } - if (_.isUndefined(shouldMakeDefaultRe) && !noQuestions) { + if (_.isUndefined(shouldMakeDefaultRe)) { if (!_.get(sdk, 'config.context.isNoAuth') && await isNewAccount()) { + // if this is a new account, don't ask and set this runtime as default shouldMakeDefaultRe = true; } else { questions.push({ @@ -266,7 +252,7 @@ const initCmd = new Command({ } } - if (_.isUndefined(shouldExecutePipeline) && !noQuestions) { + if (_.isUndefined(shouldExecutePipeline)) { questions.push({ type: 'confirm', name: 'shouldExecutePipeline', @@ -301,7 +287,8 @@ const initCmd = new Command({ const progressReporter = installationProgress.buildReporter(sdk['runner-installation'], progress); - if (token) { // Add context + if (token) { + // Create a new context and switch to that context const createContextOptions = { apiKey: token, name: INSTALLATION_DEFAULTS.CF_CONTEXT_NAME, @@ -312,8 +299,6 @@ const initCmd = new Command({ const config = await getConfigForSdk(); await sdk.configure(config); console.log(`A Codefresh context named '${INSTALLATION_DEFAULTS.CF_CONTEXT_NAME}' was added to your "cfconfig" file.`); - } else { - token = _.get(sdk, 'config.context.token'); } // Install runner and runtime @@ -330,21 +315,24 @@ const initCmd = new Command({ 'storage-class-name': storageClassName, terminateProcess: false, }; - const [err, runtimeName] = await to(installAgent.handler(agentInstallOptions)); - await handleError(err, 'Runner installation failed', progressReporter, installationProgress.events.RUNNER_INSTALLED); + const [runnerErr, runtimeName] = await to(installAgent.handler(agentInstallOptions)); + await handleError(runnerErr, 'Runner installation failed', progressReporter, installationProgress.events.RUNNER_INSTALLED); await to(progressReporter.report(installationProgress.events.RUNNER_INSTALLED, installationProgress.status.SUCCESS)); // Install monitoring - await installMonitoring.handler({ + const monitorInstallOptions = { 'kube-config-path': kubeConfigPath, 'cluster-id': kubeContextName, 'kube-context-name': kubeContextName, 'kube-namespace': kubeNamespace, - token, + token: _.get(sdk, 'config.context.token'), verbose, - noExit: true, // to prevent if from calling: process.exit() - }); + noExit: true, // to prevent if from calling inner: process.exit() + }; + const [monitorErr] = await to(installMonitoring.handler(monitorInstallOptions)); + await handleError(monitorErr, 'Monitor installation failed', progressReporter, installationProgress.events.MONITOR_INSTALLED); + await to(progressReporter.report(installationProgress.events.MONITOR_INSTALLED, installationProgress.status.SUCCESS)); // Post Installation diff --git a/lib/logic/cli-config/errors/helpers.js b/lib/logic/cli-config/errors/helpers.js new file mode 100644 index 000000000..f4d3cd876 --- /dev/null +++ b/lib/logic/cli-config/errors/helpers.js @@ -0,0 +1,23 @@ +const _ = require('lodash'); + +function prettyError(error) { + try { + const errMsg = _.get(error, 'message', error); + let errObj = JSON.parse(errMsg); + if (typeof errObj === 'string') { + errObj = JSON.parse(errObj); + } + + if (!errObj.message) { + return error; + } + + return errObj.code ? `${errObj.message} [code: ${errObj.code}]` : errObj.message; + } catch (e) { + return _.get(error, 'message', JSON.stringify(error)); + } +} + +module.exports = { + prettyError, +}; diff --git a/package.json b/package.json index 2fff1177c..b07bc5b17 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "codefresh", - "version": "0.63.3", + "version": "0.63.4", "description": "Codefresh command line utility", "main": "index.js", "preferGlobal": true, diff --git a/test-setup.js b/test-setup.js index 718216410..403246cec 100644 --- a/test-setup.js +++ b/test-setup.js @@ -20,13 +20,10 @@ global.verifyResponsesReturned = async (responses) => { * downloads spec one time for all tests * */ global.configureSdk = async () => { - if (!SDK_CONFIGURED) { - SDK_CONFIGURED = true; - Object.keys(sdk).forEach(key => delete sdk[key]); - sdk.configure(await Config.load({ - url: 'http://not.needed', - apiKey: 'not-needed', - spec: { json: openapi }, - })); - } + Object.keys(sdk).forEach(key => delete sdk[key]); + sdk.configure(await Config.load({ + url: 'http://not.needed', + apiKey: 'not-needed', + spec: { json: openapi }, + })); };