Mercurial > code > home > repos > infra
annotate dns.py @ 290:828d3f4da54b
rpi iscsi volumes
author | drewp@bigasterisk.com |
---|---|
date | Sun, 21 Apr 2024 17:09:10 -0700 |
parents | 65e28d2e0cd8 |
children | 2136320eb94d |
rev | line source |
---|---|
241 | 1 from io import StringIO |
2 import subprocess | |
288 | 3 from typing import cast |
278 | 4 |
288 | 5 import pyinfra |
34
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
6 from pyinfra import host |
278 | 7 from pyinfra.operations import files, systemd, server |
94 | 8 |
282 | 9 |
213 | 10 def dnsmasq_instance(net_name, |
11 house_iface, | |
12 dhcp_range='10.2.0.10,10.2.0.11', | |
13 listen_address='reqd', | |
14 dhcp_hosts_filename='/dev/null'): | |
88
dae714e8f620
reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents:
81
diff
changeset
|
15 files.directory(path=f'/opt/dnsmasq/{net_name}') |
213 | 16 files.template( |
17 src='templates/dnsmasq/dnsmasq.conf.j2', | |
18 dest=f'/opt/dnsmasq/{net_name}/dnsmasq.conf', | |
19 net=net_name, | |
20 house_iface=house_iface, | |
21 dhcp_range=dhcp_range, | |
22 listen_address=listen_address, | |
23 dhcp_enabled=net_name == '10.2' and host.name == 'pipe', | |
24 dns_server=listen_address, | |
25 router=listen_address, | |
26 ) | |
88
dae714e8f620
reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents:
81
diff
changeset
|
27 files.template(src='templates/dnsmasq/hosts.j2', dest=f'/opt/dnsmasq/{net_name}/hosts', net=net_name) |
282 | 28 |
241 | 29 dhcp_hosts = subprocess.check_output(['python3', '/my/serv/lanscape/src/public/make_dhcp_hosts.py'], encoding='utf8') |
30 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
|
31 |
dae714e8f620
reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents:
81
diff
changeset
|
32 files.template(src='templates/dnsmasq/dnsmasq.service.j2', |
dae714e8f620
reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents:
81
diff
changeset
|
33 dest=f'/etc/systemd/system/dnsmasq_{net_name}.service', |
dae714e8f620
reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents:
81
diff
changeset
|
34 net=net_name) |
213 | 35 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
|
36 systemd.service(service=f'dnsmasq_{net_name}', enabled=True, restarted=True, daemon_reload=True) |
94 | 37 |
213 | 38 |
179 | 39 def standard_host_dns(): |
40 files.template(src='templates/hosts.j2', dest='/etc/hosts') | |
289
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
41 if 'pi' in host.groups: |
282 | 42 files.put(dest='/etc/resolv.conf', |
43 src=StringIO(''' | |
278 | 44 # written by pyinfra |
45 nameserver 10.2.0.3 | |
46 search bigasterisk.com | |
47 ''')) | |
48 else: | |
49 files.link(path='/etc/resolv.conf', target='/run/systemd/resolve/resolv.conf', force=True) | |
50 files.template(src='templates/resolved.conf.j2', dest='/etc/systemd/resolved.conf') | |
51 systemd.service(service='systemd-resolved.service', running=True, restarted=True) | |
52 | |
94 | 53 |
278 | 54 def rpi_net_boot(): |
55 files.directory(path='/opt/dnsmasq/tftp') | |
282 | 56 |
213 | 57 |
179 | 58 standard_host_dns() |
94 | 59 |
241 | 60 # no default instance; i'll add some specific ones below |
61 systemd.service(service='dnsmasq', enabled=False, running=False) | |
62 | |
34
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
63 if host.name == 'bang': |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
64 files.directory(path='/opt/dnsmasq') |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
65 |
213 | 66 dnsmasq_instance('10.5', house_iface='unused', dhcp_range='unused', |
67 listen_address='unused') # only works after wireguard is up | |
288 | 68 # move out of this file- it's not dns |
289
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
69 if host.name == 'pipe': |
278 | 70 rpi_net_boot() |
94 | 71 files.directory(path='/opt/dnsmasq') |
116 | 72 dnsmasq_instance('10.2', |
73 house_iface='eth1', | |
241 | 74 dhcp_range='10.2.0.110,10.2.0.199', |
213 | 75 listen_address='10.2.0.3', |
116 | 76 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
|
77 out = '/opt/dnsmasq/10.2' |
51a471fa4d29
metrics on dnsmasq log errors and DHCP commands
drewp@bigasterisk.com
parents:
116
diff
changeset
|
78 # This mtail is for dhcp command counts and errors. Another monitor in lanscape/ reads the leases file. |
289
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
79 files.put(src='files/dnsmasq/metrics.mtail', dest=f'{out}/metrics.mtail') |
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
80 files.put(src='files/dnsmasq/run_mtail.sh', dest=f'{out}/run_mtail.sh') |
119
51a471fa4d29
metrics on dnsmasq log errors and DHCP commands
drewp@bigasterisk.com
parents:
116
diff
changeset
|
81 |
289
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
82 files.put(src='files/dnsmasq/dnsmasq-mtail.service', dest='/etc/systemd/system/dnsmasq-mtail.service') |
241 | 83 systemd.service(service='dnsmasq-mtail', enabled=True, restarted=True, daemon_reload=True) |
94 | 84 |
213 | 85 # Serve another dns, no dhcp, and include the dynamic-blocking file written by net_routes. |
86 dnsmasq_instance( | |
87 net_name='10.2-filtered', | |
88 house_iface='eth1', | |
89 listen_address='10.2.0.4', | |
90 ) |