2
2
const Command = require ( '../../Command' ) ;
3
3
const runnerRoot = require ( '../root/runner.cmd' ) ;
4
4
const inquirer = require ( 'inquirer' ) ;
5
- const { getAllKubeContexts, getKubeContext, getAllNamespaces } = require ( '../../helpers/kubernetes' ) ;
5
+ const { getAllKubeContexts, getKubeContext } = require ( '../../helpers/kubernetes' ) ;
6
6
const unInstallRuntime = require ( '../runtimeEnvironments/uninstall.cmd' ) ;
7
7
const unInstallAgent = require ( '../agent/uninstall.cmd' ) ;
8
8
const unInstallMonitor = require ( '../monitor/uninstall.cmd' ) ;
@@ -11,54 +11,11 @@ const DEFAULTS = require('../../defaults');
11
11
const sdk = require ( '../../../../logic/sdk' ) ;
12
12
const _ = require ( 'lodash' ) ;
13
13
const { to } = require ( './../../../../logic/cli-config/errors/awaitTo' ) ;
14
- const { prettyError } = require ( '../../../../logic/cli-config/errors/helpers ' ) ;
14
+ const { createErrorHandler , getRelatedAgents , getRelatedNamespaces } = require ( './helper ' ) ;
15
15
16
16
const defaultNamespace = 'codefresh' ;
17
-
18
- async function handleError ( error , message ) {
19
- if ( ! error ) {
20
- return ;
21
- }
22
-
23
- console . log ( `${ colors . red ( 'Error:' ) } ${ message } : ${ prettyError ( error ) } ` ) ;
24
- console . log ( colors . green ( `If you had any issues with the uninstallation process please report them at: ${ colors . blue ( 'https://github.com/codefresh-io/cli/issues/new' ) } ` ) ) ;
25
- process . exit ( 1 ) ;
26
- }
27
-
28
- // Try to get the most relevant namespaces
29
- async function getRelatedNamespaces ( kubeConfigPath , kubeContextName , runtimes ) {
30
- const [ , namespacesOnCluster ] = await to ( getAllNamespaces ( kubeConfigPath , kubeContextName ) ) ;
31
- const nsOnCluster = new Set ( namespacesOnCluster || [ ] ) ;
32
-
33
- return _ ( runtimes )
34
- . filter ( re => nsOnCluster . has ( _ . get ( re , 'runtimeScheduler.cluster.namespace' ) ) )
35
- . map ( re => _ . get ( re , 'runtimeScheduler.cluster.namespace' ) )
36
- . uniq ( )
37
- . value ( ) ;
38
- }
39
-
40
- async function getRelatedAgents ( kubeNamespace , runtimes ) {
41
- const [ listAgentsErr , agents ] = await to ( sdk . agents . list ( { } ) ) ;
42
- await handleError ( listAgentsErr , 'Failed to get agents' ) ;
43
-
44
- const relatedREs = new Set ( ) ;
45
- _ . forEach ( runtimes , ( r ) => {
46
- if ( _ . get ( r , 'runtimeScheduler.cluster.namespace' ) === kubeNamespace ) {
47
- relatedREs . add ( r . metadata . name ) ;
48
- }
49
- } ) ;
50
-
51
- const relatedAgents = [ ] ;
52
- _ . forEach ( agents , ( a ) => {
53
- _ . forEach ( _ . get ( a , 'runtimes' , [ ] ) , ( r ) => {
54
- if ( relatedREs . has ( r ) ) {
55
- relatedAgents . push ( a ) ;
56
- }
57
- } ) ;
58
- } ) ;
59
-
60
- return relatedAgents ;
61
- }
17
+ const openIssueMessage = `If you had any issues with the uninstallation process please report them at: ${ colors . blue ( 'https://github.com/codefresh-io/cli/issues/new' ) } ` ;
18
+ const handleError = createErrorHandler ( openIssueMessage ) ;
62
19
63
20
const deleteCmd = new Command ( {
64
21
root : false ,
@@ -105,6 +62,8 @@ const deleteCmd = new Command({
105
62
106
63
const [ listReErr , runtimes ] = await to ( sdk . runtimeEnvs . list ( { } ) ) ;
107
64
await handleError ( listReErr , 'Failed to get runtime environments' ) ;
65
+ const [ listAgentsErr , agents ] = await to ( sdk . agents . list ( { } ) ) ;
66
+ await handleError ( listAgentsErr , 'Failed to get agents' ) ;
108
67
109
68
console . log ( colors . green ( 'This uninstaller will guide you through the runner uninstallation process' ) ) ;
110
69
@@ -124,7 +83,8 @@ const deleteCmd = new Command({
124
83
}
125
84
126
85
if ( ! kubeNamespace ) {
127
- const relatedNamespaces = await getRelatedNamespaces ( kubeConfigPath , kubeContextName , runtimes ) ;
86
+ const [ getNamespacesErr , relatedNamespaces ] = await to ( getRelatedNamespaces ( kubeConfigPath , kubeContextName , runtimes ) ) ;
87
+ handleError ( getNamespacesErr , 'Could not get namespaces in the selected kubernetes cluster' ) ;
128
88
let answer ;
129
89
if ( ! relatedNamespaces . length ) {
130
90
answer = await inquirer . prompt ( {
@@ -147,18 +107,18 @@ const deleteCmd = new Command({
147
107
kubeNamespace = answer . namespace ;
148
108
}
149
109
150
- const agents = await getRelatedAgents ( kubeNamespace , runtimes ) ;
151
- if ( ! agents . length ) {
152
- console . log ( 'No agents related to the specified kubernetes cluster and namespace were found' ) ;
153
- process . exit ( 1 ) ;
154
- }
155
-
156
110
if ( ! agentName ) {
111
+ const relatedAgents = await getRelatedAgents ( kubeNamespace , runtimes , agents , handleError ) ;
112
+ let agentsChoices = relatedAgents ;
113
+ if ( ! relatedAgents . length ) {
114
+ console . log ( colors . yellow ( 'No agents related to the specified kubernetes cluster and namespace were found, displaying all agents' ) ) ;
115
+ agentsChoices = agents ;
116
+ }
157
117
const answer = await inquirer . prompt ( {
158
118
type : 'list' ,
159
119
name : 'name' ,
160
120
message : 'Agent name to uninstall' ,
161
- choices : agents ,
121
+ choices : agentsChoices . map ( a => ` ${ a . name } \t ${ a . runtimes . length ? `(attached runtimes: ${ a . runtimes . join ( ', ' ) } )` : '' } ` ) ,
162
122
} ) ;
163
123
agentName = answer . name ;
164
124
}
@@ -180,19 +140,39 @@ const deleteCmd = new Command({
180
140
3. Agent name: ${ colors . cyan ( agentName ) }
181
141
` ) ;
182
142
183
- if ( agent . runtimes . length === 1 ) {
143
+ const attachedRuntimes = agent . runtimes || [ ] ;
144
+
145
+ // prompt confirmation message
146
+ console . log ( `${ colors . red ( 'This process will attempt to delete the following:' ) } ` ) ;
147
+ console . log ( `\u2022 Codefresh runner with the name "${ colors . cyan ( agentName ) } "` ) ;
148
+ attachedRuntimes . forEach ( ( reName ) => { console . log ( `\u2022 Codefresh runtime with the name "${ colors . cyan ( reName ) } "` ) ; } ) ;
149
+ console . log ( '\u2022 Codefresh runner monitor component' ) ;
150
+ console . log ( `* The kubernetes namespace "${ colors . cyan ( kubeNamespace ) } " will ${ colors . underline ( 'not' ) } be deleted\n` ) ;
151
+
152
+ const answer = await inquirer . prompt ( {
153
+ type : 'confirm' ,
154
+ name : 'deletionConfirmed' ,
155
+ default : false ,
156
+ message : 'Are you sure you want to delete all of the above? (default is NO)' ,
157
+ } ) ;
158
+ if ( ! answer . deletionConfirmed ) {
159
+ console . log ( 'Deletion process aborted, exiting...' ) ;
160
+ process . exit ( 1 ) ;
161
+ }
162
+
163
+ attachedRuntimes . forEach ( async ( reName ) => {
184
164
const uninstallRuntimeOptions = {
185
165
'agent-name' : agentName ,
186
166
'runtime-kube-namespace' : kubeNamespace ,
187
167
'runtime-kube-context-name' : kubeContextName ,
188
168
'agent-kube-context-name' : kubeContextName ,
189
169
'agent-kube-namespace' : kubeNamespace ,
190
- name : agent . runtimes [ 0 ] ,
170
+ name : reName ,
191
171
terminateProcess : false ,
192
172
} ;
193
173
const [ uninstallReErr ] = await to ( unInstallRuntime . handler ( uninstallRuntimeOptions ) ) ;
194
- handleError ( uninstallReErr , ' Failed to uninstall runtime-environment' ) ;
195
- }
174
+ handleError ( uninstallReErr , ` Failed to uninstall runtime-environment " ${ colors . cyan ( reName ) } "` ) ;
175
+ } ) ;
196
176
197
177
const uninstallAgentOptions = {
198
178
'kube-namespace' : kubeNamespace ,
0 commit comments