@@ -10,15 +10,15 @@ const colors = require('colors');
1010const DEFAULTS = require ( '../../defaults' ) ;
1111const sdk = require ( '../../../../logic/sdk' ) ;
1212const _ = require ( 'lodash' ) ;
13- const YAML = require ( 'yaml' ) ;
14- const fs = require ( 'fs' ) ;
1513const { to } = require ( './../../../../logic/cli-config/errors/awaitTo' ) ;
1614const {
1715 createErrorHandler,
1816 getRelatedAgents,
1917 getRelatedNamespaces,
2018 drawCodefreshFiglet,
2119 unInstallAppProxy,
20+ mergeValuesFromValuesFile,
21+ INSTALLATION_DEFAULTS ,
2222} = require ( './helper' ) ;
2323
2424const defaultNamespace = 'codefresh' ;
@@ -29,14 +29,34 @@ async function promptConfirmationMessage({
2929 agentName,
3030 kubeNamespace,
3131 attachedRuntimes,
32+ defaultRuntime,
33+ clustersToDelete,
34+ appProxyToDelete,
3235} ) {
36+ const note = colors . underline ( colors . yellow ( 'Note' ) ) ;
37+ let newDefaultRuntime = defaultRuntime ;
38+
3339 // prompt confirmation message
3440 console . log ( `${ colors . red ( 'This process will attempt to delete the following:' ) } ` ) ;
3541 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]' : '' } ` ) ; } ) ;
3743 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` ) ;
4060
4161 const answer = await inquirer . prompt ( {
4262 type : 'confirm' ,
@@ -50,6 +70,27 @@ async function promptConfirmationMessage({
5070 }
5171}
5272
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+
5394const deleteCmd = new Command ( {
5495 root : false ,
5596 parent : runnerRoot ,
@@ -90,50 +131,39 @@ const deleteCmd = new Command({
90131 describe : 'Print logs' ,
91132 } ) ,
92133 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+
93141 const {
94142 verbose,
95- } = argv ;
96- let {
143+ force,
97144 'kube-config-path' : kubeConfigPath ,
98- url, force,
145+ } = _argv ;
146+ let {
147+ url,
99148 'kube-context-name' : kubeContextName ,
100149 'kube-namespace' : kubeNamespace ,
101150 name : agentName ,
102- values : valuesFile ,
103- } = argv ;
151+ } = _argv ;
104152
105153 const [ listReErr , runtimes ] = await to ( sdk . runtimeEnvs . list ( { } ) ) ;
106154 await handleError ( listReErr , 'Failed to get runtime environments' ) ;
107155 const [ listAgentsErr , agents ] = await to ( sdk . agents . list ( { } ) ) ;
108156 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' ) ;
109159
110- if ( ! agents . length ) {
160+ if ( ! agents || ! agents . length ) {
111161 console . log ( 'No runners found on your codefresh account' ) ;
112162 process . exit ( 0 ) ;
113163 }
114164
115165 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- }
132166
133- if ( ! agentName && valuesObj . AgentId ) {
134- agentName = valuesObj . AgentId ;
135- }
136- }
137167 if ( ! url ) {
138168 url = DEFAULTS . URL ;
139169 }
@@ -216,9 +246,21 @@ const deleteCmd = new Command({
216246` ) ;
217247
218248 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 ) ;
219254
220255 if ( ! force ) {
221- await promptConfirmationMessage ( { agentName, kubeNamespace, attachedRuntimes } ) ;
256+ await promptConfirmationMessage ( {
257+ agentName,
258+ kubeNamespace,
259+ attachedRuntimes,
260+ defaultRuntime,
261+ clustersToDelete,
262+ appProxyToDelete,
263+ } ) ;
222264 }
223265
224266 await Promise . all ( attachedRuntimes . map ( async ( reName ) => {
@@ -233,16 +275,36 @@ const deleteCmd = new Command({
233275 verbose,
234276 } ;
235277 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+
236292 if ( re . appProxy ) {
237- await unInstallAppProxy ( {
293+ const [ uninstallAppProxyErr ] = await to ( unInstallAppProxy ( {
238294 kubeConfigPath,
239295 kubeContextName,
240296 kubeNamespace,
241297 verbose,
242- } ) ;
298+ } ) ) ;
299+ handleError ( uninstallAppProxyErr , 'Failed to uninstall app-proxy' ) ;
300+ console . log ( 'App-Proxy uninstalled successfully' ) ;
243301 }
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' ) ;
246308 } ) ) ;
247309
248310 const uninstallAgentOptions = {
0 commit comments