Mercurial > code > home > repos > infra
comparison dns.py @ 278:4e424a144183
for netboot pi
author | drewp@bigasterisk.com |
---|---|
date | Sat, 30 Mar 2024 00:15:46 -0700 |
parents | 075ceead3673 |
children | 5c5c314051c5 |
comparison
equal
deleted
inserted
replaced
277:ce823a167641 | 278:4e424a144183 |
---|---|
1 from io import StringIO | 1 from io import StringIO |
2 import subprocess | 2 import subprocess |
3 from tempfile import NamedTemporaryFile | 3 |
4 from pyinfra import host | 4 from pyinfra import host |
5 from pyinfra.operations import files, systemd | 5 from pyinfra.operations import files, systemd, server |
6 from pyinfra.facts.server import Arch, LinuxDistribution | |
6 | 7 |
8 is_pi = host.get_fact(LinuxDistribution)['name'] in ['Debian', 'Raspbian GNU/Linux'] | |
7 | 9 |
8 def dnsmasq_instance(net_name, | 10 def dnsmasq_instance(net_name, |
9 house_iface, | 11 house_iface, |
10 dhcp_range='10.2.0.10,10.2.0.11', | 12 dhcp_range='10.2.0.10,10.2.0.11', |
11 listen_address='reqd', | 13 listen_address='reqd', |
34 systemd.service(service=f'dnsmasq_{net_name}', enabled=True, restarted=True, daemon_reload=True) | 36 systemd.service(service=f'dnsmasq_{net_name}', enabled=True, restarted=True, daemon_reload=True) |
35 | 37 |
36 | 38 |
37 def standard_host_dns(): | 39 def standard_host_dns(): |
38 files.template(src='templates/hosts.j2', dest='/etc/hosts') | 40 files.template(src='templates/hosts.j2', dest='/etc/hosts') |
39 files.link(path='/etc/resolv.conf', target='/run/systemd/resolve/resolv.conf', force=True) | 41 if is_pi: |
40 files.template(src='templates/resolved.conf.j2', dest='/etc/systemd/resolved.conf') | 42 files.put(dest='/etc/resolv.conf', src=StringIO(''' |
41 systemd.service(service='systemd-resolved.service', running=True, restarted=True) | 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) | |
42 | 51 |
52 | |
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) | |
64 server.shell(f'dd if=/dev/zero of={out} count=0 bs=1 seek=4G conv=excl || true') | |
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 | |
43 | 73 |
44 standard_host_dns() | 74 standard_host_dns() |
45 | 75 |
46 # no default instance; i'll add some specific ones below | 76 # no default instance; i'll add some specific ones below |
47 systemd.service(service='dnsmasq', enabled=False, running=False) | 77 systemd.service(service='dnsmasq', enabled=False, running=False) |
49 if host.name == 'bang': | 79 if host.name == 'bang': |
50 files.directory(path='/opt/dnsmasq') | 80 files.directory(path='/opt/dnsmasq') |
51 | 81 |
52 dnsmasq_instance('10.5', house_iface='unused', dhcp_range='unused', | 82 dnsmasq_instance('10.5', house_iface='unused', dhcp_range='unused', |
53 listen_address='unused') # only works after wireguard is up | 83 listen_address='unused') # only works after wireguard is up |
54 | 84 elif host.name == 'ditto': |
85 rpi_iscsi_volumes() # move out of this file- it's not dns | |
55 elif host.name == 'pipe': | 86 elif host.name == 'pipe': |
87 rpi_net_boot() | |
56 files.directory(path='/opt/dnsmasq') | 88 files.directory(path='/opt/dnsmasq') |
57 dnsmasq_instance('10.2', | 89 dnsmasq_instance('10.2', |
58 house_iface='eth1', | 90 house_iface='eth1', |
59 dhcp_range='10.2.0.110,10.2.0.199', | 91 dhcp_range='10.2.0.110,10.2.0.199', |
60 listen_address='10.2.0.3', | 92 listen_address='10.2.0.3', |