-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add workspace:delete command (#586)
* Add `workspace:delete` command Signed-off-by: Anatoliy Bazko <[email protected]>
- Loading branch information
Showing
7 changed files
with
213 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/********************************************************************* | ||
* Copyright (c) 2019 Red Hat, Inc. | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
**********************************************************************/ | ||
|
||
import { Command, flags } from '@oclif/command' | ||
import { cli } from 'cli-ux' | ||
import * as Listrq from 'listr' | ||
import * as notifier from 'node-notifier' | ||
|
||
import { CheHelper } from '../../api/che' | ||
import { KubeHelper } from '../../api/kube' | ||
import { accessToken, cheNamespace } from '../../common-flags' | ||
import { CheTasks } from '../../tasks/che' | ||
import { ApiTasks } from '../../tasks/platforms/api' | ||
|
||
export default class Delete extends Command { | ||
static description = 'delete a user\'s workspace' | ||
|
||
static flags = { | ||
help: flags.help({ char: 'h' }), | ||
chenamespace: cheNamespace, | ||
'delete-namespace': flags.boolean({ | ||
description: 'Indicates that a Kubernetes namespace where workspace was created will be deleted as well', | ||
default: false | ||
}), | ||
'access-token': accessToken | ||
} | ||
static args = [ | ||
{ | ||
name: 'workspace', | ||
description: 'The workspace id to delete', | ||
required: true | ||
} | ||
] | ||
|
||
async run() { | ||
const { flags } = this.parse(Delete) | ||
const { args } = this.parse(Delete) | ||
const ctx: any = {} | ||
ctx.workspaces = [] | ||
|
||
const apiTasks = new ApiTasks() | ||
const cheTasks = new CheTasks(flags) | ||
const cheHelper = new CheHelper(flags) | ||
const kubeHelper = new KubeHelper(flags) | ||
const tasks = new Listrq(undefined, { renderer: 'silent' }) | ||
|
||
tasks.add(apiTasks.testApiTasks(flags, this)) | ||
tasks.add(cheTasks.verifyCheNamespaceExistsTask(flags, this)) | ||
tasks.add(cheTasks.retrieveEclipseCheUrl(flags)) | ||
tasks.add(cheTasks.checkEclipseCheStatus()) | ||
tasks.add({ | ||
title: `Get workspace with id '${args.workspace}'`, | ||
task: async (ctx, task) => { | ||
const workspace = await cheHelper.getWorkspace(ctx.cheURL, args.workspace, flags['access-token']) | ||
ctx.infrastructureNamespace = workspace.attributes.infrastructureNamespace | ||
task.title = `${task.title}... done` | ||
} | ||
}) | ||
tasks.add({ | ||
title: `Delete workspace with id '${args.workspace}'`, | ||
task: async (ctx, task) => { | ||
await cheHelper.deleteWorkspace(ctx.cheURL, args.workspace, flags['access-token']) | ||
cli.log(`Workspace with id '${args.workspace}' deleted.`) | ||
task.title = `${task.title}... done` | ||
} | ||
}) | ||
tasks.add({ | ||
title: 'Verify if namespace exists', | ||
enabled: () => flags['delete-namespace'], | ||
task: async (ctx, task) => { | ||
task.title = `${task.title} '${ctx.infrastructureNamespace}'` | ||
if (ctx.infrastructureNamespace === flags.chenamespace) { | ||
cli.warn(`It is not possible to delete namespace '${ctx.infrastructureNamespace}' since it is used for Eclipse Che deployment.`) | ||
return | ||
} | ||
|
||
ctx.infrastructureNamespaceExists = await kubeHelper.namespaceExist(ctx.infrastructureNamespace) | ||
if (ctx.infrastructureNamespaceExists) { | ||
task.title = `${task.title}... found` | ||
} else { | ||
task.title = `${task.title}... not found` | ||
} | ||
} | ||
}) | ||
tasks.add({ | ||
title: 'Delete namespace', | ||
skip: ctx => !ctx.infrastructureNamespaceExists, | ||
enabled: () => flags['delete-namespace'], | ||
task: async (ctx, task) => { | ||
task.title = `${task.title} '${ctx.infrastructureNamespace}'` | ||
await kubeHelper.deleteNamespace(ctx.infrastructureNamespace) | ||
cli.log(`Namespace '${ctx.infrastructureNamespace}' deleted.`) | ||
task.title = `${task.title}... done` | ||
} | ||
}) | ||
|
||
try { | ||
await tasks.run(ctx) | ||
} catch (error) { | ||
this.error(error) | ||
} | ||
|
||
notifier.notify({ | ||
title: 'chectl', | ||
message: 'Command workspace:delete has completed successfully.' | ||
}) | ||
|
||
this.exit(0) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters