view net.py @ 320:11d3bcedb9f0

updates for tofu rebuild; some dead code; start moving tasks into subdirs with their files and templates
author drewp@bigasterisk.com
date Fri, 08 Nov 2024 23:16:56 -0800
parents 65e28d2e0cd8
children
line wrap: on
line source

from pyinfra import host
from pyinfra.operations import apt, files, server, systemd


def cleanup():
    # past attempts
    files.file(path='/etc/network/interfaces', present=False)

    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,
        )

    # On pipe:
    #   Now using a HW router for this firewall. No incoming connections.
    #   test connections from the outside:
    #   http://www.t1shopper.com/tools/port-scanner/
    # On prime:
    #   using digitalocean network config:
    #   https://cloud.digitalocean.com/networking/firewalls/f68899ae-1aac-4469-b379-59ce2bbc988f/droplets?i=7c5072
    apt.packages(packages=['ufw'], present=False)


def iptables_version():
    # https://github.com/k3s-io/k3s/issues/1812 unclear, but more importantly, this has to be set
    # on pipe in a way that works with the commands in house_net.service (and net_routes)
    server.shell(commands=[
        'update-alternatives --set iptables /usr/sbin/iptables-legacy',
        'update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy',
    ])
    # needs reboot if this changed


iptables_version()
server.sysctl(key='net.ipv6.conf.all.disable_ipv6', value=1, persist=True)

if host.name == 'prime':
    cleanup()

    files.template(
        src="files/net/prime.network",
        dest="/etc/systemd/network/99-prime.network",
    )
    systemd.service(service='systemd-networkd.service', enabled=True, running=True, restarted=True)

if host.name == 'pipe':
    cleanup()

    files.template(src="files/net/pipe_10.2.network", dest="/etc/systemd/network/99-10.2.network")
    files.template(src="files/net/pipe_isp.network", dest="/etc/systemd/network/99-isp.network")
    server.sysctl(key='net.ipv4.ip_forward', value=1, persist=True)
    files.template(src="files/net/house_net.service", dest="/etc/systemd/system/house_net.service", out_interface='eth0')
    systemd.service(service='house_net.service', daemon_reload=True, enabled=True, running=True, restarted=True)
    systemd.service(service='systemd-networkd.service', enabled=True, running=True, restarted=True)

if host.name == 'ditto':
    files.template(
        src="files/net/ditto-netplan.yaml",
        dest="/etc/netplan/00-installer-config.yaml",
        create_remote_dir=True,
    )

    systemd.service(service='systemd-networkd.service', enabled=True, running=True, restarted=True)