Mercurial > code > home > repos > infra
annotate dns.py @ 288:3af02e24eaf9
minor
author | drewp@bigasterisk.com |
---|---|
date | Sun, 21 Apr 2024 17:01:13 -0700 |
parents | e10ee3ddadcf |
children | 65e28d2e0cd8 |
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 |
8 from pyinfra.facts.server import Arch, LinuxDistribution | |
38 | 9 |
278 | 10 is_pi = host.get_fact(LinuxDistribution)['name'] in ['Debian', 'Raspbian GNU/Linux'] |
94 | 11 |
282 | 12 |
213 | 13 def dnsmasq_instance(net_name, |
14 house_iface, | |
15 dhcp_range='10.2.0.10,10.2.0.11', | |
16 listen_address='reqd', | |
17 dhcp_hosts_filename='/dev/null'): | |
88
dae714e8f620
reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents:
81
diff
changeset
|
18 files.directory(path=f'/opt/dnsmasq/{net_name}') |
213 | 19 files.template( |
20 src='templates/dnsmasq/dnsmasq.conf.j2', | |
21 dest=f'/opt/dnsmasq/{net_name}/dnsmasq.conf', | |
22 net=net_name, | |
23 house_iface=house_iface, | |
24 dhcp_range=dhcp_range, | |
25 listen_address=listen_address, | |
26 dhcp_enabled=net_name == '10.2' and host.name == 'pipe', | |
27 dns_server=listen_address, | |
28 router=listen_address, | |
29 ) | |
88
dae714e8f620
reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents:
81
diff
changeset
|
30 files.template(src='templates/dnsmasq/hosts.j2', dest=f'/opt/dnsmasq/{net_name}/hosts', net=net_name) |
282 | 31 |
241 | 32 dhcp_hosts = subprocess.check_output(['python3', '/my/serv/lanscape/src/public/make_dhcp_hosts.py'], encoding='utf8') |
33 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
|
34 |
dae714e8f620
reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents:
81
diff
changeset
|
35 files.template(src='templates/dnsmasq/dnsmasq.service.j2', |
dae714e8f620
reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents:
81
diff
changeset
|
36 dest=f'/etc/systemd/system/dnsmasq_{net_name}.service', |
dae714e8f620
reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents:
81
diff
changeset
|
37 net=net_name) |
213 | 38 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
|
39 systemd.service(service=f'dnsmasq_{net_name}', enabled=True, restarted=True, daemon_reload=True) |
94 | 40 |
213 | 41 |
179 | 42 def standard_host_dns(): |
43 files.template(src='templates/hosts.j2', dest='/etc/hosts') | |
278 | 44 if is_pi: |
282 | 45 files.put(dest='/etc/resolv.conf', |
46 src=StringIO(''' | |
278 | 47 # written by pyinfra |
48 nameserver 10.2.0.3 | |
49 search bigasterisk.com | |
50 ''')) | |
51 else: | |
52 files.link(path='/etc/resolv.conf', target='/run/systemd/resolve/resolv.conf', force=True) | |
53 files.template(src='templates/resolved.conf.j2', dest='/etc/systemd/resolved.conf') | |
54 systemd.service(service='systemd-resolved.service', running=True, restarted=True) | |
55 | |
94 | 56 |
278 | 57 def rpi_net_boot(): |
58 files.directory(path='/opt/dnsmasq/tftp') | |
59 for pi_serial, _ in pi_serial_hostname: | |
60 files.directory(path=f'/opt/dnsmasq/tftp/{pi_serial}') | |
61 # then we transfer from pi to here | |
62 | |
282 | 63 |
278 | 64 def rpi_iscsi_volumes(): |
65 iscsi_dir = '/d2/rpi-iscsi' | |
66 for _, pi_hostname in pi_serial_hostname: | |
282 | 67 out = f'{iscsi_dir}/{pi_hostname}.disk' |
278 | 68 files.directory(path=iscsi_dir) |
280 | 69 server.shell(f'dd if=/dev/zero of={out} count=0 bs=1 seek=5G conv=excl || true') |
282 | 70 files.put(dest=f"/etc/tgt/conf.d/{pi_hostname}.conf", |
71 src=StringIO(f""" | |
278 | 72 <target iqn.2024-03.com.bigasterisk:{pi_hostname}.target> |
73 backing-store {out} | |
74 initiator-name iqn.2024-03.com.bigasterisk:{pi_hostname}.initiator | |
75 </target> | |
76 """)) | |
282 | 77 # restarting is disruptive to connected pis, and they might need to be |
78 # visited: | |
79 #systemd.service(service='tgt.service', running=True, restarted=True) | |
80 | |
213 | 81 |
179 | 82 standard_host_dns() |
94 | 83 |
241 | 84 # no default instance; i'll add some specific ones below |
85 systemd.service(service='dnsmasq', enabled=False, running=False) | |
86 | |
34
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
87 if host.name == 'bang': |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
88 files.directory(path='/opt/dnsmasq') |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
89 |
213 | 90 dnsmasq_instance('10.5', house_iface='unused', dhcp_range='unused', |
91 listen_address='unused') # only works after wireguard is up | |
278 | 92 elif host.name == 'ditto': |
282 | 93 rpi_iscsi_volumes() # move out of this file- it's not dns |
94 | 94 elif host.name == 'pipe': |
288 | 95 # move out of this file- it's not dns |
278 | 96 rpi_net_boot() |
94 | 97 files.directory(path='/opt/dnsmasq') |
116 | 98 dnsmasq_instance('10.2', |
99 house_iface='eth1', | |
241 | 100 dhcp_range='10.2.0.110,10.2.0.199', |
213 | 101 listen_address='10.2.0.3', |
116 | 102 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
|
103 out = '/opt/dnsmasq/10.2' |
51a471fa4d29
metrics on dnsmasq log errors and DHCP commands
drewp@bigasterisk.com
parents:
116
diff
changeset
|
104 # 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
|
105 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
|
106 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
|
107 |
241 | 108 files.template(src='templates/dnsmasq/dnsmasq-mtail.service.j2', dest='/etc/systemd/system/dnsmasq-mtail.service') |
109 systemd.service(service='dnsmasq-mtail', enabled=True, restarted=True, daemon_reload=True) | |
94 | 110 |
213 | 111 # Serve another dns, no dhcp, and include the dynamic-blocking file written by net_routes. |
112 dnsmasq_instance( | |
113 net_name='10.2-filtered', | |
114 house_iface='eth1', | |
115 listen_address='10.2.0.4', | |
116 ) |