102
|
1 import subprocess
|
|
2 import requests
|
|
3 import tempfile
|
|
4
|
|
5 # this would be nice to write a hosts file for dhcp so it gives better names to
|
|
6 # stuff like webcams, but it needs lanscape to be up.
|
|
7
|
|
8 def prepare_dhcp_hosts():
|
|
9 lanscape_ip = subprocess.check_output([
|
|
10 'kubectl',
|
|
11 'get',
|
|
12 'svc',
|
|
13 'lanscape',
|
|
14 "-o=jsonpath={.spec.clusterIP}",
|
|
15 ],
|
|
16 encoding='ascii')
|
|
17 url = f'http://{lanscape_ip}/dnsConfig'
|
|
18 resp = requests.get(url)
|
|
19 resp.raise_for_status()
|
|
20 lanscape_config = resp.json()
|
|
21
|
|
22 dhcp_hosts = tempfile.NamedTemporaryFile(mode='wt', encoding='ascii')
|
|
23 dhcp_hosts.write("# written by pyinfra\n\n")
|
|
24 for row in lanscape_config['dhcp_table']:
|
|
25 dhcp_hosts.write(f'{row["mac"]},{row["hostname"]},{row["ip"]},24h\n')
|
|
26 dhcp_hosts.flush()
|
|
27 return dhcp_hosts |