Mercurial > code > home > repos > infra
view system.py @ 303:9e15c07d5258
get telegraf running on pipe (non k8s)
author | drewp@bigasterisk.com |
---|---|
date | Wed, 07 Aug 2024 15:14:17 -0700 |
parents | 65e28d2e0cd8 |
children |
line wrap: on
line source
import os from io import StringIO from typing import cast import pyinfra from pyinfra import host from pyinfra.operations import apt, files, server, systemd 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(): # remove when we're on longhorn 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]: svc = f'web_forward_{port}' files.template(src="templates/webforward.service.j2", dest=f"/etc/systemd/system/{svc}.service", serv_host='bang', port=port, name='web', fam='tcp') systemd.service(service=svc, enabled=True, restarted=True) def minecraft_forward(): port = 25765 for fam in ['tcp', 'udp']: svc = f'mc_smp_{fam}_forward_{port}' files.template(src="templates/webforward.service.j2", dest=f"/etc/systemd/system/{svc}.service", serv_host='ditto', port=port, name='mc_smp', fam=fam) systemd.service(service=svc, enabled=True, restarted=True) def pigpiod(): files.put(src="files/pigpiod.service", dest="/etc/systemd/system/pigpiod.service") systemd.service(service='pigpiod', daemon_reload=True, enabled=True) def rpi_iscsi_volumes(): iscsi_dir = '/d2/rpi-iscsi' for pi_hostname in cast(list, pyinfra.inventory.get_group(name='pi')): out = f'{iscsi_dir}/{pi_hostname}.disk' files.directory(path=iscsi_dir) server.shell(commands=f'dd if=/dev/zero of={out} count=0 bs=1 seek=10G conv=excl || true') files.put(dest=f"/etc/tgt/conf.d/{pi_hostname}.conf", src=StringIO(f""" <target iqn.2024-03.com.bigasterisk:{pi_hostname}.target> backing-store {out} initiator-name iqn.2024-03.com.bigasterisk:{pi_hostname}.initiator </target> """)) # restarting is disruptive to connected pis, and they might need to be # visited: #systemd.service(service='tgt.service', running=True, restarted=True) server.hostname(hostname=host.name) timezone() fstab() if host.name == 'ditto': rpi_iscsi_volumes() if 'pi' not in host.groups: files.line(path='/etc/update-manager/release-upgrades', line="^Prompt=", replace="Prompt=normal") if 'pi' in host.groups: pi_tmpfs() if host.name in ['bang', 'pipe', 'ditto']: no_sleep() if host.name in ['bang', 'ditto']: nfs_server() if host.name in ['prime', 'ditto', 'pipe']: smaller_journals() if host.name == 'prime': web_forward() minecraft_forward() if 'pi' in host.groups: pigpiod() # 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