diff --git a/dnsmasq-dhcpd-dynamic/smd.py b/dnsmasq-dhcpd-dynamic/smd.py index 632ac5e..0c3bc51 100644 --- a/dnsmasq-dhcpd-dynamic/smd.py +++ b/dnsmasq-dhcpd-dynamic/smd.py @@ -10,6 +10,12 @@ def getSMD(url): data = r.json() return data +def getNID(c_data, xname): + for c in c_data: + if xname == c['ID']: + return 'nid'+'%0*d' % (3, c['NID']) + else: + return None def main(): sighup = False @@ -18,13 +24,14 @@ def main(): bss_endpoint=os.environ['bss_endpoint'] bss_port=os.environ['bss_port'] ei_data = getSMD(f'http://{smd_endpoint}:{smd_port}/hsm/v2/Inventory/EthernetInterfaces') + component_data = getSMD(f"http://{smd_endpoint}:{smd_port}/hsm/v2/State/Components")['Components'] #hostsfile = tempfile.TemporaryFile(mode = "r+") hostsfile = open("/etc/dhcp-hostsfile-new", "w") #this for loop writes host entries for i in ei_data: if i['Type'] != 'NodeBMC': - if 'NID' in i: - nidname='nid'+'%0*d' % (3, i['NID']) + nidname=getNID(component_data, i['ComponentID']) + if nidname: print(f"{i['MACAddress']},set:{nidname},{i['IPAddresses'][0]['IPAddress']},{nidname}", file=hostsfile) else: print(f"{i['MACAddress']},set:{i['ComponentID']},{i['IPAddresses'][0]['IPAddress']},{i['ComponentID']}", file=hostsfile) @@ -43,8 +50,8 @@ def main(): #this for loop writes option entries, we wouldn't need it if the BSS wasn't MAC specific for i in ei_data: if 'bmc' not in i['Description']: - if 'NID' in i: - nidname='nid'+'%0*d' % (3, i['NID']) + nidname=getNID(component_data, i['ComponentID']) + if nidname: print(f"tag:{nidname},tag:IPXEBOOT,option:bootfile-name,\"http://{bss_endpoint}:{bss_port}/boot/v1/bootscript?mac={i['MACAddress']}\"", file=optsfile) else: print(f"tag:{i['ComponentID']},tag:IPXEBOOT,option:bootfile-name,\"http://{bss_endpoint}:{bss_port}/boot/v1/bootscript?mac={i['MACAddress']}\"", file=optsfile)