Mercurial > code > home > repos > infra
annotate dns.py @ 280:5c5c314051c5
updates for pis
author | drewp@bigasterisk.com |
---|---|
date | Sun, 14 Apr 2024 20:58:39 -0700 |
parents | 4e424a144183 |
children | e10ee3ddadcf |
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 |
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) |
241 | 28 |
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') | |
278 | 41 if is_pi: |
42 files.put(dest='/etc/resolv.conf', src=StringIO(''' | |
43 # written by pyinfra | |
44 nameserver 10.2.0.3 | |
45 search bigasterisk.com | |
46 ''')) | |
47 else: | |
48 files.link(path='/etc/resolv.conf', target='/run/systemd/resolve/resolv.conf', force=True) | |
49 files.template(src='templates/resolved.conf.j2', dest='/etc/systemd/resolved.conf') | |
50 systemd.service(service='systemd-resolved.service', running=True, restarted=True) | |
51 | |
94 | 52 |
278 | 53 def rpi_net_boot(): |
54 files.directory(path='/opt/dnsmasq/tftp') | |
55 for pi_serial, _ in pi_serial_hostname: | |
56 files.directory(path=f'/opt/dnsmasq/tftp/{pi_serial}') | |
57 # then we transfer from pi to here | |
58 | |
59 def rpi_iscsi_volumes(): | |
60 iscsi_dir = '/d2/rpi-iscsi' | |
61 for _, pi_hostname in pi_serial_hostname: | |
62 out= f'{iscsi_dir}/{pi_hostname}.disk' | |
63 files.directory(path=iscsi_dir) | |
280 | 64 server.shell(f'dd if=/dev/zero of={out} count=0 bs=1 seek=5G conv=excl || true') |
278 | 65 files.put(dest=f"/etc/tgt/conf.d/{pi_hostname}.conf", src=StringIO(f""" |
66 <target iqn.2024-03.com.bigasterisk:{pi_hostname}.target> | |
67 backing-store {out} | |
68 initiator-name iqn.2024-03.com.bigasterisk:{pi_hostname}.initiator | |
69 </target> | |
70 """)) | |
71 systemd.service(service='tgt.service', running=True, restarted=True) | |
72 | |
213 | 73 |
179 | 74 standard_host_dns() |
94 | 75 |
241 | 76 # no default instance; i'll add some specific ones below |
77 systemd.service(service='dnsmasq', enabled=False, running=False) | |
78 | |
34
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
79 if host.name == 'bang': |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
80 files.directory(path='/opt/dnsmasq') |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
81 |
213 | 82 dnsmasq_instance('10.5', house_iface='unused', dhcp_range='unused', |
83 listen_address='unused') # only works after wireguard is up | |
278 | 84 elif host.name == 'ditto': |
85 rpi_iscsi_volumes() # move out of this file- it's not dns | |
94 | 86 elif host.name == 'pipe': |
278 | 87 rpi_net_boot() |
94 | 88 files.directory(path='/opt/dnsmasq') |
116 | 89 dnsmasq_instance('10.2', |
90 house_iface='eth1', | |
241 | 91 dhcp_range='10.2.0.110,10.2.0.199', |
213 | 92 listen_address='10.2.0.3', |
116 | 93 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
|
94 out = '/opt/dnsmasq/10.2' |
51a471fa4d29
metrics on dnsmasq log errors and DHCP commands
drewp@bigasterisk.com
parents:
116
diff
changeset
|
95 # 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
|
96 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
|
97 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
|
98 |
241 | 99 files.template(src='templates/dnsmasq/dnsmasq-mtail.service.j2', dest='/etc/systemd/system/dnsmasq-mtail.service') |
100 systemd.service(service='dnsmasq-mtail', enabled=True, restarted=True, daemon_reload=True) | |
94 | 101 |
213 | 102 # Serve another dns, no dhcp, and include the dynamic-blocking file written by net_routes. |
103 dnsmasq_instance( | |
104 net_name='10.2-filtered', | |
105 house_iface='eth1', | |
106 listen_address='10.2.0.4', | |
107 ) |