Mercurial > code > home > repos > infra
annotate dns.py @ 318:2136320eb94d
dhcp_graph watcher
author | drewp@bigasterisk.com |
---|---|
date | Wed, 16 Oct 2024 20:47:04 -0700 |
parents | 828d3f4da54b |
children | 2e6dbebb2cb3 |
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 | |
34
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
62 if host.name == 'bang': |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
63 files.directory(path='/opt/dnsmasq') |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
64 |
213 | 65 dnsmasq_instance('10.5', house_iface='unused', dhcp_range='unused', |
66 listen_address='unused') # only works after wireguard is up | |
288 | 67 # move out of this file- it's not dns |
318 | 68 |
69 def watchLeasesFile(): | |
70 """summary: | |
71 1. dnsmasq_10.2 leases an address and writes to /opt/dnsmasq/10.2/leases | |
72 2. dhcp_graph_watch.path notices that change | |
73 3. dhcp_graph_update.service posts /opt/dnsmasq/10.2/leases to dhcp_graph (k8s deploy) | |
74 4. dhcp_graph serves the data as rdf | |
75 """ | |
76 dhcp_graph_url = "http://10.5.0.7:8005" | |
77 leases = "/opt/dnsmasq/10.2/leases" | |
78 files.put(dest='/etc/systemd/system/dhcp_graph_watch.path', src=StringIO(f''' | |
79 [Unit] | |
80 Description=dhcp leases file changed- run dhcp_graph_update | |
81 After=localfs.target | |
82 | |
83 [Path] | |
84 PathModified={leases} | |
85 Unit=dhcp_graph_update.service | |
86 | |
87 [Install] | |
88 WantedBy=multi-user.target | |
89 ''')) | |
90 | |
91 files.put(dest='/etc/systemd/system/dhcp_graph_update.service', src=StringIO(f''' | |
92 [Unit] | |
93 Description=Send new dhcp leases content to dhcp_graph | |
94 After=network.target | |
95 | |
96 [Service] | |
97 Type=oneshot | |
98 ExecStart=/usr/bin/curl -s {dhcp_graph_url}/leases -H "content-type: text/plain" --data-binary "@{leases}" | |
99 | |
100 [Install] | |
101 WantedBy=multi-user.target | |
102 ''')) | |
103 systemd.service(service='dhcp_graph_watch.path', enabled=True, restarted=True, daemon_reload=True) | |
104 systemd.service(service='dhcp_graph_update.service', enabled=True, restarted=True, daemon_reload=True) | |
105 | |
289
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
106 if host.name == 'pipe': |
278 | 107 rpi_net_boot() |
94 | 108 files.directory(path='/opt/dnsmasq') |
116 | 109 dnsmasq_instance('10.2', |
110 house_iface='eth1', | |
241 | 111 dhcp_range='10.2.0.110,10.2.0.199', |
213 | 112 listen_address='10.2.0.3', |
116 | 113 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
|
114 out = '/opt/dnsmasq/10.2' |
318 | 115 # 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
|
116 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
|
117 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
|
118 |
318 | 119 watchLeasesFile() |
120 | |
289
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
121 files.put(src='files/dnsmasq/dnsmasq-mtail.service', dest='/etc/systemd/system/dnsmasq-mtail.service') |
241 | 122 systemd.service(service='dnsmasq-mtail', enabled=True, restarted=True, daemon_reload=True) |
94 | 123 |
213 | 124 # Serve another dns, no dhcp, and include the dynamic-blocking file written by net_routes. |
125 dnsmasq_instance( | |
126 net_name='10.2-filtered', | |
127 house_iface='eth1', | |
128 listen_address='10.2.0.4', | |
129 ) |