annotate dns.py @ 107:d70816c7c7db

fix pipe net forwarding after update to ubuntu 22.04
author drewp
date Tue, 19 Jul 2022 17:40:02 -0700
parents 5faa7e3aa38f
children 6ec849f1a8c9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
106
5faa7e3aa38f bang can use dhcp and gets a static addr
drewp
parents: 103
diff changeset
1 import tempfile
34
d4fb38f13c79 refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff changeset
2 from pyinfra import host
102
9ec487a381e2 refactor prepare_dhcp_hosts
drewp@bigasterisk.com
parents: 94
diff changeset
3 from pyinfra.operations import apt, files, systemd
38
4026b6b8028f get dhcp mapping from lanscape
drewp@bigasterisk.com
parents: 34
diff changeset
4
94
122ba5444176 dhcp,dns to pipe
drewp@bigasterisk.com
parents: 88
diff changeset
5
106
5faa7e3aa38f bang can use dhcp and gets a static addr
drewp
parents: 103
diff changeset
6 def dnsmasq_instance(net_name, house_iface, dhcp_range, router, dhcp_hosts_filename='/dev/null'):
88
dae714e8f620 reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents: 81
diff changeset
7 files.directory(path=f'/opt/dnsmasq/{net_name}')
103
8b8ef9d8f0fd dead code and templates, reformat, maybe a little refactor
drewp@bigasterisk.com
parents: 102
diff changeset
8 files.template(src='templates/dnsmasq/dnsmasq.conf.j2',
8b8ef9d8f0fd dead code and templates, reformat, maybe a little refactor
drewp@bigasterisk.com
parents: 102
diff changeset
9 dest=f'/opt/dnsmasq/{net_name}/dnsmasq.conf',
8b8ef9d8f0fd dead code and templates, reformat, maybe a little refactor
drewp@bigasterisk.com
parents: 102
diff changeset
10 net=net_name,
8b8ef9d8f0fd dead code and templates, reformat, maybe a little refactor
drewp@bigasterisk.com
parents: 102
diff changeset
11 house_iface=house_iface,
8b8ef9d8f0fd dead code and templates, reformat, maybe a little refactor
drewp@bigasterisk.com
parents: 102
diff changeset
12 dhcp_range=dhcp_range,
8b8ef9d8f0fd dead code and templates, reformat, maybe a little refactor
drewp@bigasterisk.com
parents: 102
diff changeset
13 router=router,
8b8ef9d8f0fd dead code and templates, reformat, maybe a little refactor
drewp@bigasterisk.com
parents: 102
diff changeset
14 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
15 files.template(src='templates/dnsmasq/hosts.j2', dest=f'/opt/dnsmasq/{net_name}/hosts', net=net_name)
106
5faa7e3aa38f bang can use dhcp and gets a static addr
drewp
parents: 103
diff changeset
16 files.template(src=dhcp_hosts_filename, 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
17
dae714e8f620 reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents: 81
diff changeset
18 files.template(src='templates/dnsmasq/dnsmasq.service.j2',
dae714e8f620 reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents: 81
diff changeset
19 dest=f'/etc/systemd/system/dnsmasq_{net_name}.service',
dae714e8f620 reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents: 81
diff changeset
20 net=net_name)
dae714e8f620 reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents: 81
diff changeset
21 if net_name == '10.2':
dae714e8f620 reactor and temporarily cut dep on lanscape
drewp@bigasterisk.com
parents: 81
diff changeset
22 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
23
122ba5444176 dhcp,dns to pipe
drewp@bigasterisk.com
parents: 88
diff changeset
24
122ba5444176 dhcp,dns to pipe
drewp@bigasterisk.com
parents: 88
diff changeset
25 files.template(src='templates/hosts.j2', dest='/etc/hosts')
103
8b8ef9d8f0fd dead code and templates, reformat, maybe a little refactor
drewp@bigasterisk.com
parents: 102
diff changeset
26 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
27 files.template(src='templates/resolved.conf.j2', dest='/etc/systemd/resolved.conf')
122ba5444176 dhcp,dns to pipe
drewp@bigasterisk.com
parents: 88
diff changeset
28 systemd.service(service='systemd-resolved.service', running=True, restarted=True)
122ba5444176 dhcp,dns to pipe
drewp@bigasterisk.com
parents: 88
diff changeset
29
34
d4fb38f13c79 refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff changeset
30 if host.name == 'bang':
d4fb38f13c79 refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff changeset
31 apt.packages(packages=['dnsmasq'])
d4fb38f13c79 refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff changeset
32 systemd.service(service='dnsmasq', enabled=False, running=False)
d4fb38f13c79 refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff changeset
33 files.directory(path='/opt/dnsmasq')
d4fb38f13c79 refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
diff changeset
34
94
122ba5444176 dhcp,dns to pipe
drewp@bigasterisk.com
parents: 88
diff changeset
35 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
36
94
122ba5444176 dhcp,dns to pipe
drewp@bigasterisk.com
parents: 88
diff changeset
37 elif host.name == 'pipe':
122ba5444176 dhcp,dns to pipe
drewp@bigasterisk.com
parents: 88
diff changeset
38 apt.packages(packages=['dnsmasq'])
122ba5444176 dhcp,dns to pipe
drewp@bigasterisk.com
parents: 88
diff changeset
39 systemd.service(service='dnsmasq', enabled=False, running=False)
122ba5444176 dhcp,dns to pipe
drewp@bigasterisk.com
parents: 88
diff changeset
40 files.directory(path='/opt/dnsmasq')
106
5faa7e3aa38f bang can use dhcp and gets a static addr
drewp
parents: 103
diff changeset
41 dh = tempfile.NamedTemporaryFile()
5faa7e3aa38f bang can use dhcp and gets a static addr
drewp
parents: 103
diff changeset
42 dh.write(b'''\
5faa7e3aa38f bang can use dhcp and gets a static addr
drewp
parents: 103
diff changeset
43 60:e3:27:04:4a:85,bang,10.2.0.1,24h
5faa7e3aa38f bang can use dhcp and gets a static addr
drewp
parents: 103
diff changeset
44 ''')
5faa7e3aa38f bang can use dhcp and gets a static addr
drewp
parents: 103
diff changeset
45 dh.flush()
5faa7e3aa38f bang can use dhcp and gets a static addr
drewp
parents: 103
diff changeset
46 dnsmasq_instance('10.2', house_iface='eth1', dhcp_range='10.2.0.20,10.2.0.120', router='10.2.0.3', dhcp_hosts_filename=dh.name)
94
122ba5444176 dhcp,dns to pipe
drewp@bigasterisk.com
parents: 88
diff changeset
47
122ba5444176 dhcp,dns to pipe
drewp@bigasterisk.com
parents: 88
diff changeset
48 else:
102
9ec487a381e2 refactor prepare_dhcp_hosts
drewp@bigasterisk.com
parents: 94
diff changeset
49 pass