comparison 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
comparison
equal deleted inserted replaced
288:3af02e24eaf9 289:65e28d2e0cd8
1 import os 1 import os
2 from io import StringIO
3 from typing import cast
2 4
5 import pyinfra
3 from pyinfra import host 6 from pyinfra import host
4 from pyinfra.facts.server import LinuxDistribution
5 from pyinfra.operations import apt, files, server, systemd 7 from pyinfra.operations import apt, files, server, systemd
6 8
7 is_pi = host.get_fact(LinuxDistribution)['name'] in ['Debian', 'Raspbian GNU/Linux']
8 TZ = 'America/Los_Angeles' 9 TZ = 'America/Los_Angeles'
9 10
10 11
11 def timezone(): 12 def timezone():
12 files.link(path='/etc/localtime', target=f'/usr/share/zoneinfo/{TZ}') 13 files.link(path='/etc/localtime', target=f'/usr/share/zoneinfo/{TZ}')
15 16
16 def fstab(): 17 def fstab():
17 fstab_file = f'files/fstab/{host.name}' 18 fstab_file = f'files/fstab/{host.name}'
18 if os.path.exists(fstab_file): 19 if os.path.exists(fstab_file):
19 files.put(src=fstab_file, dest='/etc/fstab') 20 files.put(src=fstab_file, dest='/etc/fstab')
21
20 22
21 def pi_tmpfs(): 23 def pi_tmpfs():
22 for line in [ 24 for line in [
23 'tmpfs /var/log tmpfs defaults,noatime,mode=0755 0 0', 25 'tmpfs /var/log tmpfs defaults,noatime,mode=0755 0 0',
24 'tmpfs /tmp tmpfs defaults,noatime 0 0', 26 'tmpfs /tmp tmpfs defaults,noatime 0 0',
55 port=port, 57 port=port,
56 name='web', 58 name='web',
57 fam='tcp') 59 fam='tcp')
58 systemd.service(service=svc, enabled=True, restarted=True) 60 systemd.service(service=svc, enabled=True, restarted=True)
59 61
62
60 def minecraft_forward(): 63 def minecraft_forward():
61 port = 25765 64 port = 25765
62 for fam in ['tcp', 'udp']: 65 for fam in ['tcp', 'udp']:
63 svc = f'mc_smp_{fam}_forward_{port}' 66 svc = f'mc_smp_{fam}_forward_{port}'
64 files.template(src="templates/webforward.service.j2", 67 files.template(src="templates/webforward.service.j2",
67 port=port, 70 port=port,
68 name='mc_smp', 71 name='mc_smp',
69 fam=fam) 72 fam=fam)
70 systemd.service(service=svc, enabled=True, restarted=True) 73 systemd.service(service=svc, enabled=True, restarted=True)
71 74
75
76 def pigpiod():
77 files.put(src="files/pigpiod.service", dest="/etc/systemd/system/pigpiod.service")
78 systemd.service(service='pigpiod', daemon_reload=True, enabled=True)
79
80
81 def rpi_iscsi_volumes():
82 iscsi_dir = '/d2/rpi-iscsi'
83 for pi_hostname in cast(list, pyinfra.inventory.get_group(name='pi')):
84 out = f'{iscsi_dir}/{pi_hostname}.disk'
85 files.directory(path=iscsi_dir)
86 server.shell(commands=f'dd if=/dev/zero of={out} count=0 bs=1 seek=10G conv=excl || true')
87 files.put(dest=f"/etc/tgt/conf.d/{pi_hostname}.conf",
88 src=StringIO(f"""
89 <target iqn.2024-03.com.bigasterisk:{pi_hostname}.target>
90 backing-store {out}
91 initiator-name iqn.2024-03.com.bigasterisk:{pi_hostname}.initiator
92 </target>
93 """))
94 # restarting is disruptive to connected pis, and they might need to be
95 # visited:
96 #systemd.service(service='tgt.service', running=True, restarted=True)
97
98
72 server.hostname(hostname=host.name) 99 server.hostname(hostname=host.name)
73 timezone() 100 timezone()
74 fstab() 101 fstab()
75 102
76 if not is_pi: 103 if host.name == 'ditto':
104 rpi_iscsi_volumes()
105
106 if 'pi' not in host.groups:
77 files.line(path='/etc/update-manager/release-upgrades', line="^Prompt=", replace="Prompt=normal") 107 files.line(path='/etc/update-manager/release-upgrades', line="^Prompt=", replace="Prompt=normal")
78 108
79 if is_pi and host.name != 'pipe': 109 if 'pi' in host.groups:
80 pi_tmpfs() 110 pi_tmpfs()
81 111
82 if host.name in ['bang', 'pipe', 'ditto']: 112 if host.name in ['bang', 'pipe', 'ditto']:
83 no_sleep() 113 no_sleep()
84 114
85 if host.name in ['bang', 'ditto']: 115 if host.name in ['bang', 'ditto']:
86 nfs_server() 116 nfs_server()
87 117
118 if host.name in ['prime', 'ditto']:
119 smaller_journals()
120
88 if host.name == 'prime': 121 if host.name == 'prime':
89 smaller_journals()
90 web_forward() 122 web_forward()
91 minecraft_forward() 123 minecraft_forward()
124
125 if 'pi' in host.groups:
126 pigpiod()
92 127
93 # for space, consider: 128 # for space, consider:
94 # k3s crictl rmi --prune 129 # k3s crictl rmi --prune
95 # snap list --all | while read snapname ver rev trk pub notes; do if [[ $notes = *disabled* ]]; then snap remove "$snapname" --revision="$rev"; fi; done 130 # snap list --all | while read snapname ver rev trk pub notes; do if [[ $notes = *disabled* ]]; then snap remove "$snapname" --revision="$rev"; fi; done
96 # podman system reset 131 # podman system reset