-
Notifications
You must be signed in to change notification settings - Fork 39
/
tasks.py
256 lines (208 loc) · 7.41 KB
/
tasks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import contextlib
import os
import socket
import subprocess
import sys
import time
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import Any
from deploykit import DeployGroup, DeployHost, HostKeyCheck, parse_hosts
from invoke import task
ROOT = Path(__file__).parent.resolve()
os.chdir(ROOT)
@task
def decrypt_eve(_c: Any) -> None:
"""Decrypt secrets"""
eve_initrd = DeployHost("95.217.199.121", user="root", port=2222)
pw = subprocess.run(
["rbw", "get", "zfs encryption"],
text=True,
check=True,
stdout=subprocess.PIPE,
).stdout.strip()
# ssh may timeout, so we try multiple times
for _ in range(3):
with contextlib.suppress(subprocess.CalledProcessError):
eve_initrd.run("true")
eve_initrd.run(f'echo "{pw}" | systemd-tty-ask-password-agent')
@task
def reboot_and_decrypt_eve(c: Any) -> None:
"""Reboot hosts and decrypt secrets"""
eve = DeployHost("95.217.199.121", user="root")
eve.run("reboot &")
wait_for_reboot(eve)
decrypt_eve(c)
@task
def deploy(c: Any, _hosts: str = "") -> None:
"""Deploy to eve, eva, blob64 and localhost"""
c.run("clan machines update")
def try_local(host: str) -> str:
try:
socket.gethostbyname(f"{host}.lan")
except OSError:
return f"{host}.r"
else:
return f"{host}.lan"
@task
def deploy_bernie(c: Any) -> None:
"""Deploy to bernie"""
c.run("clan machines update bernie")
@task
def deploy_matchbox(c: Any) -> None:
"""Deploy to matchbox"""
c.run("clan machines update matchbox")
@task
def deploy_dotfiles(c: Any) -> None:
"""Deploy to dotfiles"""
hosts = [
DeployHost("localhost", meta=dict(flake_attr="desktop")),
DeployHost("eve.r", meta=dict(flake_attr="eve")),
]
g = DeployGroup(hosts)
def deploy_homemanager(host: DeployHost) -> None:
host.run(
f"""sudo -u joerg zsh <<'EOF'
cd $HOME
source $HOME/.zshrc
homeshick pull
homeshick symlink
homeshick cd dotfiles
nix build --out-link $HOME/.hm-activate ".#hmConfigurations.{host.meta["flake_attr"]}.activation-script"
$HOME/.hm-activate/activate
EOF""",
)
g.run_function(deploy_homemanager)
@task
def install_machine(c: Any, flake_attr: str, hostname: str) -> None:
"""Install nixos on a machine"""
ask = input(f"Install {hostname} with {flake_attr}? [y/N] ")
if ask != "y":
return
with TemporaryDirectory() as d:
root = Path(d) / "root"
root.mkdir(parents=True, exist_ok=True)
root.chmod(0o755)
host_key = root / "etc/ssh/ssh_host_ed25519_key"
host_key.parent.mkdir(parents=True, exist_ok=True)
subprocess.run(
[
"sops",
"--extract",
'["ssh_host_ed25519_key"]',
"-d",
f"nixos/{flake_attr}/secrets/secrets.yaml",
],
check=True,
stdout=host_key.open("w"),
)
c.run(
f"nix run github:numtide/nixos-anywhere -- {hostname} --debug --flake .#{flake_attr} --extra-files {root}",
echo=True,
)
def wait_for_host(h: DeployHost, shutdown: bool) -> None:
while True:
res = subprocess.run(
["ping", "-q", "-c", "1", "-w", "2", h.host],
stdout=subprocess.DEVNULL,
check=False,
)
if (shutdown and res.returncode == 1) or (not shutdown and res.returncode == 0):
break
time.sleep(1)
sys.stdout.write(".")
sys.stdout.flush()
@task
def generate_facter_json(
c: Any, hosts: str, facter: str = "github:numtide/nixos-facter"
) -> None:
"""
Deploy to servers
"""
def deploy(h: DeployHost) -> None:
ret = h.run(
["nix", "run", "--refresh", facter],
stdout=subprocess.PIPE,
)
name = h.host.split(".")[0]
path = ROOT / "machines" / name / "facter.json"
path.write_text(ret.stdout)
host_list = hosts.split(",")
g = DeployGroup([DeployHost(h, user="root") for h in host_list])
g.run_function(deploy)
def wait_for_reboot(h: DeployHost) -> None:
print(f"Wait for {h.host} to shutdown", end="")
sys.stdout.flush()
wait_for_host(h, shutdown=True)
print()
print(f"Wait for {h.host} to start", end="")
sys.stdout.flush()
wait_for_host(h, shutdown=True)
print()
@task
def add_github_user(c: Any, hosts: str = "", github_user: str = "Mic92") -> None:
def add_user(h: DeployHost) -> None:
h.run("mkdir -m700 /root/.ssh")
out = h.run_local(
f"curl https://github.com/{github_user}.keys",
stdout=subprocess.PIPE,
)
h.run(
f"echo '{out.stdout}' >> /root/.ssh/authorized_keys && chmod 700 /root/.ssh/authorized_keys",
)
g = parse_hosts(hosts, host_key_check=HostKeyCheck.NONE)
g.run_function(add_user)
@task
def reboot(c: Any, hosts: str) -> None:
"""Reboot hosts. example usage: fab --hosts clara.r,donna.r reboot"""
deploy_hosts = [DeployHost(h) for h in hosts.split(",")]
for h in deploy_hosts:
g = DeployGroup([h])
g.run("reboot &")
wait_for_reboot(h)
@task
def kexec_installer(c: Any, hosts: str) -> None:
"""Kexec into nixos installer, i.e. inv kexec-installer --hosts [email protected]"""
def do_kexec(h: DeployHost) -> None:
h.run(
"curl -L https://github.com/nix-community/nixos-images/releases/download/nixos-unstable/nixos-kexec-installer-noninteractive-x86_64-linux.tar.gz | tar -xzf- -C /root",
)
h.run("/root/kexec/run")
wait_for_reboot(h)
g = parse_hosts(hosts, host_key_check=HostKeyCheck.NONE)
g.run_function(do_kexec)
@task
def disko_mount_from_recovery(c: Any, host: str, flake: str) -> None:
"""Mount the system disk from a recovery system, i.e. inv disko-mount-from-recovery --host [email protected] --flake github:mic92/dotfiles#eve"""
h = DeployHost(host)
h.run(
f"""nix --extra-experimental-features "nix-command flakes" shell nixpkgs#git -c nix run --extra-experimental-features "nix-command flakes" github:nix-community/disko -- --flake {flake} --mode mount""",
)
@task
def boot_eve_into_recovery(c: Any) -> None:
"""Mount the system disk from a recovery system, i.e. inv disko-mount-from-recovery --host [email protected] --flake github:mic92/dotfiles#eve"""
eve_hostname = "[email protected]"
host = parse_hosts(eve_hostname).hosts[0]
kexec_installer(c, hosts=eve_hostname)
pw = subprocess.run(
["rbw", "get", "zfs encryption"],
text=True,
check=True,
stdout=subprocess.PIPE,
).stdout.strip()
# FIXME: disko does not support interactive zfs load-key:
host.run(f'zpool import -a; echo "{pw}" | zfs load-key -a')
disko_mount_from_recovery(c, host=eve_hostname, flake="github:mic92/dotfiles#eve")
@task
def unmount_from_recovery(c: Any, hosts: str, flake: str) -> None:
"""Unmount the system disk from a recovery system, i.e. inv unmount-from-recovery --host [email protected]"""
g = DeployGroup([DeployHost(h) for h in hosts.split(",")])
g.run("umount -R /mnt")
g.run("zpool export -a")
@task
def cleanup_gcroots(c: Any, hosts: str) -> None:
deploy_hosts = [DeployHost(h) for h in hosts.split(",")]
for h in deploy_hosts:
g = DeployGroup([h])
g.run("find /nix/var/nix/gcroots/auto -type s -delete")
g.run("systemctl restart nix-gc")