@@ -15,6 +15,7 @@ const sdk = require('../../../../logic/sdk');
15
15
const _ = require ( 'lodash' ) ;
16
16
const installationProgress = require ( './installation-process' ) ;
17
17
const { to } = require ( './../../../../logic/cli-config/errors/awaitTo' ) ;
18
+ const { prettyError } = require ( '../../../../logic/cli-config/errors/helpers' ) ;
18
19
19
20
const INSTALLATION_DEFAULTS = {
20
21
NAMESPACE : 'codefresh' ,
@@ -24,32 +25,15 @@ const INSTALLATION_DEFAULTS = {
24
25
CF_CONTEXT_NAME : 'cf-runner' ,
25
26
} ;
26
27
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
-
45
28
async function handleError ( error , message , progressReporter , event ) {
46
29
if ( ! error ) {
47
30
return ;
48
31
}
49
32
if ( progressReporter ) {
50
33
await to ( progressReporter . report ( event , installationProgress . status . FAILURE ) ) ;
51
34
}
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' ) } ` ) ) ;
53
37
process . exit ( 1 ) ;
54
38
}
55
39
@@ -112,10 +96,10 @@ async function createAndExecuteDemoPipeline(runtimeName, progressReporter) {
112
96
113
97
async function getRecommendedKubeNamespace ( kubeconfigPath , kubeContextName ) {
114
98
const defaultName = INSTALLATION_DEFAULTS . NAMESPACE ;
115
- const namespaces = await getAllNamespaces ( kubeconfigPath , kubeContextName ) ;
99
+ const [ err , namespaces ] = await to ( getAllNamespaces ( kubeconfigPath , kubeContextName ) ) ;
116
100
let name ;
117
101
118
- if ( ! _ . isArray ( namespaces ) || ! _ . find ( namespaces , ns => ns === defaultName ) ) {
102
+ if ( err || ! _ . isArray ( namespaces ) || ! _ . find ( namespaces , ns => ns === defaultName ) ) {
119
103
name = defaultName ; // use the default name if there are no collisions
120
104
} else {
121
105
const namespacesSet = new Set ( namespaces ) ; // for fast lookup
@@ -130,8 +114,8 @@ async function getRecommendedKubeNamespace(kubeconfigPath, kubeContextName) {
130
114
}
131
115
132
116
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' ) ) ) {
135
119
return ! pipelines . docs . length ;
136
120
}
137
121
@@ -205,13 +189,13 @@ const initCmd = new Command({
205
189
yes : noQuestions ,
206
190
verbose,
207
191
name, url,
192
+ token,
208
193
} = argv ;
209
194
let {
210
195
'kube-context-name' : kubeContextName ,
211
196
'kube-namespace' : kubeNamespace ,
212
197
'set-default-runtime' : shouldMakeDefaultRe ,
213
198
'exec-demo-pipeline' : shouldExecutePipeline ,
214
- token,
215
199
} = argv ;
216
200
if ( _ . get ( sdk , 'config.context.isNoAuth' ) && ! token ) {
217
201
console . log ( 'Not authenticated as a Codefresh account: ' ) ;
@@ -229,7 +213,7 @@ const initCmd = new Command({
229
213
shouldExecutePipeline = INSTALLATION_DEFAULTS . RUN_DEMO_PIPELINE ;
230
214
} else {
231
215
console . log ( colors . green ( 'This installer will guide you through the Codefresh Runner installation process' ) ) ;
232
- if ( ! kubeContextName && ! noQuestions ) {
216
+ if ( ! kubeContextName ) {
233
217
const contexts = getAllKubeContexts ( kubeConfigPath ) ;
234
218
const currentKubeContext = getKubeContext ( kubeConfigPath ) ;
235
219
@@ -240,10 +224,11 @@ const initCmd = new Command({
240
224
default : currentKubeContext ,
241
225
choices : contexts ,
242
226
} ) ;
243
- kubeContextName = answer . context ;
227
+ kubeContextName = answer . context ; // need this to set the default kube namespace in the next question
244
228
}
229
+
245
230
const questions = [ ] ;
246
- if ( ! kubeNamespace && ! noQuestions ) {
231
+ if ( ! kubeNamespace ) {
247
232
questions . push ( {
248
233
type : 'input' ,
249
234
name : 'namespace' ,
@@ -253,8 +238,9 @@ const initCmd = new Command({
253
238
} ) ;
254
239
}
255
240
256
- if ( _ . isUndefined ( shouldMakeDefaultRe ) && ! noQuestions ) {
241
+ if ( _ . isUndefined ( shouldMakeDefaultRe ) ) {
257
242
if ( ! _ . get ( sdk , 'config.context.isNoAuth' ) && await isNewAccount ( ) ) {
243
+ // if this is a new account, don't ask and set this runtime as default
258
244
shouldMakeDefaultRe = true ;
259
245
} else {
260
246
questions . push ( {
@@ -266,7 +252,7 @@ const initCmd = new Command({
266
252
}
267
253
}
268
254
269
- if ( _ . isUndefined ( shouldExecutePipeline ) && ! noQuestions ) {
255
+ if ( _ . isUndefined ( shouldExecutePipeline ) ) {
270
256
questions . push ( {
271
257
type : 'confirm' ,
272
258
name : 'shouldExecutePipeline' ,
@@ -301,7 +287,8 @@ const initCmd = new Command({
301
287
const progressReporter = installationProgress . buildReporter ( sdk [ 'runner-installation' ] , progress ) ;
302
288
303
289
304
- if ( token ) { // Add context
290
+ if ( token ) {
291
+ // Create a new context and switch to that context
305
292
const createContextOptions = {
306
293
apiKey : token ,
307
294
name : INSTALLATION_DEFAULTS . CF_CONTEXT_NAME ,
@@ -312,8 +299,6 @@ const initCmd = new Command({
312
299
const config = await getConfigForSdk ( ) ;
313
300
await sdk . configure ( config ) ;
314
301
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' ) ;
317
302
}
318
303
319
304
// Install runner and runtime
@@ -330,21 +315,24 @@ const initCmd = new Command({
330
315
'storage-class-name' : storageClassName ,
331
316
terminateProcess : false ,
332
317
} ;
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 ) ;
335
320
336
321
await to ( progressReporter . report ( installationProgress . events . RUNNER_INSTALLED , installationProgress . status . SUCCESS ) ) ;
337
322
338
323
// Install monitoring
339
- await installMonitoring . handler ( {
324
+ const monitorInstallOptions = {
340
325
'kube-config-path' : kubeConfigPath ,
341
326
'cluster-id' : kubeContextName ,
342
327
'kube-context-name' : kubeContextName ,
343
328
'kube-namespace' : kubeNamespace ,
344
- token,
329
+ token : _ . get ( sdk , 'config.context.token' ) ,
345
330
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
+
348
336
await to ( progressReporter . report ( installationProgress . events . MONITOR_INSTALLED , installationProgress . status . SUCCESS ) ) ;
349
337
350
338
// Post Installation
0 commit comments