view system.py @ 284:a46eb2a77df7

minecraft forwarding
author drewp@bigasterisk.com
date Mon, 15 Apr 2024 02:28:52 -0700
parents 4e424a144183
children 3af02e24eaf9
line wrap: on
line source

import os

from pyinfra import host
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']

TZ = 'America/Los_Angeles'


def timezone():
    files.link(path='/etc/localtime', target=f'/usr/share/zoneinfo/{TZ}')
    files.replace(path='/etc/timezone', text='.*', replace=TZ)


def fstab():
    fstab_file = f'files/fstab/{host.name}'
    if os.path.exists(fstab_file):
        files.put(src=fstab_file, dest='/etc/fstab')

def pi_tmpfs():
    for line in [
            'tmpfs /var/log tmpfs defaults,noatime,mode=0755 0 0',
            'tmpfs /tmp tmpfs defaults,noatime 0 0',
    ]:
        files.line(path="/etc/fstab", line=line, replace=line)

    # stop SD card corruption (along with some mounts in fstab)
    apt.packages(packages=['dphys-swapfile'], present=False)


def no_sleep():
    server.shell(commands=['systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target'])

def nfs_server():
    apt.packages(packages=['nfs-kernel-server'])
    files.template(src='templates/bang_exports.j2', dest='/etc/exports')

def smaller_journals():
    files.line(name='shorter systemctl log window, for disk space',
               path='/etc/systemd/journald.conf',
               line='MaxFileSec',
               replace="MaxFileSec=7day")

def web_forward():
    for port in [80, 443]:
        files.template(src="templates/webforward.service.j2", dest=f"/etc/systemd/system/web_forward_{port}.service", serv_host='bang', port=port, name='web', fam='tcp')
        systemd.service(service=f'web_forward_{port}', enabled=True, restarted=True)

def minecraft_forward():
    port = 25765
    for fam in ['tcp', 'udp']:
        files.template(src="templates/webforward.service.j2", dest=f"/etc/systemd/system/mc_smp_{fam}_forward_{port}.service", serv_host='ditto', port=port, name='mc_smp', fam=fam)
        systemd.service(service=f'mc_smp_{fam}_forward_{port}', enabled=True, restarted=True)

server.hostname(hostname=host.name)
timezone()
fstab()

if not is_pi:
    files.line(path='/etc/update-manager/release-upgrades', line="^Prompt=", replace="Prompt=normal")

if is_pi and host.name != 'pipe':
    pi_tmpfs()

    # this now lives on pipe in /opt/dnsmasq/tftp/f63f14b6/config.txt
    #files.template(src='templates/boot_config.txt.j2', dest='/boot/config.txt')

if host.name in ['bang', 'pipe', 'ditto']:
    no_sleep()

if host.name in ['bang', 'ditto']:
    nfs_server()

if host.name == 'prime':
    smaller_journals()
    web_forward()
    minecraft_forward()

# for space, consider:
# k3s crictl rmi --prune
# snap list --all | while read snapname ver rev trk pub notes; do if [[ $notes = *disabled* ]]; then snap remove "$snapname" --revision="$rev"; fi; done
# podman system reset