comparison dns/dns.py @ 326:5b88b38f2471

huge reorg, reog toplevel functions in preparation of a ui with nice task lists
author drewp@bigasterisk.com
date Mon, 20 Jan 2025 21:55:08 -0800
parents dns.py@4d1b6a6e65d2
children
comparison
equal deleted inserted replaced
325:4d1b6a6e65d2 326:5b88b38f2471
1 from operator import le
2 import subprocess
3 from io import StringIO
4
5 from pyinfra.context import host
6 from pyinfra.operations import files, systemd
7
8
9 def dnsmasq_instance(
10 net_name,
11 house_iface,
12 dhcp_range='10.2.0.10,10.2.0.11',
13 listen_address='reqd',
14 ):
15 files.directory(path=f'/opt/dnsmasq/{net_name}')
16 files.template(
17 src='dns/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 )
27 files.template(src='dns/templates/dnsmasq/hosts.j2', dest=f'/opt/dnsmasq/{net_name}/hosts', net=net_name)
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')
31
32 files.template(src='dns/templates/dnsmasq/dnsmasq.service.j2',
33 dest=f'/etc/systemd/system/dnsmasq_{net_name}.service',
34 net=net_name)
35 if net_name in ['10.2', '10.2-filtered']:
36 systemd.service(service=f'dnsmasq_{net_name}', enabled=True, restarted=True, daemon_reload=True)
37
38
39 def standard_host_dns():
40 files.template(src='dns/templates/hosts.j2', dest='/etc/hosts')
41 if 'pi' in host.groups:
42 files.put(dest='/etc/resolv.conf', src='dns/files/resolv.conf')
43 else:
44 files.link(path='/etc/resolv.conf', target='/run/systemd/resolve/resolv.conf', force=True)
45 files.template(src='dns/templates/resolved.conf.j2', dest='/etc/systemd/resolved.conf')
46 systemd.service(service='systemd-resolved.service', running=True, restarted=True)
47
48
49 def rpi_net_boot():
50 files.directory(path='/opt/dnsmasq/tftp')
51
52
53 def no_default_dnsmasq_instance():
54 # no default instance; i'll add some specific ones below
55 systemd.service(service='dnsmasq', enabled=False, running=False)
56
57
58 def watchLeasesFile():
59 """summary:
60 1. dnsmasq_10.2 leases an address and writes to /opt/dnsmasq/10.2/leases
61 2. dhcp_graph_watch.path notices that change
62 3. dhcp_graph_update.service posts /opt/dnsmasq/10.2/leases to dhcp_graph (k8s deploy)
63 4. dhcp_graph serves the data as rdf
64 """
65 dhcp_graph_url = "http://10.5.0.7:8005"
66 leases = "/opt/dnsmasq/10.2/leases"
67 files.template(
68 src='dns/templates/dhcp_graph_watch.path.j2',
69 dest='/etc/systemd/system/dhcp_graph_watch.path',
70 leases=leases,
71 )
72
73 files.template(
74 src='dns/templates/dhcp_graph_update.service.j2',
75 dest='/etc/systemd/system/dhcp_graph_update.service',
76 leases=leases,
77 dhcp_graph_url=dhcp_graph_url,
78 )
79 systemd.service(service='dhcp_graph_watch.path', enabled=True, restarted=True, daemon_reload=True)
80 systemd.service(service='dhcp_graph_update.service', enabled=True, restarted=True, daemon_reload=True)
81
82
83 def dnsmasq_on_pipe():
84 if host.name != 'pipe':
85 return
86 rpi_net_boot()
87 files.directory(path='/opt/dnsmasq')
88 dnsmasq_instance(
89 '10.2',
90 house_iface='eth1',
91 dhcp_range='10.2.0.110,10.2.0.240',
92 listen_address='10.2.0.3',
93 )
94 out = '/opt/dnsmasq/10.2'
95 # This mtail is for dhcp command counts and errors.
96 files.put(src='dns/files/metrics.mtail', dest=f'{out}/metrics.mtail')
97 files.put(src='dns/files/run_mtail.sh', dest=f'{out}/run_mtail.sh')
98
99 watchLeasesFile()
100
101 files.put(src='dns/files/dnsmasq-mtail.service', dest='/etc/systemd/system/dnsmasq-mtail.service')
102 systemd.service(service='dnsmasq-mtail', enabled=True, restarted=True, daemon_reload=True)
103
104 # Serve another dns, no dhcp, and include the dynamic-blocking file written by net_routes.
105 dnsmasq_instance(
106 net_name='10.2-filtered',
107 house_iface='eth1',
108 listen_address='10.2.0.4',
109 )
110
111
112 operations = [
113 standard_host_dns,
114 no_default_dnsmasq_instance,
115 dnsmasq_on_pipe,
116 ]