Mercurial > code > home > repos > infra
annotate dns.py @ 289:65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
author | drewp@bigasterisk.com |
---|---|
date | Sun, 21 Apr 2024 17:07:23 -0700 |
parents | 3af02e24eaf9 |
children | 828d3f4da54b |
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') | |
56 for pi_serial, _ in pi_serial_hostname: | |
57 files.directory(path=f'/opt/dnsmasq/tftp/{pi_serial}') | |
58 # then we transfer from pi to here | |
59 | |
282 | 60 |
278 | 61 def rpi_iscsi_volumes(): |
62 iscsi_dir = '/d2/rpi-iscsi' | |
63 for _, pi_hostname in pi_serial_hostname: | |
282 | 64 out = f'{iscsi_dir}/{pi_hostname}.disk' |
278 | 65 files.directory(path=iscsi_dir) |
280 | 66 server.shell(f'dd if=/dev/zero of={out} count=0 bs=1 seek=5G conv=excl || true') |
282 | 67 files.put(dest=f"/etc/tgt/conf.d/{pi_hostname}.conf", |
68 src=StringIO(f""" | |
278 | 69 <target iqn.2024-03.com.bigasterisk:{pi_hostname}.target> |
70 backing-store {out} | |
71 initiator-name iqn.2024-03.com.bigasterisk:{pi_hostname}.initiator | |
72 </target> | |
73 """)) | |
282 | 74 # restarting is disruptive to connected pis, and they might need to be |
75 # visited: | |
76 #systemd.service(service='tgt.service', running=True, restarted=True) | |
77 | |
213 | 78 |
179 | 79 standard_host_dns() |
94 | 80 |
241 | 81 # no default instance; i'll add some specific ones below |
82 systemd.service(service='dnsmasq', enabled=False, running=False) | |
83 | |
34
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
84 if host.name == 'bang': |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
85 files.directory(path='/opt/dnsmasq') |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
86 |
213 | 87 dnsmasq_instance('10.5', house_iface='unused', dhcp_range='unused', |
88 listen_address='unused') # only works after wireguard is up | |
278 | 89 elif host.name == 'ditto': |
282 | 90 rpi_iscsi_volumes() # move out of this file- it's not dns |
288 | 91 # 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
|
92 if host.name == 'pipe': |
278 | 93 rpi_net_boot() |
94 | 94 files.directory(path='/opt/dnsmasq') |
116 | 95 dnsmasq_instance('10.2', |
96 house_iface='eth1', | |
241 | 97 dhcp_range='10.2.0.110,10.2.0.199', |
213 | 98 listen_address='10.2.0.3', |
116 | 99 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
|
100 out = '/opt/dnsmasq/10.2' |
51a471fa4d29
metrics on dnsmasq log errors and DHCP commands
drewp@bigasterisk.com
parents:
116
diff
changeset
|
101 # 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
|
102 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
|
103 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
|
104 |
289
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
105 files.put(src='files/dnsmasq/dnsmasq-mtail.service', dest='/etc/systemd/system/dnsmasq-mtail.service') |
241 | 106 systemd.service(service='dnsmasq-mtail', enabled=True, restarted=True, daemon_reload=True) |
94 | 107 |
213 | 108 # Serve another dns, no dhcp, and include the dynamic-blocking file written by net_routes. |
109 dnsmasq_instance( | |
110 net_name='10.2-filtered', | |
111 house_iface='eth1', | |
112 listen_address='10.2.0.4', | |
113 ) |