#!/usr/bin/env python

from argparse import ArgumentParser
import logging

from jujupy import SimpleEnvironment
from substrate import AWSAccount


def parse_args():
    parser = ArgumentParser('Clean up leftover resources.')
    parser.add_argument('env', help='The juju environment to use.')
    return parser.parse_args()


def main():
    args = parse_args()
    logging.basicConfig(level=logging.INFO)
    logging.getLogger('boto').setLevel(logging.CRITICAL)
    env = SimpleEnvironment.from_config(args.env)
    substrate = AWSAccount.from_config(env.config)
    all_groups = dict(substrate.list_security_groups())
    instance_groups = dict(substrate.list_instance_security_groups())
    non_instance_groups = dict((k, v) for k, v in all_groups.items()
                               if k not in instance_groups)
    unclean = substrate.delete_detached_interfaces(
        non_instance_groups.keys())
    for group_id in unclean:
        non_instance_groups.pop(group_id, None)
    substrate.destroy_security_groups(non_instance_groups.values())
    print "Unable to delete {} groups.".format(len(unclean))
    for group_id in unclean:
        print all_groups[group_id]


if __name__ == '__main__':
    main()

@@ -282,24 +282,94 @@
 t to use.')%0A
+    parser.add_argument('--verbose', '-v', action='count', default=0)%0A
     return p
@@ -421,24 +421,93 @@
 arse_args()%0A
+    log_level = max(logging.WARN - args.verbose * 10, logging.DEBUG)%0A
     logging.
@@ -531,17 +531,14 @@
 =log
-ging.INFO
+_level
 )%0A  
@@ -1032,24 +1032,192 @@
 ups.keys())%0A
+    logging.info('Unable to delete %7B%7D groups'.format(len(unclean)))%0A    for group_id in unclean:%0A        logging.debug('Cannot delete %7B%7D'.format(all_groups%5Bgroup_id%5D))%0A
     for grou
@@ -1353,133 +1353,8 @@
 ())%0A
-    print %22Unable to delete %7B%7D groups.%22.format(len(unclean))%0A    for group_id in unclean:%0A        print all_groups%5Bgroup_id%5D%0A
 %0A%0Aif

Tweak logging.