-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxmox-nixos-update.nix
280 lines (253 loc) · 9.34 KB
/
proxmox-nixos-update.nix
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
{
pkgs,
config,
proxmox-nixos-update,
...
}:
let
proxmox-nixos-update-bin = "${proxmox-nixos-update}/bin/nixpkgs-update";
proxmoxNixOSUpdateSystemDependencies = with pkgs; [
nix # for nix-shell used by python packges to update fetchers
git # used by update-scripts
openssh # used by git
gnugrep
gnused
curl
getent # used by hub
cachix
apacheHttpd # for rotatelogs, used by worker script
socat # used by worker script
python3
perl
];
mkWorker = name: {
after = [
"network-online.target"
"proxmox-nixos-update-supervisor.service"
];
wants = [ "network-online.target" ];
wantedBy = [ "multi-user.target" ];
description = "proxmox-nixos-update ${name} service";
enable = true;
restartIfChanged = true;
path = proxmoxNixOSUpdateSystemDependencies;
environment.XDG_CONFIG_HOME = "/var/lib/proxmox-nixos-update/worker";
environment.XDG_CACHE_HOME = "/var/cache/proxmox-nixos-update/worker";
environment.XDG_RUNTIME_DIR = "/run/proxmox-nixos-update"; # for nix-update update scripts
environment.NIX_PATH = "nixpkgs=${pkgs.path}";
serviceConfig = {
Type = "simple";
User = "proxmox-nixos-update";
Group = "proxmox-nixos-update";
Restart = "on-failure";
RestartSec = "5s";
WorkingDirectory = "/var/lib/proxmox-nixos-update/worker";
StateDirectory = "proxmox-nixos-update/worker";
StateDirectoryMode = "700";
CacheDirectory = "proxmox-nixos-update/worker";
CacheDirectoryMode = "700";
LogsDirectory = "proxmox-nixos-update/";
LogsDirectoryMode = "755";
RuntimeDirectory = "proxmox-nixos-update-worker";
RuntimeDirectoryMode = "700";
StandardOutput = "journal";
};
script = ''
mkdir -p "$LOGS_DIRECTORY/~workers/"
# This is for public logs at proxmox-nixos-update-logs.nix-community.org/~workers
exec > >(rotatelogs -eD "$LOGS_DIRECTORY"'/~workers/%Y-%m-%d-${name}.stdout.log' 86400)
exec 2> >(rotatelogs -eD "$LOGS_DIRECTORY"'/~workers/%Y-%m-%d-${name}.stderr.log' 86400 >&2)
socket=/run/proxmox-nixos-update-supervisor/work.sock
function run-proxmox-nixos-update {
exit_code=0
set -x
timeout 6h ${proxmox-nixos-update-bin} update --pr "$attr_path $payload" || exit_code=$?
set +x
if [ $exit_code -eq 124 ]; then
echo "Update was interrupted because it was taking too long."
fi
msg="DONE $attr_path $exit_code"
}
msg=READY
while true; do
response=$(echo "$msg" | socat -t5 UNIX-CONNECT:"$socket" - || true)
case "$response" in
"") # connection error; retry
sleep 5
;;
NOJOBS)
msg=READY
sleep 60
;;
JOB\ *)
read -r attr_path payload <<< "''${response#JOB }"
# If one worker is initializing the proxmox-nixos clone, the other will
# try to use the incomplete clone, consuming a bunch of jobs and
# throwing them away. So we use a crude locking mechanism to
# run only one worker when there isn't a proxmox-nixos directory yet.
# Once the directory exists and this initial lock is released,
# multiple workers can run concurrently.
lockdir="$XDG_CACHE_HOME/.proxmox-nixos.lock"
if [ ! -e "$XDG_CACHE_HOME/proxmox-nixos" ] && mkdir "$lockdir"; then
trap 'rmdir "$lockdir"' EXIT
run-proxmox-nixos-update
rmdir "$lockdir"
trap - EXIT
continue
fi
while [ -e "$lockdir" ]; do
sleep 10
done
run-proxmox-nixos-update
esac
done
'';
};
mkFetcher = name: cmd: {
after = [ "network-online.target" ];
wants = [ "network-online.target" ];
path = proxmoxNixOSUpdateSystemDependencies ++ [
(pkgs.python3.withPackages (
p: with p; [
requests
dateutil
libversion
cachecontrol
lockfile
filelock
]
))
];
environment.API_TOKEN_FILE = "${config.age.secrets.github-token.path}";
environment.XDG_CACHE_HOME = "/var/cache/proxmox-nixos-update/fetcher/";
serviceConfig = {
Type = "simple";
User = "proxmox-nixos-update";
Group = "proxmox-nixos-update";
Restart = "on-failure";
RestartSec = "30m";
LogsDirectory = "proxmox-nixos-update/";
LogsDirectoryMode = "755";
StateDirectory = "proxmox-nixos-update";
StateDirectoryMode = "700";
CacheDirectory = "proxmox-nixos-update/fetcher";
CacheDirectoryMode = "700";
};
script = ''
mkdir -p "$LOGS_DIRECTORY/~fetchers"
cd "$LOGS_DIRECTORY/~fetchers"
run_name="${name}.$(date +%s).txt"
rm -f ${name}.*.txt.part
${cmd} > "$run_name.part"
rm -f ${name}.*.txt
mv "$run_name.part" "$run_name"
'';
startAt = "0/12:10"; # every 12 hours
};
in
{
users.groups.proxmox-nixos-update = { };
users.users.proxmox-nixos-update = {
useDefaultShell = true;
isNormalUser = true; # The hub cli seems to really want stuff to be set up like a normal user
extraGroups = [ "proxmox-nixos-update" ];
};
systemd.services.proxmox-nixos-update-delete-done = {
startAt = "0/12:10"; # every 12 hours
after = [ "network-online.target" ];
wants = [ "network-online.target" ];
description = "proxmox-nixos-update delete done branches";
restartIfChanged = true;
path = proxmoxNixOSUpdateSystemDependencies;
environment.XDG_CONFIG_HOME = "/var/lib/proxmox-nixos-update/worker";
environment.XDG_CACHE_HOME = "/var/cache/proxmox-nixos-update/worker";
serviceConfig = {
Type = "simple";
User = "proxmox-nixos-update";
Group = "proxmox-nixos-update";
Restart = "on-abort";
RestartSec = "5s";
WorkingDirectory = "/var/lib/proxmox-nixos-update/worker";
StateDirectory = "proxmox-nixos-update/worker";
StateDirectoryMode = "700";
CacheDirectoryMode = "700";
LogsDirectory = "proxmox-nixos-update/";
LogsDirectoryMode = "755";
StandardOutput = "journal";
};
script = "${proxmox-nixos-update-bin} delete-done --delete";
};
systemd.services.proxmox-nixos-update-fetch-updatescript = mkFetcher "updatescript" "${pkgs.nix}/bin/nix eval --raw -f ${./packages-with-update-script.nix}";
systemd.services.proxmox-nixos-update-worker1 = mkWorker "worker1";
systemd.services.proxmox-nixos-update-worker2 = mkWorker "worker2";
systemd.services.proxmox-nixos-update-supervisor = {
wantedBy = [ "multi-user.target" ];
description = "proxmox-nixos-update supervisor service";
enable = true;
restartIfChanged = true;
path = with pkgs; [
apacheHttpd
(python3.withPackages (ps: [ ps.asyncinotify ]))
];
serviceConfig = {
Type = "simple";
User = "proxmox-nixos-update";
Group = "proxmox-nixos-update";
Restart = "on-failure";
RestartSec = "5s";
LogsDirectory = "proxmox-nixos-update/";
LogsDirectoryMode = "755";
RuntimeDirectory = "proxmox-nixos-update-supervisor/";
RuntimeDirectoryMode = "755";
StandardOutput = "journal";
};
script = ''
mkdir -p "$LOGS_DIRECTORY/~supervisor"
# This is for public logs at proxmox-nixos-update-logs.nix-community.org/~supervisor
exec > >(rotatelogs -eD "$LOGS_DIRECTORY"'/~supervisor/%Y-%m-%d.stdout.log' 86400)
exec 2> >(rotatelogs -eD "$LOGS_DIRECTORY"'/~supervisor/%Y-%m-%d.stderr.log' 86400 >&2)
# Fetcher output is hosted at proxmox-nixos-update-logs.nix-community.org/~fetchers
python3 ${./supervisor.py} "$LOGS_DIRECTORY/~supervisor/state.db" "$LOGS_DIRECTORY/~fetchers" "$RUNTIME_DIRECTORY/work.sock"
'';
};
systemd.services.proxmox-nixos-update-delete-old-logs = {
startAt = "daily";
# delete logs older than 18 months, delete worker logs older than 3 months, delete empty directories
script = ''
${pkgs.findutils}/bin/find /var/log/proxmox-nixos-update -type f -mtime +548 -delete
${pkgs.findutils}/bin/find /var/log/proxmox-nixos-update/~workers -type f -mtime +90 -delete
${pkgs.findutils}/bin/find /var/log/proxmox-nixos-update -type d -empty -delete
'';
serviceConfig.Type = "oneshot";
};
systemd.tmpfiles.rules = [
"L+ /home/proxmox-nixos-update/.gitconfig - - - - ${./gitconfig.txt}"
"d /home/proxmox-nixos-update/.ssh 700 proxmox-nixos-update proxmox-nixos-update - -"
"e /var/cache/proxmox-nixos-update/worker/nixpkgs-review - - - 1d -"
];
age.secrets.github-token = {
path = "/var/lib/proxmox-nixos-update/worker/github_token.txt";
owner = "proxmox-nixos-update";
group = "proxmox-nixos-update";
file = ./secrets/bot-github-token.age;
};
age.secrets.ssh-key = {
path = "/home/proxmox-nixos-update/.ssh/id_ed25519";
owner = "proxmox-nixos-update";
group = "proxmox-nixos-update";
file = ./secrets/ssh-bot-priv.age;
};
services.nginx.enable = true;
services.nginx.recommendedZstdSettings = false;
services.nginx.virtualHosts."proxmox-nixos-update-logs.saumon.network" = {
forceSSL = true;
enableACME = true;
locations."/" = {
alias = "/var/log/proxmox-nixos-update/";
extraConfig = ''
charset utf-8;
autoindex on;
'';
};
};
}