726.1.1
by Aaron Bentley
Implement first stab at clean-resources command. |
1 |
#!/usr/bin/env python
|
2 |
||
3 |
from argparse import ArgumentParser |
|
4 |
import logging |
|
5 |
||
1241.1.3
by seman.said at canonical
Clean up resources in all regions. |
6 |
from boto.ec2 import regions |
7 |
||
726.1.1
by Aaron Bentley
Implement first stab at clean-resources command. |
8 |
from jujupy import SimpleEnvironment |
9 |
from substrate import AWSAccount |
|
10 |
||
11 |
||
1241.1.3
by seman.said at canonical
Clean up resources in all regions. |
12 |
def parse_args(argv=None): |
726.1.1
by Aaron Bentley
Implement first stab at clean-resources command. |
13 |
parser = ArgumentParser('Clean up leftover resources.') |
14 |
parser.add_argument('env', help='The juju environment to use.') |
|
726.1.8
by Aaron Bentley
Tweak logging. |
15 |
parser.add_argument('--verbose', '-v', action='count', default=0) |
1241.1.3
by seman.said at canonical
Clean up resources in all regions. |
16 |
parser.add_argument('-a', '--all-regions', action='store_true', |
17 |
help='Take action on all regions.') |
|
18 |
return parser.parse_args(argv) |
|
19 |
||
20 |
||
21 |
def get_regions(args, env): |
|
22 |
if args.all_regions: |
|
23 |
return [region.name for region in filter( |
|
24 |
lambda x: '-gov-' not in x.name, regions())] |
|
1674.1.12
by Aaron Bentley
Switch clean_resources to get_region. |
25 |
return [env.get_region()] |
726.1.1
by Aaron Bentley
Implement first stab at clean-resources command. |
26 |
|
27 |
||
1241.1.4
by seman.said at canonical
Updated clean_resources unit tests. |
28 |
def clean(args): |
726.1.1
by Aaron Bentley
Implement first stab at clean-resources command. |
29 |
env = SimpleEnvironment.from_config(args.env) |
1241.1.3
by seman.said at canonical
Clean up resources in all regions. |
30 |
selected_regions = get_regions(args, env) |
31 |
for region in selected_regions: |
|
1699.2.8
by Aaron Bentley
Update clean_resources for new from_boot_config. |
32 |
with AWSAccount.from_boot_config(env, region=region) as substrate: |
1241.1.3
by seman.said at canonical
Clean up resources in all regions. |
33 |
logging.info('Cleaning resources in {}.'.format(substrate.region)) |
34 |
all_groups = dict(substrate.iter_security_groups()) |
|
35 |
instance_groups = dict(substrate.iter_instance_security_groups()) |
|
36 |
non_instance_groups = dict((k, v) for k, v in all_groups.items() |
|
37 |
if k not in instance_groups) |
|
38 |
unclean = substrate.delete_detached_interfaces( |
|
39 |
non_instance_groups.keys()) |
|
40 |
logging.info('Unable to delete {} groups'.format(len(unclean))) |
|
41 |
for group_id in unclean: |
|
42 |
logging.debug('Cannot delete {}'.format(all_groups[group_id])) |
|
43 |
for group_id in unclean: |
|
44 |
non_instance_groups.pop(group_id, None) |
|
45 |
substrate.destroy_security_groups(non_instance_groups.values()) |
|
726.1.1
by Aaron Bentley
Implement first stab at clean-resources command. |
46 |
|
47 |
||
1241.1.4
by seman.said at canonical
Updated clean_resources unit tests. |
48 |
def main(): |
49 |
args = parse_args() |
|
1241.1.5
by seman.said at canonical
Moved logging config to main function. |
50 |
log_level = max(logging.WARN - args.verbose * 10, logging.DEBUG) |
51 |
logging.basicConfig(level=log_level) |
|
52 |
logging.getLogger('boto').setLevel(logging.CRITICAL) |
|
1241.1.4
by seman.said at canonical
Updated clean_resources unit tests. |
53 |
clean(args) |
54 |
||
55 |
||
726.1.1
by Aaron Bentley
Implement first stab at clean-resources command. |
56 |
if __name__ == '__main__': |
57 |
main() |