armada/armada/cli/delete.py

154 lines
5.0 KiB
Python

# 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 oslo_config import cfg
from armada.cli import CliAction
from armada import const
from armada.handlers.chart_delete import ChartDelete
from armada.handlers.manifest import Manifest
from armada.handlers.tiller import Tiller
from armada.utils.release import release_prefixer
CONF = cfg.CONF
@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 deletes 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.option('--debug', help="Enable debug logging.", is_flag=True)
@click.pass_context
def delete_charts(ctx, manifest, releases, no_purge, tiller_host, tiller_port,
debug):
CONF.debug = debug
DeleteChartManifest(ctx, manifest, releases, no_purge, tiller_host,
tiller_port).safe_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):
with Tiller(
tiller_host=self.tiller_host,
tiller_port=self.tiller_port) as tiller:
self.handle(tiller)
def handle(self, tiller):
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.delete_release(r, purge=self.purge)
else:
raise NotImplementedError()
if self.manifest:
target_deletes = []
with open(self.manifest) as f:
documents = list(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):
chart = ch.get('chart')
release_name = release_prefixer(
prefix, chart.get('release'))
if release_name in known_release_names:
target_deletes.append((chart, 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_deletes:
self.logger.info("There's no release to delete.")
return
if not self.ctx.obj.get('api', False):
for chart, release in target_deletes:
chart_delete = ChartDelete(
chart, release, tiller, purge=self.purge)
chart_delete.delete()
else:
raise NotImplementedError()