Commit 731538d3 authored by Simon Redman's avatar Simon Redman

Use local node's ability to DNS lookup IP addresses for all FQDNs in the parsed network

parent 9c4ac85c
......@@ -19,7 +19,7 @@ import argparse
import copy
from collections import namedtuple
import json
import socket
# A lan is defined by a netmask and a cost
Lan = namedtuple('Lan', ['netmask', 'cost', ])
......@@ -112,6 +112,33 @@ def parse_topomap(topomap):
return to_return
def get_management_ips_of_nodes(nodes, node_nickname):
"""
Given a list of node names (e.g., 'node1'), resolve their FQDN to their management ips
:param nodes: List of node names
:type nodes: list[str]
:param node_nickname: Nickname of a single node from which the FQDN will be parsed. E.x.: node1.instance.project.emulab.net
:return: mapping of node names to ip addresses
"""
FQDN_template = "{host}.{instance}.{project}.{domain}"
management_ips = {}
nickname_parts = node_nickname.split('.')
# nickname_parts[0] is the hostname of a particular node and the part we will be replacing
instance = nickname_parts[1]
project = nickname_parts[2]
domain = '.'.join(nickname_parts[3:])
for host in nodes:
FQDN = FQDN_template.format(host=host, instance=instance, project=project, domain=domain)
IP = socket.gethostbyname(FQDN)
management_ips[host] = IP
return management_ips
def parse_topomap_to_netjson(topomap):
"""
Convert the Emulab topomap file into NetJSON [http://netjson.org/]
......@@ -131,6 +158,8 @@ def parse_topomap_to_netjson(topomap):
netjson['links'] = links
topodict = parse_topomap(topomap)
node_names = [node for node in topodict['nodes'].keys()]
management_ips = get_management_ips_of_nodes(node_names, socket.gethostname())
for node_name in topodict['nodes']:
node = {}
......@@ -138,7 +167,9 @@ def parse_topomap_to_netjson(topomap):
local_addresses = []
properties = {}
node['id'] = node_name # This might more usefully be the node's management IP
local_addresses.append(management_ips[node_name])
node['id'] = management_ips[node_name]
node['label'] = node_name
node['local_addresses'] = local_addresses
node['properties'] = properties
......@@ -188,7 +219,7 @@ if __name__ == "__main__":
parser.add_argument("--in-file", action='store', default='/var/emulab/boot/topomap', type=str,
help="Path to the netinfo file to parse")
parser.add_argument("--out-file", action='store', default='./netinfo.json', type=str,
help="Path to the where the result should be written. Existing file will be deleted")
help="Path to the where the result should be written. Existing file will be overwritten")
args = parser.parse_args()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment