-
Notifications
You must be signed in to change notification settings - Fork 0
/
assets.py
executable file
·162 lines (149 loc) · 5.18 KB
/
assets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env python
import cli
import sys
import aws
import json
import string
# hierarchy of resource types:
#
# instance < vpc
# instance < subnet
# instance < network-interface
# instance ~ security-groups
#
# network-interface < subnet
# network-interface ~ security-group
# netowrk-interface < attachment = instance
type_defs = {
'vpc': {
'id': 'VpcId',
'list': [ 'ec2', 'describe-vpcs', 'Vpcs'],
'delete': [ 'ec2', 'delete-vpc', '--vpc-id', '{}' ],
'children': [
{ 'type': 'load-balancer', 'parent-ref': 'VPCId' },
{ 'type': 'route-table', 'filter': [ '--filters', 'Name=vpc-id,Values={}' ] },
{ 'type': 'subnet', 'filter': [ '--filters', 'Name=vpc-id,Values={}' ] },
{ 'type': 'network-interface', 'filter': [ '--filters', 'Name=vpc-id,Values={},Name=attachment.delete-on-termination,Values=false' ] },
{ 'type': 'security-group', 'filter': [ '--filters', 'Name=vpc-id,Values={}' ] },
]
},
'route-table': {
'id': 'RouteTableId',
'list': [ 'ec2', 'describe-route-tables', 'RouteTables' ],
'delete': [ 'ec2', 'delete-route-table', '--route-table-id', '{}' ],
},
'network-interface': {
'id': 'NetworkInterfaceId',
'list': [ 'ec2', 'describe-network-interfaces', 'NetworkInterfaces' ],
'delete': [ 'ec2', 'delete-network-interface', '--network-interface-id', '{}' ],
},
'load-balancer': {
'id': 'LoadBalancerName',
'list': [ 'elb', 'describe-load-balancers', 'LoadBalancerDescriptions' ],
'delete': [ 'elb', 'delete-load-balancer', '--load-balancer-name', '{}' ],
},
'instance': {
'id': 'InstanceId',
'list': [ 'ec2', 'describe-instances', 'Reservations,Instances' ],
'delete': [ 'ec2', 'terminate-instances', '--instance-ids', '{}' ],
# 'children': [
# { 'type': 'network-interface', 'filter': [ '--filters', 'Name=attachment.instance-id,Values={}' ] },
# ]
},
'subnet': {
'id': 'SubnetId',
'list': [ 'ec2', 'describe-subnets', 'Subnets' ],
'delete': [ 'ec2', 'delete-subnet', '--subnet-id', '{}' ],
'children': [
{ 'type': 'instance', 'filter': [ '--filters', 'Name=subnet-id,Values={}' ] },
]
},
'security-group': {
'id': 'GroupId',
'list': [ 'ec2', 'describe-security-groups', 'SecurityGroups' ],
'delete': [ 'ec2', 'delete-security-group', '--group-id', '{}' ],
},
}
def get_array(type_name, type_def, parent, fields):
items = parent.get(fields[0])
items = items if items is not None else []
if len(fields) == 1:
return [
{
'type': type_name,
'id': item.get(type_def["id"]),
'item': item
}
for item in items
]
else:
flattened = []
for item in items:
flattened += get_array(type_name, type_def, item, fields[1:])
return flattened
def get_items(type_name, item_id=None, filter=[]):
type_def = type_defs.get(type_name, None)
if type_def is None:
print "Unknown type", type_name
sys.exit(1)
container = json.loads(aws.aws_cli_verbose(type_def["list"][:-1] + filter))
fields = string.split(type_def["list"][-1], ',')
items = get_array(type_name, type_def, container, fields)
if item_id is not None:
items = [item for item in items if item["id"] == item_id]
return items
def get_tree(type_name, item_id=None, filter=[]):
items = get_items(type_name, item_id, filter)
for item in items:
type_def = type_defs.get(type_name)
subtypes = type_def.get("children", [])
for subtype in subtypes:
subtype_name = subtype["type"]
filter = subtype.get("filter", [])
filter = [element.replace('{}', item["id"]) for element in filter]
children = get_tree(subtype_name, filter=filter)
parent_ref = subtype.get("parent-ref", None)
if parent_ref is not None:
children = [child for child in children if child["item"].get(parent_ref) == item["id"]]
item["children"] = item.get("children", []) + children
return items
def print_tree(tree, indent=0):
for item in tree:
print ' ' * indent + item["type"], item["id"]
print_tree(item.get("children", []), indent + 3)
def delete_tree(tree, indent=0):
for item in tree:
children = item.get("children", [])
if len(children) > 0:
print ' ' * indent + "deleting dependencies of", item["type"], item["id"]
delete_tree(children, indent + 3)
type_def = type_defs[item["type"]]
command = type_def.get("delete", None)
if command is not None:
print ' ' * indent + "deleting", item["type"], item["id"]
command = [element.replace('{}', item["id"]) for element in command]
aws.aws_cli_verbose(command)
def list_cmd(argv):
cli.exit_with_usage(argv) if len(argv) < 2 else None
type_name = argv[1]
items = get_items(type_name)
print '\n'.join([item["id"] for item in items])
def tree_cmd(argv):
cli.exit_with_usage(argv) if len(argv) < 3 else None
type_name = argv[1]
item_id = argv[2] if argv[2] != "all" else None
tree = get_tree(type_name, item_id)
print_tree(tree)
def delete_cmd(argv):
cli.exit_with_usage(argv) if len(argv) < 3 else None
type_name = argv[1]
item_id = argv[2] if argv[2] != "all" else None
tree = get_tree(type_name, item_id)
delete_tree(tree)
commands = {
"list": { "func": list_cmd, "usage": "list <type>" },
"tree": { "func": tree_cmd, "usage": "tree <type> all|<id>" },
"delete": { "func": delete_cmd, "usage": "delete <type> all|<id>" },
}
if __name__ == '__main__':
cli.cli(sys.argv, commands)