Skip to content

Instantly share code, notes, and snippets.

@nrb
Created August 6, 2015 22:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nrb/db9223b3402e42758ccc to your computer and use it in GitHub Desktop.
Save nrb/db9223b3402e42758ccc to your computer and use it in GitHub Desktop.
Juno -> Kilo inventory migrator
#!/usr/bin/env python
import json
from pprint import pprint
import sys
def get_bridges(inventory):
"Returns bridge names and interface information for provider networks."
all_vars = inventory['all']['vars']
provider_networks = all_vars['provider_networks']
bridges = {}
for net_name in ('storage', 'tunnel', 'container'):
bridges[net_name] = {}
for pn in provider_networks:
network = pn['network']
if 'ip_from_q' not in network.keys():
continue
if network['ip_from_q'] == net_name:
bridges[net_name]['name'] = network['container_bridge']
bridges[net_name]['iface'] = network['container_interface']
return bridges
def update_container_vars(inventory, bridges):
"""
Maps container network information to the new kilo structure.
Kilo introduced a 'container_networks' dict per host that looks like
this:
"container_networks": {
"container_address": {
"address": <ip>,
"bridge": <bridge name>,
"interface": <interface name>,
"netmask": <netmask>
},
"storage_address":{
<same keypairs above>
}
}
In Juno, this was all contained in variables at the host level.
"""
hostvars = inventory['_meta']['hostvars']
for hostname, host in hostvars.items():
if 'container_networks' in host.keys():
continue
networks = {}
for net_type in ('container', 'storage', 'tunnel'):
if ('%s_address' % net_type) not in host.keys():
continue
net_addr = {
'address': host.get('%s_address' % net_type),
'netmask': host.get('%s_netmask' % net_type),
'bridge': bridges.get('%s' % net_type)['name'],
'interface': bridges.get('%s' % net_type)['iface'],
}
networks['%s_address' % net_type] = net_addr
host['container_networks'] = networks
scrub_old_keys(host)
def scrub_old_keys(host):
"Removes key/value pairs that are not present in kilo."
# Being explicit here, since 'container_address'
# remains a top-level item in the dictionary
old_keys = [
'container_network',
'container_netmask',
'storage_address',
'storage_netmask',
'tunnel_address',
'tunnel_netmask',
]
for key in old_keys:
if key in host.keys():
del host[key]
if __name__ == "__main__":
with open('rpc_inventory.json', 'r') as fp:
inventory = json.load(fp)
bridges = get_bridges(inventory)
update_container_vars(inventory, bridges)
with open('temp.json', 'w') as fp:
json.dump(inventory, indent=4, sort_keys=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment