Mercurial > code > home > repos > infra
annotate 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 |
rev | line source |
---|---|
1 | 1 import os |
289
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
2 from io import StringIO |
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
3 from typing import cast |
10
1fec9fe18a4e
more system.py cleanup; add pi /boot/config.txt
drewp@bigasterisk.com
parents:
6
diff
changeset
|
4 |
289
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
5 import pyinfra |
1 | 6 from pyinfra import host |
12
15c5ce7c74b5
refactor, cleanup, split large deploys
drewp@bigasterisk.com
parents:
10
diff
changeset
|
7 from pyinfra.operations import apt, files, server, systemd |
1 | 8 |
9 TZ = 'America/Los_Angeles' | |
10 | |
12
15c5ce7c74b5
refactor, cleanup, split large deploys
drewp@bigasterisk.com
parents:
10
diff
changeset
|
11 |
91 | 12 def timezone(): |
13 files.link(path='/etc/localtime', target=f'/usr/share/zoneinfo/{TZ}') | |
14 files.replace(path='/etc/timezone', text='.*', replace=TZ) | |
138 | 15 |
1 | 16 |
91 | 17 def fstab(): |
18 fstab_file = f'files/fstab/{host.name}' | |
19 if os.path.exists(fstab_file): | |
20 files.put(src=fstab_file, dest='/etc/fstab') | |
1 | 21 |
289
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
22 |
91 | 23 def pi_tmpfs(): |
1 | 24 for line in [ |
25 'tmpfs /var/log tmpfs defaults,noatime,mode=0755 0 0', | |
26 'tmpfs /tmp tmpfs defaults,noatime 0 0', | |
27 ]: | |
28 files.line(path="/etc/fstab", line=line, replace=line) | |
29 | |
30 # stop SD card corruption (along with some mounts in fstab) | |
31 apt.packages(packages=['dphys-swapfile'], present=False) | |
32 | |
33 | |
91 | 34 def no_sleep(): |
34
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
12
diff
changeset
|
35 server.shell(commands=['systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target']) |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
12
diff
changeset
|
36 |
288 | 37 |
91 | 38 def nfs_server(): |
288 | 39 # remove when we're on longhorn |
34
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
12
diff
changeset
|
40 apt.packages(packages=['nfs-kernel-server']) |
d4fb38f13c79
refactor dns and some other non-net setup
drewp@bigasterisk.com
parents:
12
diff
changeset
|
41 files.template(src='templates/bang_exports.j2', dest='/etc/exports') |
37
fbd0849dfdbd
redo networking to be much simpler. Uses systemd-networkd
drewp@bigasterisk.com
parents:
34
diff
changeset
|
42 |
288 | 43 |
91 | 44 def smaller_journals(): |
37
fbd0849dfdbd
redo networking to be much simpler. Uses systemd-networkd
drewp@bigasterisk.com
parents:
34
diff
changeset
|
45 files.line(name='shorter systemctl log window, for disk space', |
fbd0849dfdbd
redo networking to be much simpler. Uses systemd-networkd
drewp@bigasterisk.com
parents:
34
diff
changeset
|
46 path='/etc/systemd/journald.conf', |
fbd0849dfdbd
redo networking to be much simpler. Uses systemd-networkd
drewp@bigasterisk.com
parents:
34
diff
changeset
|
47 line='MaxFileSec', |
fbd0849dfdbd
redo networking to be much simpler. Uses systemd-networkd
drewp@bigasterisk.com
parents:
34
diff
changeset
|
48 replace="MaxFileSec=7day") |
fbd0849dfdbd
redo networking to be much simpler. Uses systemd-networkd
drewp@bigasterisk.com
parents:
34
diff
changeset
|
49 |
288 | 50 |
203 | 51 def web_forward(): |
37
fbd0849dfdbd
redo networking to be much simpler. Uses systemd-networkd
drewp@bigasterisk.com
parents:
34
diff
changeset
|
52 for port in [80, 443]: |
288 | 53 svc = f'web_forward_{port}' |
54 files.template(src="templates/webforward.service.j2", | |
55 dest=f"/etc/systemd/system/{svc}.service", | |
56 serv_host='bang', | |
57 port=port, | |
58 name='web', | |
59 fam='tcp') | |
60 systemd.service(service=svc, enabled=True, restarted=True) | |
91 | 61 |
289
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
62 |
284 | 63 def minecraft_forward(): |
64 port = 25765 | |
65 for fam in ['tcp', 'udp']: | |
288 | 66 svc = f'mc_smp_{fam}_forward_{port}' |
67 files.template(src="templates/webforward.service.j2", | |
68 dest=f"/etc/systemd/system/{svc}.service", | |
69 serv_host='ditto', | |
70 port=port, | |
71 name='mc_smp', | |
72 fam=fam) | |
73 systemd.service(service=svc, enabled=True, restarted=True) | |
284 | 74 |
289
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
75 |
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
76 def pigpiod(): |
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
77 files.put(src="files/pigpiod.service", dest="/etc/systemd/system/pigpiod.service") |
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
78 systemd.service(service='pigpiod', daemon_reload=True, enabled=True) |
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
79 |
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
80 |
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
81 def rpi_iscsi_volumes(): |
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
82 iscsi_dir = '/d2/rpi-iscsi' |
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
83 for pi_hostname in cast(list, pyinfra.inventory.get_group(name='pi')): |
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
84 out = f'{iscsi_dir}/{pi_hostname}.disk' |
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
85 files.directory(path=iscsi_dir) |
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
86 server.shell(commands=f'dd if=/dev/zero of={out} count=0 bs=1 seek=10G conv=excl || true') |
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
87 files.put(dest=f"/etc/tgt/conf.d/{pi_hostname}.conf", |
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
88 src=StringIO(f""" |
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
89 <target iqn.2024-03.com.bigasterisk:{pi_hostname}.target> |
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
90 backing-store {out} |
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
91 initiator-name iqn.2024-03.com.bigasterisk:{pi_hostname}.initiator |
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
92 </target> |
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
93 """)) |
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
94 # restarting is disruptive to connected pis, and they might need to be |
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
95 # visited: |
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
96 #systemd.service(service='tgt.service', running=True, restarted=True) |
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
97 |
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
98 |
145 | 99 server.hostname(hostname=host.name) |
91 | 100 timezone() |
101 fstab() | |
102 | |
289
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
103 if host.name == 'ditto': |
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
104 rpi_iscsi_volumes() |
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
105 |
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
106 if 'pi' not in host.groups: |
91 | 107 files.line(path='/etc/update-manager/release-upgrades', line="^Prompt=", replace="Prompt=normal") |
108 | |
289
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
109 if 'pi' in host.groups: |
91 | 110 pi_tmpfs() |
278 | 111 |
146 | 112 if host.name in ['bang', 'pipe', 'ditto']: |
91 | 113 no_sleep() |
114 | |
194
0d37dde619d0
zfs now serving from ditto, new pool is stor7
drewp@bigasterisk.com
parents:
155
diff
changeset
|
115 if host.name in ['bang', 'ditto']: |
91 | 116 nfs_server() |
117 | |
289
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
118 if host.name in ['prime', 'ditto']: |
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
119 smaller_journals() |
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
120 |
91 | 121 if host.name == 'prime': |
247 | 122 web_forward() |
284 | 123 minecraft_forward() |
247 | 124 |
289
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
125 if 'pi' in host.groups: |
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
126 pigpiod() |
65e28d2e0cd8
move static templates to files/ ; use inventory tags for selecting hosts+features ; other refactors
drewp@bigasterisk.com
parents:
288
diff
changeset
|
127 |
247 | 128 # for space, consider: |
129 # k3s crictl rmi --prune | |
130 # snap list --all | while read snapname ver rev trk pub notes; do if [[ $notes = *disabled* ]]; then snap remove "$snapname" --revision="$rev"; fi; done | |
131 # podman system reset |