comparison master/bin/cull_network.py @ 7:a7637c994964

cull_network.py, parse_load_balance.py, vmss_setup.sh x 2, vmss_create.sh: From kenneth, slight mods in some cases share.sh: Distribute files to workers wrun.sh, invoke.sh: From last year, slight mods
author Henry S. Thompson <ht@markup.co.uk>
date Sun, 30 Sep 2018 20:53:43 +0000
parents
children
comparison
equal deleted inserted replaced
6:a3b30e707e83 7:a7637c994964
1 #!/usr/bin/env python2
2 #Print commands to delete load balancers, NICs, and IPs not attached to any VM
3 #Plenty of bugs here. Assumes a stock setup with nothing fancy.
4 import json
5 import subprocess
6
7 def query(command):
8 return json.load(subprocess.Popen(command, stdout=subprocess.PIPE).stdout)
9
10 lb = query(["az", "network", "lb", "list", "-o", "json"])
11 vmss = query(["az", "vmss", "list", "-o", "json"])
12
13 in_use_lbs = []
14 for m in vmss:
15 balancer = m["virtualMachineProfile"]["networkProfile"]["networkInterfaceConfigurations"][0]["ipConfigurations"][0]["loadBalancerBackendAddressPools"]
16 if balancer:
17 in_use_lbs.append('/'.join(balancer[0]["id"].split('/')[0:9]))
18
19 allocated_lbs = ['/'.join(l["frontendIpConfigurations"][0]["id"].split('/')[0:9]) for l in lb]
20
21 unused_lbs = set(allocated_lbs) - set(in_use_lbs)
22 for l in unused_lbs:
23 split = l.split('/')
24 print "az network lb delete -g " + split[4] + " -n " + split[8]
25
26 #TODO: exclude the lbs that are to be deleted
27 ip_used_by_lb = [l["frontendIpConfigurations"][0]["publicIpAddress"]["id"] for l in lb]
28
29
30 vm = query(["az", "vm", "list", "-o", "json"])
31 in_use_nics = [v["networkProfile"]["networkInterfaces"][0]["id"] for v in vm]
32 nics = query(["az", "network", "nic", "list", "-o", "json"])
33 allocated_nics = [n["id"] for n in nics]
34 for n in set(allocated_nics) - set(in_use_nics):
35 split = n.split('/')
36 print "az network nic delete -g " + split[4] + " -n " + split[8]
37
38 #TODO: exclude the nics that are to be deleted
39 ip_used_by_nic = [n["ipConfigurations"][0]["publicIpAddress"]["id"] for n in nics]
40 in_use_ips = ip_used_by_lb + ip_used_by_nic
41
42 allocated_ips = [i["id"] for i in query(["az", "network", "public-ip", "list", "-o", "json"])]
43 for i in set(allocated_ips) - set(in_use_ips):
44 split = i.split('/')
45 print "az network public-ip delete -g " + split[4] + " -n " + split[8]
46
47 nsg_all = [n['id'] for n in query(["az", "network", "nsg", "list", "-o", "json"])]
48 for i in set(nsg_all) - set([n['networkSecurityGroup']['id'] for n in nics]):
49 split = i.split('/')
50 print "az network nsg delete -g " + split[4] + " -n " + split[8]