Mercurial > code > home > repos > infra
comparison 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 |
comparison
equal
deleted
inserted
replaced
37:fbd0849dfdbd | 38:4026b6b8028f |
---|---|
1 import subprocess | |
2 import tempfile | |
3 | |
4 import requests | |
1 from pyinfra import host | 5 from pyinfra import host |
2 from pyinfra.operations import apt, files, server, systemd | 6 from pyinfra.operations import apt, files, server, systemd |
7 | |
8 | |
9 def prepare_dhcp_hosts(): | |
10 lanscape_ip = subprocess.check_output([ | |
11 'kubectl', | |
12 'get', | |
13 'svc', | |
14 '--field-selector=metadata.name=lanscape', | |
15 "-o=jsonpath={.items[0].spec.clusterIP}", | |
16 ], | |
17 encoding='ascii') | |
18 url = f'http://{lanscape_ip}/dnsConfig' | |
19 resp = requests.get(url) | |
20 resp.raise_for_status() | |
21 lanscape_config = resp.json() | |
22 | |
23 dhcp_hosts = tempfile.NamedTemporaryFile(mode='wt', encoding='ascii') | |
24 dhcp_hosts.write("# written by pyinfra\n\n") | |
25 for row in lanscape_config['dhcp_table']: | |
26 dhcp_hosts.write(f'{row["mac"]},{row["hostname"]},{row["ip"]},infinite\n') | |
27 dhcp_hosts.flush() | |
28 return dhcp_hosts | |
29 | |
3 | 30 |
4 if host.name == 'bang': | 31 if host.name == 'bang': |
5 apt.packages(packages=['dnsmasq']) | 32 apt.packages(packages=['dnsmasq']) |
6 systemd.service(service='dnsmasq', enabled=False, running=False) | 33 systemd.service(service='dnsmasq', enabled=False, running=False) |
7 files.directory(path='/opt/dnsmasq') | 34 files.directory(path='/opt/dnsmasq') |
8 | 35 |
36 dhcp_hosts = prepare_dhcp_hosts() | |
37 | |
9 for net_name in ['10.1', '10.2', '10.5']: | 38 for net_name in ['10.1', '10.2', '10.5']: |
10 files.directory(path=f'/opt/dnsmasq/{net_name}') | 39 files.directory(path=f'/opt/dnsmasq/{net_name}') |
11 files.template(src='templates/dnsmasq/dnsmasq.conf.j2', dest=f'/opt/dnsmasq/{net_name}/dnsmasq.conf', net=net_name) | 40 files.template(src='templates/dnsmasq/dnsmasq.conf.j2', dest=f'/opt/dnsmasq/{net_name}/dnsmasq.conf', net=net_name) |
12 files.template(src='templates/dnsmasq/hosts.j2', dest=f'/opt/dnsmasq/{net_name}/hosts', net=net_name) | 41 files.template(src='templates/dnsmasq/hosts.j2', dest=f'/opt/dnsmasq/{net_name}/hosts', net=net_name) |
13 files.template(src='templates/dnsmasq/dhcp_hosts.j2', dest=f'/opt/dnsmasq/{net_name}/dhcp_hosts', net=net_name) | 42 files.template(src=dhcp_hosts.name, dest=f'/opt/dnsmasq/{net_name}/dhcp_hosts', net=net_name) |
14 | 43 |
15 files.template(src='templates/dnsmasq/dnsmasq.service.j2', | 44 files.template(src='templates/dnsmasq/dnsmasq.service.j2', |
16 dest=f'/etc/systemd/system/dnsmasq_{net_name}.service', | 45 dest=f'/etc/systemd/system/dnsmasq_{net_name}.service', |
17 net=net_name) | 46 net=net_name) |
18 systemd.service(service=f'dnsmasq_{net_name}', restarted=True, daemon_reload=True) | 47 systemd.service(service=f'dnsmasq_{net_name}', restarted=True, daemon_reload=True) |