Mercurial > code > home > repos > infra
annotate dns.py @ 70:136d86d06ce6
dns improvements
author | drewp@bigasterisk.com |
---|---|
date | Sat, 11 Jun 2022 22:53:23 -0700 |
parents | 49a69852a4f4 |
children | 3f7d4626234c |
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', | |
14 '--field-selector=metadata.name=lanscape', | |
15 "-o=jsonpath={.items[0].spec.clusterIP}", | |
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 | |
30 | |
70 | 31 #files.link('/etc/resolv.conf', '/run/systemd/resolve/stub-resolv.conf') |
32 | |
33 # files.file(path='/etc/resolv.conf', present=False) | |
34 # files.link(path='/etc/resolv.conf', present=False) # bug | |
35 # server.shell(["rm -f /etc/resolv.conf"]) # broken fix | |
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) | |
41 | |
34
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
42 if host.name == 'bang': |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
43 apt.packages(packages=['dnsmasq']) |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
44 systemd.service(service='dnsmasq', enabled=False, running=False) |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
45 files.directory(path='/opt/dnsmasq') |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
46 |
38 | 47 dhcp_hosts = prepare_dhcp_hosts() |
48 | |
61
b46df76991b6
10.1 cleanups; verbose settings; address updates
drewp@bigasterisk.com
parents:
53
diff
changeset
|
49 for net_name in ['10.2', '10.5']: |
34
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
50 files.directory(path=f'/opt/dnsmasq/{net_name}') |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
51 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
|
52 files.template(src='templates/dnsmasq/hosts.j2', dest=f'/opt/dnsmasq/{net_name}/hosts', net=net_name) |
38 | 53 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
|
54 |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
55 files.template(src='templates/dnsmasq/dnsmasq.service.j2', |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
56 dest=f'/etc/systemd/system/dnsmasq_{net_name}.service', |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
57 net=net_name) |
44 | 58 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
|
59 |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
60 if host.name in [ |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
61 'garage', |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
62 'dash', |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
63 'slash', |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
64 'frontbed', |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
65 'prime', |
70 | 66 'pipe' |
34
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
67 ]: |
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) |