-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathtasks.py
176 lines (142 loc) · 5.36 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
import contextlib
import os
import subprocess
import sys
import time
from pathlib import Path
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_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)
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()
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 root@95.217.199.121"""
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 root@eve.i --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 root@eve.i --flake github:mic92/dotfiles#eve"""
eve_hostname = "root@95.217.199.121"
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 root@eve.i"""
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")