22const Command = require ( '../../Command' ) ;
33const runnerRoot = require ( '../root/runner.cmd' ) ;
44const inquirer = require ( 'inquirer' ) ;
5- const { getAllKubeContexts, getKubeContext, getAllNamespaces } = require ( '../../helpers/kubernetes' ) ;
5+ const { getAllKubeContexts, getKubeContext } = require ( '../../helpers/kubernetes' ) ;
66const unInstallRuntime = require ( '../runtimeEnvironments/uninstall.cmd' ) ;
77const unInstallAgent = require ( '../agent/uninstall.cmd' ) ;
88const unInstallMonitor = require ( '../monitor/uninstall.cmd' ) ;
@@ -11,54 +11,11 @@ const DEFAULTS = require('../../defaults');
1111const sdk = require ( '../../../../logic/sdk' ) ;
1212const _ = require ( 'lodash' ) ;
1313const { to } = require ( './../../../../logic/cli-config/errors/awaitTo' ) ;
14- const { prettyError } = require ( '../../../../logic/cli-config/errors/helpers ' ) ;
14+ const { createErrorHandler , getRelatedAgents , getRelatedNamespaces } = require ( './helper ' ) ;
1515
1616const 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 ) ;
6219
6320const deleteCmd = new Command ( {
6421 root : false ,
@@ -105,6 +62,8 @@ const deleteCmd = new Command({
10562
10663 const [ listReErr , runtimes ] = await to ( sdk . runtimeEnvs . list ( { } ) ) ;
10764 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' ) ;
10867
10968 console . log ( colors . green ( 'This uninstaller will guide you through the runner uninstallation process' ) ) ;
11069
@@ -124,7 +83,8 @@ const deleteCmd = new Command({
12483 }
12584
12685 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' ) ;
12888 let answer ;
12989 if ( ! relatedNamespaces . length ) {
13090 answer = await inquirer . prompt ( {
@@ -147,18 +107,18 @@ const deleteCmd = new Command({
147107 kubeNamespace = answer . namespace ;
148108 }
149109
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-
156110 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+ }
157117 const answer = await inquirer . prompt ( {
158118 type : 'list' ,
159119 name : 'name' ,
160120 message : 'Agent name to uninstall' ,
161- choices : agents ,
121+ choices : agentsChoices . map ( a => ` ${ a . name } \t ${ a . runtimes . length ? `(attached runtimes: ${ a . runtimes . join ( ', ' ) } )` : '' } ` ) ,
162122 } ) ;
163123 agentName = answer . name ;
164124 }
@@ -180,19 +140,39 @@ const deleteCmd = new Command({
1801403. Agent name: ${ colors . cyan ( agentName ) }
181141` ) ;
182142
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 ) => {
184164 const uninstallRuntimeOptions = {
185165 'agent-name' : agentName ,
186166 'runtime-kube-namespace' : kubeNamespace ,
187167 'runtime-kube-context-name' : kubeContextName ,
188168 'agent-kube-context-name' : kubeContextName ,
189169 'agent-kube-namespace' : kubeNamespace ,
190- name : agent . runtimes [ 0 ] ,
170+ name : reName ,
191171 terminateProcess : false ,
192172 } ;
193173 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+ } ) ;
196176
197177 const uninstallAgentOptions = {
198178 'kube-namespace' : kubeNamespace ,
0 commit comments