comparison dns.py @ 289:65e28d2e0cd8

move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
author drewp@bigasterisk.com
date Sun, 21 Apr 2024 17:07:23 -0700
parents 3af02e24eaf9
children 828d3f4da54b
comparison
equal deleted inserted replaced
288:3af02e24eaf9 289:65e28d2e0cd8
3 from typing import cast 3 from typing import cast
4 4
5 import pyinfra 5 import pyinfra
6 from pyinfra import host 6 from pyinfra import host
7 from pyinfra.operations import files, systemd, server 7 from pyinfra.operations import files, systemd, server
8 from pyinfra.facts.server import Arch, LinuxDistribution
9
10 is_pi = host.get_fact(LinuxDistribution)['name'] in ['Debian', 'Raspbian GNU/Linux']
11 8
12 9
13 def dnsmasq_instance(net_name, 10 def dnsmasq_instance(net_name,
14 house_iface, 11 house_iface,
15 dhcp_range='10.2.0.10,10.2.0.11', 12 dhcp_range='10.2.0.10,10.2.0.11',
39 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)
40 37
41 38
42 def standard_host_dns(): 39 def standard_host_dns():
43 files.template(src='templates/hosts.j2', dest='/etc/hosts') 40 files.template(src='templates/hosts.j2', dest='/etc/hosts')
44 if is_pi: 41 if 'pi' in host.groups:
45 files.put(dest='/etc/resolv.conf', 42 files.put(dest='/etc/resolv.conf',
46 src=StringIO(''' 43 src=StringIO('''
47 # written by pyinfra 44 # written by pyinfra
48 nameserver 10.2.0.3 45 nameserver 10.2.0.3
49 search bigasterisk.com 46 search bigasterisk.com
89 86
90 dnsmasq_instance('10.5', house_iface='unused', dhcp_range='unused', 87 dnsmasq_instance('10.5', house_iface='unused', dhcp_range='unused',
91 listen_address='unused') # only works after wireguard is up 88 listen_address='unused') # only works after wireguard is up
92 elif host.name == 'ditto': 89 elif host.name == 'ditto':
93 rpi_iscsi_volumes() # move out of this file- it's not dns 90 rpi_iscsi_volumes() # move out of this file- it's not dns
94 elif host.name == 'pipe':
95 # move out of this file- it's not dns 91 # move out of this file- it's not dns
92 if host.name == 'pipe':
96 rpi_net_boot() 93 rpi_net_boot()
97 files.directory(path='/opt/dnsmasq') 94 files.directory(path='/opt/dnsmasq')
98 dnsmasq_instance('10.2', 95 dnsmasq_instance('10.2',
99 house_iface='eth1', 96 house_iface='eth1',
100 dhcp_range='10.2.0.110,10.2.0.199', 97 dhcp_range='10.2.0.110,10.2.0.199',
101 listen_address='10.2.0.3', 98 listen_address='10.2.0.3',
102 dhcp_hosts_filename='templates/dnsmasq/dhcp_hosts.j2') 99 dhcp_hosts_filename='templates/dnsmasq/dhcp_hosts.j2')
103 out = '/opt/dnsmasq/10.2' 100 out = '/opt/dnsmasq/10.2'
104 # This mtail is for dhcp command counts and errors. Another monitor in lanscape/ reads the leases file. 101 # This mtail is for dhcp command counts and errors. Another monitor in lanscape/ reads the leases file.
105 files.template(src='templates/dnsmasq/metrics.mtail.j2', dest=f'{out}/metrics.mtail') 102 files.put(src='files/dnsmasq/metrics.mtail', dest=f'{out}/metrics.mtail')
106 files.template(src='templates/dnsmasq/run_mtail.sh', dest=f'{out}/run_mtail.sh') 103 files.put(src='files/dnsmasq/run_mtail.sh', dest=f'{out}/run_mtail.sh')
107 104
108 files.template(src='templates/dnsmasq/dnsmasq-mtail.service.j2', dest='/etc/systemd/system/dnsmasq-mtail.service') 105 files.put(src='files/dnsmasq/dnsmasq-mtail.service', dest='/etc/systemd/system/dnsmasq-mtail.service')
109 systemd.service(service='dnsmasq-mtail', enabled=True, restarted=True, daemon_reload=True) 106 systemd.service(service='dnsmasq-mtail', enabled=True, restarted=True, daemon_reload=True)
110 107
111 # Serve another dns, no dhcp, and include the dynamic-blocking file written by net_routes. 108 # Serve another dns, no dhcp, and include the dynamic-blocking file written by net_routes.
112 dnsmasq_instance( 109 dnsmasq_instance(
113 net_name='10.2-filtered', 110 net_name='10.2-filtered',