Mercurial > code > home > repos > infra
diff dns.py @ 38:4026b6b8028f
get dhcp mapping from lanscape
author | drewp@bigasterisk.com |
---|---|
date | Sun, 16 Jan 2022 12:12:21 -0800 |
parents | d4fb38f13c79 |
children | e3249dd163fc |
line wrap: on
line diff
--- a/dns.py Sat Jan 15 17:10:10 2022 -0800 +++ b/dns.py Sun Jan 16 12:12:21 2022 -0800 @@ -1,16 +1,45 @@ +import subprocess +import tempfile + +import requests from pyinfra import host from pyinfra.operations import apt, files, server, systemd + +def prepare_dhcp_hosts(): + lanscape_ip = subprocess.check_output([ + 'kubectl', + 'get', + 'svc', + '--field-selector=metadata.name=lanscape', + "-o=jsonpath={.items[0].spec.clusterIP}", + ], + encoding='ascii') + url = f'http://{lanscape_ip}/dnsConfig' + resp = requests.get(url) + resp.raise_for_status() + lanscape_config = resp.json() + + dhcp_hosts = tempfile.NamedTemporaryFile(mode='wt', encoding='ascii') + dhcp_hosts.write("# written by pyinfra\n\n") + for row in lanscape_config['dhcp_table']: + dhcp_hosts.write(f'{row["mac"]},{row["hostname"]},{row["ip"]},infinite\n') + dhcp_hosts.flush() + return dhcp_hosts + + if host.name == 'bang': apt.packages(packages=['dnsmasq']) systemd.service(service='dnsmasq', enabled=False, running=False) files.directory(path='/opt/dnsmasq') + dhcp_hosts = prepare_dhcp_hosts() + for net_name in ['10.1', '10.2', '10.5']: files.directory(path=f'/opt/dnsmasq/{net_name}') files.template(src='templates/dnsmasq/dnsmasq.conf.j2', dest=f'/opt/dnsmasq/{net_name}/dnsmasq.conf', net=net_name) files.template(src='templates/dnsmasq/hosts.j2', dest=f'/opt/dnsmasq/{net_name}/hosts', net=net_name) - files.template(src='templates/dnsmasq/dhcp_hosts.j2', dest=f'/opt/dnsmasq/{net_name}/dhcp_hosts', net=net_name) + files.template(src=dhcp_hosts.name, dest=f'/opt/dnsmasq/{net_name}/dhcp_hosts', net=net_name) files.template(src='templates/dnsmasq/dnsmasq.service.j2', dest=f'/etc/systemd/system/dnsmasq_{net_name}.service',