Mercurial > code > home > repos > infra
annotate wireguard.py @ 279:1cb4aeec8fc6
pi_setup code to prepare a pi for netboot
author | drewp@bigasterisk.com |
---|---|
date | Sun, 14 Apr 2024 20:54:35 -0700 |
parents | 705698800bfb |
children | 65e28d2e0cd8 |
rev | line source |
---|---|
5 | 1 import subprocess |
2 | |
3 from pyinfra import host | |
4 from pyinfra.facts.files import FindInFile | |
259 | 5 from pyinfra.operations import files, systemd |
6 | |
215
db8787bd800e
wireguard now uses ditto (and prime) as hubs for home/remote
drewp@bigasterisk.com
parents:
115
diff
changeset
|
7 import wireguard_pubkey |
5 | 8 |
9 # other options: | |
10 # https://www.reddit.com/r/WireGuard/comments/fkr240/shortest_path_between_peers/ | |
11 # https://github.com/k4yt3x/wireguard-mesh-configurator | |
12 # https://github.com/mawalu/wireguard-private-networking | |
13 # | |
14 | |
15 | |
215
db8787bd800e
wireguard now uses ditto (and prime) as hubs for home/remote
drewp@bigasterisk.com
parents:
115
diff
changeset
|
16 def peer_block(hostname, allowed_ips, endpoint=None, keepalive=None): |
89
2fddde57231b
no connman to surprisingly rewrite net configs
drewp@bigasterisk.com
parents:
76
diff
changeset
|
17 # if allowed_ips.startswith('10.5'): |
2fddde57231b
no connman to surprisingly rewrite net configs
drewp@bigasterisk.com
parents:
76
diff
changeset
|
18 # # k3s nets also need to travel over wg |
2fddde57231b
no connman to surprisingly rewrite net configs
drewp@bigasterisk.com
parents:
76
diff
changeset
|
19 # allowed_ips += ', 10.42.0.0/24, 10.43.0.0/24' |
2fddde57231b
no connman to surprisingly rewrite net configs
drewp@bigasterisk.com
parents:
76
diff
changeset
|
20 |
215
db8787bd800e
wireguard now uses ditto (and prime) as hubs for home/remote
drewp@bigasterisk.com
parents:
115
diff
changeset
|
21 public_key = wireguard_pubkey.pubkey[hostname] |
5 | 22 out = f'''\ |
23 | |
24 [Peer] | |
25 # {hostname} | |
26 PublicKey = {public_key} | |
27 AllowedIPs = {allowed_ips} | |
28 ''' | |
29 if endpoint is not None: | |
30 out += f'Endpoint = {endpoint}\n' | |
31 if keepalive is not None: | |
32 out += f'PersistentKeepalive = {keepalive}\n' | |
33 return out | |
34 | |
35 | |
36 for wireguard_interface in ['wg0', 'bogasterisk']: | |
37 if wireguard_interface == 'bogasterisk' and host.name != 'prime': | |
38 continue | |
39 | |
40 # note- this is specific to the wg0 setup. Other conf files don't use it. | |
259 | 41 wireguard_ip = host.host_data.get('wireguard_address') |
42 if wireguard_interface == 'wg0' and wireguard_ip is None: | |
43 continue | |
5 | 44 |
45 # new pi may fail with 'Unable to access interface: Protocol not supported'. reboot fixes. | |
46 | |
47 priv_key_lines = host.get_fact(FindInFile, path=f'/etc/wireguard/{wireguard_interface}.conf', pattern=r'PrivateKey.*') | |
48 if not priv_key_lines: | |
49 priv_key = subprocess.check_output(['wg', 'genkey']).strip().decode('ascii') | |
50 else: | |
51 priv_key = priv_key_lines[0].split(' = ')[1] | |
52 | |
53 pub_key = subprocess.check_output(['wg', 'pubkey'], input=priv_key.encode('ascii')).strip().decode('ascii') | |
115 | 54 # todo: if this was new, it should be added to a file of pubkeys that |
55 # peer_block can refer to. meanwhile, edit the template. | |
5 | 56 |
57 files.template( | |
12 | 58 src=f'templates/wireguard/{wireguard_interface}.conf.j2', |
5 | 59 dest=f'/etc/wireguard/{wireguard_interface}.conf', |
60 mode='600', | |
61 wireguard_ip=wireguard_ip, | |
62 priv_key=priv_key, | |
63 peer_block=peer_block, | |
64 ) | |
65 svc = f'wg-quick@{wireguard_interface}.service' | |
76
de387eae06cf
still trying to sequence dhcp->wireguard->dns startup
drewp@bigasterisk.com
parents:
71
diff
changeset
|
66 |
de387eae06cf
still trying to sequence dhcp->wireguard->dns startup
drewp@bigasterisk.com
parents:
71
diff
changeset
|
67 files.template(src='templates/wireguard/wg.service.j2', |
de387eae06cf
still trying to sequence dhcp->wireguard->dns startup
drewp@bigasterisk.com
parents:
71
diff
changeset
|
68 dest=f'/etc/systemd/system/{svc}', |
de387eae06cf
still trying to sequence dhcp->wireguard->dns startup
drewp@bigasterisk.com
parents:
71
diff
changeset
|
69 wireguard_interface=wireguard_interface) |
272 | 70 systemd.service(service=svc, enabled=True, restarted=True, daemon_reload=True) |
76
de387eae06cf
still trying to sequence dhcp->wireguard->dns startup
drewp@bigasterisk.com
parents:
71
diff
changeset
|
71 |
9 | 72 systemd.service(service=svc, daemon_reload=True, restarted=True, enabled=True) |
71 | 73 |
97
9b7d7ea79f16
stop trying separate dns on 10.5 net. just use names like 'bang5'
drewp@bigasterisk.com
parents:
89
diff
changeset
|
74 # if host.name == 'bang': |
9b7d7ea79f16
stop trying separate dns on 10.5 net. just use names like 'bang5'
drewp@bigasterisk.com
parents:
89
diff
changeset
|
75 # systemd.service(service=f'dnsmasq_10.5', enabled=True, restarted=True, daemon_reload=True) |