Mercurial > code > home > repos > infra
annotate dns.py @ 81:bf1573dd1947
dns fixes
author | drewp@bigasterisk.com |
---|---|
date | Sun, 26 Jun 2022 01:40:48 -0700 |
parents | 3f7d4626234c |
children | dae714e8f620 |
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(): | |
10 lanscape_ip = subprocess.check_output([ | |
11 'kubectl', | |
12 'get', | |
13 'svc', | |
79 | 14 'lanscape', |
15 "-o=jsonpath={.spec.clusterIP}", | |
38 | 16 ], |
17 encoding='ascii') | |
18 url = f'http://{lanscape_ip}/dnsConfig' | |
19 resp = requests.get(url) | |
20 resp.raise_for_status() | |
21 lanscape_config = resp.json() | |
22 | |
23 dhcp_hosts = tempfile.NamedTemporaryFile(mode='wt', encoding='ascii') | |
24 dhcp_hosts.write("# written by pyinfra\n\n") | |
25 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
|
26 dhcp_hosts.write(f'{row["mac"]},{row["hostname"]},{row["ip"]},24h\n') |
38 | 27 dhcp_hosts.flush() |
28 return dhcp_hosts | |
29 | |
81 | 30 def resolv_conf_use_systemd_networkd(): |
31 files.link(path='/etc/resolv.conf', target='/run/systemd/resolve/resolv.conf', force=True) | |
32 def resolv_conf_static_file(): | |
33 files.file(path='/etc/resolv.conf', present=False, force=True) | |
34 files.template(src='templates/resolv.conf.j2', | |
35 dest='/etc/resolv.conf', | |
36 # review this- it's probably a bad dep on bang. maybe both 10.5.0.1 and a public ns would be ok | |
37 ns='10.5.0.1' if host.name in ['prime', 'plus'] else '10.2.0.1', | |
38 force=True) | |
70 | 39 |
34
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
40 if host.name == 'bang': |
81 | 41 resolv_conf_static_file() |
34
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
42 apt.packages(packages=['dnsmasq']) |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
43 systemd.service(service='dnsmasq', enabled=False, running=False) |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
44 files.directory(path='/opt/dnsmasq') |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
45 |
38 | 46 dhcp_hosts = prepare_dhcp_hosts() |
47 | |
61
b46df76991b6
10.1 cleanups; verbose settings; address updates
drewp@bigasterisk.com
parents:
53
diff
changeset
|
48 for net_name in ['10.2', '10.5']: |
34
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
49 files.directory(path=f'/opt/dnsmasq/{net_name}') |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
50 files.template(src='templates/dnsmasq/dnsmasq.conf.j2', dest=f'/opt/dnsmasq/{net_name}/dnsmasq.conf', net=net_name) |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
51 files.template(src='templates/dnsmasq/hosts.j2', dest=f'/opt/dnsmasq/{net_name}/hosts', net=net_name) |
38 | 52 files.template(src=dhcp_hosts.name, dest=f'/opt/dnsmasq/{net_name}/dhcp_hosts', net=net_name) |
34
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
53 |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
54 files.template(src='templates/dnsmasq/dnsmasq.service.j2', |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
55 dest=f'/etc/systemd/system/dnsmasq_{net_name}.service', |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
56 net=net_name) |
44 | 57 systemd.service(service=f'dnsmasq_{net_name}', enabled=True, restarted=True, daemon_reload=True) |
34
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
58 |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
59 if host.name in [ |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
60 'garage', |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
61 'dash', |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
62 'slash', |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
63 'frontbed', |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
64 'prime', |
70 | 65 'pipe' |
34
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
66 ]: |
81 | 67 resolv_conf_use_systemd_networkd() |
34
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
68 files.template(src='templates/hosts.j2', dest='/etc/hosts') |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
69 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
|
70 systemd.service(service='systemd-resolved.service', running=True, restarted=True) |