Mercurial > code > home > repos > infra
annotate 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 |
rev | line source |
---|---|
326
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
1 from operator import le |
318 | 2 import subprocess |
241 | 3 from io import StringIO |
278 | 4 |
326
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
5 from pyinfra.context import host |
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
6 from pyinfra.operations import files, systemd |
94 | 7 |
282 | 8 |
326
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
9 def dnsmasq_instance( |
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
10 net_name, |
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
11 house_iface, |
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
12 dhcp_range='10.2.0.10,10.2.0.11', |
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
13 listen_address='reqd', |
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
14 ): |
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( |
326
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
17 src='dns/templates/dnsmasq/dnsmasq.conf.j2', |
213 | 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 ) | |
326
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
27 files.template(src='dns/templates/dnsmasq/hosts.j2', dest=f'/opt/dnsmasq/{net_name}/hosts', net=net_name) |
282 | 28 |
241 | 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 |
326
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
32 files.template(src='dns/templates/dnsmasq/dnsmasq.service.j2', |
88
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(): |
326
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
40 files.template(src='dns/templates/hosts.j2', dest='/etc/hosts') |
289
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
41 if 'pi' in host.groups: |
326
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
42 files.put(dest='/etc/resolv.conf', src='dns/files/resolv.conf') |
278 | 43 else: |
44 files.link(path='/etc/resolv.conf', target='/run/systemd/resolve/resolv.conf', force=True) | |
326
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
45 files.template(src='dns/templates/resolved.conf.j2', dest='/etc/systemd/resolved.conf') |
278 | 46 systemd.service(service='systemd-resolved.service', running=True, restarted=True) |
47 | |
94 | 48 |
278 | 49 def rpi_net_boot(): |
50 files.directory(path='/opt/dnsmasq/tftp') | |
282 | 51 |
213 | 52 |
326
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
53 def no_default_dnsmasq_instance(): |
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
54 # no default instance; i'll add some specific ones below |
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
55 systemd.service(service='dnsmasq', enabled=False, running=False) |
241 | 56 |
318 | 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" | |
326
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
67 files.template( |
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
68 src='dns/templates/dhcp_graph_watch.path.j2', |
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
69 dest='/etc/systemd/system/dhcp_graph_watch.path', |
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
70 leases=leases, |
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
71 ) |
318 | 72 |
326
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
73 files.template( |
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
74 src='dns/templates/dhcp_graph_update.service.j2', |
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
75 dest='/etc/systemd/system/dhcp_graph_update.service', |
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
76 leases=leases, |
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
77 dhcp_graph_url=dhcp_graph_url, |
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
78 ) |
318 | 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 | |
320
11d3bcedb9f0
updates for tofu rebuild; some dead code; start moving tasks into subdirs with their files and templates
drewp@bigasterisk.com
parents:
319
diff
changeset
|
82 |
326
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
83 def dnsmasq_on_pipe(): |
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
84 if host.name != 'pipe': |
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
85 return |
278 | 86 rpi_net_boot() |
94 | 87 files.directory(path='/opt/dnsmasq') |
326
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
88 dnsmasq_instance( |
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
89 '10.2', |
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
90 house_iface='eth1', |
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
91 dhcp_range='10.2.0.110,10.2.0.240', |
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
92 listen_address='10.2.0.3', |
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
93 ) |
119
51a471fa4d29
metrics on dnsmasq log errors and DHCP commands
drewp@bigasterisk.com
parents:
116
diff
changeset
|
94 out = '/opt/dnsmasq/10.2' |
318 | 95 # This mtail is for dhcp command counts and errors. |
326
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
96 files.put(src='dns/files/metrics.mtail', dest=f'{out}/metrics.mtail') |
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
97 files.put(src='dns/files/run_mtail.sh', dest=f'{out}/run_mtail.sh') |
119
51a471fa4d29
metrics on dnsmasq log errors and DHCP commands
drewp@bigasterisk.com
parents:
116
diff
changeset
|
98 |
318 | 99 watchLeasesFile() |
100 | |
326
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
101 files.put(src='dns/files/dnsmasq-mtail.service', dest='/etc/systemd/system/dnsmasq-mtail.service') |
241 | 102 systemd.service(service='dnsmasq-mtail', enabled=True, restarted=True, daemon_reload=True) |
94 | 103 |
213 | 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 ) | |
326
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
110 |
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
111 |
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
112 operations = [ |
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
113 standard_host_dns, |
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
114 no_default_dnsmasq_instance, |
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
115 dnsmasq_on_pipe, |
5b88b38f2471
huge reorg, reog toplevel functions in preparation of a ui with nice task lists
drewp@bigasterisk.com
parents:
325
diff
changeset
|
116 ] |