annotate dns.py @ 102:9ec487a381e2

refactor prepare_dhcp_hosts
author drewp@bigasterisk.com
date Fri, 15 Jul 2022 14:51:12 -0700
parents 122ba5444176
children 8b8ef9d8f0fd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
34
d4fb38f13c79 refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff changeset
1 from pyinfra import host
102
9ec487a381e2 refactor prepare_dhcp_hosts
drewp@bigasterisk.com
parents: 94
diff changeset
2 from pyinfra.operations import apt, files, systemd
38
4026b6b8028f get dhcp mapping from lanscape
drewp@bigasterisk.com
parents: 34
diff changeset
3
94
122ba5444176 dhcp,dns to pipe
drewp@bigasterisk.com
parents: 88
diff changeset
4
81
bf1573dd1947 dns fixes
drewp@bigasterisk.com
parents: 79
diff changeset
5 def resolv_conf_use_systemd_networkd():
bf1573dd1947 dns fixes
drewp@bigasterisk.com
parents: 79
diff changeset
6 files.link(path='/etc/resolv.conf', target='/run/systemd/resolve/resolv.conf', force=True)
94
122ba5444176 dhcp,dns to pipe
drewp@bigasterisk.com
parents: 88
diff changeset
7
122ba5444176 dhcp,dns to pipe
drewp@bigasterisk.com
parents: 88
diff changeset
8
81
bf1573dd1947 dns fixes
drewp@bigasterisk.com
parents: 79
diff changeset
9 def resolv_conf_static_file():
bf1573dd1947 dns fixes
drewp@bigasterisk.com
parents: 79
diff changeset
10 files.file(path='/etc/resolv.conf', present=False, force=True)
94
122ba5444176 dhcp,dns to pipe
drewp@bigasterisk.com
parents: 88
diff changeset
11 files.template(
122ba5444176 dhcp,dns to pipe
drewp@bigasterisk.com
parents: 88
diff changeset
12 src='templates/resolv.conf.j2',
122ba5444176 dhcp,dns to pipe
drewp@bigasterisk.com
parents: 88
diff changeset
13 dest='/etc/resolv.conf',
122ba5444176 dhcp,dns to pipe
drewp@bigasterisk.com
parents: 88
diff changeset
14 # review this- it's probably a bad dep on bang. maybe both 10.5.0.1 and a public ns would be ok
122ba5444176 dhcp,dns to pipe
drewp@bigasterisk.com
parents: 88
diff changeset
15 ns='10.5.0.1' if host.name in ['prime', 'plus'] else '10.2.0.3',
122ba5444176 dhcp,dns to pipe
drewp@bigasterisk.com
parents: 88
diff changeset
16 force=True)
122ba5444176 dhcp,dns to pipe
drewp@bigasterisk.com
parents: 88
diff changeset
17
70
136d86d06ce6 dns improvements
drewp@bigasterisk.com
parents: 65
diff changeset
18
94
122ba5444176 dhcp,dns to pipe
drewp@bigasterisk.com
parents: 88
diff changeset
19 def dnsmasq_instance(net_name, house_iface, dhcp_range, router):
88
dae714e8f620 reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents: 81
diff changeset
20 files.directory(path=f'/opt/dnsmasq/{net_name}')
94
122ba5444176 dhcp,dns to pipe
drewp@bigasterisk.com
parents: 88
diff changeset
21 files.template(
122ba5444176 dhcp,dns to pipe
drewp@bigasterisk.com
parents: 88
diff changeset
22 src='templates/dnsmasq/dnsmasq.conf.j2',
122ba5444176 dhcp,dns to pipe
drewp@bigasterisk.com
parents: 88
diff changeset
23 dest=f'/opt/dnsmasq/{net_name}/dnsmasq.conf',
122ba5444176 dhcp,dns to pipe
drewp@bigasterisk.com
parents: 88
diff changeset
24 net=net_name,
122ba5444176 dhcp,dns to pipe
drewp@bigasterisk.com
parents: 88
diff changeset
25 house_iface=house_iface,
122ba5444176 dhcp,dns to pipe
drewp@bigasterisk.com
parents: 88
diff changeset
26 dhcp_range=dhcp_range,
122ba5444176 dhcp,dns to pipe
drewp@bigasterisk.com
parents: 88
diff changeset
27 router=router,
102
9ec487a381e2 refactor prepare_dhcp_hosts
drewp@bigasterisk.com
parents: 94
diff changeset
28 dhcp_enabled=net_name == '10.2' and host.name == 'pipe')
88
dae714e8f620 reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents: 81
diff changeset
29 files.template(src='templates/dnsmasq/hosts.j2', dest=f'/opt/dnsmasq/{net_name}/hosts', net=net_name)
94
122ba5444176 dhcp,dns to pipe
drewp@bigasterisk.com
parents: 88
diff changeset
30 files.template(src='/dev/null', dest=f'/opt/dnsmasq/{net_name}/dhcp_hosts', net=net_name)
88
dae714e8f620 reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents: 81
diff changeset
31
dae714e8f620 reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents: 81
diff changeset
32 files.template(src='templates/dnsmasq/dnsmasq.service.j2',
dae714e8f620 reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents: 81
diff changeset
33 dest=f'/etc/systemd/system/dnsmasq_{net_name}.service',
dae714e8f620 reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents: 81
diff changeset
34 net=net_name)
dae714e8f620 reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents: 81
diff changeset
35 if net_name == '10.2':
dae714e8f620 reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents: 81
diff changeset
36 systemd.service(service=f'dnsmasq_{net_name}', enabled=True, restarted=True, daemon_reload=True)
94
122ba5444176 dhcp,dns to pipe
drewp@bigasterisk.com
parents: 88
diff changeset
37
122ba5444176 dhcp,dns to pipe
drewp@bigasterisk.com
parents: 88
diff changeset
38
122ba5444176 dhcp,dns to pipe
drewp@bigasterisk.com
parents: 88
diff changeset
39 files.template(src='templates/hosts.j2', dest='/etc/hosts')
122ba5444176 dhcp,dns to pipe
drewp@bigasterisk.com
parents: 88
diff changeset
40 resolv_conf_use_systemd_networkd()
122ba5444176 dhcp,dns to pipe
drewp@bigasterisk.com
parents: 88
diff changeset
41 files.template(src='templates/resolved.conf.j2', dest='/etc/systemd/resolved.conf')
122ba5444176 dhcp,dns to pipe
drewp@bigasterisk.com
parents: 88
diff changeset
42 systemd.service(service='systemd-resolved.service', running=True, restarted=True)
122ba5444176 dhcp,dns to pipe
drewp@bigasterisk.com
parents: 88
diff changeset
43
34
d4fb38f13c79 refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff changeset
44 if host.name == 'bang':
d4fb38f13c79 refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff changeset
45 apt.packages(packages=['dnsmasq'])
d4fb38f13c79 refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff changeset
46 systemd.service(service='dnsmasq', enabled=False, running=False)
d4fb38f13c79 refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff changeset
47 files.directory(path='/opt/dnsmasq')
d4fb38f13c79 refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff changeset
48
94
122ba5444176 dhcp,dns to pipe
drewp@bigasterisk.com
parents: 88
diff changeset
49 dnsmasq_instance('10.5', house_iface='unused', dhcp_range='unused', router='unused') # only works after wireguard is up
34
d4fb38f13c79 refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff changeset
50
94
122ba5444176 dhcp,dns to pipe
drewp@bigasterisk.com
parents: 88
diff changeset
51 elif host.name == 'pipe':
122ba5444176 dhcp,dns to pipe
drewp@bigasterisk.com
parents: 88
diff changeset
52 apt.packages(packages=['dnsmasq'])
122ba5444176 dhcp,dns to pipe
drewp@bigasterisk.com
parents: 88
diff changeset
53 systemd.service(service='dnsmasq', enabled=False, running=False)
122ba5444176 dhcp,dns to pipe
drewp@bigasterisk.com
parents: 88
diff changeset
54 files.directory(path='/opt/dnsmasq')
122ba5444176 dhcp,dns to pipe
drewp@bigasterisk.com
parents: 88
diff changeset
55 dnsmasq_instance('10.2', house_iface='eth1', dhcp_range='10.2.0.20,10.2.0.120', router='10.2.0.3')
122ba5444176 dhcp,dns to pipe
drewp@bigasterisk.com
parents: 88
diff changeset
56
122ba5444176 dhcp,dns to pipe
drewp@bigasterisk.com
parents: 88
diff changeset
57 else:
102
9ec487a381e2 refactor prepare_dhcp_hosts
drewp@bigasterisk.com
parents: 94
diff changeset
58 pass