Mercurial > code > home > repos > infra
annotate dns.py @ 325:4d1b6a6e65d2
minor project edits
author | drewp@bigasterisk.com |
---|---|
date | Mon, 20 Jan 2025 14:10:19 -0800 |
parents | 11d3bcedb9f0 |
children |
rev | line source |
---|---|
318 | 1 import subprocess |
241 | 2 from io import StringIO |
278 | 3 |
288 | 4 import pyinfra |
34
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
5 from pyinfra import host |
318 | 6 from pyinfra.operations import files, server, systemd |
94 | 7 |
282 | 8 |
213 | 9 def dnsmasq_instance(net_name, |
10 house_iface, | |
11 dhcp_range='10.2.0.10,10.2.0.11', | |
12 listen_address='reqd', | |
13 dhcp_hosts_filename='/dev/null'): | |
88
dae714e8f620
reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents:
81
diff
changeset
|
14 files.directory(path=f'/opt/dnsmasq/{net_name}') |
213 | 15 files.template( |
16 src='templates/dnsmasq/dnsmasq.conf.j2', | |
17 dest=f'/opt/dnsmasq/{net_name}/dnsmasq.conf', | |
18 net=net_name, | |
19 house_iface=house_iface, | |
20 dhcp_range=dhcp_range, | |
21 listen_address=listen_address, | |
22 dhcp_enabled=net_name == '10.2' and host.name == 'pipe', | |
23 dns_server=listen_address, | |
24 router=listen_address, | |
25 ) | |
88
dae714e8f620
reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents:
81
diff
changeset
|
26 files.template(src='templates/dnsmasq/hosts.j2', dest=f'/opt/dnsmasq/{net_name}/hosts', net=net_name) |
282 | 27 |
241 | 28 dhcp_hosts = subprocess.check_output(['python3', '/my/serv/lanscape/src/public/make_dhcp_hosts.py'], encoding='utf8') |
29 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
|
30 |
dae714e8f620
reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents:
81
diff
changeset
|
31 files.template(src='templates/dnsmasq/dnsmasq.service.j2', |
dae714e8f620
reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents:
81
diff
changeset
|
32 dest=f'/etc/systemd/system/dnsmasq_{net_name}.service', |
dae714e8f620
reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents:
81
diff
changeset
|
33 net=net_name) |
213 | 34 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
|
35 systemd.service(service=f'dnsmasq_{net_name}', enabled=True, restarted=True, daemon_reload=True) |
94 | 36 |
213 | 37 |
179 | 38 def standard_host_dns(): |
39 files.template(src='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
|
40 if 'pi' in host.groups: |
282 | 41 files.put(dest='/etc/resolv.conf', |
42 src=StringIO(''' | |
278 | 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) | |
51 | |
94 | 52 |
278 | 53 def rpi_net_boot(): |
54 files.directory(path='/opt/dnsmasq/tftp') | |
282 | 55 |
213 | 56 |
179 | 57 standard_host_dns() |
94 | 58 |
241 | 59 # no default instance; i'll add some specific ones below |
60 systemd.service(service='dnsmasq', enabled=False, running=False) | |
61 | |
318 | 62 |
63 def watchLeasesFile(): | |
64 """summary: | |
65 1. dnsmasq_10.2 leases an address and writes to /opt/dnsmasq/10.2/leases | |
66 2. dhcp_graph_watch.path notices that change | |
67 3. dhcp_graph_update.service posts /opt/dnsmasq/10.2/leases to dhcp_graph (k8s deploy) | |
68 4. dhcp_graph serves the data as rdf | |
69 """ | |
70 dhcp_graph_url = "http://10.5.0.7:8005" | |
71 leases = "/opt/dnsmasq/10.2/leases" | |
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
|
72 files.put(dest='/etc/systemd/system/dhcp_graph_watch.path', |
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
|
73 src=StringIO(f''' |
318 | 74 [Unit] |
75 Description=dhcp leases file changed- run dhcp_graph_update | |
76 After=localfs.target | |
77 | |
78 [Path] | |
79 PathModified={leases} | |
80 Unit=dhcp_graph_update.service | |
81 | |
82 [Install] | |
83 WantedBy=multi-user.target | |
84 ''')) | |
85 | |
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
|
86 files.put(dest='/etc/systemd/system/dhcp_graph_update.service', |
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
|
87 src=StringIO(f''' |
318 | 88 [Unit] |
89 Description=Send new dhcp leases content to dhcp_graph | |
90 After=network.target | |
91 | |
92 [Service] | |
93 Type=oneshot | |
94 ExecStart=/usr/bin/curl -s {dhcp_graph_url}/leases -H "content-type: text/plain" --data-binary "@{leases}" | |
95 | |
96 [Install] | |
97 WantedBy=multi-user.target | |
98 ''')) | |
99 systemd.service(service='dhcp_graph_watch.path', enabled=True, restarted=True, daemon_reload=True) | |
100 systemd.service(service='dhcp_graph_update.service', enabled=True, restarted=True, daemon_reload=True) | |
101 | |
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
|
102 |
289
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
103 if host.name == 'pipe': |
278 | 104 rpi_net_boot() |
94 | 105 files.directory(path='/opt/dnsmasq') |
116 | 106 dnsmasq_instance('10.2', |
107 house_iface='eth1', | |
319 | 108 dhcp_range='10.2.0.110,10.2.0.240', |
213 | 109 listen_address='10.2.0.3', |
116 | 110 dhcp_hosts_filename='templates/dnsmasq/dhcp_hosts.j2') |
119
51a471fa4d29
metrics on dnsmasq log errors and DHCP commands
drewp@bigasterisk.com
parents:
116
diff
changeset
|
111 out = '/opt/dnsmasq/10.2' |
318 | 112 # This mtail is for dhcp command counts and errors. |
289
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
113 files.put(src='files/dnsmasq/metrics.mtail', dest=f'{out}/metrics.mtail') |
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
114 files.put(src='files/dnsmasq/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
|
115 |
318 | 116 watchLeasesFile() |
117 | |
289
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
118 files.put(src='files/dnsmasq/dnsmasq-mtail.service', dest='/etc/systemd/system/dnsmasq-mtail.service') |
241 | 119 systemd.service(service='dnsmasq-mtail', enabled=True, restarted=True, daemon_reload=True) |
94 | 120 |
213 | 121 # Serve another dns, no dhcp, and include the dynamic-blocking file written by net_routes. |
122 dnsmasq_instance( | |
123 net_name='10.2-filtered', | |
124 house_iface='eth1', | |
125 listen_address='10.2.0.4', | |
126 ) |