#!/usr/bin/env python
# Copyright 2015-2018 Yelp Inc.
# 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.
"""
Usage: ./cleanup_tron_namespaces.py [options]

Delete namespaces that aren't configured in SOA configs for a Tron cluster.

Gets the list of namespaces from Tron, then compares to the namespaces
defined in SOA configs.

- -d <SOA_DIR>, --soa-dir <SOA_DIR>: Specify a SOA config dir to read from
"""
import argparse
import sys

from paasta_tools import tron_tools
from paasta_tools.utils import paasta_print


def parse_args():
    parser = argparse.ArgumentParser(description='Cleans up stale Tron namespaces.')
    parser.add_argument(
        '-d', '--soa-dir', dest='soa_dir', metavar='SOA_DIR',
        default=tron_tools.DEFAULT_SOA_DIR,
        help='Use a different soa config directory',
    )
    args = parser.parse_args()
    return args


def main():
    args = parse_args()

    client = tron_tools.get_tron_client()
    namespaces = client.list_namespaces()
    expected_namespaces = tron_tools.get_tron_namespaces_for_cluster(soa_dir=args.soa_dir)
    to_delete = set(namespaces) - set(expected_namespaces)

    if not to_delete:
        paasta_print('No Tron namespaces to remove')
        sys.exit(0)

    successes = []
    errors = []
    for namespace in to_delete:
        try:
            client.update_namespace(namespace, '')
            successes.append(namespace)
        except Exception as e:
            errors.append((namespace, e))

    if successes:
        paasta_print('Successfully removed namespaces:\n', '\n  '.join(successes))

    if errors:
        paasta_print(
            'Failed to remove namespaces:\n  ' + '\n  '.join(
                ['{namespace}: {error}'.format(namespace=namespace, error=str(error)) for namespace, error in errors],
            ),
        )
        sys.exit(1)


if __name__ == '__main__':
    main()

@@ -890,16 +890,85 @@
 ad from%0A
+- --dry-run: Print namespaces to be deleted instead of deleting them%0A
 %22%22%22%0Aimpo
@@ -1362,24 +1362,186 @@
 ory',%0A    )%0A
+    parser.add_argument(%0A        '--dry-run', dest='dry_run', action='store_true',%0A        help='Print namespaces to be deleted, instead of deleting them',%0A    )%0A
     args = p
@@ -1863,32 +1863,32 @@
  not to_delete:%0A
-
         paasta_p
@@ -1945,16 +1945,151 @@
 xit(0)%0A%0A
+    if args.dry_run:%0A        paasta_print('Dry run, would have removed namespaces:%5Cn  ' + '%5Cn  '.join(to_delete))%0A        sys.exit(0)%0A%0A
     succ

Add dry-run to Tron cleanup script