@@ -4,6 +4,8 @@ const runnerRoot = require('../root/runner.cmd');
4
4
const inquirer = require ( 'inquirer' ) ;
5
5
const { getAllKubeContexts, getKubeContext } = require ( '../../helpers/kubernetes' ) ;
6
6
const installAgent = require ( '../agent/install.cmd' ) ;
7
+ const pipelinesRunCmd = require ( '../pipeline/run.cmd' ) ;
8
+ const installMonitoring = require ( '../monitor/install.cmd' ) ;
7
9
const createContext = require ( '../auth/create-context.cmd' ) ;
8
10
const getAgents = require ( '../agent/get.cmd' ) ;
9
11
const { getConfigForSdk } = require ( '../../commad-line-interface' ) ;
@@ -16,8 +18,79 @@ const INSTALLATION_DEFAULTS = {
16
18
NAMESPACE : 'codefresh' ,
17
19
MAKE_DEFAULT_RE : true ,
18
20
RUN_DEMO_PIPELINE : true ,
21
+ DEMO_PIPELINE_NAME : 'Hello Codefresh' ,
22
+ CF_CONTEXT_NAME : 'cf-runner' ,
19
23
} ;
20
24
25
+ function prettyError ( error ) {
26
+ try {
27
+ const errMsg = _ . get ( error , 'message' , error ) ;
28
+ let errObj = JSON . parse ( errMsg ) ;
29
+ if ( typeof errObj === 'string' ) {
30
+ errObj = JSON . parse ( errObj ) ;
31
+ }
32
+ return _ . get ( errObj , 'message' , error ) ;
33
+ } catch ( e ) {
34
+ return _ . get ( error , 'message' , JSON . stringify ( error ) ) ;
35
+ }
36
+ }
37
+
38
+ async function createDemoPipeline ( runtimeName ) {
39
+ const pipeline = await sdk . pipelines . create ( { metadata : { name : INSTALLATION_DEFAULTS . DEMO_PIPELINE_NAME } } ) ;
40
+ pipeline . spec . runtimeEnvironment = {
41
+ name : runtimeName ,
42
+ } ;
43
+ pipeline . spec . steps = { } ;
44
+ pipeline . spec . stages = [ 'test' ] ;
45
+ pipeline . spec . steps . test = {
46
+ stage : 'test' ,
47
+ title : 'test' ,
48
+ image : 'ubuntu:latest' ,
49
+ commands : [ 'echo hello codefresh' ] ,
50
+ } ;
51
+
52
+ await sdk . pipelines . replace (
53
+ { name : INSTALLATION_DEFAULTS . DEMO_PIPELINE_NAME } ,
54
+ {
55
+ kind : pipeline . kind ,
56
+ spec : pipeline . spec ,
57
+ metadata : pipeline . metadata ,
58
+ version : pipeline . version ,
59
+ } ,
60
+ ) ;
61
+ }
62
+
63
+ async function createAndExecuteDemoPipeline ( runtimeName ) {
64
+ let demoPipelineExists = false ;
65
+
66
+ try {
67
+ const pipelines = await sdk . pipelines . list ( { id : INSTALLATION_DEFAULTS . DEMO_PIPELINE_NAME } ) ;
68
+ if ( _ . get ( pipelines , 'docs.length' ) ) {
69
+ demoPipelineExists = true ;
70
+ }
71
+ } catch ( error ) {
72
+ console . log ( `Failed to fetch account pipelines, cause: ${ error . message } ` ) ;
73
+ }
74
+
75
+ if ( ! demoPipelineExists ) {
76
+ console . log ( `Creating demo pipeline with the name: "${ colors . cyan ( INSTALLATION_DEFAULTS . DEMO_PIPELINE_NAME ) } "` ) ;
77
+ try {
78
+ await createDemoPipeline ( runtimeName ) ;
79
+ } catch ( error ) {
80
+ console . log ( `${ colors . red ( 'Error: ' ) } Failed to create demo pipeline, cause: ${ prettyError ( error ) } ` ) ;
81
+ }
82
+ } else {
83
+ console . log ( `Demo pipeline with the name: "${ colors . cyan ( INSTALLATION_DEFAULTS . DEMO_PIPELINE_NAME ) } " already exists` ) ;
84
+ }
85
+
86
+ console . log ( `${ colors . yellow ( '*NOTE* Running a pipeline for the first time might take a longer than usual' ) } ` ) ;
87
+ console . log ( `Executing pipeline "${ colors . cyan ( INSTALLATION_DEFAULTS . DEMO_PIPELINE_NAME ) } "` ) ;
88
+ await pipelinesRunCmd . handler ( {
89
+ name : INSTALLATION_DEFAULTS . DEMO_PIPELINE_NAME ,
90
+ exitProcess : false ,
91
+ } ) ;
92
+ }
93
+
21
94
const initCmd = new Command ( {
22
95
root : false ,
23
96
parent : runnerRoot ,
@@ -100,13 +173,14 @@ const initCmd = new Command({
100
173
'skip-version-check' : skipVersionCheck ,
101
174
yes : noQuestions ,
102
175
verbose,
103
- name, token , url,
176
+ name, url,
104
177
} = argv ;
105
178
let {
106
179
'kube-context-name' : kubeContextName ,
107
180
'kube-namespace' : kubeNamespace ,
108
181
'set-default-runtime' : shouldMakeDefaultRe ,
109
182
'exec-demo-pipeline' : shouldExecutePipeline ,
183
+ token,
110
184
} = argv ;
111
185
112
186
if ( _ . get ( sdk , 'config.context.isNoAuth' ) && ! token ) {
@@ -123,98 +197,126 @@ const initCmd = new Command({
123
197
kubeNamespace = INSTALLATION_DEFAULTS . NAMESPACE ;
124
198
shouldMakeDefaultRe = INSTALLATION_DEFAULTS . MAKE_DEFAULT_RE ;
125
199
shouldExecutePipeline = INSTALLATION_DEFAULTS . RUN_DEMO_PIPELINE ;
126
- }
200
+ } else {
201
+ const questions = [ ] ;
202
+ if ( ! kubeContextName && ! noQuestions ) {
203
+ const contexts = getAllKubeContexts ( kubeConfigPath ) ;
204
+ const currentKubeContext = getKubeContext ( kubeConfigPath ) ;
127
205
128
- const questions = [ ] ;
129
- if ( ! kubeContextName && ! noQuestions ) {
130
- const contexts = getAllKubeContexts ( kubeConfigPath ) ;
131
- const currentKubeContext = getKubeContext ( kubeConfigPath ) ;
132
-
133
- questions . push ( {
134
- type : 'list' ,
135
- name : 'context' ,
136
- message : 'Name of Kubernetes context to use' ,
137
- default : currentKubeContext ,
138
- choices : contexts ,
139
- } ) ;
140
- }
141
- if ( ! kubeNamespace && ! noQuestions ) {
142
- questions . push ( {
143
- type : 'input' ,
144
- name : 'namespace' ,
145
- default : INSTALLATION_DEFAULTS . NAMESPACE ,
146
- message : 'Kubernetes namespace to install into (will be created if it does not exist)' ,
147
- validate : value => ( value !== undefined && value !== '' ) || 'Please enter namespace\'s name' ,
148
- } ) ;
149
- }
206
+ questions . push ( {
207
+ type : 'list' ,
208
+ name : 'context' ,
209
+ message : 'Name of Kubernetes context to use' ,
210
+ default : currentKubeContext ,
211
+ choices : contexts ,
212
+ } ) ;
213
+ }
214
+ if ( ! kubeNamespace && ! noQuestions ) {
215
+ questions . push ( {
216
+ type : 'input' ,
217
+ name : 'namespace' ,
218
+ default : INSTALLATION_DEFAULTS . NAMESPACE ,
219
+ message : 'Kubernetes namespace to install into (will be created if it does not exist)' ,
220
+ validate : value => ( value !== undefined && value !== '' ) || 'Please enter namespace\'s name' ,
221
+ } ) ;
222
+ }
150
223
151
- if ( _ . isUndefined ( shouldMakeDefaultRe ) && ! noQuestions ) {
152
- questions . push ( {
153
- type : 'confirm' ,
154
- name : 'shouldMakeDefaultRe' ,
155
- default : INSTALLATION_DEFAULTS . MAKE_DEFAULT_RE ,
156
- message : 'Set this as the default runtime environment for your Codefresh account? (Y/N)' ,
157
- } ) ;
158
- }
224
+ if ( _ . isUndefined ( shouldMakeDefaultRe ) && ! noQuestions ) {
225
+ questions . push ( {
226
+ type : 'confirm' ,
227
+ name : 'shouldMakeDefaultRe' ,
228
+ default : INSTALLATION_DEFAULTS . MAKE_DEFAULT_RE ,
229
+ message : 'Set this as the default runtime environment for your Codefresh account? (Y/N)' ,
230
+ } ) ;
231
+ }
159
232
160
- if ( _ . isUndefined ( shouldExecutePipeline ) && ! noQuestions ) {
161
- questions . push ( {
162
- type : 'confirm' ,
163
- name : 'shouldExecutePipeline' ,
164
- default : INSTALLATION_DEFAULTS . RUN_DEMO_PIPELINE ,
165
- message : 'Run demo pipeline after install? (Y/N)' ,
166
- } ) ;
167
- }
233
+ if ( _ . isUndefined ( shouldExecutePipeline ) && ! noQuestions ) {
234
+ questions . push ( {
235
+ type : 'confirm' ,
236
+ name : 'shouldExecutePipeline' ,
237
+ default : INSTALLATION_DEFAULTS . RUN_DEMO_PIPELINE ,
238
+ message : 'Run demo pipeline after install? (Y/N)' ,
239
+ } ) ;
240
+ }
168
241
169
- console . log ( colors . green ( 'This installer will guide you through the Codefresh Runner installation process' ) ) ;
170
- const answers = await inquirer . prompt ( questions ) ;
171
- kubeContextName = kubeContextName || answers . context ;
172
- kubeNamespace = kubeNamespace || answers . namespace ;
173
- shouldMakeDefaultRe = shouldMakeDefaultRe || answers . shouldMakeDefaultRe ;
174
- shouldExecutePipeline = shouldExecutePipeline || answers . shouldExecutePipeline ;
242
+ console . log ( colors . green ( 'This installer will guide you through the Codefresh Runner installation process' ) ) ;
243
+ const answers = await inquirer . prompt ( questions ) ;
244
+ kubeContextName = kubeContextName || answers . context ;
245
+ kubeNamespace = kubeNamespace || answers . namespace ;
246
+ shouldMakeDefaultRe = shouldMakeDefaultRe || answers . shouldMakeDefaultRe ;
247
+ shouldExecutePipeline = shouldExecutePipeline || answers . shouldExecutePipeline ;
248
+ }
175
249
176
- console . log ( colors . green ( `\nInstallation options summary:
177
- ${ colors . white ( ' 1. Kubernetes Context:' ) } ${ colors . cyan ( kubeContextName ) }
178
- ${ colors . white ( ' 2. Kubernetes Namespace:' ) } ${ colors . cyan ( kubeNamespace ) }
179
- ${ colors . white ( ' 3. Set this as default account runtime-environment:' ) } ${ colors . cyan ( shouldMakeDefaultRe ) }
180
- ${ colors . white ( ' 4. Execute demo pipeline after install:' ) } ${ colors . cyan ( shouldExecutePipeline ) }
181
- ` ) ) ;
250
+ console . log ( `\n ${ colors . green ( 'Installation options summary:' ) }
251
+ 1. Kubernetes Context: ${ colors . cyan ( kubeContextName ) }
252
+ 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 ) }
255
+ ` ) ;
182
256
183
257
if ( token ) { // Add context
184
- await createContext . handler ( {
185
- apiKey : token ,
186
- name : 'cf-runner' ,
187
- url,
188
- } ) ;
189
- const config = await getConfigForSdk ( ) ;
190
- await sdk . configure ( config ) ;
191
- console . log ( 'A Codefresh context named "cf-runner" was added to your "cfconfig" file.' ) ;
258
+ try {
259
+ await createContext . handler ( {
260
+ apiKey : token ,
261
+ name : INSTALLATION_DEFAULTS . CF_CONTEXT_NAME ,
262
+ url,
263
+ } ) ;
264
+ const config = await getConfigForSdk ( ) ;
265
+ await sdk . configure ( config ) ;
266
+ console . log ( `A Codefresh context named '${ INSTALLATION_DEFAULTS . CF_CONTEXT_NAME } ' was added to your "cfconfig" file.` ) ;
267
+ } catch ( error ) {
268
+ console . log ( `${ colors . red ( 'Error:' ) } Could not use the provided token, failed with error: ${ prettyError ( error ) } ` ) ;
269
+ process . exit ( 1 ) ;
270
+ }
271
+ } else {
272
+ token = _ . get ( sdk , 'config.context.token' ) ;
192
273
}
193
274
194
275
// Install runner and runtime
195
- await installAgent . handler ( {
196
- name,
276
+ let runtimeName ;
277
+ try {
278
+ runtimeName = await installAgent . handler ( {
279
+ name,
280
+ 'kube-context-name' : kubeContextName ,
281
+ 'kube-node-selector' : kubeNodeSelector ,
282
+ 'dry-run' : dryRun ,
283
+ 'in-cluster' : inCluster ,
284
+ 'kube-namespace' : kubeNamespace ,
285
+ 'kubernetes-runner-type' : kubernetesRunnerType ,
286
+ tolerations,
287
+ 'venona-version' : venonaVersion ,
288
+ 'kube-config-path' : kubeConfigPath ,
289
+ 'skip-version-check' : skipVersionCheck ,
290
+ 'install-runtime' : true ,
291
+ verbose,
292
+ 'make-default-runtime' : shouldMakeDefaultRe ,
293
+ terminateProcess : false ,
294
+ } ) ;
295
+ } catch ( error ) {
296
+ console . log ( `${ colors . red ( 'Error: ' ) } Runner installation failed with error: ${ prettyError ( error ) } ` ) ;
297
+ process . exit ( 1 ) ;
298
+ }
299
+
300
+ // Install monitoring
301
+ await installMonitoring . handler ( {
302
+ 'kube-config-path' : kubeConfigPath ,
303
+ 'cluster-id' : kubeContextName ,
197
304
'kube-context-name' : kubeContextName ,
198
- 'kube-node-selector' : kubeNodeSelector ,
199
- 'dry-run' : dryRun ,
200
- 'in-cluster' : inCluster ,
201
305
'kube-namespace' : kubeNamespace ,
202
- 'kubernetes-runner-type' : kubernetesRunnerType ,
203
- tolerations,
204
- 'venona-version' : venonaVersion ,
205
- 'kube-config-path' : kubeConfigPath ,
206
- 'skip-version-check' : skipVersionCheck ,
207
- 'install-runtime' : true ,
306
+ token,
208
307
verbose,
209
- 'make-default-runtime' : shouldMakeDefaultRe ,
210
- terminateProcess : false ,
211
- createDemoPipeline : true ,
212
- executeDemoPipeline : shouldExecutePipeline ,
308
+ noExit : true , // to prevent if from calling: process.exit()
213
309
} ) ;
214
- console . log ( colors . green ( 'Runner Status:\n' ) ) ;
310
+
311
+ // Post Installation
312
+ if ( shouldExecutePipeline ) {
313
+ await createAndExecuteDemoPipeline ( runtimeName ) ;
314
+ }
315
+
316
+ console . log ( colors . green ( '\nRunner Status:' ) ) ;
215
317
await getAgents . handler ( { } ) ;
216
318
console . log ( colors . green ( `\nDocumenation link: ${ colors . blue ( 'https://codefresh.io/docs/docs/enterprise/codefresh-runner/#codefresh-runner-preview-release' ) } ` ) ) ;
217
- 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' ) } ` ) ) ;
319
+ 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' ) } ` ) ) ;
218
320
process . exit ( ) ; // TODO : This is not needed - needed to be fixed
219
321
} ,
220
322
} ) ;
0 commit comments