Repository navigation
Expand file tree
/
Copy pathMakefile
More file actions
127 lines (114 loc) · 8.08 KB
/
Copy pathMakefile
File metadata and controls
127 lines (114 loc) · 8.08 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
# Test sandbox for the homeserver — a throwaway, fully isolated copy you spin up to try
# image updates, hsctl updates, restic updates, or a real restore, WITHOUT touching your
# live system. It runs its own nested Docker daemon (docker-in-docker); nothing it does
# reaches the host's Docker, containers, or volumes.
#
# make sandbox build + start the sandbox (admin UI on http://localhost:PORT)
# make sandbox-restore restore a real backup into it so you can see your data (the
# dashboard's Backups -> Restore does the same)
# make sandbox-shell open a shell inside the sandbox
# make sandbox-logs follow the sandbox logs
# make sandbox-down stop + clean up everything
#
# make uninstall remove the whole homeserver install from this machine, KEEPING
# all user data by default: dashboard service + timer, hsctl,
# every app's containers and images, the shared docker network,
# orphans from renamed/removed app dirs, the test sandbox, and
# the generated secrets/config in this repo all go — the app data
# volumes and the backups stay. CONFIRM=yes skips the prompt.
# make uninstall DATA=yes also delete the app data volumes (ALL APP DATA). Always
# asks for a typed double confirmation, even with CONFIRM=yes.
# make uninstall ALL=yes delete the volumes AND backup.conf, .restic-password,
# (= make uninstall-all) .backup-env, backups/ — every snapshot gone, permanently.
# Always double-confirms. (make has no --all option, hence ALL=yes.)
#
# Knobs (override on the command line, e.g. `make sandbox PORT=19000`):
# IMAGES image manifest to use (default sandbox/images.env)
# PORT host port for the admin UI (default 18088)
# PASS admin password in the sandbox (default test)
# BACKUP_DISK the disk holding your backups, mounted READ-WRITE at /mnt inside the sandbox:
# a device or LABEL=/UUID= (e.g. LABEL=Backup). It must not be mounted on the host.
# REPO restic repo the sandbox backs up to and restores from: a path inside the
# sandbox's /mnt with BACKUP_DISK (e.g. /mnt/restic_v2), else a local folder on the
# host, mounted read-write. Point it at a COPY of your repo, not the live one.
# SNAPSHOT snapshot id to restore (default latest)
SANDBOX_IMAGE ?= hsctl-sandbox
SANDBOX_NAME ?= hsctl-sandbox
DATA_VOL ?= hsctl-sandbox-data
VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo dev)
IMAGES ?= sandbox/images.env
PORT ?= 18088
PASS ?= test
# Host LAN IP the apps should trust (so the restored Nextcloud/Vaultwarden open in a browser).
ACCESS_HOST ?= $(shell ip -4 route get 1.1.1.1 2>/dev/null | sed -n 's/.*src \([0-9.]*\).*/\1/p')
PASSFILE ?= .restic-password
SNAPSHOT ?= latest
BACKUP_DISK ?=
REPO ?= $(shell sed -n 's/^RESTIC_REPO=//p' backup.conf 2>/dev/null)
# restic needs its repository writable even to restore (it takes a lock), so the repo is never
# mounted read-only. With BACKUP_DISK the whole disk is mounted at /mnt inside the sandbox (it
# must not be mounted on the host too: two read-write mounts of one filesystem corrupt it);
# otherwise a local REPO folder on the host is bind-mounted at the same path. The password file,
# when there is one, goes in read-only; without it, set it on the sandbox's Backups page.
REPO_MOUNT := $(if $(BACKUP_DISK),,$(if $(wildcard $(REPO)),-v $(abspath $(REPO)):$(abspath $(REPO))))
PASS_MOUNT := $(if $(wildcard $(PASSFILE)),-v $(abspath $(PASSFILE)):/backup-pass:ro)
.PHONY: sandbox sandbox-restore sandbox-shell sandbox-logs sandbox-down sandbox-purge uninstall uninstall-all help
help:
@sed -n 's/^#\( \|$$\)//p' Makefile | sed -n '1,31p'
sandbox: ## build the sandbox image (with your current hsctl) and start it
@mkdir -p sandbox/_build
CGO_ENABLED=0 go build -C hsctl -ldflags "-X main.version=$(VERSION)" -o ../sandbox/_build/hsctl .
@if [ -n "$(BACKUP_DISK)" ] && findmnt -rn -S "$(BACKUP_DISK)" >/dev/null 2>&1; then \
echo "$(BACKUP_DISK) is mounted on this machine — unmount it first: the sandbox mounts it read-write itself"; exit 1; fi
docker build -f sandbox/Dockerfile -t $(SANDBOX_IMAGE) .
-docker stop -t 8 $(SANDBOX_NAME) >/dev/null 2>&1 # graceful: lets the old loopback detach
-docker rm -f $(SANDBOX_NAME) >/dev/null 2>&1 # belt-and-suspenders (no-op after --rm)
@# A --rm container is still being removed for a moment after it stops; its name is taken until then.
@for _ in $$(seq 1 30); do docker container inspect $(SANDBOX_NAME) >/dev/null 2>&1 || exit 0; sleep 1; done; \
echo "the old $(SANDBOX_NAME) container is still there"; exit 1
docker run -d --rm --privileged --name $(SANDBOX_NAME) \
-p $(PORT):8088 \
-p 18443:18443 -p 18444:18444 -p 18445:18445 \
-p 18446:18446 -p 18447:18447 -p 18448:18448 \
-e HSCTL_UI_PASSWORD=$(PASS) -e ACCESS_HOST=$(ACCESS_HOST) -e SANDBOX_PORT=$(PORT) \
-e BACKUP_DISK="$(BACKUP_DISK)" -e SANDBOX_REPO="$(if $(BACKUP_DISK),$(REPO),$(if $(wildcard $(REPO)),$(abspath $(REPO))))" \
-v $(DATA_VOL):/var/lib/docker \
-v $(abspath $(IMAGES)):/sandbox/images.env:ro \
$(REPO_MOUNT) $(PASS_MOUNT) \
$(SANDBOX_IMAGE)
@echo ""
@echo "Sandbox starting: http://$(ACCESS_HOST):$(PORT)/admin (admin / $(PASS))"
@echo " Bring the stack up: in the UI -> Commands -> 'Start all services',"
@echo " or restore your real data: UI -> Backups -> Restore (or make sandbox-restore)"
@echo " Then open the RESTORED apps over HTTPS (your trusted cert; live ports + 10000):"
@echo " Vaultwarden : https://$(ACCESS_HOST):18443 (passwords)"
@echo " Nextcloud : https://$(ACCESS_HOST):18444 (files)"
@echo " Pi-hole : https://$(ACCESS_HOST):18445/admin"
@if [ -z "$(BACKUP_DISK)$(REPO_MOUNT)" ]; then echo " (no backups wired in — pass BACKUP_DISK=LABEL=<label> REPO=/mnt/<repo>, or a local REPO=<folder>)"; fi
sandbox-restore: ## restore a real backup into the running sandbox (SNAPSHOT=latest) — the dashboard's Restore, from the terminal
docker exec -it $(SANDBOX_NAME) hsctl backup restore --into-volumes --yes $(filter-out latest,$(SNAPSHOT))
sandbox-shell: ## open a shell inside the sandbox
docker exec -it $(SANDBOX_NAME) bash
sandbox-logs: ## follow the sandbox logs
docker logs -f $(SANDBOX_NAME)
sandbox-down: ## stop the sandbox and sweep its loopback device off the host
-docker stop -t 8 $(SANDBOX_NAME) >/dev/null 2>&1
-docker run --rm --privileged --entrypoint bash $(SANDBOX_IMAGE) -c \
'losetup -a 2>/dev/null | grep -i sandboxdisk | cut -d: -f1 | xargs -r -n1 losetup -d' >/dev/null 2>&1
@echo "sandbox stopped; host is clean. (Nested images cached in volume '$(DATA_VOL)' for fast re-runs; 'make sandbox-purge' to delete.)"
sandbox-purge: sandbox-down ## also delete the cached nested images/data volume
-docker volume rm $(DATA_VOL) >/dev/null 2>&1
@echo "purged $(DATA_VOL)."
# ---- uninstall -------------------------------------------------------------------------
# Single implementation: `sudo hsctl uninstall` (hsctl/internal/install/uninstall.go). These targets are
# thin wrappers so the old `make uninstall` muscle memory keeps working — the installed
# hsctl is used when present, else one is built from this checkout (needs Go).
# DATA=yes -> --data (also delete app volumes), ALL=yes -> --all (volumes + backups),
# CONFIRM=yes -> --yes (skips the prompt for the data-keeping default only; the
# data-deleting tiers always ask for a typed double confirmation).
HSCTL_BIN ?= $(shell command -v hsctl || echo ./hsctl/hsctl)
uninstall: ## remove the stack + hsctl; keeps app data and backups (DATA=yes / ALL=yes delete more)
@[ -x "$(HSCTL_BIN)" ] || $(MAKE) --no-print-directory -C hsctl build
sudo $(HSCTL_BIN) uninstall $(if $(filter yes,$(ALL)),--all,$(if $(filter yes,$(DATA)),--data)) $(if $(filter yes,$(CONFIRM)),--yes)
uninstall-all: ## uninstall EVERYTHING incl. app data + backups (always double-confirms)
@$(MAKE) --no-print-directory ALL=yes uninstall