Mercurial > code > home > repos > infra
annotate dns.py @ 88:dae714e8f620
reactor and temporarily cut dep on lanscape
author | drewp@bigasterisk.com |
---|---|
date | Sun, 10 Jul 2022 19:50:52 -0700 |
parents | bf1573dd1947 |
children | 122ba5444176 |
rev | line source |
---|---|
38 | 1 import subprocess |
2 import tempfile | |
3 | |
4 import requests | |
34
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
5 from pyinfra import host |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
6 from pyinfra.operations import apt, files, server, systemd |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
7 |
38 | 8 |
9 def prepare_dhcp_hosts(): | |
88
dae714e8f620
reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents:
81
diff
changeset
|
10 empty = tempfile.NamedTemporaryFile(mode='wt') |
dae714e8f620
reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents:
81
diff
changeset
|
11 return empty |
38 | 12 lanscape_ip = subprocess.check_output([ |
13 'kubectl', | |
14 'get', | |
15 'svc', | |
79 | 16 'lanscape', |
17 "-o=jsonpath={.spec.clusterIP}", | |
38 | 18 ], |
19 encoding='ascii') | |
20 url = f'http://{lanscape_ip}/dnsConfig' | |
21 resp = requests.get(url) | |
22 resp.raise_for_status() | |
23 lanscape_config = resp.json() | |
24 | |
25 dhcp_hosts = tempfile.NamedTemporaryFile(mode='wt', encoding='ascii') | |
26 dhcp_hosts.write("# written by pyinfra\n\n") | |
27 for row in lanscape_config['dhcp_table']: | |
65
49a69852a4f4
let's get in less trouble by using dhcp more universally
drewp@bigasterisk.com
parents:
61
diff
changeset
|
28 dhcp_hosts.write(f'{row["mac"]},{row["hostname"]},{row["ip"]},24h\n') |
38 | 29 dhcp_hosts.flush() |
30 return dhcp_hosts | |
31 | |
81 | 32 def resolv_conf_use_systemd_networkd(): |
33 files.link(path='/etc/resolv.conf', target='/run/systemd/resolve/resolv.conf', force=True) | |
34 def resolv_conf_static_file(): | |
35 files.file(path='/etc/resolv.conf', present=False, force=True) | |
36 files.template(src='templates/resolv.conf.j2', | |
37 dest='/etc/resolv.conf', | |
38 # review this- it's probably a bad dep on bang. maybe both 10.5.0.1 and a public ns would be ok | |
39 ns='10.5.0.1' if host.name in ['prime', 'plus'] else '10.2.0.1', | |
40 force=True) | |
70 | 41 |
88
dae714e8f620
reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents:
81
diff
changeset
|
42 def dnsmasq_instance(dhcp_hosts, net_name): |
dae714e8f620
reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents:
81
diff
changeset
|
43 files.directory(path=f'/opt/dnsmasq/{net_name}') |
dae714e8f620
reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents:
81
diff
changeset
|
44 files.template(src='templates/dnsmasq/dnsmasq.conf.j2', dest=f'/opt/dnsmasq/{net_name}/dnsmasq.conf', net=net_name) |
dae714e8f620
reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents:
81
diff
changeset
|
45 files.template(src='templates/dnsmasq/hosts.j2', dest=f'/opt/dnsmasq/{net_name}/hosts', net=net_name) |
dae714e8f620
reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents:
81
diff
changeset
|
46 files.template(src=dhcp_hosts.name, dest=f'/opt/dnsmasq/{net_name}/dhcp_hosts', net=net_name) |
dae714e8f620
reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents:
81
diff
changeset
|
47 |
dae714e8f620
reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents:
81
diff
changeset
|
48 files.template(src='templates/dnsmasq/dnsmasq.service.j2', |
dae714e8f620
reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents:
81
diff
changeset
|
49 dest=f'/etc/systemd/system/dnsmasq_{net_name}.service', |
dae714e8f620
reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents:
81
diff
changeset
|
50 net=net_name) |
dae714e8f620
reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents:
81
diff
changeset
|
51 if net_name == '10.2': |
dae714e8f620
reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents:
81
diff
changeset
|
52 systemd.service(service=f'dnsmasq_{net_name}', enabled=True, restarted=True, daemon_reload=True) |
dae714e8f620
reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents:
81
diff
changeset
|
53 # 10.5 is after wireguard setup |
dae714e8f620
reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents:
81
diff
changeset
|
54 |
34
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
55 if host.name == 'bang': |
81 | 56 resolv_conf_static_file() |
34
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
57 apt.packages(packages=['dnsmasq']) |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
58 systemd.service(service='dnsmasq', enabled=False, running=False) |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
59 files.directory(path='/opt/dnsmasq') |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
60 |
38 | 61 dhcp_hosts = prepare_dhcp_hosts() |
62 | |
88
dae714e8f620
reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents:
81
diff
changeset
|
63 dnsmasq_instance(dhcp_hosts, '10.2') |
dae714e8f620
reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents:
81
diff
changeset
|
64 dnsmasq_instance(dhcp_hosts, '10.5') # only works after wireguard is up |
34
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
65 |
88
dae714e8f620
reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents:
81
diff
changeset
|
66 |
34
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
67 if host.name in [ |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
68 'garage', |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
69 'dash', |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
70 'slash', |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
71 'frontbed', |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
72 'prime', |
70 | 73 'pipe' |
34
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
74 ]: |
81 | 75 resolv_conf_use_systemd_networkd() |
34
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
76 files.template(src='templates/hosts.j2', dest='/etc/hosts') |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
77 files.template(src='templates/resolved.conf.j2', dest='/etc/systemd/resolved.conf') |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
78 systemd.service(service='systemd-resolved.service', running=True, restarted=True) |