Skip to content

Instantly share code, notes, and snippets.

@emonty
Created February 9, 2015 16:05
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 emonty/d8d3c7f98d8f7f46e5f1 to your computer and use it in GitHub Desktop.
Save emonty/d8d3c7f98d8f7f46e5f1 to your computer and use it in GitHub Desktop.
# Test to see if config-drive exists. If not, skip and assume DHCP networking
# will work becasue sanity
if ! blkid -t LABEL="config-2" ; then
exit 0
fi
mkdir -p /mnt/config
mount /dev/disk/by-label/config-2 /mnt/config
python /usr/local/bin/read_vendor_json.py
import json
import platform
import pprint
import sys
post_up=" post-up route add -net {net} netmask {mask} gw {gw} || true\n"
pre_down=" pre-down route del -net {net} netmask {mask} gw {gw} || true\n"
def _write_rh_interface(name, interface):
files_to_write=dict()
results = """# Automatically generated, do not edit
DEVICE={name}
BOOTPROTO=static
HWADDR={hwaddr}
IPADDR={ip_address}
NETMASK={netmask}
ONBOOT=yes
NM_CONTROLLED=no
""".format(
name=name,
hwaddr=interface['mac_address'],
ip_address=interface['ip_address'],
netmask=interface['netmask'],
)
routes=[]
for route in interface['routes']:
if route['network'] == '0.0.0.0' and route['netmask'] == '0.0.0.0':
results += "DEFROUTE=yes\n"
results += "GATEWAY={gw}\n".format(gw=route['gateway'])
else:
routes.append(dict(
net=route['network'], mask=route['netmask'],
gw=route['gateway']))
if routes:
route_content = ""
for x in range(0, len(routes)):
route_content += "ADDRESS{x}={net}\n".format(x=x, **routes[x])
route_content += "NETMASK{x}={mask}\n".format(x=x, **routes[x])
route_content += "GATEWAY{x}={gw}\n".format(x=x, **routes[x])
files_to_write['/etc/sysconfig/network-scripts/route-{name}'.format(
name=name)] = route_content
files_to_write['/etc/sysconfig/network-scripts/ifcfg-{name}'.format(
name=name)] = results
return files_to_write
def write_redhat_interfaces(interfaces):
files_to_write = dict()
for iname, interface in interfaces.items():
interface_name = interface['id'].replace('network', 'eth')
files_to_write.update(_write_rh_interface(interface_name, interface))
return files_to_write
def write_debian_interfaces(interfaces):
results = ""
for iname, interface in interfaces.items():
link_type="inet"
if interface['type'] == 'ipv6':
link_type="inet6"
interface_name = interface['id'].replace('network', 'eth')
results += "auth {0}\n".format(interface_name)
results += "iface {name} {link_type} static\n".format(
name=interface_name, link_type=link_type)
results += " address {0}\n".format(interface['ip_address'])
results += " netmask {0}\n".format(interface['netmask'])
for route in interface['routes']:
if route['network'] == '0.0.0.0' and route['netmask'] == '0.0.0.0':
results += " gateway {0}\n".format(route['gateway'])
else:
results += post_up.format(
net=route['network'], mask=route['netmask'],
gw=route['gateway'])
results += pre_down.format(
net=route['network'], mask=route['netmask'],
gw=route['gateway'])
return {'/etc/network/interfaces': results}
def write_dns_info(dns_servers):
results = ""
for server in dns_servers:
results += "nameserver {0}\n".format(server)
return {'/etc/resolv.conf': results}
def main():
v=json.load(open('vendor_data.json'))
net=v['network_info']
dns_servers=[ f['address'] for f in net['services'] if f['type'] == 'dns']
interfaces = {}
for network in net['networks']:
interfaces[network['link']] = network
for link in net['links']:
interfaces[link['id']]['mac_address'] = link['ethernet_mac_address']
distro=platform.dist()[0].lower()
if distro in ('debian', 'ubuntu'):
files_to_write = write_debian_interfaces(interfaces)
elif distro in ('redhat', 'centos', 'fedora', 'suse', 'opensuse'):
files_to_write = write_redhat_interfaces(interfaces)
files_to_write.update(write_dns_info(dns_servers))
for k,v in files_to_write.items():
print "### Write {0}".format(k)
print v
if __name__ == '__main__':
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment