Skip to content

Instantly share code, notes, and snippets.

@celebdor
Created September 19, 2017 11:39
Show Gist options
  • Save celebdor/93133419a1820e6bb2991e89e2077390 to your computer and use it in GitHub Desktop.
Save celebdor/93133419a1820e6bb2991e89e2077390 to your computer and use it in GitHub Desktop.
Binds kuryr owned DOWN ports to the available trunk ports
import itertools
import sys
import os
import os_client_config
neutron = os_client_config.make_client('network', cloud='envvars')
trunks = neutron.list_trunks()['trunks']
trunk_allocation = {}
for trunk in trunks:
used_ids = set([subport['segmentation_id'] for subport in trunk['sub_ports']])
trunk_allocation[trunk['id']] = used_ids
d_ports = neutron.list_ports(device_owner='compute:kuryr', status='DOWN')['ports']
sub_port_ids = [port['id'] for port in d_ports]
ports_per_trunk = len(sub_port_ids) / len(trunks)
for trunk_id, used_ids in trunk_allocation.items():
trunk_subport_ids = sub_port_ids[:ports_per_trunk]
del sub_port_ids[:ports_per_trunk]
available_ids = (id for id in range(1, 4096) if id not in used_ids)
vlan_ids = itertools.islice(available_ids, ports_per_trunk)
subports = []
for port_id, vlan_id in itertools.izip(trunk_subport_ids, vlan_ids):
subports.append({
'port_id': port_id,
'segmentation_type': 'vlan',
'segmentation_id': vlan_id
})
print('Adding ports %s to trunk %s' % (trunk_subport_ids, trunk_id))
neutron.trunk_add_subports(trunk_id,
{'sub_ports': subports})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment