Repository navigation
Expand file tree
/
Copy pathkit
More file actions
executable file
·418 lines (392 loc) · 15.5 KB
/
Copy pathkit
File metadata and controls
executable file
·418 lines (392 loc) · 15.5 KB
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#!/bin/bash
# mac-server-kit: install and manage a macOS home server.
#
# Usage:
# ./kit prepare [options] [<module>...]
# what to have ready before installing. Options:
# --vpn <protonvpn|nordvpn>, --media-server <plex|jellyfin|both>,
# --secrets-template <file>, --answers-template <file>
# ./kit install [--answers <file>] [--secrets <file>]
# set up or update this Mac (safe to re-run)
# ./kit install --plan [...] what an install would change, changing nothing (also with
# add and disable)
# ./kit check can this Mac run the kit, and is it ready? (changes nothing)
# ./kit add <module>... enable modules, then re-run install
# ./kit disable <module>... disable modules (their data stays), then re-run install
# ./kit status enabled modules, version holds and their age, to-dos, health
#
# Machine files live in ~/server: answers.conf and secrets.conf are yours to edit; the kit only
# appends keys that are missing (and edits the MODULES= line). See docs/design.md.
# Module lists are space-separated names ([a-z0-9-] only) that are split on purpose.
# shellcheck disable=SC2086,SC2046
set -u -o pipefail
KIT_DIR="$(cd "$(dirname "$0")" && pwd -P)"
# shellcheck source=lib/common.sh
. "$KIT_DIR/lib/common.sh"
# shellcheck source=lib/plan.sh
. "$KIT_DIR/lib/plan.sh"
# shellcheck source=lib/config.sh
. "$KIT_DIR/lib/config.sh"
# shellcheck source=lib/modules.sh
. "$KIT_DIR/lib/modules.sh"
# shellcheck source=lib/preflight.sh
. "$KIT_DIR/lib/preflight.sh"
# shellcheck source=lib/prepare.sh
. "$KIT_DIR/lib/prepare.sh"
# shellcheck source=lib/project.sh
. "$KIT_DIR/lib/project.sh"
# shellcheck source=lib/api.sh
. "$KIT_DIR/lib/api.sh"
# shellcheck source=lib/tailscale.sh
. "$KIT_DIR/lib/tailscale.sh"
# shellcheck source=lib/homarr.sh
. "$KIT_DIR/lib/homarr.sh"
# shellcheck source=lib/runtime.sh
. "$KIT_DIR/lib/runtime.sh"
# shellcheck source=lib/native.sh
. "$KIT_DIR/lib/native.sh"
# shellcheck source=lib/mac.sh
. "$KIT_DIR/lib/mac.sh"
# shellcheck source=modules/core/bin/kit-registry.sh
. "$KIT_DIR/modules/core/bin/kit-registry.sh"
# shellcheck source=modules/core/bin/kit-dmg.sh
. "$KIT_DIR/modules/core/bin/kit-dmg.sh"
usage() { sed -n '2,/^$/p' "$0" | sed 's/^# \{0,1\}//'; }
# Host preflight, then its verdict and to-do list when something needs doing
host_checks() {
[ "${KIT_SKIP_HOST_CHECKS:-0}" = 1 ] && return 0
preflight_host && return 0
preflight_summary
return 1
}
# Ask for what's missing, check every value, resolve the modules and record them.
# Sets ENABLED.
ENABLED=''
configure() {
local base rest m
config_init
modules_discover
base=$(modules_base)
section "Answers and secrets"
note "Saved in $(tilde "$SERVER_DIR/answers.conf") and $(tilde "$SERVER_DIR/secrets.conf"); only missing ones are asked."
run_prompts $base
ENABLED=$(modules_resolve) || die "fix the modules above (MEDIA_SERVER= and MODULES= in $(shown "$ANSWERS"))"
rest=''
for m in $ENABLED; do in_list "$m" "$base" || rest=$(list_add "$rest" "$m"); done
run_prompts $rest
if [ -n "$PROMPTS_MISSING" ] && ! in_plan; then
die "no terminal to ask for: $PROMPTS_MISSING. Add them to $(shown "$ANSWERS") or $(shown "$SECRETS"), or run ./kit install in Terminal."
fi
in_plan || ok_again "Answers and secrets complete"
section "Checking values"
check_values $ENABLED || die "fix the values above, then run ./kit install again"
ok_again "Every value has the right format"
import_commit
if in_plan; then
plan_modules $ENABLED
return 0
fi
write_enabled $ENABLED
ok_again "Modules: $ENABLED"
}
# A plan's line for the module list: which modules this install would add or take away
plan_modules() { # modules...
local m was added='' gone=''
was=$(read_enabled 2>/dev/null || true)
for m in "$@"; do in_list "$m" "$was" || added=$(list_add "$added" "$m"); done
for m in $was; do in_list "$m" "$*" || gone=$(list_add "$gone" "$m"); done
note "Modules: $*"
if [ -n "$was" ]; then
[ -z "$added" ] || plan_add "Enable $added"
[ -z "$gone" ] || plan_add "Disable $gone (their containers and jobs go; their data stays)"
fi
}
cmd_install() {
local secrets_file='' answers_file=''
while [ $# -gt 0 ]; do
case "$1" in
--secrets) [ $# -ge 2 ] || die "--secrets needs a file"; secrets_file=$2; shift 2 ;;
--answers) [ $# -ge 2 ] || die "--answers needs a file"; answers_file=$2; shift 2 ;;
--plan) shift ;; # plan_enter has set KIT_PLAN
*) die "unknown option for install: $1" ;;
esac
done
if in_plan; then
printf '%smac-server-kit install --plan%s: what an install would do. Nothing is changed or asked.\n' "$C_BLD" "$C_OFF"
note "+ the install would do this ⚠ check before installing ? can't tell without running it"
else
printf '%smac-server-kit install%s\n' "$C_BLD" "$C_OFF"
fi
if [ ! -f "$ANSWERS" ] && [ -z "$answers_file" ]; then
note "First install here? ./kit prepare lists what to have ready."
fi
host_checks || die "nothing was installed. Fix the above, then run ./kit install again"
# For the summary at the end (run_summary): how long it took and what it did
SECONDS=0
KIT_RUN_LOG=$(mktemp "${TMPDIR:-/tmp}/kit-run.XXXXXX") || KIT_RUN_LOG=''
# shellcheck disable=SC2064 # the file's name now (a plan's is in PLAN_DIR, removed with it)
[ -z "$KIT_RUN_LOG" ] || in_plan || trap "rm -f '$KIT_RUN_LOG'" EXIT
PREV_ENABLED=$(read_enabled 2>/dev/null || true) # for the notes of modules new this run
in_plan && plan_config_copy
[ -z "$answers_file" ] || import_answers_file "$answers_file"
[ -z "$secrets_file" ] || import_secrets_file "$secrets_file"
configure
in_plan && plan_report_config $ENABLED
section "Folders"
layout_create
zprofile_add
section "Mac settings"
mac_settings
mac_auto_login
if has_hooks install $ENABLED; then
section "Native apps"
run_hooks install $ENABLED
fi
section "Compose projects"
generate_projects $ENABLED
in_plan && plan_report_projects $ENABLED
section "App settings"
run_hooks seed $ENABLED
section "Start"
if in_plan; then
plan_start $ENABLED
else
start_projects $ENABLED
tailnet_read
fi
# The jobs before the wiring: the native apps they run (Jellyfin) are set up through their APIs
section "Health and jobs"
runtime_install $ENABLED
section "Connecting the apps"
if in_plan; then
plan_wire $ENABLED
homarr_widgets $ENABLED
plan_summary
return
fi
KIT_RUN_RECORD=1
run_hooks wire $ENABLED
homarr_widgets $ENABLED
serve_refresh $ENABLED
KIT_RUN_RECORD=0
report_install $ENABLED
run_summary
}
cmd_prepare() {
local secrets_tpl='' answers_tpl='' named='' m spec enabled listed
modules_discover
while [ $# -gt 0 ]; do
case "$1" in
--vpn|--media-server)
[ $# -ge 2 ] || die "$1 needs a value"
case "$1" in --vpn) m=VPN_PROVIDER ;; *) m=MEDIA_SERVER ;; esac
spec=$(prompt_entry answers "$m" | cut -d'|' -f4)
validate "$spec" "$2" || die "$1 $VALID_MSG"
override_answer "$m" "$2"
shift 2 ;;
--secrets-template) [ $# -ge 2 ] || die "$1 needs a file"; secrets_tpl=$2; shift 2 ;;
--answers-template) [ $# -ge 2 ] || die "$1 needs a file"; answers_tpl=$2; shift 2 ;;
-*) die "unknown option for prepare: $1" ;;
*)
mod_exists "$1" || die "unknown module: $1 (available: $(optional_modules))"
[ "$(mod_conf "$1" KIND)" = optional ] || die "$1 is part of the base install; no need to name it"
named=$(list_add "$named" "$1")
shift ;;
esac
done
listed=$(listed_modules)
for m in $named; do listed=$(list_add "$listed" "$m"); done
override_answer MODULES "$listed"
enabled=$(modules_resolve) || die "name the missing modules too"
ENABLED=$enabled
if [ -z "$secrets_tpl$answers_tpl" ]; then
prepare_list $enabled
return
fi
if [ -n "$answers_tpl" ]; then
answers_template "$answers_tpl" $enabled
ok "Wrote $answers_tpl: change what you want"
fi
if [ -n "$secrets_tpl" ]; then
secrets_template "$secrets_tpl" $enabled
ok "Wrote $secrets_tpl (private): fill in the keys"
fi
info "Then: ./kit install${answers_tpl:+ --answers $answers_tpl}${secrets_tpl:+ --secrets $secrets_tpl}"
}
cmd_check() {
local bad=0 enabled
host_checks || bad=1
section "Answers and secrets"
if [ ! -f "$ANSWERS" ]; then
info "No $(shown "$ANSWERS") yet: ./kit install asks for everything"
return $bad
fi
modules_discover
if enabled=$(modules_resolve); then
ENABLED=$enabled
ok "Modules: $enabled"
if check_values $enabled; then ok "Every value that is set has the right format"; else bad=1; fi
else
bad=1
fi
[ $bad = 0 ] && { section "Ready"; ok "./kit install can run"; }
return $bad
}
# Modules listed in MODULES= (not the base ones, not the media-server choice)
listed_modules() { kv_get "$ANSWERS" MODULES || true; }
# MODULES= for this run: written by add and disable, or in a plan only used
set_modules() { # value
if in_plan; then
override_answer MODULES "$1"
note "MODULES=${1:-(none)} for this plan only"
return 0
fi
kv_replace "$ANSWERS" MODULES "$1"
ok "MODULES=${1:-(none)}"
}
cmd_add() {
local m listed new
set -- $(printf '%s\n' "$@" | grep -vx -- --plan)
[ $# -gt 0 ] || die "usage: ./kit add <module>... [--plan]"
config_init
modules_discover
listed=$(listed_modules)
new=$listed
for m in "$@"; do
mod_exists "$m" || die "unknown module: $m (available: $(optional_modules))"
[ "$(mod_conf "$m" KIND)" = base ] && die "$m is part of the base install and is always enabled"
if in_list "$m" "$(modules_requested)"; then
info "$m is already enabled"
continue
fi
new=$(list_add "$new" "$m")
done
[ "$new" = "$listed" ] && return 0
modules_resolve $(modules_base) $(media_server_modules) $new >/dev/null \
|| die "nothing changed; add the missing modules in the same command"
set_modules "$new"
cmd_install
}
cmd_disable() {
local m listed new
set -- $(printf '%s\n' "$@" | grep -vx -- --plan)
[ $# -gt 0 ] || die "usage: ./kit disable <module>... [--plan]"
config_init
modules_discover
listed=$(listed_modules)
new=''
for m in "$@"; do
mod_exists "$m" || die "unknown module: $m"
[ "$(mod_conf "$m" KIND)" = base ] && die "$m is part of the base install and can't be disabled"
if in_list "$m" "$(media_server_modules)"; then
die "$m is your media server choice; change MEDIA_SERVER= in $(shown "$ANSWERS") instead"
fi
in_list "$m" "$listed" || die "$m isn't enabled"
done
for m in $listed; do in_list "$m" "$*" || new=$(list_add "$new" "$m"); done
modules_resolve $(modules_base) $(media_server_modules) $new >/dev/null \
|| die "nothing changed; disable the modules that need it in the same command"
set_modules "$new"
note "Containers, jobs and data are handled by the install steps (data is always kept)."
cmd_install
}
optional_modules() {
local m out=''
for m in $MODULES_ALL; do [ "$(mod_conf "$m" KIND)" = optional ] && out=$(list_add "$out" "$m"); done
printf '%s' "$out"
}
# copy-backups' last result (~/server/logs/copy-backups.status: tried, result, why, last copied)
backup_copy_status() {
local drive st tried result why last_ok
drive=$(answer BACKUP_DRIVE 2>/dev/null || true)
if [ -z "$drive" ]; then
info "Off: set BACKUP_DRIVE in $(shown "$ANSWERS") to a drive (or a folder on one), then run ./kit install"
return 0
fi
info "To $drive, after each weekly backup-all"
st=$SERVER_DIR/logs/copy-backups.status
if [ ! -f "$st" ] || ! IFS=$'\t' read -r tried result why last_ok <"$st" || [ -z "$tried" ]; then
note "None yet (the first backup-all copies)"
return 0
fi
case "$result" in
ok) ok_again "Last copy $(date -r "$tried" '+%Y-%m-%d %H:%M'): worked" ;;
*)
warn "Last try $(date -r "$tried" '+%Y-%m-%d %H:%M'): $result ($why)"
if [ -n "$last_ok" ]; then note "Last copy that worked: $(date -r "$last_ok" '+%Y-%m-%d %H:%M')"; else note "No copy has worked yet"; fi ;;
esac
}
cmd_status() {
local enabled recorded m kind line svc tag date reason src age
modules_discover
section "Modules"
if [ ! -f "$ANSWERS" ]; then
info "Not configured yet: run ./kit install"
return 0
fi
enabled=$(modules_resolve) || return 1
for m in $enabled; do
kind=$(mod_conf "$m" KIND)
printf ' %-12s %s%-8s%s %s\n' "$m" "$C_DIM" "$kind" "$C_OFF" "$(mod_conf "$m" DESCRIPTION)"
done
recorded=$(read_enabled || true)
if [ "$recorded" != "$enabled" ]; then
warn "answers.conf has changed since the last install; run ./kit install to apply it"
fi
line=''
for m in $(optional_modules); do in_list "$m" "$enabled" || line=$(list_add "$line" "$m"); done
[ -z "$line" ] || note "Available to add: $line"
section "Version holds"
line=$(holds_list $enabled)
if [ -z "$line" ]; then
info "None"
else
printf '%s\n' "$line" | while IFS='|' read -r svc tag date reason src; do
[ -n "$svc" ] || continue
if [ "$tag" = none ]; then
printf ' %-16s %s\n' "$svc" "hold lifted ($src)"
continue
fi
age=$(months_since "$date")
if [ -z "$age" ]; then
warn "$svc held at $tag ($src) has no yyyy-mm date: add one so its age shows"
elif [ -z "$reason" ]; then
warn "$svc held at $tag for $age months ($src) has no reason: add one"
elif [ "$age" -ge 6 ]; then
warn "$svc held at $tag for $age months ($src), still needed? $reason"
else
printf ' %-16s %-12s %s months (%s) %s\n' "$svc" "$tag" "$age" "$src" "$reason"
fi
done
fi
section "External backup copies"
backup_copy_status
todo_report $enabled
[ "$TODO_COUNT" -eq 0 ] || note "As the last install saw them; ./kit install checks again."
section "Health"
if [ -x "$SERVER_DIR/bin/check-health" ]; then
"$SERVER_DIR/bin/check-health" --summary || true
else
info "Health monitoring isn't installed yet"
fi
}
# --plan with install, add or disable: a preview (lib/plan.sh), set up before the command runs
case "${1:-}" in
install|add|disable)
for a in "$@"; do
[ "$a" = --plan ] || continue
plan_enter "$@"
plan_guards
break
done ;;
esac
case "${1:-}" in
prepare) shift; cmd_prepare "$@" ;;
install) shift; cmd_install "$@" ;;
check) shift; cmd_check "$@" ;;
add) shift; cmd_add "$@" ;;
disable) shift; cmd_disable "$@" ;;
status) shift; cmd_status "$@" ;;
-h|--help|help|'') usage ;;
*) usage >&2; exit 64 ;;
esac