changeset 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 948d9d72267d
children 67fc0d4b1859
files net.py templates/netplan_new.yaml.j2 templates/resolved.conf.j2
diffstat 3 files changed, 100 insertions(+), 42 deletions(-) [+]
line wrap: on
line diff
--- a/net.py	Tue Dec 28 22:08:21 2021 -0800
+++ b/net.py	Tue Dec 28 22:09:43 2021 -0800
@@ -1,53 +1,66 @@
 from pyinfra import host
-from pyinfra.facts.hardware import Ipv4Addrs
-from pyinfra.facts.server import LinuxDistribution
 from pyinfra.operations import apt, files, server, systemd
 
-is_pi = host.get_fact(LinuxDistribution)['name'] in ['Debian', 'Raspbian GNU/Linux']
 is_wifi = host.name in ['frontdoor', 'living', 'plus']
 
-if is_wifi:
-    # todo: netplan could do this, below
-    files.put(src="secrets/wpa_supplicant.conf", dest="/etc/wpa_supplicant/wpa_supplicant.conf")
-
-files.template(src='templates/hosts.j2', dest='/etc/hosts')
+if host.name in [
+        'garage',
+        'dash',
+        'slash',
+        'frontbed',
+        'prime',
+]:
+    # previous version
+    files.file(path='/etc/netplan/99-pyinfra-written.yaml', present=False)
 
-if host.name == 'prime':
-    # prime should have gotten this through netplan, but i give up.
-    #
-    # Note the DNS list: this list is tried randomly, not in order, so we could have
-    # some trouble with internal names
-    files.template(src='templates/prime_resolved.conf.j2', dest='/etc/systemd/resolved.conf')
-else:
-    files.template(src='templates/resolved.conf.j2', dest='/etc/systemd/resolved.conf')
-systemd.service(service='systemd-resolved.service', running=True, restarted=True)
+    for search_dir in [
+            # search path per `man systemd.network`:
+            # /lib/systemd/network              # These OS files are ok.
+            '/usr/local/lib/systemd/network/',  # Probably no such dir.
+            '/run/systemd/network/',  # Previous netplan attempts dumped in here.
+            '/etc/systemd/network/',  # I'm going to work in here.
+    ]:
+        files.sync(
+            src="files/empty_dir/",
+            dest=search_dir,
+            delete=True,
+        )
 
-ns = '10.2.0.1'
-if host.name == 'prime':
-    ns = '8.8.8.8'
-elif host.name in ['dash', 'slash']:
-    ns = '10.1.0.1'
-files.template(src='templates/resolv.conf.j2', dest='/etc/resolv.conf', ns=ns)
+    addr = host.host_data['addr']
+    if addr.startswith('10.'):
+        net = addr[:4]
+        gateway = net + '.0.1'
+        dns = gateway
+    elif addr == '162.243.138.136':
+        gateway = '162.243.138.1'
+        dns = '10.5.0.1 8.8.8.8 8.8.4.4'
+    else:
+        raise ValueError(addr)
+    files.template(src="templates/house.network.j2",
+                   dest="/etc/systemd/network/99-house.network",
+                   mac=host.host_data['mac'],
+                   addr=addr,
+                   gateway=gateway,
+                   dns=dns)
+    systemd.service(service='systemd-networkd.service', running=True, restarted=True)
 
-if host.name in ['dash', 'slash', 'garage', 'frontbed', 'dot']:
-    # might need to upgrade pi systemd if there are errors in this part
-    apt.packages(packages=['netplan.io'])
-    for bad in [
-            '01-netcfg.yaml',
-            '01-network-manager-all.yaml',
-            '00-installer-config.yaml',
-            '99-ansible-written.yaml',
-            '99-dns.conf',
-            ]:
-        files.file(path=f'/etc/netplan/{bad}', present=False)
-    addrs = host.get_fact(Ipv4Addrs)
-    ipv4Interface = host.host_data['interface']
-    ipv4Address = host.host_data['addr']
-    files.template(src='templates/netplan.yaml.j2',
-                   dest='/etc/netplan/99-pyinfra-written.yaml',
-                   ipv4Interface=ipv4Interface,
-                   ipv4Address=ipv4Address)
-    server.shell(commands=['netplan apply'])
+    # you may have to rm this file first: https://github.com/Fizzadar/pyinfra/issues/719
+    files.link(path='/etc/resolv.conf', target='/run/systemd/resolve/resolv.conf', force=True)
+    files.template(src='templates/resolved.conf.j2', dest='/etc/systemd/resolved.conf')
+
+    if is_wifi:
+        files.put(src="secrets/wpa_supplicant.conf", dest="/etc/wpa_supplicant/wpa_supplicant.conf")
+
+    files.template(src='templates/hosts.j2', dest='/etc/hosts')
+
+    systemd.service(service='systemd-resolved.service', running=True, restarted=True)
+
+    # ns = '10.2.0.1'
+    # if host.name == 'prime':
+    #     ns = '8.8.8.8'
+    # elif host.name in ['slash']:
+    #     ns = '10.1.0.1'
+    # files.template(src='templates/resolv.conf.j2', dest='/etc/resolv.conf', ns=ns)
 
 apt.packages(packages=['network-manager'], present=host.name in ['plus'])
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/templates/netplan_new.yaml.j2	Tue Dec 28 22:09:43 2021 -0800
@@ -0,0 +1,36 @@
+# written by pyinfra
+
+network:
+  version: 2
+{% if host.name == 'garage' %}
+  ethernets:
+    match:
+      macaddress: b8:27:eb:81:17:92
+      link-local: [ ipv4 ]
+      addresses: ['10.2.0.14/32']
+      routes:
+      - to: 0.0.0.0/0
+        via: 10.2.0.1
+      nameservers:
+        search: ['bigasterisk.com']
+        addresses: 
+        - 10.2.0.1
+    wifi:
+      link-local: []
+{% endif %}
+
+
+{% if host.name == 'dash' %}
+  ethernets:
+    match:
+      macaddress: b8:97:5a:17:d7:1f
+      link-local: [ ipv4 ]
+      addresses: ['10.1.0.5/32']
+      routes:
+      - to: default
+        via: 10.1.0.1
+      nameservers:
+        search: ['bigasterisk.com']
+        addresses: 
+        - 10.1.0.1
+{% endif %}
\ No newline at end of file
--- a/templates/resolved.conf.j2	Tue Dec 28 22:08:21 2021 -0800
+++ b/templates/resolved.conf.j2	Tue Dec 28 22:09:43 2021 -0800
@@ -2,6 +2,14 @@
 
 # See resolved.conf(5) for details
 
+{% if host.name == 'prime' %}
+[Resolve]
+# This list is tried randomly, not in order, so we could have
+# some trouble with internal names
+DNS=10.5.0.1 8.8.8.8 8.8.4.4
+Domains=bigasterisk.com
+
+{% else %}
 [Resolve]
 DNS=10.2.0.1
 #FallbackDNS=
@@ -13,3 +21,4 @@
 #Cache=yes
 #DNSStubListener=yes
 #ReadEtcHosts=yes
+{% endif %}
\ No newline at end of file