From 19a49a9298d2e924274c81a5116f6ad26d2c2725 Mon Sep 17 00:00:00 2001 From: Travis Cotton Date: Thu, 4 Apr 2024 13:27:22 -0600 Subject: [PATCH] trying to make less smd calls --- dnsmasq-dhcpd-dynamic/smd.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/dnsmasq-dhcpd-dynamic/smd.py b/dnsmasq-dhcpd-dynamic/smd.py index 4153ee5..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,14 +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': - component = getSMD(f"http://{smd_endpoint}:{smd_port}/hsm/v2/State/Components/{i['ComponentID']}") - if 'NID' in component: - nidname='nid'+'%0*d' % (3, component['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) @@ -44,9 +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']: - component = getSMD(f"http://{smd_endpoint}:{smd_port}/hsm/v2/State/Components/{i['ComponentID']}") - if 'NID' in component: - nidname='nid'+'%0*d' % (3, component['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)