diff --git a/armada/cli/delete.py b/armada/cli/delete.py new file mode 100644 index 00000000..883bae33 --- /dev/null +++ b/armada/cli/delete.py @@ -0,0 +1,136 @@ +# Copyright 2017 The Armada Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import yaml + +import click + +from armada.cli import CliAction +from armada import const +from armada.handlers.manifest import Manifest +from armada.handlers.tiller import Tiller +from armada.utils.release import release_prefix + + +@click.group() +def delete(): + """ Delete releases by targeting specific releases or via a manifest file. + + """ + + +DESC = """ +This command deletes releases. + +The delete command will delete the releases either via a manifest +or by targeting specific releases. + +To delete all the releases that are created by the armada manifest: + + $ armada delete --manifest examples/simple.yaml + +To delete releases by the name: + + $ armada delete --releases blog-1 + + or + + $ armada delete --releases blog-1,blog-2,blog-3 + +""" + +SHORT_DESC = "command delete releases" + + +@delete.command(name='delete', help=DESC, short_help=SHORT_DESC) +@click.option('--manifest', help='Armada manifest file', type=str) +@click.option( + '--releases', help='Comma-separated list of release names', type=str) +@click.option( + '--no-purge', help="Deletes release without purge option", is_flag=True) +@click.option('--tiller-host', help="Tiller Host IP") +@click.option( + '--tiller-port', help="Tiller host Port", type=int, default=44134) +@click.pass_context +def delete_charts(ctx, manifest, releases, no_purge, tiller_host, tiller_port): + DeleteChartManifest( + ctx, manifest, releases, no_purge, tiller_host, tiller_port).invoke() + + +class DeleteChartManifest(CliAction): + def __init__(self, ctx, manifest, releases, no_purge, tiller_host, + tiller_port): + + super(DeleteChartManifest, self).__init__() + self.ctx = ctx + self.manifest = manifest + self.releases = releases + self.purge = not no_purge + self.tiller_host = tiller_host + self.tiller_port = tiller_port + + def invoke(self): + tiller = Tiller( + tiller_host=self.tiller_host, tiller_port=self.tiller_port) + known_release_names = [release[0] for release in tiller.list_charts()] + + if self.releases: + target_releases = [r.strip() for r in self.releases.split(',') + if r.strip() in known_release_names] + if not target_releases: + self.logger.info("There's no release to delete.") + return + + if not self.ctx.obj.get('api', False): + for r in target_releases: + self.logger.info("Deleting release %s", r) + tiller.uninstall_release(r, purge=self.purge) + + else: + raise NotImplementedError() + + if self.manifest: + target_releases = [] + + with open(self.manifest) as f: + documents = yaml.safe_load_all(f.read()) + try: + armada_obj = Manifest(documents).get_manifest() + prefix = armada_obj.get(const.KEYWORD_ARMADA).get( + const.KEYWORD_PREFIX) + + for group in armada_obj.get(const.KEYWORD_ARMADA).get( + const.KEYWORD_GROUPS): + for ch in group.get(const.KEYWORD_CHARTS): + release_name = release_prefix( + prefix, ch.get('chart').get('chart_name')) + if release_name in known_release_names: + target_releases.append(release_name) + except yaml.YAMLError as e: + mark = e.problem_mark + self.logger.info("While parsing the manifest file, %s. " + "Error position: (%s:%s)", e.problem, + mark.line + 1, mark.column + 1) + + if not target_releases: + self.logger.info("There's no release to delete.") + return + + if not self.ctx.obj.get('api', False): + for r in target_releases: + self.logger.info("Deleting release %s", r) + tiller.uninstall_release(r, purge=self.purge) + + else: + raise NotImplementedError() diff --git a/armada/handlers/tiller.py b/armada/handlers/tiller.py index 1bd22136..35da56ed 100644 --- a/armada/handlers/tiller.py +++ b/armada/handlers/tiller.py @@ -446,6 +446,8 @@ class Tiller(object): # build release install request try: stub = ReleaseServiceStub(self.channel) + LOG.info("Uninstall %s release with disable_hooks=%s, " + "purge=%s flags", release, disable_hooks, purge) release_request = UninstallReleaseRequest( name=release, disable_hooks=disable_hooks, purge=purge) diff --git a/armada/shell.py b/armada/shell.py index 11d67bf6..35aa8ac6 100644 --- a/armada/shell.py +++ b/armada/shell.py @@ -19,6 +19,7 @@ from oslo_config import cfg from oslo_log import log from armada.cli.apply import apply_create +from armada.cli.delete import delete_charts from armada.cli.test import test_charts from armada.cli.tiller import tiller_service from armada.cli.validate import validate_manifest @@ -47,6 +48,7 @@ def main(ctx, debug, api, url, token): \b $ armada apply + $ armada delete $ armada test $ armada tiller $ armada validate @@ -87,6 +89,7 @@ def main(ctx, debug, api, url, token): main.add_command(apply_create) +main.add_command(delete_charts) main.add_command(test_charts) main.add_command(tiller_service) main.add_command(validate_manifest)