Mercurial > code > home > repos > infra
comparison dns.py @ 241:075ceead3673
dns work
author | drewp@bigasterisk.com |
---|---|
date | Mon, 11 Dec 2023 21:22:40 -0800 |
parents | 33db4d39e554 |
children | 4e424a144183 |
comparison
equal
deleted
inserted
replaced
240:b58f05be720a | 241:075ceead3673 |
---|---|
1 from io import StringIO | |
2 import subprocess | |
3 from tempfile import NamedTemporaryFile | |
1 from pyinfra import host | 4 from pyinfra import host |
2 from pyinfra.operations import apt, files, systemd | 5 from pyinfra.operations import files, systemd |
3 | 6 |
4 | 7 |
5 def dnsmasq_instance(net_name, | 8 def dnsmasq_instance(net_name, |
6 house_iface, | 9 house_iface, |
7 dhcp_range='10.2.0.10,10.2.0.11', | 10 dhcp_range='10.2.0.10,10.2.0.11', |
18 dhcp_enabled=net_name == '10.2' and host.name == 'pipe', | 21 dhcp_enabled=net_name == '10.2' and host.name == 'pipe', |
19 dns_server=listen_address, | 22 dns_server=listen_address, |
20 router=listen_address, | 23 router=listen_address, |
21 ) | 24 ) |
22 files.template(src='templates/dnsmasq/hosts.j2', dest=f'/opt/dnsmasq/{net_name}/hosts', net=net_name) | 25 files.template(src='templates/dnsmasq/hosts.j2', dest=f'/opt/dnsmasq/{net_name}/hosts', net=net_name) |
23 files.template(src=dhcp_hosts_filename, dest=f'/opt/dnsmasq/{net_name}/dhcp_hosts', net=net_name) | 26 |
27 dhcp_hosts = subprocess.check_output(['python3', '/my/serv/lanscape/src/public/make_dhcp_hosts.py'], encoding='utf8') | |
28 files.put(src=StringIO(dhcp_hosts), dest=f'/opt/dnsmasq/{net_name}/dhcp_hosts') | |
24 | 29 |
25 files.template(src='templates/dnsmasq/dnsmasq.service.j2', | 30 files.template(src='templates/dnsmasq/dnsmasq.service.j2', |
26 dest=f'/etc/systemd/system/dnsmasq_{net_name}.service', | 31 dest=f'/etc/systemd/system/dnsmasq_{net_name}.service', |
27 net=net_name) | 32 net=net_name) |
28 if net_name in ['10.2', '10.2-filtered']: | 33 if net_name in ['10.2', '10.2-filtered']: |
36 systemd.service(service='systemd-resolved.service', running=True, restarted=True) | 41 systemd.service(service='systemd-resolved.service', running=True, restarted=True) |
37 | 42 |
38 | 43 |
39 standard_host_dns() | 44 standard_host_dns() |
40 | 45 |
46 # no default instance; i'll add some specific ones below | |
47 systemd.service(service='dnsmasq', enabled=False, running=False) | |
48 | |
41 if host.name == 'bang': | 49 if host.name == 'bang': |
42 systemd.service(service='dnsmasq', enabled=False, running=False) | |
43 files.directory(path='/opt/dnsmasq') | 50 files.directory(path='/opt/dnsmasq') |
44 | 51 |
45 dnsmasq_instance('10.5', house_iface='unused', dhcp_range='unused', | 52 dnsmasq_instance('10.5', house_iface='unused', dhcp_range='unused', |
46 listen_address='unused') # only works after wireguard is up | 53 listen_address='unused') # only works after wireguard is up |
47 | 54 |
48 elif host.name == 'ditto': | |
49 systemd.service(service='dnsmasq', enabled=False, running=False) | |
50 | |
51 elif host.name == 'pipe': | 55 elif host.name == 'pipe': |
52 systemd.service(service='dnsmasq', enabled=False, running=False) | |
53 files.directory(path='/opt/dnsmasq') | 56 files.directory(path='/opt/dnsmasq') |
54 dnsmasq_instance('10.2', | 57 dnsmasq_instance('10.2', |
55 house_iface='eth1', | 58 house_iface='eth1', |
56 dhcp_range='10.2.0.101,10.2.0.240', | 59 dhcp_range='10.2.0.110,10.2.0.199', |
57 listen_address='10.2.0.3', | 60 listen_address='10.2.0.3', |
58 dhcp_hosts_filename='templates/dnsmasq/dhcp_hosts.j2') | 61 dhcp_hosts_filename='templates/dnsmasq/dhcp_hosts.j2') |
59 out = '/opt/dnsmasq/10.2' | 62 out = '/opt/dnsmasq/10.2' |
60 # This mtail is for dhcp command counts and errors. Another monitor in lanscape/ reads the leases file. | 63 # This mtail is for dhcp command counts and errors. Another monitor in lanscape/ reads the leases file. |
61 files.template(src='templates/dnsmasq/metrics.mtail.j2', dest=f'{out}/metrics.mtail') | 64 files.template(src='templates/dnsmasq/metrics.mtail.j2', dest=f'{out}/metrics.mtail') |
62 files.template(src='templates/dnsmasq/run_mtail.sh', dest=f'{out}/run_mtail.sh') | 65 files.template(src='templates/dnsmasq/run_mtail.sh', dest=f'{out}/run_mtail.sh') |
63 | 66 |
64 files.template(src='templates/dnsmasq/dnsmasq-mtail.service.j2', dest=f'/etc/systemd/system/dnsmasq-mtail.service') | 67 files.template(src='templates/dnsmasq/dnsmasq-mtail.service.j2', dest='/etc/systemd/system/dnsmasq-mtail.service') |
65 systemd.service(service=f'dnsmasq-mtail', enabled=True, restarted=True, daemon_reload=True) | 68 systemd.service(service='dnsmasq-mtail', enabled=True, restarted=True, daemon_reload=True) |
66 | 69 |
67 # Serve another dns, no dhcp, and include the dynamic-blocking file written by net_routes. | 70 # Serve another dns, no dhcp, and include the dynamic-blocking file written by net_routes. |
68 dnsmasq_instance( | 71 dnsmasq_instance( |
69 net_name='10.2-filtered', | 72 net_name='10.2-filtered', |
70 house_iface='eth1', | 73 house_iface='eth1', |