@@ -15,6 +15,7 @@ const sdk = require('../../../../logic/sdk');
1515const _ = require ( 'lodash' ) ;
1616const installationProgress = require ( './installation-process' ) ;
1717const { to } = require ( './../../../../logic/cli-config/errors/awaitTo' ) ;
18+ const { prettyError } = require ( '../../../../logic/cli-config/errors/helpers' ) ;
1819
1920const 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-
4528async 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
11397async 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
132116async 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
0 commit comments