22const Command = require ( '../../Command' ) ;
33const runnerRoot = require ( '../root/runner.cmd' ) ;
44const inquirer = require ( 'inquirer' ) ;
5- const { getAllKubeContexts, getKubeContext } = require ( '../../helpers/kubernetes' ) ;
5+ const { getAllKubeContexts, getKubeContext, getAllNamespaces } = require ( '../../helpers/kubernetes' ) ;
66const installAgent = require ( '../agent/install.cmd' ) ;
77const pipelinesRunCmd = require ( '../pipeline/run.cmd' ) ;
88const installMonitoring = require ( '../monitor/install.cmd' ) ;
@@ -18,7 +18,7 @@ const INSTALLATION_DEFAULTS = {
1818 NAMESPACE : 'codefresh' ,
1919 MAKE_DEFAULT_RE : true ,
2020 RUN_DEMO_PIPELINE : true ,
21- DEMO_PIPELINE_NAME : 'Hello Codefresh' ,
21+ DEMO_PIPELINE_NAME : 'Codefresh-Runner Demo ' ,
2222 CF_CONTEXT_NAME : 'cf-runner' ,
2323} ;
2424
@@ -29,7 +29,12 @@ function prettyError(error) {
2929 if ( typeof errObj === 'string' ) {
3030 errObj = JSON . parse ( errObj ) ;
3131 }
32- return _ . get ( errObj , 'message' , error ) ;
32+
33+ if ( ! errObj . message ) {
34+ return error ;
35+ }
36+
37+ return errObj . code ? `${ errObj . message } [code: ${ errObj . code } ]` : errObj . message ;
3338 } catch ( e ) {
3439 return _ . get ( error , 'message' , JSON . stringify ( error ) ) ;
3540 }
@@ -45,8 +50,8 @@ async function createDemoPipeline(runtimeName) {
4550 pipeline . spec . steps . test = {
4651 stage : 'test' ,
4752 title : 'test' ,
48- image : 'ubuntu :latest' ,
49- commands : [ 'echo hello codefresh' ] ,
53+ image : 'alpine :latest' ,
54+ commands : [ 'echo hello codefresh runner! ' ] ,
5055 } ;
5156
5257 await sdk . pipelines . replace (
@@ -83,14 +88,33 @@ async function createAndExecuteDemoPipeline(runtimeName) {
8388 console . log ( `Demo pipeline with the name: "${ colors . cyan ( INSTALLATION_DEFAULTS . DEMO_PIPELINE_NAME ) } " already exists` ) ;
8489 }
8590
86- console . log ( `${ colors . yellow ( '*NOTE* Running a pipeline for the first time might take a longer than usual' ) } ` ) ;
91+ console . log ( `${ colors . yellow ( '*NOTE* Running a pipeline for the first time might take longer than usual. ' ) } ` ) ;
8792 console . log ( `Executing pipeline "${ colors . cyan ( INSTALLATION_DEFAULTS . DEMO_PIPELINE_NAME ) } "` ) ;
8893 await pipelinesRunCmd . handler ( {
8994 name : INSTALLATION_DEFAULTS . DEMO_PIPELINE_NAME ,
9095 exitProcess : false ,
9196 } ) ;
9297}
9398
99+ async function getRecommendedKubeNamespace ( kubeconfigPath , kubeContextName ) {
100+ const defaultName = INSTALLATION_DEFAULTS . NAMESPACE ;
101+ const namespaces = await getAllNamespaces ( kubeconfigPath , kubeContextName ) ;
102+ let name ;
103+
104+ if ( ! _ . isArray ( namespaces ) || ! _ . find ( namespaces , ns => ns === defaultName ) ) {
105+ name = defaultName ; // use the default name if there are no collisions
106+ } else {
107+ const namespacesSet = new Set ( namespaces ) ; // for fast lookup
108+ let i = 1 ;
109+ while ( namespacesSet . has ( `${ defaultName } -${ i } ` ) ) {
110+ i += 1 ;
111+ }
112+ name = `${ defaultName } -${ i } ` ;
113+ }
114+
115+ return name ;
116+ }
117+
94118const initCmd = new Command ( {
95119 root : false ,
96120 parent : runnerRoot ,
@@ -194,28 +218,30 @@ const initCmd = new Command({
194218 if ( noQuestions ) {
195219 // set defaults
196220 kubeContextName = getKubeContext ( kubeConfigPath ) ;
197- kubeNamespace = INSTALLATION_DEFAULTS . NAMESPACE ;
221+ kubeNamespace = await getRecommendedKubeNamespace ( kubeConfigPath , kubeContextName ) ;
198222 shouldMakeDefaultRe = INSTALLATION_DEFAULTS . MAKE_DEFAULT_RE ;
199223 shouldExecutePipeline = INSTALLATION_DEFAULTS . RUN_DEMO_PIPELINE ;
200224 } else {
201- const questions = [ ] ;
225+ console . log ( colors . green ( 'This installer will guide you through the Codefresh Runner installation process' ) ) ;
202226 if ( ! kubeContextName && ! noQuestions ) {
203227 const contexts = getAllKubeContexts ( kubeConfigPath ) ;
204228 const currentKubeContext = getKubeContext ( kubeConfigPath ) ;
205229
206- questions . push ( {
230+ const answer = await inquirer . prompt ( {
207231 type : 'list' ,
208232 name : 'context' ,
209233 message : 'Name of Kubernetes context to use' ,
210234 default : currentKubeContext ,
211235 choices : contexts ,
212236 } ) ;
237+ kubeContextName = answer . context ;
213238 }
239+ const questions = [ ] ;
214240 if ( ! kubeNamespace && ! noQuestions ) {
215241 questions . push ( {
216242 type : 'input' ,
217243 name : 'namespace' ,
218- default : INSTALLATION_DEFAULTS . NAMESPACE ,
244+ default : await getRecommendedKubeNamespace ( kubeConfigPath , kubeContextName ) ,
219245 message : 'Kubernetes namespace to install into (will be created if it does not exist)' ,
220246 validate : value => ( value !== undefined && value !== '' ) || 'Please enter namespace\'s name' ,
221247 } ) ;
@@ -239,19 +265,18 @@ const initCmd = new Command({
239265 } ) ;
240266 }
241267
242- console . log ( colors . green ( 'This installer will guide you through the Codefresh Runner installation process' ) ) ;
243268 const answers = await inquirer . prompt ( questions ) ;
244269 kubeContextName = kubeContextName || answers . context ;
245270 kubeNamespace = kubeNamespace || answers . namespace ;
246- shouldMakeDefaultRe = shouldMakeDefaultRe || answers . shouldMakeDefaultRe ;
247- shouldExecutePipeline = shouldExecutePipeline || answers . shouldExecutePipeline ;
271+ shouldMakeDefaultRe = _ . isUndefined ( shouldMakeDefaultRe ) ? answers . shouldMakeDefaultRe : shouldMakeDefaultRe ;
272+ shouldExecutePipeline = _ . isUndefined ( shouldExecutePipeline ) ? answers . shouldExecutePipeline : shouldExecutePipeline ;
248273 }
249274
250275 console . log ( `\n${ colors . green ( 'Installation options summary:' ) }
2512761. Kubernetes Context: ${ colors . cyan ( kubeContextName ) }
2522772. Kubernetes Namespace: ${ colors . cyan ( kubeNamespace ) }
253- 3. Set this as default account runtime-environment: ${ colors . cyan ( shouldMakeDefaultRe ) }
254- 4. Execute demo pipeline after install: ${ colors . cyan ( shouldExecutePipeline ) }
278+ 3. Set this as default account runtime-environment: ${ colors . cyan ( ! ! shouldMakeDefaultRe ) }
279+ 4. Execute demo pipeline after install: ${ colors . cyan ( ! ! shouldExecutePipeline ) }
255280` ) ;
256281
257282 if ( token ) { // Add context
0 commit comments