Skip to content

Commit 556ceec

Browse files
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
1 parent 2f807af commit 556ceec

File tree

5 files changed

+94
-58
lines changed

5 files changed

+94
-58
lines changed

lib/interface/cli/commands/hybrid/delete.cmd.js

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,21 @@ const colors = require('colors');
1010
const DEFAULTS = require('../../defaults');
1111
const sdk = require('../../../../logic/sdk');
1212
const _ = require('lodash');
13+
const { to } = require('./../../../../logic/cli-config/errors/awaitTo');
14+
const { prettyError } = require('../../../../logic/cli-config/errors/helpers');
1315

1416
const defaultNamespace = 'codefresh';
1517

18+
async function handleError(error, message) {
19+
if (!error) {
20+
return;
21+
}
22+
23+
console.log(`${colors.red('Error:')} ${message}: ${prettyError(error)}`);
24+
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')}`));
25+
process.exit(1);
26+
}
27+
1628
const deleteCmd = new Command({
1729
root: false,
1830
parent: runnerRoot,
@@ -78,7 +90,8 @@ const deleteCmd = new Command({
7890
validate: value => (value !== undefined && value !== '') || 'Please enter namespace\'s name',
7991
});
8092
}
81-
const agents = await sdk.agents.list({});
93+
const [listAgentsErr, agents] = await to(sdk.agents.list({}));
94+
await handleError(listAgentsErr, 'Failed to get agents');
8295
if (!agentName) {
8396
questions.push({
8497
type: 'list',
@@ -95,38 +108,53 @@ const deleteCmd = new Command({
95108
// check that agent exists
96109
const agent = _.find(agents, curr => curr.name === agentName);
97110
if (!agent) {
98-
console.log(colors.red(`Agent with name ${agentName} doesn\'t exists`));
111+
console.log(colors.red(`Agent with name ${agentName} doesn't exists`));
99112
return;
100113
}
101114
if (agent.runtimes && agent.runtimes > 1) {
102-
console.log('Can\'t delete runner with more than one runtime , use runtime delete command');
115+
console.log('Can\'t delete runner with more than one runtime, use runtime delete command');
103116
return;
104117
}
105-
console.log(colors.green(`Uninstallation options summary : \n Context: ${colors.blue(kubeContextName)} \n Namespace: ${colors.blue(kubeNamespace)} \n Agent name: ${colors.blue(agentName)} `));
118+
119+
console.log(`\n${colors.green('Uninstallation options summary:')}
120+
1. Kubernetes Context: ${colors.cyan(kubeContextName)}
121+
2. Kubernetes Namespace: ${colors.cyan(kubeNamespace)}
122+
3. Agent name: ${colors.cyan(agentName)}
123+
`);
124+
106125
if (agent.runtimes.length === 1) {
107-
await unInstallRuntime.handler({
126+
const uninstallRuntimeOptions = {
108127
'agent-name': agentName,
109128
'runtime-kube-namespace': kubeNamespace,
110129
'runtime-kube-context-name': kubeContextName,
111130
'agent-kube-context-name': kubeContextName,
112131
'agent-kube-namespace': kubeNamespace,
113132
name: agent.runtimes[0],
114133
terminateProcess: false,
115-
});
134+
};
135+
const [uninstallReErr] = await to(unInstallRuntime.handler(uninstallRuntimeOptions));
136+
handleError(uninstallReErr, 'Failed to uninstall runtime-environment');
116137
}
117-
await unInstallAgent.handler({
138+
139+
const uninstallAgentOptions = {
118140
'kube-namespace': kubeNamespace,
119141
'kube-context-name': kubeContextName,
120142
name: agentName,
121143
terminateProcess: false,
122-
});
123-
await unInstallMonitor.handler({
144+
};
145+
const [uninstallAgentErr] = await to(unInstallAgent.handler(uninstallAgentOptions));
146+
handleError(uninstallAgentErr, 'Failed to uninstall agent');
147+
148+
const uninstallMonitorOptions = {
124149
'kube-namespace': kubeNamespace,
125150
'kube-context-name': kubeContextName,
126151
noExit: true, // to prevent if from calling: process.exit()
152+
};
153+
const [uninstallMonitorErr] = await to(unInstallMonitor.handler(uninstallMonitorOptions));
154+
handleError(uninstallMonitorErr, 'Failed to uninstall monitor');
127155

128-
});
129156
console.log('Successfully uninstalled Codefresh Runner');
157+
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')}`));
130158
process.exit(); // TODO : This is not needed - needed to be fixed
131159
},
132160
});

lib/interface/cli/commands/hybrid/init.cmd.js

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const sdk = require('../../../../logic/sdk');
1515
const _ = require('lodash');
1616
const installationProgress = require('./installation-process');
1717
const { to } = require('./../../../../logic/cli-config/errors/awaitTo');
18+
const { prettyError } = require('../../../../logic/cli-config/errors/helpers');
1819

1920
const INSTALLATION_DEFAULTS = {
2021
NAMESPACE: 'codefresh',
@@ -24,32 +25,15 @@ const INSTALLATION_DEFAULTS = {
2425
CF_CONTEXT_NAME: 'cf-runner',
2526
};
2627

27-
function prettyError(error) {
28-
try {
29-
const errMsg = _.get(error, 'message', error);
30-
let errObj = JSON.parse(errMsg);
31-
if (typeof errObj === 'string') {
32-
errObj = JSON.parse(errObj);
33-
}
34-
35-
if (!errObj.message) {
36-
return error;
37-
}
38-
39-
return errObj.code ? `${errObj.message} [code: ${errObj.code}]` : errObj.message;
40-
} catch (e) {
41-
return _.get(error, 'message', JSON.stringify(error));
42-
}
43-
}
44-
4528
async function handleError(error, message, progressReporter, event) {
4629
if (!error) {
4730
return;
4831
}
4932
if (progressReporter) {
5033
await to(progressReporter.report(event, installationProgress.status.FAILURE));
5134
}
52-
console.log(`${colors.red('Error: ')} ${message}: ${prettyError(error)}`);
35+
console.log(`${colors.red('Error:')} ${message}: ${prettyError(error)}`);
36+
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')}`));
5337
process.exit(1);
5438
}
5539

@@ -112,10 +96,10 @@ async function createAndExecuteDemoPipeline(runtimeName, progressReporter) {
11296

11397
async function getRecommendedKubeNamespace(kubeconfigPath, kubeContextName) {
11498
const defaultName = INSTALLATION_DEFAULTS.NAMESPACE;
115-
const namespaces = await getAllNamespaces(kubeconfigPath, kubeContextName);
99+
const [err, namespaces] = await to(getAllNamespaces(kubeconfigPath, kubeContextName));
116100
let name;
117101

118-
if (!_.isArray(namespaces) || !_.find(namespaces, ns => ns === defaultName)) {
102+
if (err || !_.isArray(namespaces) || !_.find(namespaces, ns => ns === defaultName)) {
119103
name = defaultName; // use the default name if there are no collisions
120104
} else {
121105
const namespacesSet = new Set(namespaces); // for fast lookup
@@ -130,8 +114,8 @@ async function getRecommendedKubeNamespace(kubeconfigPath, kubeContextName) {
130114
}
131115

132116
async function isNewAccount() {
133-
const pipelines = await sdk.pipelines.list({ });
134-
if (_.isArray(_.get(pipelines, 'docs'))) {
117+
const [pipelines, err] = await to(sdk.pipelines.list({ }));
118+
if (!err && _.isArray(_.get(pipelines, 'docs'))) {
135119
return !pipelines.docs.length;
136120
}
137121

@@ -205,13 +189,13 @@ const initCmd = new Command({
205189
yes: noQuestions,
206190
verbose,
207191
name, url,
192+
token,
208193
} = argv;
209194
let {
210195
'kube-context-name': kubeContextName,
211196
'kube-namespace': kubeNamespace,
212197
'set-default-runtime': shouldMakeDefaultRe,
213198
'exec-demo-pipeline': shouldExecutePipeline,
214-
token,
215199
} = argv;
216200
if (_.get(sdk, 'config.context.isNoAuth') && !token) {
217201
console.log('Not authenticated as a Codefresh account: ');
@@ -229,7 +213,7 @@ const initCmd = new Command({
229213
shouldExecutePipeline = INSTALLATION_DEFAULTS.RUN_DEMO_PIPELINE;
230214
} else {
231215
console.log(colors.green('This installer will guide you through the Codefresh Runner installation process'));
232-
if (!kubeContextName && !noQuestions) {
216+
if (!kubeContextName) {
233217
const contexts = getAllKubeContexts(kubeConfigPath);
234218
const currentKubeContext = getKubeContext(kubeConfigPath);
235219

@@ -240,10 +224,11 @@ const initCmd = new Command({
240224
default: currentKubeContext,
241225
choices: contexts,
242226
});
243-
kubeContextName = answer.context;
227+
kubeContextName = answer.context; // need this to set the default kube namespace in the next question
244228
}
229+
245230
const questions = [];
246-
if (!kubeNamespace && !noQuestions) {
231+
if (!kubeNamespace) {
247232
questions.push({
248233
type: 'input',
249234
name: 'namespace',
@@ -253,8 +238,9 @@ const initCmd = new Command({
253238
});
254239
}
255240

256-
if (_.isUndefined(shouldMakeDefaultRe) && !noQuestions) {
241+
if (_.isUndefined(shouldMakeDefaultRe)) {
257242
if (!_.get(sdk, 'config.context.isNoAuth') && await isNewAccount()) {
243+
// if this is a new account, don't ask and set this runtime as default
258244
shouldMakeDefaultRe = true;
259245
} else {
260246
questions.push({
@@ -266,7 +252,7 @@ const initCmd = new Command({
266252
}
267253
}
268254

269-
if (_.isUndefined(shouldExecutePipeline) && !noQuestions) {
255+
if (_.isUndefined(shouldExecutePipeline)) {
270256
questions.push({
271257
type: 'confirm',
272258
name: 'shouldExecutePipeline',
@@ -301,7 +287,8 @@ const initCmd = new Command({
301287
const progressReporter = installationProgress.buildReporter(sdk['runner-installation'], progress);
302288

303289

304-
if (token) { // Add context
290+
if (token) {
291+
// Create a new context and switch to that context
305292
const createContextOptions = {
306293
apiKey: token,
307294
name: INSTALLATION_DEFAULTS.CF_CONTEXT_NAME,
@@ -312,8 +299,6 @@ const initCmd = new Command({
312299
const config = await getConfigForSdk();
313300
await sdk.configure(config);
314301
console.log(`A Codefresh context named '${INSTALLATION_DEFAULTS.CF_CONTEXT_NAME}' was added to your "cfconfig" file.`);
315-
} else {
316-
token = _.get(sdk, 'config.context.token');
317302
}
318303

319304
// Install runner and runtime
@@ -330,21 +315,24 @@ const initCmd = new Command({
330315
'storage-class-name': storageClassName,
331316
terminateProcess: false,
332317
};
333-
const [err, runtimeName] = await to(installAgent.handler(agentInstallOptions));
334-
await handleError(err, 'Runner installation failed', progressReporter, installationProgress.events.RUNNER_INSTALLED);
318+
const [runnerErr, runtimeName] = await to(installAgent.handler(agentInstallOptions));
319+
await handleError(runnerErr, 'Runner installation failed', progressReporter, installationProgress.events.RUNNER_INSTALLED);
335320

336321
await to(progressReporter.report(installationProgress.events.RUNNER_INSTALLED, installationProgress.status.SUCCESS));
337322

338323
// Install monitoring
339-
await installMonitoring.handler({
324+
const monitorInstallOptions = {
340325
'kube-config-path': kubeConfigPath,
341326
'cluster-id': kubeContextName,
342327
'kube-context-name': kubeContextName,
343328
'kube-namespace': kubeNamespace,
344-
token,
329+
token: _.get(sdk, 'config.context.token'),
345330
verbose,
346-
noExit: true, // to prevent if from calling: process.exit()
347-
});
331+
noExit: true, // to prevent if from calling inner: process.exit()
332+
};
333+
const [monitorErr] = await to(installMonitoring.handler(monitorInstallOptions));
334+
await handleError(monitorErr, 'Monitor installation failed', progressReporter, installationProgress.events.MONITOR_INSTALLED);
335+
348336
await to(progressReporter.report(installationProgress.events.MONITOR_INSTALLED, installationProgress.status.SUCCESS));
349337

350338
// Post Installation
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const _ = require('lodash');
2+
3+
function prettyError(error) {
4+
try {
5+
const errMsg = _.get(error, 'message', error);
6+
let errObj = JSON.parse(errMsg);
7+
if (typeof errObj === 'string') {
8+
errObj = JSON.parse(errObj);
9+
}
10+
11+
if (!errObj.message) {
12+
return error;
13+
}
14+
15+
return errObj.code ? `${errObj.message} [code: ${errObj.code}]` : errObj.message;
16+
} catch (e) {
17+
return _.get(error, 'message', JSON.stringify(error));
18+
}
19+
}
20+
21+
module.exports = {
22+
prettyError,
23+
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "codefresh",
3-
"version": "0.63.3",
3+
"version": "0.63.4",
44
"description": "Codefresh command line utility",
55
"main": "index.js",
66
"preferGlobal": true,

test-setup.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,10 @@ global.verifyResponsesReturned = async (responses) => {
2020
* downloads spec one time for all tests
2121
* */
2222
global.configureSdk = async () => {
23-
if (!SDK_CONFIGURED) {
24-
SDK_CONFIGURED = true;
25-
Object.keys(sdk).forEach(key => delete sdk[key]);
26-
sdk.configure(await Config.load({
27-
url: 'http://not.needed',
28-
apiKey: 'not-needed',
29-
spec: { json: openapi },
30-
}));
31-
}
23+
Object.keys(sdk).forEach(key => delete sdk[key]);
24+
sdk.configure(await Config.load({
25+
url: 'http://not.needed',
26+
apiKey: 'not-needed',
27+
spec: { json: openapi },
28+
}));
3229
};

0 commit comments

Comments
 (0)