diff system.py @ 289:65e28d2e0cd8

move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
author drewp@bigasterisk.com
date Sun, 21 Apr 2024 17:07:23 -0700
parents 3af02e24eaf9
children 9e15c07d5258
line wrap: on
line diff
--- a/system.py	Sun Apr 21 17:01:13 2024 -0700
+++ b/system.py	Sun Apr 21 17:07:23 2024 -0700
@@ -1,10 +1,11 @@
 import os
+from io import StringIO
+from typing import cast
 
+import pyinfra
 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'
 
 
@@ -18,6 +19,7 @@
     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',
@@ -57,6 +59,7 @@
                        fam='tcp')
         systemd.service(service=svc, enabled=True, restarted=True)
 
+
 def minecraft_forward():
     port = 25765
     for fam in ['tcp', 'udp']:
@@ -69,14 +72,41 @@
                        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 not is_pi:
+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 is_pi and host.name != 'pipe':
+if 'pi' in host.groups:
     pi_tmpfs()
 
 if host.name in ['bang', 'pipe', 'ditto']:
@@ -85,11 +115,16 @@
 if host.name in ['bang', 'ditto']:
     nfs_server()
 
+if host.name in ['prime', 'ditto']:
+    smaller_journals()
+
 if host.name == 'prime':
-    smaller_journals()
     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