@@ -13,6 +13,8 @@ const colors = require('colors');
13
13
const DEFAULTS = require ( '../../defaults' ) ;
14
14
const sdk = require ( '../../../../logic/sdk' ) ;
15
15
const _ = require ( 'lodash' ) ;
16
+ const installationProgress = require ( './installation-process' ) ;
17
+ const { to } = require ( './../../../../logic/cli-config/errors/awaitTo' ) ;
16
18
17
19
const INSTALLATION_DEFAULTS = {
18
20
NAMESPACE : 'codefresh' ,
@@ -40,7 +42,18 @@ function prettyError(error) {
40
42
}
41
43
}
42
44
43
- async function createDemoPipeline ( runtimeName ) {
45
+ async function handleError ( error , message , progressReporter , event ) {
46
+ if ( ! error ) {
47
+ return ;
48
+ }
49
+ if ( progressReporter ) {
50
+ await to ( progressReporter . report ( event , installationProgress . status . FAILURE ) ) ;
51
+ }
52
+ console . log ( `${ colors . red ( 'Error: ' ) } ${ message } : ${ prettyError ( error ) } ` ) ;
53
+ process . exit ( 1 ) ;
54
+ }
55
+
56
+ async function createDemoPipeline ( runtimeName , progressReporter ) {
44
57
const pipeline = await sdk . pipelines . create ( { metadata : { name : INSTALLATION_DEFAULTS . DEMO_PIPELINE_NAME } } ) ;
45
58
pipeline . spec . runtimeEnvironment = {
46
59
name : runtimeName ,
@@ -63,37 +76,37 @@ async function createDemoPipeline(runtimeName) {
63
76
version : pipeline . version ,
64
77
} ,
65
78
) ;
79
+
80
+ await to ( progressReporter . report ( installationProgress . events . PIPELINE_CREATED , installationProgress . status . SUCCESS ) ) ;
66
81
}
67
82
68
- async function createAndExecuteDemoPipeline ( runtimeName ) {
83
+ async function createAndExecuteDemoPipeline ( runtimeName , progressReporter ) {
69
84
let demoPipelineExists = false ;
70
85
71
- try {
72
- const pipelines = await sdk . pipelines . list ( { id : INSTALLATION_DEFAULTS . DEMO_PIPELINE_NAME } ) ;
73
- if ( _ . get ( pipelines , 'docs.length' ) ) {
74
- demoPipelineExists = true ;
75
- }
76
- } catch ( error ) {
77
- console . log ( `Failed to fetch account pipelines, cause: ${ error . message } ` ) ;
86
+ const [ getPipelinesError , pipelines ] = await to ( sdk . pipelines . list ( { id : INSTALLATION_DEFAULTS . DEMO_PIPELINE_NAME } ) ) ;
87
+ if ( getPipelinesError ) {
88
+ console . log ( `Failed to fetch account pipelines, cause: ${ getPipelinesError . message } ` ) ;
89
+ } else if ( _ . get ( pipelines , 'docs.length' ) ) {
90
+ demoPipelineExists = true ;
78
91
}
79
92
80
93
if ( ! demoPipelineExists ) {
81
94
console . log ( `Creating demo pipeline with the name: "${ colors . cyan ( INSTALLATION_DEFAULTS . DEMO_PIPELINE_NAME ) } "` ) ;
82
- try {
83
- await createDemoPipeline ( runtimeName ) ;
84
- } catch ( error ) {
85
- console . log ( `${ colors . red ( 'Error: ' ) } Failed to create demo pipeline, cause: ${ prettyError ( error ) } ` ) ;
86
- }
95
+ const [ createDemoPipelineError ] = await to ( createDemoPipeline ( runtimeName , progressReporter ) ) ;
96
+ await handleError ( createDemoPipelineError , 'Failed to create demo pipeline' , progressReporter , installationProgress . events . PIPELINE_CREATED ) ;
87
97
} else {
88
98
console . log ( `Demo pipeline with the name: "${ colors . cyan ( INSTALLATION_DEFAULTS . DEMO_PIPELINE_NAME ) } " already exists` ) ;
89
99
}
90
100
91
101
console . log ( `${ colors . yellow ( '*NOTE* Running a pipeline for the first time might take longer than usual.' ) } ` ) ;
92
102
console . log ( `Executing pipeline "${ colors . cyan ( INSTALLATION_DEFAULTS . DEMO_PIPELINE_NAME ) } "` ) ;
93
- await pipelinesRunCmd . handler ( {
103
+ const [ pipelineExecutionError ] = await to ( pipelinesRunCmd . handler ( {
94
104
name : INSTALLATION_DEFAULTS . DEMO_PIPELINE_NAME ,
95
105
exitProcess : false ,
96
- } ) ;
106
+ } ) ) ;
107
+ await handleError ( pipelineExecutionError , 'Failed to run demo pipeline' , progressReporter , installationProgress . events . PIPELINE_EXECUTED ) ;
108
+
109
+ await to ( progressReporter . report ( installationProgress . events . PIPELINE_EXECUTED , installationProgress . status . SUCCESS ) ) ;
97
110
}
98
111
99
112
async function getRecommendedKubeNamespace ( kubeconfigPath , kubeContextName ) {
@@ -215,7 +228,6 @@ const initCmd = new Command({
215
228
'exec-demo-pipeline' : shouldExecutePipeline ,
216
229
token,
217
230
} = argv ;
218
-
219
231
if ( _ . get ( sdk , 'config.context.isNoAuth' ) && ! token ) {
220
232
console . log ( 'Not authenticated as a Codefresh account: ' ) ;
221
233
console . log ( 'In order to install a Codefresh Runner you need to provide ' +
@@ -292,48 +304,55 @@ const initCmd = new Command({
292
304
4. Execute demo pipeline after install: ${ colors . cyan ( ! ! shouldExecutePipeline ) }
293
305
` ) ;
294
306
307
+ const [ , progress ] = await to ( async ( ) => installationProgress . create ( sdk [ 'runner-installation' ] , {
308
+ options : {
309
+ kubeContextName,
310
+ kubeNamespace,
311
+ shouldMakeDefaultRe,
312
+ shouldExecutePipeline,
313
+ } ,
314
+ } ) ) ;
315
+
316
+ const progressReporter = installationProgress . buildReporter ( sdk [ 'runner-installation' ] , progress ) ;
317
+
318
+
295
319
if ( token ) { // Add context
296
- try {
297
- await createContext . handler ( {
298
- apiKey : token ,
299
- name : INSTALLATION_DEFAULTS . CF_CONTEXT_NAME ,
300
- url,
301
- } ) ;
302
- const config = await getConfigForSdk ( ) ;
303
- await sdk . configure ( config ) ;
304
- console . log ( `A Codefresh context named '${ INSTALLATION_DEFAULTS . CF_CONTEXT_NAME } ' was added to your "cfconfig" file.` ) ;
305
- } catch ( error ) {
306
- console . log ( `${ colors . red ( 'Error:' ) } Could not use the provided token, failed with error: ${ prettyError ( error ) } ` ) ;
307
- process . exit ( 1 ) ;
308
- }
320
+ const createContextOptions = {
321
+ apiKey : token ,
322
+ name : INSTALLATION_DEFAULTS . CF_CONTEXT_NAME ,
323
+ url,
324
+ } ;
325
+ const [ err ] = await to ( createContext . handler ( createContextOptions ) ) ;
326
+ await handleError ( err , 'Failed to use the provided token' ) ;
327
+ const config = await getConfigForSdk ( ) ;
328
+ await sdk . configure ( config ) ;
329
+ console . log ( `A Codefresh context named '${ INSTALLATION_DEFAULTS . CF_CONTEXT_NAME } ' was added to your "cfconfig" file.` ) ;
309
330
} else {
310
331
token = _ . get ( sdk , 'config.context.token' ) ;
311
332
}
312
333
313
334
// Install runner and runtime
314
- let runtimeName ;
315
- try {
316
- runtimeName = await installAgent . handler ( {
317
- name,
318
- 'kube-context-name' : kubeContextName ,
319
- 'kube-node-selector' : kubeNodeSelector ,
320
- 'dry-run' : dryRun ,
321
- 'in-cluster' : inCluster ,
322
- 'kube-namespace' : kubeNamespace ,
323
- 'kubernetes-runner-type' : kubernetesRunnerType ,
324
- tolerations,
325
- 'venona-version' : venonaVersion ,
326
- 'kube-config-path' : kubeConfigPath ,
327
- 'skip-version-check' : skipVersionCheck ,
328
- 'install-runtime' : true ,
329
- verbose,
330
- 'make-default-runtime' : shouldMakeDefaultRe ,
331
- terminateProcess : false ,
332
- } ) ;
333
- } catch ( error ) {
334
- console . log ( `${ colors . red ( 'Error: ' ) } Runner installation failed with error: ${ prettyError ( error ) } ` ) ;
335
- process . exit ( 1 ) ;
336
- }
335
+ const agentInstallOptions = {
336
+ name,
337
+ 'kube-context-name' : kubeContextName ,
338
+ 'kube-node-selector' : kubeNodeSelector ,
339
+ 'dry-run' : dryRun ,
340
+ 'in-cluster' : inCluster ,
341
+ 'kube-namespace' : kubeNamespace ,
342
+ 'kubernetes-runner-type' : kubernetesRunnerType ,
343
+ tolerations,
344
+ 'venona-version' : venonaVersion ,
345
+ 'kube-config-path' : kubeConfigPath ,
346
+ 'skip-version-check' : skipVersionCheck ,
347
+ 'install-runtime' : true ,
348
+ verbose,
349
+ 'make-default-runtime' : shouldMakeDefaultRe ,
350
+ terminateProcess : false ,
351
+ } ;
352
+ const [ err , runtimeName ] = await to ( installAgent . handler ( agentInstallOptions ) ) ;
353
+ await handleError ( err , 'Runner installation failed' , progressReporter , installationProgress . events . RUNNER_INSTALLED ) ;
354
+
355
+ await to ( progressReporter . report ( installationProgress . events . RUNNER_INSTALLED , installationProgress . status . SUCCESS ) ) ;
337
356
338
357
// Install monitoring
339
358
await installMonitoring . handler ( {
@@ -345,16 +364,18 @@ const initCmd = new Command({
345
364
verbose,
346
365
noExit : true , // to prevent if from calling: process.exit()
347
366
} ) ;
367
+ await to ( progressReporter . report ( installationProgress . events . MONITOR_INSTALLED , installationProgress . status . SUCCESS ) ) ;
348
368
349
369
// Post Installation
350
370
if ( shouldExecutePipeline ) {
351
- await createAndExecuteDemoPipeline ( runtimeName ) ;
371
+ await createAndExecuteDemoPipeline ( runtimeName , progressReporter ) ;
352
372
}
353
373
354
374
console . log ( colors . green ( '\nRunner Status:' ) ) ;
355
375
await getAgents . handler ( { } ) ;
356
376
console . log ( colors . green ( `\nDocumenation link: ${ colors . blue ( 'https://codefresh.io/docs/docs/enterprise/codefresh-runner/#codefresh-runner-preview-release' ) } ` ) ) ;
357
377
console . log ( colors . green ( `If you had any issues with the installation please report them at: ${ colors . blue ( 'https://github.com/codefresh-io/cli/issues/new' ) } ` ) ) ;
378
+ await to ( progressReporter . report ( installationProgress . events . FINISHED , installationProgress . status . SUCCESS ) ) ;
358
379
process . exit ( ) ; // TODO : This is not needed - needed to be fixed
359
380
} ,
360
381
} ) ;
0 commit comments