Mercurial > code > home > repos > infra
annotate dns.py @ 44:e3249dd163fc
turn on 10.5 dnsmasq service
author | drewp@bigasterisk.com |
---|---|
date | Mon, 14 Feb 2022 21:52:47 -0800 |
parents | 4026b6b8028f |
children | 8945bf71da22 |
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']: | |
26 dhcp_hosts.write(f'{row["mac"]},{row["hostname"]},{row["ip"]},infinite\n') | |
27 dhcp_hosts.flush() | |
28 return dhcp_hosts | |
29 | |
30 | |
34
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
31 if host.name == 'bang': |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
32 apt.packages(packages=['dnsmasq']) |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
33 systemd.service(service='dnsmasq', enabled=False, running=False) |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
34 files.directory(path='/opt/dnsmasq') |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
35 |
38 | 36 dhcp_hosts = prepare_dhcp_hosts() |
37 | |
34
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
38 for net_name in ['10.1', '10.2', '10.5']: |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
39 files.directory(path=f'/opt/dnsmasq/{net_name}') |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
40 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
|
41 files.template(src='templates/dnsmasq/hosts.j2', dest=f'/opt/dnsmasq/{net_name}/hosts', net=net_name) |
38 | 42 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
|
43 |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
44 files.template(src='templates/dnsmasq/dnsmasq.service.j2', |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
45 dest=f'/etc/systemd/system/dnsmasq_{net_name}.service', |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
46 net=net_name) |
44 | 47 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
|
48 |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
49 if host.name in [ |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
50 'garage', |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
51 'dash', |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
52 'slash', |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
53 'frontbed', |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
54 'prime', |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
55 ]: |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
56 files.template(src='templates/hosts.j2', dest='/etc/hosts') |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
57 |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
58 files.link(path='/etc/resolv.conf', target='/run/systemd/resolve/resolv.conf') |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
59 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
|
60 |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff
changeset
|
61 systemd.service(service='systemd-resolved.service', running=True, restarted=True) |