Mercurial > code > home > repos > infra
changeset 38:4026b6b8028f
get dhcp mapping from lanscape
author | drewp@bigasterisk.com |
---|---|
date | Sun, 16 Jan 2022 12:12:21 -0800 |
parents | fbd0849dfdbd |
children | 3a52d3e69724 |
files | dns.py templates/dnsmasq/dhcp_hosts.j2 templates/dnsmasq/dnsmasq.conf.j2 |
diffstat | 3 files changed, 33 insertions(+), 2 deletions(-) [+] |
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',
--- a/templates/dnsmasq/dhcp_hosts.j2 Sat Jan 15 17:10:10 2022 -0800 +++ b/templates/dnsmasq/dhcp_hosts.j2 Sun Jan 16 12:12:21 2022 -0800 @@ -1,3 +1,5 @@ +this is old; see /my/serv/lanscape/netdevices.n3 + b8:97:5a:17:d7:1f,dash,10.1.0.5,infinite c8:60:00:98:ec:74,slash,10.1.0.6,infinite 7c:c3:a1:b0:83:03,kelsi-imac,10.1.0.7,infinite
--- a/templates/dnsmasq/dnsmasq.conf.j2 Sat Jan 15 17:10:10 2022 -0800 +++ b/templates/dnsmasq/dnsmasq.conf.j2 Sun Jan 16 12:12:21 2022 -0800 @@ -46,7 +46,7 @@ dhcp-hostsfile=/opt/dnsmasq/{{ net }}/dhcp_hosts dhcp-leasefile=/opt/dnsmasq/{{ net }}/leases dhcp-range=ens5,10.2.0.0,static,infinite -dhcp-range=tag:!known,ens5,10.2.0.21,10.2.0.120,24h +dhcp-range=tag:!known,ens5,10.2.0.125,10.2.0.200,24h dhcp-option=ens5,option:dns-server,10.2.0.1 dhcp-option=ens5,option:router,10.2.0.1 {% endif %}