comparison dns.py @ 282:e10ee3ddadcf

pi changes
author drewp@bigasterisk.com
date Mon, 15 Apr 2024 02:27:07 -0700
parents 5c5c314051c5
children 3af02e24eaf9
comparison
equal deleted inserted replaced
281:957eb07e06e6 282:e10ee3ddadcf
4 from pyinfra import host 4 from pyinfra import host
5 from pyinfra.operations import files, systemd, server 5 from pyinfra.operations import files, systemd, server
6 from pyinfra.facts.server import Arch, LinuxDistribution 6 from pyinfra.facts.server import Arch, LinuxDistribution
7 7
8 is_pi = host.get_fact(LinuxDistribution)['name'] in ['Debian', 'Raspbian GNU/Linux'] 8 is_pi = host.get_fact(LinuxDistribution)['name'] in ['Debian', 'Raspbian GNU/Linux']
9
9 10
10 def dnsmasq_instance(net_name, 11 def dnsmasq_instance(net_name,
11 house_iface, 12 house_iface,
12 dhcp_range='10.2.0.10,10.2.0.11', 13 dhcp_range='10.2.0.10,10.2.0.11',
13 listen_address='reqd', 14 listen_address='reqd',
23 dhcp_enabled=net_name == '10.2' and host.name == 'pipe', 24 dhcp_enabled=net_name == '10.2' and host.name == 'pipe',
24 dns_server=listen_address, 25 dns_server=listen_address,
25 router=listen_address, 26 router=listen_address,
26 ) 27 )
27 files.template(src='templates/dnsmasq/hosts.j2', dest=f'/opt/dnsmasq/{net_name}/hosts', net=net_name) 28 files.template(src='templates/dnsmasq/hosts.j2', dest=f'/opt/dnsmasq/{net_name}/hosts', net=net_name)
28 29
29 dhcp_hosts = subprocess.check_output(['python3', '/my/serv/lanscape/src/public/make_dhcp_hosts.py'], encoding='utf8') 30 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') 31 files.put(src=StringIO(dhcp_hosts), dest=f'/opt/dnsmasq/{net_name}/dhcp_hosts')
31 32
32 files.template(src='templates/dnsmasq/dnsmasq.service.j2', 33 files.template(src='templates/dnsmasq/dnsmasq.service.j2',
33 dest=f'/etc/systemd/system/dnsmasq_{net_name}.service', 34 dest=f'/etc/systemd/system/dnsmasq_{net_name}.service',
37 38
38 39
39 def standard_host_dns(): 40 def standard_host_dns():
40 files.template(src='templates/hosts.j2', dest='/etc/hosts') 41 files.template(src='templates/hosts.j2', dest='/etc/hosts')
41 if is_pi: 42 if is_pi:
42 files.put(dest='/etc/resolv.conf', src=StringIO(''' 43 files.put(dest='/etc/resolv.conf',
44 src=StringIO('''
43 # written by pyinfra 45 # written by pyinfra
44 nameserver 10.2.0.3 46 nameserver 10.2.0.3
45 search bigasterisk.com 47 search bigasterisk.com
46 ''')) 48 '''))
47 else: 49 else:
48 files.link(path='/etc/resolv.conf', target='/run/systemd/resolve/resolv.conf', force=True) 50 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') 51 files.template(src='templates/resolved.conf.j2', dest='/etc/systemd/resolved.conf')
50 systemd.service(service='systemd-resolved.service', running=True, restarted=True) 52 systemd.service(service='systemd-resolved.service', running=True, restarted=True)
51 53
52 54
55 pi_serial_hostname = [
56 ]
57
58
53 def rpi_net_boot(): 59 def rpi_net_boot():
54 files.directory(path='/opt/dnsmasq/tftp') 60 files.directory(path='/opt/dnsmasq/tftp')
55 for pi_serial, _ in pi_serial_hostname: 61 for pi_serial, _ in pi_serial_hostname:
56 files.directory(path=f'/opt/dnsmasq/tftp/{pi_serial}') 62 files.directory(path=f'/opt/dnsmasq/tftp/{pi_serial}')
57 # then we transfer from pi to here 63 # then we transfer from pi to here
58 64
65
59 def rpi_iscsi_volumes(): 66 def rpi_iscsi_volumes():
60 iscsi_dir = '/d2/rpi-iscsi' 67 iscsi_dir = '/d2/rpi-iscsi'
61 for _, pi_hostname in pi_serial_hostname: 68 for _, pi_hostname in pi_serial_hostname:
62 out= f'{iscsi_dir}/{pi_hostname}.disk' 69 out = f'{iscsi_dir}/{pi_hostname}.disk'
63 files.directory(path=iscsi_dir) 70 files.directory(path=iscsi_dir)
64 server.shell(f'dd if=/dev/zero of={out} count=0 bs=1 seek=5G conv=excl || true') 71 server.shell(f'dd if=/dev/zero of={out} count=0 bs=1 seek=5G conv=excl || true')
65 files.put(dest=f"/etc/tgt/conf.d/{pi_hostname}.conf", src=StringIO(f""" 72 files.put(dest=f"/etc/tgt/conf.d/{pi_hostname}.conf",
73 src=StringIO(f"""
66 <target iqn.2024-03.com.bigasterisk:{pi_hostname}.target> 74 <target iqn.2024-03.com.bigasterisk:{pi_hostname}.target>
67 backing-store {out} 75 backing-store {out}
68 initiator-name iqn.2024-03.com.bigasterisk:{pi_hostname}.initiator 76 initiator-name iqn.2024-03.com.bigasterisk:{pi_hostname}.initiator
69 </target> 77 </target>
70 """)) 78 """))
71 systemd.service(service='tgt.service', running=True, restarted=True) 79 # restarting is disruptive to connected pis, and they might need to be
72 80 # visited:
81 #systemd.service(service='tgt.service', running=True, restarted=True)
82
73 83
74 standard_host_dns() 84 standard_host_dns()
75 85
76 # no default instance; i'll add some specific ones below 86 # no default instance; i'll add some specific ones below
77 systemd.service(service='dnsmasq', enabled=False, running=False) 87 systemd.service(service='dnsmasq', enabled=False, running=False)
80 files.directory(path='/opt/dnsmasq') 90 files.directory(path='/opt/dnsmasq')
81 91
82 dnsmasq_instance('10.5', house_iface='unused', dhcp_range='unused', 92 dnsmasq_instance('10.5', house_iface='unused', dhcp_range='unused',
83 listen_address='unused') # only works after wireguard is up 93 listen_address='unused') # only works after wireguard is up
84 elif host.name == 'ditto': 94 elif host.name == 'ditto':
85 rpi_iscsi_volumes() # move out of this file- it's not dns 95 rpi_iscsi_volumes() # move out of this file- it's not dns
86 elif host.name == 'pipe': 96 elif host.name == 'pipe':
87 rpi_net_boot() 97 rpi_net_boot()
88 files.directory(path='/opt/dnsmasq') 98 files.directory(path='/opt/dnsmasq')
89 dnsmasq_instance('10.2', 99 dnsmasq_instance('10.2',
90 house_iface='eth1', 100 house_iface='eth1',