Mercurial > code > home > repos > infra
annotate system.py @ 3:61945df2a392
updates to work on recent raspbian installs
author | drewp@bigasterisk.com |
---|---|
date | Sun, 07 Nov 2021 15:42:37 -0800 |
parents | 7f7af7e2ba8d |
children | aa633eb49c63 |
rev | line source |
---|---|
1 | 1 import os |
2 from pyinfra import host | |
3 from pyinfra.operations import server, files, apt, ssh, systemd | |
4 from pyinfra.facts.server import LinuxDistribution | |
5 | |
3
61945df2a392
updates to work on recent raspbian installs
drewp@bigasterisk.com
parents:
2
diff
changeset
|
6 bang_is_old = True # remove after upgrade |
61945df2a392
updates to work on recent raspbian installs
drewp@bigasterisk.com
parents:
2
diff
changeset
|
7 is_pi = host.get_fact(LinuxDistribution)['name'] in ['Debian', 'Raspbian GNU/Linux'] |
1 | 8 is_wifi_pi = host.name in ['frontdoor', 'living'] |
9 | |
10 TZ = 'America/Los_Angeles' | |
11 | |
12 # | |
13 # system | |
14 # | |
15 | |
16 server.hostname(hostname=host.name) | |
17 files.link(path='/etc/localtime', target=f'/usr/share/zoneinfo/{TZ}') | |
18 files.replace(path='/etc/timezone', match='.*', replace=TZ) | |
19 apt.packages(update=True, | |
20 cache_time=86400, | |
21 packages=['tzdata'], | |
22 force=True, | |
23 env={ | |
24 'TZ': TZ, | |
25 'LANG': 'en_US.UTF-8', | |
26 'DEBIAN_FRONTEND': 'noninteractive' | |
27 }) | |
28 | |
29 # | |
30 # fstab | |
31 # | |
32 | |
33 fstab_file = f'files/{host.name}_fstab' | |
34 if os.path.exists(fstab_file): | |
35 files.put(src=fstab_file, dest='/etc/fstab') | |
36 if is_pi: | |
37 for line in [ | |
38 'tmpfs /var/log tmpfs defaults,noatime,mode=0755 0 0', | |
39 'tmpfs /tmp tmpfs defaults,noatime 0 0', | |
40 ]: | |
41 files.line(path="/etc/fstab", line=line, replace=line) | |
42 | |
43 # stop SD card corruption (along with some mounts in fstab) | |
44 apt.packages(packages=['dphys-swapfile'], present=False) | |
45 | |
46 # | |
47 # pkgs | |
48 # | |
49 | |
50 if not is_pi: | |
51 apt.key(keyserver='keyserver.ubuntu.com', keyid='8B48AD6246925553') | |
52 | |
53 if is_pi: | |
54 apt.packages(packages=['dirmngr', 'gnupg2', 'apt-utils']) | |
55 files.template(src='templates/pi_sources.list.j2', dest='/etc/apt/sources.list', rel='buster') | |
56 | |
57 apt.key(src='https://ftp-master.debian.org/keys/archive-key-8.asc') | |
58 apt.key(src='https://ftp-master.debian.org/keys/archive-key-8-security.asc') | |
59 apt.key(src='https://ftp-master.debian.org/keys/archive-key-9-security.asc') | |
60 | |
61 files.file(path='/etc/apt/sources.list.d/raspi.list', present=False) | |
62 | |
63 if is_wifi_pi: | |
64 files.put(dest="/etc/network/interfaces.d/wlan0", src="files/pi_wlan0_powersave") | |
65 ssh.command(host.name, "iw wlan0 set power_save off") | |
66 | |
67 # see https://www.raspberrypi.org/documentation/configuration/config-txt/memory.md#:~:text=txt-,gpu_mem,-Specifies | |
68 # to port to pyinfra | |
69 #- name: unused display; give ram to OS | |
70 # lineinfile: dest=/boot/config.txt line="gpu_mem=16" regexp="^gpu_mem=" | |
71 # when: "'with_x11' not in group_names" | |
72 | |
73 # for beacon | |
74 #enable_uart=1 | |
75 #dtoverlay=pi3-miniuart-bt | |
76 #core_freq=250 | |
77 | |
78 # for tiny_screen | |
79 #to port to pyinfra | |
80 #- lineinfile: dest=/boot/config.txt line="dtparam=spi=on" regexp="^dtparam=spi=" | |
81 | |
82 # i hope this is deletable | |
83 # downgrade strictness so I can install from https://archive.raspberrypi.org/ | |
84 # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=907788 | |
85 #- lineinfile: dest=/etc/ssl/openssl.cnf line="#CipherString = DEFAULT@SECLEVEL=2" regexp="CipherString ?=" | |
86 | |
87 # may be fixed in k3s, not sure | |
88 # raspbian defaults to `iptables -V` -> iptables v1.8.4 (nf_tables), which won't work with k3s | |
89 # - command: update-alternatives --set iptables /usr/sbin/iptables-legacy | |
90 if not is_pi: | |
91 apt.key(src='https://dl.google.com/linux/linux_signing_key.pub') | |
92 apt.repo(src='deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main') | |
93 | |
94 apt.key(src='https://packages.microsoft.com/keys/microsoft.asc') | |
95 apt.repo(src="deb [arch=amd64,arm64,armhf] http://packages.microsoft.com/repos/code stable main") | |
96 | |
97 apt.ppa(src="ppa:savoury1/blender") | |
98 | |
99 apt.key(keyserver='keyserver.ubuntu.com', keyid='F24AEA9FB05498B7') | |
100 apt.repo(src="deb [arch=amd64,i386] https://repo.steampowered.com/steam/ stable steam") | |
101 | |
3
61945df2a392
updates to work on recent raspbian installs
drewp@bigasterisk.com
parents:
2
diff
changeset
|
102 if False and is_pi: |
1 | 103 apt.key(src="https://download.docker.com/linux/raspbian/gpg") |
104 apt.repo(src="deb [arch=armhf] https://download.docker.com/linux/raspbian stretch stable") | |
105 apt.repo(src='deb http://deb.debian.org/debian/ unstable main') # maybe for WG | |
106 | |
107 apt.packages(packages=[ | |
108 'build-essential', | |
3
61945df2a392
updates to work on recent raspbian installs
drewp@bigasterisk.com
parents:
2
diff
changeset
|
109 # 'i2c-tools', |
1 | 110 'rsync', |
111 ]) | |
112 | |
2 | 113 if not is_pi: |
3
61945df2a392
updates to work on recent raspbian installs
drewp@bigasterisk.com
parents:
2
diff
changeset
|
114 apt.packages(packages=[ |
61945df2a392
updates to work on recent raspbian installs
drewp@bigasterisk.com
parents:
2
diff
changeset
|
115 'keychain', |
61945df2a392
updates to work on recent raspbian installs
drewp@bigasterisk.com
parents:
2
diff
changeset
|
116 'python3-docker', |
61945df2a392
updates to work on recent raspbian installs
drewp@bigasterisk.com
parents:
2
diff
changeset
|
117 'python3-invoke', |
61945df2a392
updates to work on recent raspbian installs
drewp@bigasterisk.com
parents:
2
diff
changeset
|
118 'python3-pip', |
61945df2a392
updates to work on recent raspbian installs
drewp@bigasterisk.com
parents:
2
diff
changeset
|
119 'python3-virtualenv', |
61945df2a392
updates to work on recent raspbian installs
drewp@bigasterisk.com
parents:
2
diff
changeset
|
120 'sysstat', |
61945df2a392
updates to work on recent raspbian installs
drewp@bigasterisk.com
parents:
2
diff
changeset
|
121 ]) |
61945df2a392
updates to work on recent raspbian installs
drewp@bigasterisk.com
parents:
2
diff
changeset
|
122 |
61945df2a392
updates to work on recent raspbian installs
drewp@bigasterisk.com
parents:
2
diff
changeset
|
123 if not is_pi and not bang_is_old: |
2 | 124 apt.packages(packages='mlocate', present=False) |
125 apt.packages(packages='plocate') | |
126 | |
1 | 127 # |
128 # ssh | |
129 # | |
130 | |
131 systemd.service( | |
132 service='ssh', | |
133 running=True, | |
134 enabled=True, | |
135 ) | |
136 | |
137 files.line(path='/etc/ssh/ssh_config', line="HashKnownHosts", replace="HashKnownHosts no") | |
138 | |
139 if is_pi: | |
140 auth_keys = '/home/pi/.ssh/authorized_keys' | |
141 files.file(path=auth_keys, user='pi', group='pi', mode=600) | |
142 for pubkey in [ | |
143 'ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBNlR7hereUHqw/RHQau0F7+vQZKAxduM+SD4R76FhC+4Zi078Pv04ZLe9qdM/NBlB/grLGhG58vaGmnWPpJ3QJs= drewp@plus', | |
144 'ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBOR+iV8Qm/rAfmq0epXYfnp5ZTfBl8eidFzw1GmyZ3fPUFAshWn839fQ5DPj9xDPtMy9kTtrB5bK1SnynFzDqzQ= drewp@bang', | |
145 ]: | |
146 files.line(path=auth_keys, line=pubkey, replace=pubkey) | |
147 | |
148 # | |
149 # docker (delete this?) | |
150 # | |
151 | |
3
61945df2a392
updates to work on recent raspbian installs
drewp@bigasterisk.com
parents:
2
diff
changeset
|
152 # don't try to get aufs-dkms on rpi-- https://github.com/docker/for-linux/issues/709 |
61945df2a392
updates to work on recent raspbian installs
drewp@bigasterisk.com
parents:
2
diff
changeset
|
153 if not is_pi: |
61945df2a392
updates to work on recent raspbian installs
drewp@bigasterisk.com
parents:
2
diff
changeset
|
154 apt.packages(packages=['docker.io'], no_recommends=True) |
61945df2a392
updates to work on recent raspbian installs
drewp@bigasterisk.com
parents:
2
diff
changeset
|
155 files.put(src='files/docker_daemon.json', dest='/etc/docker/daemon.json') |
61945df2a392
updates to work on recent raspbian installs
drewp@bigasterisk.com
parents:
2
diff
changeset
|
156 systemd.service(service='docker', running=True, enabled=True, restarted=True) |
1 | 157 |
158 if not is_pi: | |
159 files.line(path='/etc/update-manager/release-upgrades', line="^Prompt=", replace="Prompt=normal") | |
160 | |
161 files.line(path='/etc/ssh/sshd_config', line="^UseDNS\b", replace="UseDNS no") | |
162 systemd.service(service='sshd', reloaded=True) | |
163 | |
164 # | |
165 # special hosts | |
166 # | |
167 | |
168 if host.name == "bang": | |
169 apt.packages(packages=[ | |
170 'libzfs2linux', | |
171 'zfsutils-linux', | |
172 'zfs-zed', | |
173 'zfs-auto-snapshot', | |
174 ]) | |
175 | |
176 # This is usable on pi where we don't care when they reboot: | |
177 #- name: apt_upgrade | |
178 # apt: upgrade=full | |
179 #- name: Check if a reboot is required | |
180 # register: file | |
181 # stat: path=/var/run/reboot-required get_md5=no | |
182 #- name: Reboot the server | |
183 # command: /sbin/reboot | |
184 # when: file.stat.exists == true |