@@ -10,15 +10,15 @@ const colors = require('colors');
10
10
const DEFAULTS = require ( '../../defaults' ) ;
11
11
const sdk = require ( '../../../../logic/sdk' ) ;
12
12
const _ = require ( 'lodash' ) ;
13
- const YAML = require ( 'yaml' ) ;
14
- const fs = require ( 'fs' ) ;
15
13
const { to } = require ( './../../../../logic/cli-config/errors/awaitTo' ) ;
16
14
const {
17
15
createErrorHandler,
18
16
getRelatedAgents,
19
17
getRelatedNamespaces,
20
18
drawCodefreshFiglet,
21
19
unInstallAppProxy,
20
+ mergeValuesFromValuesFile,
21
+ INSTALLATION_DEFAULTS ,
22
22
} = require ( './helper' ) ;
23
23
24
24
const defaultNamespace = 'codefresh' ;
@@ -29,14 +29,34 @@ async function promptConfirmationMessage({
29
29
agentName,
30
30
kubeNamespace,
31
31
attachedRuntimes,
32
+ defaultRuntime,
33
+ clustersToDelete,
34
+ appProxyToDelete,
32
35
} ) {
36
+ const note = colors . underline ( colors . yellow ( 'Note' ) ) ;
37
+ let newDefaultRuntime = defaultRuntime ;
38
+
33
39
// prompt confirmation message
34
40
console . log ( `${ colors . red ( 'This process will attempt to delete the following:' ) } ` ) ;
35
41
console . log ( `\u2022 Codefresh runner with the name "${ colors . cyan ( agentName ) } "` ) ;
36
- attachedRuntimes . forEach ( ( reName ) => { console . log ( `\u2022 Codefresh runtime with the name "${ colors . cyan ( reName ) } "` ) ; } ) ;
42
+ attachedRuntimes . forEach ( ( reName ) => { console . log ( `\u2022 Codefresh runtime with the name "${ colors . cyan ( reName ) } "${ defaultRuntime === reName ? ' [default runtime]' : '' } ` ) ; } ) ;
37
43
console . log ( '\u2022 Codefresh runner monitor component' ) ;
38
- console . log ( '\u2022 Codefresh App-Proxy component (if exists)' ) ;
39
- console . log ( `* The kubernetes namespace "${ colors . cyan ( kubeNamespace ) } " will ${ colors . underline ( 'not' ) } be deleted\n` ) ;
44
+ clustersToDelete . forEach ( ( cluster ) => { console . log ( `\u2022 Codefresh cluster integration: "${ colors . cyan ( cluster . selector ) } "` ) ; } ) ;
45
+ appProxyToDelete . forEach ( ( appProxy ) => { console . log ( `\u2022 App-Proxy component: "${ colors . cyan ( appProxy ) } "` ) ; } ) ;
46
+
47
+ // if this includes your default runtime
48
+ if ( attachedRuntimes . length > 0 ) {
49
+ if ( defaultRuntime && attachedRuntimes . includes ( defaultRuntime ) ) {
50
+ newDefaultRuntime = INSTALLATION_DEFAULTS . SAAS_RUNTIME ;
51
+ console . log ( `* ${ note } : "${ colors . cyan ( defaultRuntime ) } " is set as your default runtime-environment,`
52
+ + ` if you delete this runtime your new default runtime will be: "${ colors . cyan ( newDefaultRuntime ) } "` ) ;
53
+ }
54
+
55
+ console . log ( `* ${ note } : any pipeline set to run on one of the above runtime environments will be moved to`
56
+ + ` run on your ${ newDefaultRuntime !== defaultRuntime ? 'new ' : '' } default runtime environment: "${ colors . cyan ( newDefaultRuntime ) } "` ) ;
57
+ }
58
+
59
+ console . log ( `* ${ note } : The kubernetes namespace "${ colors . cyan ( kubeNamespace ) } " will ${ colors . underline ( 'not' ) } be deleted\n` ) ;
40
60
41
61
const answer = await inquirer . prompt ( {
42
62
type : 'confirm' ,
@@ -50,6 +70,27 @@ async function promptConfirmationMessage({
50
70
}
51
71
}
52
72
73
+ // returns the clusters that are referenced by the runtimesToDelete and not referenced by runtimesToKeep
74
+ function getClustersToDelete ( clusters , runtimes , runtimesToDelete ) {
75
+ const potentialClustersNames = _ ( runtimesToDelete ) . map ( re => _ . get ( re , 'runtimeScheduler.cluster.clusterProvider.selector' ) ) . compact ( ) . value ( ) ;
76
+ const runtimesToDeleteNames = _ ( runtimesToDelete ) . map ( re => _ . get ( re , 'metadata.name' ) ) . compact ( ) . value ( ) ;
77
+
78
+ const potentialClustersToDelete = _ . filter ( clusters , c => potentialClustersNames . includes ( c . selector ) ) ;
79
+ const runtimesToKeep = _ . filter ( runtimes , re => ! runtimesToDeleteNames . includes ( _ . get ( re , 'metadata.name' ) ) ) ;
80
+
81
+ return _ . filter ( potentialClustersToDelete , c => ! _ . find ( runtimesToKeep , re => _ . get ( re , 'runtimeScheduler.cluster.clusterProvider.selector' ) === c . selector ) ) ;
82
+ }
83
+
84
+ function getAppProxyToDelete ( runtimesToDelete ) {
85
+ return _ . reduce ( runtimesToDelete , ( acc , re ) => {
86
+ const appProxy = _ . get ( re , 'appProxy.externalIP' ) ;
87
+ if ( appProxy ) {
88
+ acc . push ( appProxy ) ;
89
+ }
90
+ return acc ;
91
+ } , [ ] ) ;
92
+ }
93
+
53
94
const deleteCmd = new Command ( {
54
95
root : false ,
55
96
parent : runnerRoot ,
@@ -90,50 +131,39 @@ const deleteCmd = new Command({
90
131
describe : 'Print logs' ,
91
132
} ) ,
92
133
handler : async ( argv ) => {
134
+ let _argv = argv ;
135
+
136
+ // read values from values file
137
+ if ( argv . values ) {
138
+ _argv = mergeValuesFromValuesFile ( _argv , _argv . values , handleError ) ;
139
+ }
140
+
93
141
const {
94
142
verbose,
95
- } = argv ;
96
- let {
143
+ force,
97
144
'kube-config-path' : kubeConfigPath ,
98
- url, force,
145
+ } = _argv ;
146
+ let {
147
+ url,
99
148
'kube-context-name' : kubeContextName ,
100
149
'kube-namespace' : kubeNamespace ,
101
150
name : agentName ,
102
- values : valuesFile ,
103
- } = argv ;
151
+ } = _argv ;
104
152
105
153
const [ listReErr , runtimes ] = await to ( sdk . runtimeEnvs . list ( { } ) ) ;
106
154
await handleError ( listReErr , 'Failed to get runtime environments' ) ;
107
155
const [ listAgentsErr , agents ] = await to ( sdk . agents . list ( { } ) ) ;
108
156
await handleError ( listAgentsErr , 'Failed to get agents' ) ;
157
+ const [ listClustersErr , clusters ] = await to ( sdk . clusters . list ( ) ) ;
158
+ await handleError ( listClustersErr , 'Failed to get cluster integrations' ) ;
109
159
110
- if ( ! agents . length ) {
160
+ if ( ! agents || ! agents . length ) {
111
161
console . log ( 'No runners found on your codefresh account' ) ;
112
162
process . exit ( 0 ) ;
113
163
}
114
164
115
165
console . log ( colors . green ( 'This uninstaller will guide you through the runner uninstallation process' ) ) ;
116
- if ( valuesFile ) {
117
- const valuesFileStr = fs . readFileSync ( valuesFile , 'utf8' ) ;
118
- valuesObj = YAML . parse ( valuesFileStr ) ;
119
-
120
- if ( ! kubeConfigPath && valuesObj . ConfigPath ) {
121
- kubeConfigPath = valuesObj . ConfigPath ;
122
- }
123
- if ( ! kubeNamespace && valuesObj . Namespace ) {
124
- kubeNamespace = valuesObj . Namespace ;
125
- }
126
- if ( ! kubeContextName && valuesObj . Context ) {
127
- kubeContextName = valuesObj . Context ;
128
- }
129
- if ( ! url && valuesObj . CodefreshHost ) {
130
- url = valuesObj . CodefreshHost ;
131
- }
132
166
133
- if ( ! agentName && valuesObj . AgentId ) {
134
- agentName = valuesObj . AgentId ;
135
- }
136
- }
137
167
if ( ! url ) {
138
168
url = DEFAULTS . URL ;
139
169
}
@@ -216,9 +246,21 @@ const deleteCmd = new Command({
216
246
` ) ;
217
247
218
248
const attachedRuntimes = agent . runtimes || [ ] ;
249
+ const runtimesToDelete = _ . filter ( runtimes , re => attachedRuntimes . includes ( _ . get ( re , 'metadata.name' ) ) ) ;
250
+ const defaultRuntime = _ . get ( _ . find ( runtimes , re => re . default ) , 'metadata.name' ) ;
251
+
252
+ const clustersToDelete = getClustersToDelete ( clusters , runtimes , runtimesToDelete ) ;
253
+ const appProxyToDelete = getAppProxyToDelete ( runtimesToDelete ) ;
219
254
220
255
if ( ! force ) {
221
- await promptConfirmationMessage ( { agentName, kubeNamespace, attachedRuntimes } ) ;
256
+ await promptConfirmationMessage ( {
257
+ agentName,
258
+ kubeNamespace,
259
+ attachedRuntimes,
260
+ defaultRuntime,
261
+ clustersToDelete,
262
+ appProxyToDelete,
263
+ } ) ;
222
264
}
223
265
224
266
await Promise . all ( attachedRuntimes . map ( async ( reName ) => {
@@ -233,16 +275,36 @@ const deleteCmd = new Command({
233
275
verbose,
234
276
} ;
235
277
const re = runtimes . find ( runtime => runtime . metadata . name === reName ) ;
278
+ if ( ! re ) {
279
+ // re already deleted
280
+ return ;
281
+ }
282
+
283
+ // remove runtime cluster components
284
+ console . log ( `Deleting runtime-environment: ${ colors . cyan ( reName ) } from cluster` ) ;
285
+ const [ uninstallReErr ] = await to ( unInstallRuntime . handler ( uninstallRuntimeOptions ) ) ;
286
+ handleError ( uninstallReErr , `Failed to uninstall runtime-environment "${ colors . cyan ( reName ) } "` ) ;
287
+
288
+ // delete codefresh entity
289
+ console . log ( `Deleting runtime-environment: "${ colors . cyan ( reName ) } " from codefresh` ) ;
290
+ await to ( sdk . runtimeEnvs . delete ( { name : reName , force : true } ) ) ; // also delete default runtime
291
+
236
292
if ( re . appProxy ) {
237
- await unInstallAppProxy ( {
293
+ const [ uninstallAppProxyErr ] = await to ( unInstallAppProxy ( {
238
294
kubeConfigPath,
239
295
kubeContextName,
240
296
kubeNamespace,
241
297
verbose,
242
- } ) ;
298
+ } ) ) ;
299
+ handleError ( uninstallAppProxyErr , 'Failed to uninstall app-proxy' ) ;
300
+ console . log ( 'App-Proxy uninstalled successfully' ) ;
243
301
}
244
- const [ uninstallReErr ] = await to ( unInstallRuntime . handler ( uninstallRuntimeOptions ) ) ;
245
- handleError ( uninstallReErr , `Failed to uninstall runtime-environment "${ colors . cyan ( reName ) } "` ) ;
302
+ } ) ) ;
303
+
304
+ await Promise . all ( clustersToDelete . map ( async ( cluster ) => {
305
+ console . log ( `Deleting cluster integration: "${ colors . cyan ( cluster . selector ) } "` ) ;
306
+ const [ clusterDeleteErr ] = await to ( sdk . clusters . delete ( { id : cluster . _id , provider : cluster . provider } ) ) ;
307
+ handleError ( clusterDeleteErr , 'Failed to delete cluster integration' ) ;
246
308
} ) ) ;
247
309
248
310
const uninstallAgentOptions = {
0 commit comments