2
2
const Command = require ( '../../Command' ) ;
3
3
const runnerRoot = require ( '../root/runner.cmd' ) ;
4
4
const inquirer = require ( 'inquirer' ) ;
5
- const { getAllKubeContexts, getKubeContext } = require ( '../../helpers/kubernetes' ) ;
5
+ const { getAllKubeContexts, getKubeContext, getAllNamespaces } = require ( '../../helpers/kubernetes' ) ;
6
6
const installAgent = require ( '../agent/install.cmd' ) ;
7
7
const pipelinesRunCmd = require ( '../pipeline/run.cmd' ) ;
8
8
const installMonitoring = require ( '../monitor/install.cmd' ) ;
@@ -18,7 +18,7 @@ const INSTALLATION_DEFAULTS = {
18
18
NAMESPACE : 'codefresh' ,
19
19
MAKE_DEFAULT_RE : true ,
20
20
RUN_DEMO_PIPELINE : true ,
21
- DEMO_PIPELINE_NAME : 'Hello Codefresh' ,
21
+ DEMO_PIPELINE_NAME : 'Codefresh-Runner Demo ' ,
22
22
CF_CONTEXT_NAME : 'cf-runner' ,
23
23
} ;
24
24
@@ -29,7 +29,12 @@ function prettyError(error) {
29
29
if ( typeof errObj === 'string' ) {
30
30
errObj = JSON . parse ( errObj ) ;
31
31
}
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 ;
33
38
} catch ( e ) {
34
39
return _ . get ( error , 'message' , JSON . stringify ( error ) ) ;
35
40
}
@@ -45,8 +50,8 @@ async function createDemoPipeline(runtimeName) {
45
50
pipeline . spec . steps . test = {
46
51
stage : 'test' ,
47
52
title : 'test' ,
48
- image : 'ubuntu :latest' ,
49
- commands : [ 'echo hello codefresh' ] ,
53
+ image : 'alpine :latest' ,
54
+ commands : [ 'echo hello codefresh runner! ' ] ,
50
55
} ;
51
56
52
57
await sdk . pipelines . replace (
@@ -83,14 +88,33 @@ async function createAndExecuteDemoPipeline(runtimeName) {
83
88
console . log ( `Demo pipeline with the name: "${ colors . cyan ( INSTALLATION_DEFAULTS . DEMO_PIPELINE_NAME ) } " already exists` ) ;
84
89
}
85
90
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. ' ) } ` ) ;
87
92
console . log ( `Executing pipeline "${ colors . cyan ( INSTALLATION_DEFAULTS . DEMO_PIPELINE_NAME ) } "` ) ;
88
93
await pipelinesRunCmd . handler ( {
89
94
name : INSTALLATION_DEFAULTS . DEMO_PIPELINE_NAME ,
90
95
exitProcess : false ,
91
96
} ) ;
92
97
}
93
98
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
+
94
118
const initCmd = new Command ( {
95
119
root : false ,
96
120
parent : runnerRoot ,
@@ -194,28 +218,30 @@ const initCmd = new Command({
194
218
if ( noQuestions ) {
195
219
// set defaults
196
220
kubeContextName = getKubeContext ( kubeConfigPath ) ;
197
- kubeNamespace = INSTALLATION_DEFAULTS . NAMESPACE ;
221
+ kubeNamespace = await getRecommendedKubeNamespace ( kubeConfigPath , kubeContextName ) ;
198
222
shouldMakeDefaultRe = INSTALLATION_DEFAULTS . MAKE_DEFAULT_RE ;
199
223
shouldExecutePipeline = INSTALLATION_DEFAULTS . RUN_DEMO_PIPELINE ;
200
224
} else {
201
- const questions = [ ] ;
225
+ console . log ( colors . green ( 'This installer will guide you through the Codefresh Runner installation process' ) ) ;
202
226
if ( ! kubeContextName && ! noQuestions ) {
203
227
const contexts = getAllKubeContexts ( kubeConfigPath ) ;
204
228
const currentKubeContext = getKubeContext ( kubeConfigPath ) ;
205
229
206
- questions . push ( {
230
+ const answer = await inquirer . prompt ( {
207
231
type : 'list' ,
208
232
name : 'context' ,
209
233
message : 'Name of Kubernetes context to use' ,
210
234
default : currentKubeContext ,
211
235
choices : contexts ,
212
236
} ) ;
237
+ kubeContextName = answer . context ;
213
238
}
239
+ const questions = [ ] ;
214
240
if ( ! kubeNamespace && ! noQuestions ) {
215
241
questions . push ( {
216
242
type : 'input' ,
217
243
name : 'namespace' ,
218
- default : INSTALLATION_DEFAULTS . NAMESPACE ,
244
+ default : await getRecommendedKubeNamespace ( kubeConfigPath , kubeContextName ) ,
219
245
message : 'Kubernetes namespace to install into (will be created if it does not exist)' ,
220
246
validate : value => ( value !== undefined && value !== '' ) || 'Please enter namespace\'s name' ,
221
247
} ) ;
@@ -239,19 +265,18 @@ const initCmd = new Command({
239
265
} ) ;
240
266
}
241
267
242
- console . log ( colors . green ( 'This installer will guide you through the Codefresh Runner installation process' ) ) ;
243
268
const answers = await inquirer . prompt ( questions ) ;
244
269
kubeContextName = kubeContextName || answers . context ;
245
270
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 ;
248
273
}
249
274
250
275
console . log ( `\n${ colors . green ( 'Installation options summary:' ) }
251
276
1. Kubernetes Context: ${ colors . cyan ( kubeContextName ) }
252
277
2. 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 ) }
255
280
` ) ;
256
281
257
282
if ( token ) { // Add context
0 commit comments