Mercurial > code > home > repos > infra
comparison dns.py @ 318:2136320eb94d
dhcp_graph watcher
author | drewp@bigasterisk.com |
---|---|
date | Wed, 16 Oct 2024 20:47:04 -0700 |
parents | 828d3f4da54b |
children | 2e6dbebb2cb3 |
comparison
equal
deleted
inserted
replaced
317:2d3e90461011 | 318:2136320eb94d |
---|---|
1 import subprocess | |
1 from io import StringIO | 2 from io import StringIO |
2 import subprocess | |
3 from typing import cast | |
4 | 3 |
5 import pyinfra | 4 import pyinfra |
6 from pyinfra import host | 5 from pyinfra import host |
7 from pyinfra.operations import files, systemd, server | 6 from pyinfra.operations import files, server, systemd |
8 | 7 |
9 | 8 |
10 def dnsmasq_instance(net_name, | 9 def dnsmasq_instance(net_name, |
11 house_iface, | 10 house_iface, |
12 dhcp_range='10.2.0.10,10.2.0.11', | 11 dhcp_range='10.2.0.10,10.2.0.11', |
64 files.directory(path='/opt/dnsmasq') | 63 files.directory(path='/opt/dnsmasq') |
65 | 64 |
66 dnsmasq_instance('10.5', house_iface='unused', dhcp_range='unused', | 65 dnsmasq_instance('10.5', house_iface='unused', dhcp_range='unused', |
67 listen_address='unused') # only works after wireguard is up | 66 listen_address='unused') # only works after wireguard is up |
68 # move out of this file- it's not dns | 67 # move out of this file- it's not dns |
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 | |
69 if host.name == 'pipe': | 106 if host.name == 'pipe': |
70 rpi_net_boot() | 107 rpi_net_boot() |
71 files.directory(path='/opt/dnsmasq') | 108 files.directory(path='/opt/dnsmasq') |
72 dnsmasq_instance('10.2', | 109 dnsmasq_instance('10.2', |
73 house_iface='eth1', | 110 house_iface='eth1', |
74 dhcp_range='10.2.0.110,10.2.0.199', | 111 dhcp_range='10.2.0.110,10.2.0.199', |
75 listen_address='10.2.0.3', | 112 listen_address='10.2.0.3', |
76 dhcp_hosts_filename='templates/dnsmasq/dhcp_hosts.j2') | 113 dhcp_hosts_filename='templates/dnsmasq/dhcp_hosts.j2') |
77 out = '/opt/dnsmasq/10.2' | 114 out = '/opt/dnsmasq/10.2' |
78 # This mtail is for dhcp command counts and errors. Another monitor in lanscape/ reads the leases file. | 115 # This mtail is for dhcp command counts and errors. |
79 files.put(src='files/dnsmasq/metrics.mtail', dest=f'{out}/metrics.mtail') | 116 files.put(src='files/dnsmasq/metrics.mtail', dest=f'{out}/metrics.mtail') |
80 files.put(src='files/dnsmasq/run_mtail.sh', dest=f'{out}/run_mtail.sh') | 117 files.put(src='files/dnsmasq/run_mtail.sh', dest=f'{out}/run_mtail.sh') |
118 | |
119 watchLeasesFile() | |
81 | 120 |
82 files.put(src='files/dnsmasq/dnsmasq-mtail.service', dest='/etc/systemd/system/dnsmasq-mtail.service') | 121 files.put(src='files/dnsmasq/dnsmasq-mtail.service', dest='/etc/systemd/system/dnsmasq-mtail.service') |
83 systemd.service(service='dnsmasq-mtail', enabled=True, restarted=True, daemon_reload=True) | 122 systemd.service(service='dnsmasq-mtail', enabled=True, restarted=True, daemon_reload=True) |
84 | 123 |
85 # Serve another dns, no dhcp, and include the dynamic-blocking file written by net_routes. | 124 # Serve another dns, no dhcp, and include the dynamic-blocking file written by net_routes. |