Mercurial > code > home > repos > infra
annotate dns.py @ 241:075ceead3673
dns work
author | drewp@bigasterisk.com |
---|---|
date | Mon, 11 Dec 2023 21:22:40 -0800 |
parents | 33db4d39e554 |
children | 4e424a144183 |
rev | line source |
---|---|
241 | 1 from io import StringIO |
2 import subprocess | |
3 from tempfile import NamedTemporaryFile | |
34
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
4 from pyinfra import host |
241 | 5 from pyinfra.operations import files, systemd |
38 | 6 |
94 | 7 |
213 | 8 def dnsmasq_instance(net_name, |
9 house_iface, | |
10 dhcp_range='10.2.0.10,10.2.0.11', | |
11 listen_address='reqd', | |
12 dhcp_hosts_filename='/dev/null'): | |
88
dae714e8f620
reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents:
81
diff
changeset
|
13 files.directory(path=f'/opt/dnsmasq/{net_name}') |
213 | 14 files.template( |
15 src='templates/dnsmasq/dnsmasq.conf.j2', | |
16 dest=f'/opt/dnsmasq/{net_name}/dnsmasq.conf', | |
17 net=net_name, | |
18 house_iface=house_iface, | |
19 dhcp_range=dhcp_range, | |
20 listen_address=listen_address, | |
21 dhcp_enabled=net_name == '10.2' and host.name == 'pipe', | |
22 dns_server=listen_address, | |
23 router=listen_address, | |
24 ) | |
88
dae714e8f620
reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents:
81
diff
changeset
|
25 files.template(src='templates/dnsmasq/hosts.j2', dest=f'/opt/dnsmasq/{net_name}/hosts', net=net_name) |
241 | 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') | |
88
dae714e8f620
reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents:
81
diff
changeset
|
29 |
dae714e8f620
reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents:
81
diff
changeset
|
30 files.template(src='templates/dnsmasq/dnsmasq.service.j2', |
dae714e8f620
reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents:
81
diff
changeset
|
31 dest=f'/etc/systemd/system/dnsmasq_{net_name}.service', |
dae714e8f620
reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents:
81
diff
changeset
|
32 net=net_name) |
213 | 33 if net_name in ['10.2', '10.2-filtered']: |
88
dae714e8f620
reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents:
81
diff
changeset
|
34 systemd.service(service=f'dnsmasq_{net_name}', enabled=True, restarted=True, daemon_reload=True) |
94 | 35 |
213 | 36 |
179 | 37 def standard_host_dns(): |
38 files.template(src='templates/hosts.j2', dest='/etc/hosts') | |
39 files.link(path='/etc/resolv.conf', target='/run/systemd/resolve/resolv.conf', force=True) | |
40 files.template(src='templates/resolved.conf.j2', dest='/etc/systemd/resolved.conf') | |
41 systemd.service(service='systemd-resolved.service', running=True, restarted=True) | |
94 | 42 |
213 | 43 |
179 | 44 standard_host_dns() |
94 | 45 |
241 | 46 # no default instance; i'll add some specific ones below |
47 systemd.service(service='dnsmasq', enabled=False, running=False) | |
48 | |
34
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
49 if host.name == 'bang': |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
50 files.directory(path='/opt/dnsmasq') |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
51 |
213 | 52 dnsmasq_instance('10.5', house_iface='unused', dhcp_range='unused', |
53 listen_address='unused') # only works after wireguard is up | |
34
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
54 |
94 | 55 elif host.name == 'pipe': |
56 files.directory(path='/opt/dnsmasq') | |
116 | 57 dnsmasq_instance('10.2', |
58 house_iface='eth1', | |
241 | 59 dhcp_range='10.2.0.110,10.2.0.199', |
213 | 60 listen_address='10.2.0.3', |
116 | 61 dhcp_hosts_filename='templates/dnsmasq/dhcp_hosts.j2') |
119
51a471fa4d29
metrics on dnsmasq log errors and DHCP commands
drewp@bigasterisk.com
parents:
116
diff
changeset
|
62 out = '/opt/dnsmasq/10.2' |
51a471fa4d29
metrics on dnsmasq log errors and DHCP commands
drewp@bigasterisk.com
parents:
116
diff
changeset
|
63 # This mtail is for dhcp command counts and errors. Another monitor in lanscape/ reads the leases file. |
51a471fa4d29
metrics on dnsmasq log errors and DHCP commands
drewp@bigasterisk.com
parents:
116
diff
changeset
|
64 files.template(src='templates/dnsmasq/metrics.mtail.j2', dest=f'{out}/metrics.mtail') |
51a471fa4d29
metrics on dnsmasq log errors and DHCP commands
drewp@bigasterisk.com
parents:
116
diff
changeset
|
65 files.template(src='templates/dnsmasq/run_mtail.sh', dest=f'{out}/run_mtail.sh') |
51a471fa4d29
metrics on dnsmasq log errors and DHCP commands
drewp@bigasterisk.com
parents:
116
diff
changeset
|
66 |
241 | 67 files.template(src='templates/dnsmasq/dnsmasq-mtail.service.j2', dest='/etc/systemd/system/dnsmasq-mtail.service') |
68 systemd.service(service='dnsmasq-mtail', enabled=True, restarted=True, daemon_reload=True) | |
94 | 69 |
213 | 70 # Serve another dns, no dhcp, and include the dynamic-blocking file written by net_routes. |
71 dnsmasq_instance( | |
72 net_name='10.2-filtered', | |
73 house_iface='eth1', | |
74 listen_address='10.2.0.4', | |
75 ) |