comparison net.py @ 22:301dad3cdb7b

WIP updating to use systemd networkd but it's not done or working, here
author drewp@bigasterisk.com
date Tue, 28 Dec 2021 22:09:43 -0800
parents 0c1496e11b8f
children d4fb38f13c79
comparison
equal deleted inserted replaced
21:948d9d72267d 22:301dad3cdb7b
1 from pyinfra import host 1 from pyinfra import host
2 from pyinfra.facts.hardware import Ipv4Addrs
3 from pyinfra.facts.server import LinuxDistribution
4 from pyinfra.operations import apt, files, server, systemd 2 from pyinfra.operations import apt, files, server, systemd
5 3
6 is_pi = host.get_fact(LinuxDistribution)['name'] in ['Debian', 'Raspbian GNU/Linux']
7 is_wifi = host.name in ['frontdoor', 'living', 'plus'] 4 is_wifi = host.name in ['frontdoor', 'living', 'plus']
8 5
9 if is_wifi: 6 if host.name in [
10 # todo: netplan could do this, below 7 'garage',
11 files.put(src="secrets/wpa_supplicant.conf", dest="/etc/wpa_supplicant/wpa_supplicant.conf") 8 'dash',
9 'slash',
10 'frontbed',
11 'prime',
12 ]:
13 # previous version
14 files.file(path='/etc/netplan/99-pyinfra-written.yaml', present=False)
12 15
13 files.template(src='templates/hosts.j2', dest='/etc/hosts') 16 for search_dir in [
17 # search path per `man systemd.network`:
18 # /lib/systemd/network # These OS files are ok.
19 '/usr/local/lib/systemd/network/', # Probably no such dir.
20 '/run/systemd/network/', # Previous netplan attempts dumped in here.
21 '/etc/systemd/network/', # I'm going to work in here.
22 ]:
23 files.sync(
24 src="files/empty_dir/",
25 dest=search_dir,
26 delete=True,
27 )
14 28
15 if host.name == 'prime': 29 addr = host.host_data['addr']
16 # prime should have gotten this through netplan, but i give up. 30 if addr.startswith('10.'):
17 # 31 net = addr[:4]
18 # Note the DNS list: this list is tried randomly, not in order, so we could have 32 gateway = net + '.0.1'
19 # some trouble with internal names 33 dns = gateway
20 files.template(src='templates/prime_resolved.conf.j2', dest='/etc/systemd/resolved.conf') 34 elif addr == '162.243.138.136':
21 else: 35 gateway = '162.243.138.1'
36 dns = '10.5.0.1 8.8.8.8 8.8.4.4'
37 else:
38 raise ValueError(addr)
39 files.template(src="templates/house.network.j2",
40 dest="/etc/systemd/network/99-house.network",
41 mac=host.host_data['mac'],
42 addr=addr,
43 gateway=gateway,
44 dns=dns)
45 systemd.service(service='systemd-networkd.service', running=True, restarted=True)
46
47 # you may have to rm this file first: https://github.com/Fizzadar/pyinfra/issues/719
48 files.link(path='/etc/resolv.conf', target='/run/systemd/resolve/resolv.conf', force=True)
22 files.template(src='templates/resolved.conf.j2', dest='/etc/systemd/resolved.conf') 49 files.template(src='templates/resolved.conf.j2', dest='/etc/systemd/resolved.conf')
23 systemd.service(service='systemd-resolved.service', running=True, restarted=True)
24 50
25 ns = '10.2.0.1' 51 if is_wifi:
26 if host.name == 'prime': 52 files.put(src="secrets/wpa_supplicant.conf", dest="/etc/wpa_supplicant/wpa_supplicant.conf")
27 ns = '8.8.8.8'
28 elif host.name in ['dash', 'slash']:
29 ns = '10.1.0.1'
30 files.template(src='templates/resolv.conf.j2', dest='/etc/resolv.conf', ns=ns)
31 53
32 if host.name in ['dash', 'slash', 'garage', 'frontbed', 'dot']: 54 files.template(src='templates/hosts.j2', dest='/etc/hosts')
33 # might need to upgrade pi systemd if there are errors in this part 55
34 apt.packages(packages=['netplan.io']) 56 systemd.service(service='systemd-resolved.service', running=True, restarted=True)
35 for bad in [ 57
36 '01-netcfg.yaml', 58 # ns = '10.2.0.1'
37 '01-network-manager-all.yaml', 59 # if host.name == 'prime':
38 '00-installer-config.yaml', 60 # ns = '8.8.8.8'
39 '99-ansible-written.yaml', 61 # elif host.name in ['slash']:
40 '99-dns.conf', 62 # ns = '10.1.0.1'
41 ]: 63 # files.template(src='templates/resolv.conf.j2', dest='/etc/resolv.conf', ns=ns)
42 files.file(path=f'/etc/netplan/{bad}', present=False)
43 addrs = host.get_fact(Ipv4Addrs)
44 ipv4Interface = host.host_data['interface']
45 ipv4Address = host.host_data['addr']
46 files.template(src='templates/netplan.yaml.j2',
47 dest='/etc/netplan/99-pyinfra-written.yaml',
48 ipv4Interface=ipv4Interface,
49 ipv4Address=ipv4Address)
50 server.shell(commands=['netplan apply'])
51 64
52 apt.packages(packages=['network-manager'], present=host.name in ['plus']) 65 apt.packages(packages=['network-manager'], present=host.name in ['plus'])
53 66
54 if host.name == 'bang': 67 if host.name == 'bang':
55 files.template(src='templates/bang_interfaces.j2', dest='/etc/network/interfaces', user='root', group='root', mode='644') 68 files.template(src='templates/bang_interfaces.j2', dest='/etc/network/interfaces', user='root', group='root', mode='644')