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