create-freeholder 0.2.0-beta.4 → 0.2.0-beta.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (158) hide show
  1. package/dist/template/.changeset/blog-playground-content.md +4 -0
  2. package/dist/template/.changeset/blog-wordpress-structure.md +6 -0
  3. package/dist/template/.changeset/managed-preview-assistant.md +4 -0
  4. package/dist/template/.changeset/native-linux-install.md +6 -0
  5. package/dist/template/.changeset/public-beta-5.md +4 -0
  6. package/dist/template/.changeset/public-beta-6.md +4 -0
  7. package/dist/template/.github/ISSUE_TEMPLATE/bug_report.yml +1 -1
  8. package/dist/template/.github/workflows/release-tarballs.yml +117 -0
  9. package/dist/template/AGENTS.md +15 -2
  10. package/dist/template/Dockerfile +3 -0
  11. package/dist/template/MASTER.md +114 -11
  12. package/dist/template/README.md +1 -1
  13. package/dist/template/app/(admin)/admin/assistant/page.tsx +9 -1
  14. package/dist/template/app/(admin)/admin/blog/PostSettings.tsx +26 -0
  15. package/dist/template/app/(admin)/admin/blog/[kind]/page.tsx +53 -0
  16. package/dist/template/app/(admin)/admin/blog/actions.ts +186 -6
  17. package/dist/template/app/(admin)/admin/blog/comments/page.tsx +25 -0
  18. package/dist/template/app/(admin)/admin/blog/page.tsx +21 -5
  19. package/dist/template/app/(admin)/admin/imports/[id]/page.tsx +2 -0
  20. package/dist/template/app/(admin)/admin/pages/[id]/page.tsx +3 -0
  21. package/dist/template/app/(admin)/assistant-actions.ts +13 -6
  22. package/dist/template/app/(public)/[[...slug]]/ReaderComments.tsx +25 -0
  23. package/dist/template/app/(public)/[[...slug]]/blog-parts.tsx +153 -0
  24. package/dist/template/app/(public)/[[...slug]]/comment-actions.ts +16 -0
  25. package/dist/template/app/(public)/[[...slug]]/page.tsx +182 -18
  26. package/dist/template/app/api/chat/assistant/call/route.ts +35 -0
  27. package/dist/template/db/migrations/0061_blog_structure.sql +89 -0
  28. package/dist/template/db/migrations/0062_blog_reader_comments.sql +24 -0
  29. package/dist/template/db/migrations/meta/_journal.json +14 -0
  30. package/dist/template/deploy/README.md +26 -9
  31. package/dist/template/deploy/blog-wordpress-2026-11-07.md +146 -0
  32. package/dist/template/deploy/docker-selfhost/.env.example +11 -1
  33. package/dist/template/deploy/docker-selfhost/README.md +79 -50
  34. package/dist/template/deploy/docker-selfhost/infra/backup.sh +81 -0
  35. package/dist/template/deploy/docker-selfhost/infra/compose.https.yml +37 -1
  36. package/dist/template/deploy/docker-selfhost/infra/compose.yml +37 -1
  37. package/dist/template/deploy/docker-selfhost/migrate.md +8 -5
  38. package/dist/template/deploy/docker-selfhost/recipe.yaml +9 -10
  39. package/dist/template/deploy/docker-selfhost/selfhost.sh +206 -0
  40. package/dist/template/deploy/docker-selfhost/verify.md +11 -4
  41. package/dist/template/deploy/f-criteria-matrix.md +89 -0
  42. package/dist/template/deploy/linux/.env.example +28 -0
  43. package/dist/template/deploy/linux/README.md +181 -0
  44. package/dist/template/deploy/linux/freeholder +655 -0
  45. package/dist/template/deploy/linux/install.sh +173 -0
  46. package/dist/template/deploy/linux/migrate.md +17 -0
  47. package/dist/template/deploy/linux/recipe.yaml +50 -0
  48. package/dist/template/deploy/linux/share/Caddyfile.template +19 -0
  49. package/dist/template/deploy/linux/share/systemd/freeholder-backup.service +13 -0
  50. package/dist/template/deploy/linux/share/systemd/freeholder-backup.timer +12 -0
  51. package/dist/template/deploy/linux/share/systemd/freeholder.service +41 -0
  52. package/dist/template/deploy/linux/verify.md +22 -0
  53. package/dist/template/deploy/managed-preview-assistant.md +99 -0
  54. package/dist/template/eslint.config.mjs +3 -1
  55. package/dist/template/gitignore.template +1 -0
  56. package/dist/template/locales/ar.json +85 -6
  57. package/dist/template/locales/en.json +83 -4
  58. package/dist/template/locales/es.json +83 -4
  59. package/dist/template/locales/fr.json +85 -6
  60. package/dist/template/next.config.ts +2 -0
  61. package/dist/template/package.json +3 -2
  62. package/dist/template/packages/cli/package.json +1 -1
  63. package/dist/template/packages/create-freeholder/package.json +1 -1
  64. package/dist/template/packages/freeholder-app/package.json +1 -1
  65. package/dist/template/packages/mobile-app/package.json +1 -1
  66. package/dist/template/packages/plugin-kit/package.json +1 -1
  67. package/dist/template/packages/sdk/package.json +1 -1
  68. package/dist/template/packages/sdk/src/generated.ts +177 -9
  69. package/dist/template/packages/sdk/src/version.ts +1 -1
  70. package/dist/template/packages/templates/package.json +1 -1
  71. package/dist/template/proxy.ts +18 -1
  72. package/dist/template/public/managed-assistant/0.1.10/duplex-codec/NOTICE.txt +117 -0
  73. package/dist/template/public/managed-assistant/0.1.10/duplex-codec/audio-output-processor.js +267 -0
  74. package/dist/template/public/managed-assistant/0.1.10/duplex-codec/decoderWorker.min.js +847 -0
  75. package/dist/template/public/managed-assistant/0.1.10/duplex-codec/decoderWorker.min.wasm +0 -0
  76. package/dist/template/public/managed-assistant/0.1.10/duplex-codec/encoderWorker.min.js +1 -0
  77. package/dist/template/public/managed-assistant/0.1.10/manifest.json +12 -0
  78. package/dist/template/public/managed-assistant/0.1.10/pm-ai-duplex-client.js +1376 -0
  79. package/dist/template/scripts/build-release.d.mts +4 -0
  80. package/dist/template/scripts/build-release.mjs +171 -0
  81. package/dist/template/scripts/launch-prepare.mjs +6 -3
  82. package/dist/template/scripts/license-headers.mjs +3 -0
  83. package/dist/template/scripts/recipe-matrix.sh +22 -7
  84. package/dist/template/scripts/verify-wordpress-migration.d.mts +8 -0
  85. package/dist/template/scripts/verify-wordpress-migration.mjs +105 -0
  86. package/dist/template/security/adversarial-audit-2026-10-08.md +1 -1
  87. package/dist/template/src/adapters/ai/types.ts +5 -0
  88. package/dist/template/src/adapters/paradise/assistant.ts +37 -0
  89. package/dist/template/src/adapters/paradise/http.ts +4 -3
  90. package/dist/template/src/adapters/storage/local-mode.ts +54 -0
  91. package/dist/template/src/adapters/storage/local.ts +53 -26
  92. package/dist/template/src/core/contract/json-schema.ts +8 -0
  93. package/dist/template/src/core/demo/playground-inserts.ts +29 -0
  94. package/dist/template/src/core/demo/playground-policy.ts +9 -0
  95. package/dist/template/src/core/doctor/index.ts +19 -10
  96. package/dist/template/src/core/env.ts +8 -1
  97. package/dist/template/src/core/http/csp.ts +10 -2
  98. package/dist/template/src/core/http/csrf-client.ts +10 -0
  99. package/dist/template/src/core/import/service.ts +217 -12
  100. package/dist/template/src/core/import/wordpress.ts +80 -15
  101. package/dist/template/src/core/notifications/service.ts +1 -0
  102. package/dist/template/src/core/retention/registry.ts +2 -0
  103. package/dist/template/src/core/search/registry.ts +2 -0
  104. package/dist/template/src/core/seo/feeds.ts +2 -0
  105. package/dist/template/src/core/seo/jsonld.ts +46 -2
  106. package/dist/template/src/core/seo/legacy-proxy.ts +53 -0
  107. package/dist/template/src/core/seo/meta.ts +10 -7
  108. package/dist/template/src/core/seo/service.ts +14 -1
  109. package/dist/template/src/core/update/preflight.ts +3 -2
  110. package/dist/template/src/core/update/release-declaration.json +1 -1
  111. package/dist/template/src/core/update/service.ts +7 -17
  112. package/dist/template/src/modules/assistant/contract.ts +1 -1
  113. package/dist/template/src/modules/assistant/limits.ts +3 -0
  114. package/dist/template/src/modules/assistant/preview.ts +124 -0
  115. package/dist/template/src/modules/assistant/provider.ts +7 -0
  116. package/dist/template/src/modules/assistant/service.ts +21 -5
  117. package/dist/template/src/modules/cms/blocks/PreviewAssistantCall.tsx +176 -0
  118. package/dist/template/src/modules/cms/blocks/SiteChatClient.tsx +10 -4
  119. package/dist/template/src/modules/cms/blocks/surfaces.tsx +10 -0
  120. package/dist/template/src/modules/cms/blog-comments-schema.ts +27 -0
  121. package/dist/template/src/modules/cms/blog-comments.ts +111 -0
  122. package/dist/template/src/modules/cms/blog-schema.ts +146 -0
  123. package/dist/template/src/modules/cms/blog-service.ts +999 -0
  124. package/dist/template/src/modules/cms/blog.tsx +245 -25
  125. package/dist/template/src/modules/cms/manifest.ts +7 -1
  126. package/dist/template/src/modules/cms/read.ts +18 -0
  127. package/dist/template/src/modules/cms/service.ts +22 -4
  128. package/dist/template/src/modules/cms/tables.ts +2 -0
  129. package/dist/template/src/modules/seed/launch.ts +31 -3
  130. package/dist/template/tests/browser/blog-structure.spec.ts +69 -0
  131. package/dist/template/tests/core/agent-launch.test.ts +7 -2
  132. package/dist/template/tests/core/analytics-consent.test.ts +1 -1
  133. package/dist/template/tests/core/blog-playground.test.ts +106 -0
  134. package/dist/template/tests/core/blog-structure-comments.test.ts +166 -0
  135. package/dist/template/tests/core/client-readiness-security.test.ts +3 -1
  136. package/dist/template/tests/core/csp.test.ts +1 -1
  137. package/dist/template/tests/core/deploy-recipe.test.ts +54 -4
  138. package/dist/template/tests/core/internal-services.test.ts +4 -0
  139. package/dist/template/tests/core/legacy-redirect-proxy.test.ts +59 -0
  140. package/dist/template/tests/core/linux-install.test.ts +169 -0
  141. package/dist/template/tests/core/local-storage-mode.test.ts +29 -0
  142. package/dist/template/tests/core/locale-routing.test.ts +1 -1
  143. package/dist/template/tests/core/playground-cache.test.ts +1 -1
  144. package/dist/template/tests/core/playground.test.ts +3 -3
  145. package/dist/template/tests/core/preview-assistant-call.test.ts +88 -0
  146. package/dist/template/tests/core/private-preview-webhooks.test.ts +1 -1
  147. package/dist/template/tests/core/private-preview.test.ts +1 -1
  148. package/dist/template/tests/core/proxy-locale.test.ts +1 -1
  149. package/dist/template/tests/core/sdk-schema.test.ts +20 -0
  150. package/dist/template/tests/core/seo.test.ts +1 -1
  151. package/dist/template/tests/core/site-chat-client.test.ts +50 -0
  152. package/dist/template/tests/core/storage.test.ts +38 -2
  153. package/dist/template/tests/core/wordpress-import-flow.test.ts +81 -18
  154. package/dist/template/tests/core/wordpress-import.test.ts +25 -12
  155. package/dist/template/tests/core/wordpress-migration-verification.test.ts +37 -0
  156. package/dist/template/tests/modules/preview-assistant.test.ts +164 -0
  157. package/dist/template-manifest.json +490 -175
  158. package/package.json +1 -1
@@ -0,0 +1,655 @@
1
+ #!/bin/sh
2
+ # shellcheck disable=SC3043,SC2010 # `local` is in dash, bash and busybox sh; listed names are our own timestamps and versions.
3
+ # Copyright (C) 2026 Tony Aly
4
+ # SPDX-License-Identifier: Apache-2.0
5
+ #
6
+ # `freeholder`: run a native (no Docker) Freeholder install (C3.44).
7
+ # install.sh puts this at /usr/local/bin/freeholder. Run `freeholder help`.
8
+ #
9
+ # Layout it manages (deploy/linux/README.md):
10
+ # /opt/freeholder/releases/VERSION unpacked release tarballs, root-owned
11
+ # /opt/freeholder/current symlink to the running release
12
+ # /etc/freeholder/freeholder.env settings and secrets (root:freeholder 640)
13
+ # /var/lib/freeholder/media uploaded media (local storage)
14
+ # /var/lib/freeholder/backups nightly backups (root 700)
15
+ # /etc/caddy/Caddyfile HTTPS reverse proxy
16
+ set -eu
17
+
18
+ REPOSITORY="CampDenman/freeholder"
19
+ OPT=/opt/freeholder
20
+ ETC=/etc/freeholder
21
+ ENV_FILE=$ETC/freeholder.env
22
+ DATA=/var/lib/freeholder
23
+ MEDIA=$DATA/media
24
+ BACKUPS=$DATA/backups
25
+ STATE=$DATA/state
26
+ CADDYFILE=/etc/caddy/Caddyfile
27
+ LOCAL_URL=http://127.0.0.1:3000
28
+
29
+ say() { printf 'freeholder: %s\n' "$*"; }
30
+ warn() { printf 'freeholder: warning: %s\n' "$*" >&2; }
31
+ die() { printf 'freeholder: error: %s\n' "$*" >&2; exit 1; }
32
+ need_root() { [ "$(id -u)" = 0 ] || die "run it as root: sudo freeholder $*"; }
33
+ interactive() { [ -t 0 ] && [ -z "${ASSUME_YES:-}" ]; }
34
+
35
+ # Functions declare their variables `local` (dash, bash and busybox sh all
36
+ # support it): restore calls backup, and a shared name would be overwritten.
37
+
38
+ # The release this copy of the script belongs to.
39
+ self_dir=$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd -P)
40
+ self_release=$(dirname -- "$self_dir")
41
+
42
+ env_get() {
43
+ [ -f "$ENV_FILE" ] || return 0
44
+ sed -n "s/^$1=//p" "$ENV_FILE" | tail -n 1
45
+ }
46
+
47
+ env_set() {
48
+ # Values are hex, URLs, paths or hostnames, so no quoting is needed and a
49
+ # value with whitespace or quotes is refused rather than mangled.
50
+ case "$2" in *[[:space:]\"\'\\]*) die "refusing to store a value for $1 with spaces, quotes or backslashes" ;; esac
51
+ tmp="$ENV_FILE.tmp.$$"
52
+ if [ -f "$ENV_FILE" ]; then grep -v "^$1=" "$ENV_FILE" > "$tmp" || true; else : > "$tmp"; fi
53
+ printf '%s=%s\n' "$1" "$2" >> "$tmp"
54
+ chown root:freeholder "$tmp"
55
+ chmod 640 "$tmp"
56
+ mv "$tmp" "$ENV_FILE"
57
+ }
58
+
59
+ secret() { openssl rand -hex 32; }
60
+
61
+ ensure_secret() {
62
+ [ -n "$(env_get "$1")" ] || env_set "$1" "$(secret)"
63
+ }
64
+
65
+ current_version() { [ -f "$OPT/current/VERSION" ] && cat "$OPT/current/VERSION" || echo unknown; }
66
+
67
+ app_url() { env_get APP_URL; }
68
+ site_host() { app_url | sed -e 's#^https\{0,1\}://##' -e 's#/.*$##'; }
69
+
70
+ # ---------------------------------------------------------------- addresses
71
+
72
+ private_ipv4() {
73
+ case "$1" in
74
+ 10.*|192.168.*|127.*|169.254.*|100.6[4-9].*|100.[7-9][0-9].*|100.1[01][0-9].*|100.12[0-7].*) return 0 ;;
75
+ 172.1[6-9].*|172.2[0-9].*|172.3[01].*) return 0 ;;
76
+ esac
77
+ return 1
78
+ }
79
+
80
+ public_ipv4() {
81
+ ip=$(ip -4 route get 1.1.1.1 2>/dev/null | awk '{for (i = 1; i < NF; i++) if ($i == "src") {print $(i + 1); exit}}')
82
+ if [ -z "$ip" ] || private_ipv4 "$ip"; then
83
+ # Behind NAT (most clouds other than DigitalOcean, Hetzner and Linode put
84
+ # a private address on the interface), ask what address the internet sees.
85
+ ip=$(curl -4 -fsS --max-time 10 https://checkip.amazonaws.com 2>/dev/null | tr -d '[:space:]' || true)
86
+ fi
87
+ case "$ip" in
88
+ *.*.*.*) printf '%s\n' "$ip" ;;
89
+ *) return 1 ;;
90
+ esac
91
+ }
92
+
93
+ sslip_host() { printf '%s.sslip.io\n' "$(printf '%s' "$1" | tr . -)"; }
94
+
95
+ resolves_here() {
96
+ # $1 host, $2 this server's public IPv4
97
+ getent ahostsv4 "$1" 2>/dev/null | awk '{print $1}' | grep -qx "$2"
98
+ }
99
+
100
+ valid_host() {
101
+ case "$1" in
102
+ ''|*[!A-Za-z0-9.-]*|.*|*.|*..*) return 1 ;;
103
+ *.*) return 0 ;;
104
+ esac
105
+ return 1
106
+ }
107
+
108
+ # ---------------------------------------------------------------- health
109
+
110
+ wait_for() {
111
+ # $1 url, $2 seconds
112
+ deadline=$(( $(date +%s) + $2 ))
113
+ while [ "$(date +%s)" -lt "$deadline" ]; do
114
+ if curl -fsS --max-time 10 -o /dev/null "$1" 2>/dev/null; then return 0; fi
115
+ sleep 3
116
+ done
117
+ return 1
118
+ }
119
+
120
+ restarts() { systemctl show -p NRestarts --value freeholder 2>/dev/null || echo 0; }
121
+
122
+ wait_ready() {
123
+ # Ready means /api/health answers 200. A release that crashes on start is
124
+ # restarted by systemd every few seconds; three crashes is a failure, not
125
+ # something to wait out.
126
+ # systemd keeps NRestarts across stop and start, so count from now.
127
+ local deadline baseline
128
+ say "waiting for Freeholder to start (migrations run first)"
129
+ deadline=$(( $(date +%s) + ${1:-240} ))
130
+ baseline=$(restarts)
131
+ while [ "$(date +%s)" -lt "$deadline" ]; do
132
+ if curl -fsS --max-time 10 -o /dev/null "$LOCAL_URL/api/health" 2>/dev/null; then return 0; fi
133
+ if [ "$(restarts)" -ge $((baseline + 3)) ]; then
134
+ warn "the service crashed three times while starting"
135
+ return 1
136
+ fi
137
+ sleep 3
138
+ done
139
+ return 1
140
+ }
141
+
142
+ # ---------------------------------------------------------------- caddy
143
+
144
+ write_caddyfile() {
145
+ host=$1
146
+ email=$(env_get ACME_EMAIL)
147
+ template="$OPT/current/share/Caddyfile.template"
148
+ [ -f "$template" ] || template="$self_release/share/Caddyfile.template"
149
+ if [ -f "$CADDYFILE" ] && ! grep -q 'managed-by-freeholder' "$CADDYFILE"; then
150
+ # The Caddy package installs a placeholder that serves :80. Anything else
151
+ # is somebody's configuration, so it is kept beside ours.
152
+ if ! grep -qE '^:80([[:space:]]|\{|$)' "$CADDYFILE"; then
153
+ die "$CADDYFILE has your own configuration. Move it to /etc/caddy/sites/ and import it, then rerun; nothing was changed."
154
+ fi
155
+ cp "$CADDYFILE" "$CADDYFILE.before-freeholder"
156
+ fi
157
+ if [ -n "$email" ]; then global="{
158
+ email $email
159
+ }"; else global=""; fi
160
+ tmp="$CADDYFILE.tmp.$$"
161
+ awk -v global="$global" -v host="$host" '{ gsub(/__GLOBAL__/, global); gsub(/__DOMAIN__/, host); print }' "$template" > "$tmp"
162
+ chmod 644 "$tmp"
163
+ if command -v caddy >/dev/null 2>&1; then
164
+ caddy validate --adapter caddyfile --config "$tmp" >/dev/null 2>&1 || { rm -f "$tmp"; die "the generated Caddyfile did not validate"; }
165
+ fi
166
+ mv "$tmp" "$CADDYFILE"
167
+ }
168
+
169
+ open_firewall() {
170
+ if command -v ufw >/dev/null 2>&1 && ufw status 2>/dev/null | grep -q '^Status: active'; then
171
+ ufw allow 80/tcp >/dev/null
172
+ ufw allow 443/tcp >/dev/null
173
+ say "opened ports 80 and 443 in ufw"
174
+ fi
175
+ }
176
+
177
+ choose_host() {
178
+ # $1 requested domain (may be empty). Prints the host to serve now.
179
+ requested=$1
180
+ ip=$(public_ipv4 || true)
181
+ if [ -n "$requested" ]; then
182
+ valid_host "$requested" || die "not a domain name: $requested"
183
+ if [ -n "$ip" ] && ! resolves_here "$requested" "$ip"; then
184
+ warn "$requested does not point at this server ($ip) yet." >&2
185
+ warn "using https://$(sslip_host "$ip") until it does; then run: sudo freeholder domain $requested" >&2
186
+ printf '%s\n' "$(sslip_host "$ip")"
187
+ return
188
+ fi
189
+ printf '%s\n' "$requested"
190
+ return
191
+ fi
192
+ [ -n "$ip" ] || die "could not find this server's public IPv4 address; pass --domain"
193
+ sslip_host "$ip"
194
+ }
195
+
196
+ # ---------------------------------------------------------------- setup
197
+
198
+ prompt() {
199
+ # $1 question, $2 default
200
+ printf '%s' "$1" > /dev/tty
201
+ IFS= read -r answer < /dev/tty || answer=""
202
+ [ -n "$answer" ] || answer=$2
203
+ printf '%s\n' "$answer"
204
+ }
205
+
206
+ prompt_secret() {
207
+ printf '%s' "$1" > /dev/tty
208
+ stty -echo < /dev/tty
209
+ IFS= read -r answer < /dev/tty || answer=""
210
+ stty echo < /dev/tty
211
+ printf '\n' > /dev/tty
212
+ printf '%s\n' "$answer"
213
+ }
214
+
215
+ configure_s3() {
216
+ for name in S3_ENDPOINT S3_REGION S3_BUCKET S3_ACCESS_KEY_ID S3_SECRET_ACCESS_KEY; do
217
+ value=$(printenv "$name" || true)
218
+ [ -n "$value" ] || value=$(env_get "$name")
219
+ if [ -z "$value" ]; then
220
+ interactive || die "--storage s3 needs $name (set it in the environment or run interactively)"
221
+ if [ "$name" = S3_SECRET_ACCESS_KEY ]; then value=$(prompt_secret "$name: "); else value=$(prompt "$name: " ""); fi
222
+ fi
223
+ [ -n "$value" ] || die "$name is required for S3 storage"
224
+ env_set "$name" "$value"
225
+ done
226
+ env_set FREEHOLDER_STORAGE s3
227
+ }
228
+
229
+ cmd_setup() {
230
+ need_root setup
231
+ domain="" storage="" email=""
232
+ while [ $# -gt 0 ]; do
233
+ case "$1" in
234
+ --domain) domain=$2; shift 2 ;;
235
+ --email) email=$2; shift 2 ;;
236
+ --storage) storage=$2; shift 2 ;;
237
+ --yes) ASSUME_YES=1; shift ;;
238
+ *) die "setup: unknown option $1" ;;
239
+ esac
240
+ done
241
+ id freeholder >/dev/null 2>&1 || die "run install.sh first; it creates the freeholder user and installs PostgreSQL and Caddy"
242
+
243
+ install -d -m 750 -o root -g freeholder "$ETC"
244
+ install -d -m 750 -o freeholder -g freeholder "$DATA" "$MEDIA" "$DATA/cache" "$DATA/cache/next"
245
+ install -d -m 700 -o root -g root "$BACKUPS" "$STATE"
246
+ [ -f "$ENV_FILE" ] || { : > "$ENV_FILE"; chown root:freeholder "$ENV_FILE"; chmod 640 "$ENV_FILE"; }
247
+
248
+ # Only the domain is a question, and an empty answer is fine.
249
+ existing=$(app_url)
250
+ if [ -z "$domain" ] && [ -z "$existing" ] && interactive; then
251
+ domain=$(prompt "Domain for this site (press Enter to use a temporary address and add the domain later): " "")
252
+ fi
253
+ if [ -n "$domain" ] || [ -z "$existing" ]; then
254
+ host=$(choose_host "$domain")
255
+ env_set APP_URL "https://$host"
256
+ fi
257
+ [ -z "$email" ] || env_set ACME_EMAIL "$email"
258
+
259
+ ensure_secret BOOTSTRAP_SECRET
260
+ ensure_secret SESSION_SECRET
261
+ ensure_secret CREDENTIAL_KEY
262
+ ensure_secret POSTGRES_PASSWORD
263
+ password=$(env_get POSTGRES_PASSWORD)
264
+ env_set DATABASE_URL "postgres://freeholder:${password}@127.0.0.1:5432/freeholder"
265
+ env_set TRUSTED_CLIENT_IP_HEADER x-freeholder-client-ip
266
+ [ -n "$(env_get FREEHOLDER_SEED_DEMO)" ] || env_set FREEHOLDER_SEED_DEMO 0
267
+ case "${storage:-$(env_get FREEHOLDER_STORAGE)}" in
268
+ s3) configure_s3 ;;
269
+ ""|local)
270
+ env_set FREEHOLDER_STORAGE local
271
+ env_set LOCAL_STORAGE_ROOT "$MEDIA" ;;
272
+ *) die "--storage is local or s3" ;;
273
+ esac
274
+
275
+ say "creating the database"
276
+ # The password travels on stdin, never in a process listing.
277
+ (cd / && runuser -u postgres -- psql -v ON_ERROR_STOP=1 -qtA postgres) <<SQL
278
+ SELECT 'CREATE ROLE freeholder LOGIN' WHERE NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'freeholder')\gexec
279
+ ALTER ROLE freeholder WITH LOGIN PASSWORD '${password}';
280
+ SELECT 'CREATE DATABASE freeholder OWNER freeholder' WHERE NOT EXISTS (SELECT 1 FROM pg_database WHERE datname = 'freeholder')\gexec
281
+ SQL
282
+
283
+ link_release_cache "$OPT/current"
284
+ install -m 644 "$OPT/current/share/systemd/freeholder.service" /etc/systemd/system/freeholder.service
285
+ install -m 644 "$OPT/current/share/systemd/freeholder-backup.service" /etc/systemd/system/freeholder-backup.service
286
+ install -m 644 "$OPT/current/share/systemd/freeholder-backup.timer" /etc/systemd/system/freeholder-backup.timer
287
+ systemctl daemon-reload
288
+ systemctl enable freeholder.service freeholder-backup.timer >/dev/null 2>&1
289
+ systemctl restart freeholder.service
290
+ systemctl start freeholder-backup.timer
291
+
292
+ host=$(site_host)
293
+ write_caddyfile "$host"
294
+ open_firewall
295
+ systemctl enable caddy >/dev/null 2>&1
296
+ systemctl reload-or-restart caddy
297
+
298
+ wait_ready 300 || die "Freeholder did not become ready; see: journalctl -u freeholder -n 100"
299
+ say "waiting for the HTTPS certificate for $host"
300
+ if wait_for "https://$host/api/health" 180; then
301
+ say "https://$host is live"
302
+ else
303
+ warn "https://$host is not answering yet. Check that ports 80 and 443 are open to the internet and that $host points at this server, then run: sudo freeholder status"
304
+ fi
305
+ echo
306
+ say "Your private owner claim link (open it yourself; anyone with it can claim the site until you do):"
307
+ claim_link
308
+ echo
309
+ say "Next: open that link, create your owner account, save the recovery codes."
310
+ say "Run 'sudo freeholder status' any time. Backups run nightly; 'sudo freeholder backup' runs one now."
311
+ }
312
+
313
+ link_release_cache() {
314
+ # Next writes its runtime cache in .next/cache; the release itself is
315
+ # read-only to the service, so the cache lives in the data directory.
316
+ cache="$1/app/.next/cache"
317
+ if [ ! -L "$cache" ]; then
318
+ rm -rf "$cache"
319
+ ln -s "$DATA/cache/next" "$cache"
320
+ fi
321
+ }
322
+
323
+ claim_link() {
324
+ url=$(app_url)
325
+ secret=$(env_get BOOTSTRAP_SECRET)
326
+ [ -n "$url" ] && [ -n "$secret" ] || die "no claim link: run 'sudo freeholder setup' first"
327
+ # The secret is hex, so it needs no URL encoding.
328
+ printf '%s/setup#claim=%s\n' "$url" "$secret"
329
+ }
330
+
331
+ cmd_claim_link() {
332
+ need_root claim-link
333
+ claim_link
334
+ say "It works until the owner account exists. Keep it out of chat, screenshots and tickets." >&2
335
+ }
336
+
337
+ # ---------------------------------------------------------------- status
338
+
339
+ cmd_status() {
340
+ failures=0
341
+ check() { if "$@" >/dev/null 2>&1; then printf ' ok %s\n' "$label"; else printf ' FAIL %s\n' "$label"; failures=$((failures + 1)); fi; }
342
+ note() { printf ' info %s\n' "$*"; }
343
+ echo "Freeholder $(current_version) at $(app_url)"
344
+ label="freeholder service running"; check systemctl is-active --quiet freeholder
345
+ label="PostgreSQL running"; check systemctl is-active --quiet postgresql
346
+ label="Caddy running"; check systemctl is-active --quiet caddy
347
+ label="app ready on 127.0.0.1:3000 (/api/health)"; check curl -fsS --max-time 10 "$LOCAL_URL/api/health"
348
+ host=$(site_host)
349
+ label="https://$host/api/health with a valid certificate"; check curl -fsS --max-time 15 "https://$host/api/health"
350
+ expiry=$(echo | openssl s_client -servername "$host" -connect "$host:443" 2>/dev/null | openssl x509 -noout -enddate 2>/dev/null | cut -d= -f2 || true)
351
+ [ -z "$expiry" ] || note "certificate valid until $expiry (Caddy renews it)"
352
+ if [ "$(env_get FREEHOLDER_STORAGE)" = local ]; then
353
+ label="media directory $MEDIA writable by the service"; check runuser -u freeholder -- test -w "$MEDIA"
354
+ note "media uses $(du -sh "$MEDIA" 2>/dev/null | cut -f1) on this server"
355
+ else
356
+ note "media is in S3-compatible storage"
357
+ fi
358
+ label="nightly backup timer enabled"; check systemctl is-enabled --quiet freeholder-backup.timer
359
+ if [ -d "$BACKUPS" ] && [ "$(id -u)" = 0 ]; then
360
+ latest=$(ls -1 "$BACKUPS" 2>/dev/null | grep -v '\.partial$' | sort | tail -n 1)
361
+ if [ -n "$latest" ]; then note "latest backup $latest"; else note "no backup yet (the first runs tonight; 'sudo freeholder backup' runs one now)"; fi
362
+ fi
363
+ free=$(df -P "$DATA" | awk 'NR == 2 {print int($4 / 1024)}')
364
+ label="at least 1 GB free on $DATA (${free} MB)"; check test "$free" -ge 1024
365
+ if [ "$failures" -gt 0 ]; then echo "$failures check(s) failed. Logs: journalctl -u freeholder -n 100"; return 1; fi
366
+ echo "All checks passed."
367
+ }
368
+
369
+ # ---------------------------------------------------------------- backup
370
+
371
+ pg_as_postgres() { (cd / && PGOPTIONS="--client-min-messages=warning" runuser -u postgres -- "$@"); }
372
+
373
+ cmd_backup() {
374
+ need_root backup
375
+ local keep reason stamp dir size
376
+ keep=$(env_get BACKUP_KEEP); keep=${keep:-14}
377
+ reason=nightly
378
+ while [ $# -gt 0 ]; do
379
+ case "$1" in
380
+ --scheduled) reason=nightly; shift ;;
381
+ --reason) reason=$2; shift 2 ;;
382
+ --keep) keep=$2; shift 2 ;;
383
+ *) die "backup: unknown option $1" ;;
384
+ esac
385
+ done
386
+ case "$keep" in ''|*[!0-9]*) die "BACKUP_KEEP must be a number" ;; esac
387
+ install -d -m 700 -o root -g root "$BACKUPS"
388
+ stamp=$(date -u +%Y-%m-%dT%H-%M-%SZ)
389
+ dir="$BACKUPS/$stamp"
390
+ rm -rf "$dir.partial"
391
+ install -d -m 700 "$dir.partial"
392
+ # Custom format: compressed, checksummed by pg_restore, restorable into a
393
+ # fresh database. Never --clean against the live one.
394
+ pg_as_postgres pg_dump --format=custom --no-owner --no-privileges freeholder > "$dir.partial/database.dump"
395
+ size=$(stat -c %s "$dir.partial/database.dump")
396
+ [ "$size" -ge 1024 ] || { rm -rf "$dir.partial"; die "the database dump is only $size bytes; refusing to keep a likely-empty backup"; }
397
+ if [ "$(env_get FREEHOLDER_STORAGE)" = local ] && [ -d "$MEDIA" ]; then
398
+ tar -C "$DATA" -czf "$dir.partial/media.tar.gz" media
399
+ fi
400
+ cp "$OPT/current/RELEASE.json" "$dir.partial/RELEASE.json" 2>/dev/null || true
401
+ printf 'reason=%s\nversion=%s\n' "$reason" "$(current_version)" > "$dir.partial/BACKUP"
402
+ (cd "$dir.partial" && sha256sum ./* > SHA256SUMS)
403
+ mv "$dir.partial" "$dir"
404
+ say "backup $stamp: database $(du -h "$dir/database.dump" | cut -f1)$([ -f "$dir/media.tar.gz" ] && printf ', media %s' "$(du -h "$dir/media.tar.gz" | cut -f1)") in $dir"
405
+ # Keep the newest $keep backups; pre-update backups are kept with them.
406
+ ls -1 "$BACKUPS" | grep -E '^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9-]+Z$' | sort | head -n "-$keep" | while read -r old; do
407
+ rm -rf "${BACKUPS:?}/$old"
408
+ done
409
+ offsite_copy "$dir"
410
+ printf '%s\n' "$stamp" > "$STATE/last-backup"
411
+ }
412
+
413
+ offsite_copy() {
414
+ # Optional: copy each backup to an S3-compatible bucket with rclone, if
415
+ # BACKUP_RCLONE_REMOTE names a configured remote (deploy/linux/README.md).
416
+ remote=$(env_get BACKUP_RCLONE_REMOTE)
417
+ [ -n "$remote" ] || return 0
418
+ command -v rclone >/dev/null 2>&1 || { warn "BACKUP_RCLONE_REMOTE is set but rclone is not installed (apt install rclone)"; return 0; }
419
+ if rclone copy "$1" "$remote/$(basename "$1")"; then say "copied to $remote"; else warn "offsite copy to $remote failed; the local backup is intact"; return 1; fi
420
+ }
421
+
422
+ # ---------------------------------------------------------------- restore
423
+
424
+ resolve_backup() {
425
+ case "$1" in
426
+ /*) printf '%s\n' "$1" ;;
427
+ latest) printf '%s/%s\n' "$BACKUPS" "$(ls -1 "$BACKUPS" | grep -E '^[0-9]{4}-' | grep -v '\.partial$' | sort | tail -n 1)" ;;
428
+ *) printf '%s/%s\n' "$BACKUPS" "$1" ;;
429
+ esac
430
+ }
431
+
432
+ restore_database() {
433
+ # $1 dump file. Restores into a scratch database, then swaps it in, so a
434
+ # failed restore leaves the current database untouched.
435
+ pg_as_postgres dropdb --if-exists freeholder_restore
436
+ pg_as_postgres createdb -O freeholder freeholder_restore
437
+ if ! pg_as_postgres pg_restore --no-owner --no-privileges --role=freeholder --exit-on-error -d freeholder_restore < "$1"; then
438
+ pg_as_postgres dropdb --if-exists freeholder_restore
439
+ return 1
440
+ fi
441
+ pg_as_postgres dropdb --if-exists freeholder_previous
442
+ pg_as_postgres psql -v ON_ERROR_STOP=1 -qtA postgres <<'SQL'
443
+ SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = 'freeholder' AND pid <> pg_backend_pid();
444
+ ALTER DATABASE freeholder RENAME TO freeholder_previous;
445
+ ALTER DATABASE freeholder_restore RENAME TO freeholder;
446
+ SQL
447
+ }
448
+
449
+ cmd_restore() {
450
+ need_root restore
451
+ local dir answer
452
+ [ $# -ge 1 ] || die "usage: sudo freeholder restore BACKUP (a name from 'ls $BACKUPS', or 'latest')"
453
+ dir=$(resolve_backup "$1")
454
+ [ -f "$dir/database.dump" ] && [ -f "$dir/SHA256SUMS" ] || die "$dir is not a Freeholder backup"
455
+ (cd "$dir" && sha256sum --quiet -c SHA256SUMS) || die "$dir failed its checksum; nothing was changed"
456
+ if interactive; then
457
+ answer=$(prompt "Replace the site's current data with the backup from $(basename "$dir")? A safety backup is taken first. [y/N] " n)
458
+ case "$answer" in y|Y|yes) ;; *) die "cancelled" ;; esac
459
+ fi
460
+ say "taking a safety backup of the current data"
461
+ cmd_backup --reason "before-restore"
462
+ systemctl stop freeholder
463
+ say "restoring the database"
464
+ if ! restore_database "$dir/database.dump"; then
465
+ systemctl start freeholder
466
+ die "the database restore failed; the current database was not changed"
467
+ fi
468
+ if [ -f "$dir/media.tar.gz" ]; then
469
+ say "restoring media"
470
+ rm -rf "$DATA/media.restore" "$DATA/media.previous"
471
+ install -d -m 750 -o freeholder -g freeholder "$DATA/media.restore"
472
+ tar -C "$DATA/media.restore" -xzf "$dir/media.tar.gz" --strip-components=1
473
+ chown -R freeholder:freeholder "$DATA/media.restore"
474
+ [ ! -d "$MEDIA" ] || mv "$MEDIA" "$DATA/media.previous"
475
+ mv "$DATA/media.restore" "$MEDIA"
476
+ fi
477
+ systemctl start freeholder
478
+ wait_ready 300 || die "Freeholder did not come back after the restore; see journalctl -u freeholder. The replaced database is kept as freeholder_previous."
479
+ say "restored $(basename "$dir"). The replaced database is kept as freeholder_previous and media as $DATA/media.previous until the next restore."
480
+ }
481
+
482
+ # ---------------------------------------------------------------- update
483
+
484
+ latest_version() {
485
+ # Releases are currently prereleases, which GitHub's /releases/latest
486
+ # skips, so take the newest published release from the list.
487
+ curl -fsSL --max-time 20 "https://api.github.com/repos/$REPOSITORY/releases?per_page=10" \
488
+ | sed -n 's/^ *"tag_name": *"v\{0,1\}\([^"]*\)".*/\1/p' | head -n 1
489
+ }
490
+
491
+ machine_arch() {
492
+ case "$(uname -m)" in
493
+ x86_64|amd64) echo x64 ;;
494
+ aarch64|arm64) echo arm64 ;;
495
+ *) die "unsupported architecture $(uname -m)" ;;
496
+ esac
497
+ }
498
+
499
+ fetch_release() {
500
+ # $1 version, $2 optional local tarball. Unpacks to releases/$1.
501
+ local version release name work base expected actual top
502
+ version=$1
503
+ release="$OPT/releases/$version"
504
+ [ ! -x "$release/bin/freeholder" ] || return 0
505
+ name="freeholder-$version-linux-$(machine_arch)"
506
+ work=$(mktemp -d)
507
+ if [ -n "${2:-}" ]; then
508
+ cp "$2" "$work/$name.tar.gz"; cp "$2.sha256" "$work/$name.tar.gz.sha256"
509
+ else
510
+ base="https://github.com/$REPOSITORY/releases/download/v$version"
511
+ say "downloading $name"
512
+ curl -fsSL --retry 3 -o "$work/$name.tar.gz" "$base/$name.tar.gz" || { rm -rf "$work"; die "could not download $base/$name.tar.gz"; }
513
+ curl -fsSL --retry 3 -o "$work/$name.tar.gz.sha256" "$base/$name.tar.gz.sha256" || { rm -rf "$work"; die "could not download its checksum"; }
514
+ fi
515
+ expected=$(awk '{print $1; exit}' "$work/$name.tar.gz.sha256")
516
+ actual=$(sha256sum "$work/$name.tar.gz" | awk '{print $1}')
517
+ [ -n "$expected" ] && [ "$expected" = "$actual" ] || { rm -rf "$work"; die "checksum mismatch; nothing was changed"; }
518
+ say "checksum verified ($actual)"
519
+ tar -xzf "$work/$name.tar.gz" -C "$work"
520
+ top=$(tar -tzf "$work/$name.tar.gz" | head -n 1 | cut -d/ -f1)
521
+ [ -x "$work/$top/bin/freeholder" ] || { rm -rf "$work"; die "the tarball is not a Freeholder release"; }
522
+ rm -rf "$release.partial"
523
+ mv "$work/$top" "$release.partial"
524
+ chown -R root:root "$release.partial"
525
+ mv "$release.partial" "$release"
526
+ rm -rf "$work"
527
+ }
528
+
529
+ switch_to() {
530
+ link_release_cache "$OPT/releases/$1"
531
+ ln -sfn "releases/$1" "$OPT/current.new"
532
+ mv -T "$OPT/current.new" "$OPT/current"
533
+ # Units may change between releases.
534
+ for unit in freeholder.service freeholder-backup.service freeholder-backup.timer; do
535
+ install -m 644 "$OPT/current/share/systemd/$unit" "/etc/systemd/system/$unit"
536
+ done
537
+ systemctl daemon-reload
538
+ }
539
+
540
+ cmd_update() {
541
+ need_root update
542
+ local target tarball from backup
543
+ target="" tarball=""
544
+ while [ $# -gt 0 ]; do
545
+ case "$1" in
546
+ --tarball) tarball=$2; shift 2 ;;
547
+ latest) target=$(latest_version); [ -n "$target" ] || die "could not look up the latest release"; shift ;;
548
+ *) target=${1#v}; shift ;;
549
+ esac
550
+ done
551
+ [ -n "$target" ] || die "usage: sudo freeholder update VERSION (or 'latest'); see https://github.com/$REPOSITORY/releases"
552
+ case "$target" in *[!0-9A-Za-z.-]*) die "not a release version: $target" ;; esac
553
+ from=$(current_version)
554
+ [ "$from" != "$target" ] || { say "already running $target"; return 0; }
555
+ fetch_release "$target" "$tarball"
556
+ say "backing up before the update"
557
+ cmd_backup --reason "before-update-to-$target"
558
+ backup=$(cat "$STATE/last-backup")
559
+ printf 'from=%s\nto=%s\nbackup=%s\n' "$from" "$target" "$backup" > "$STATE/last-update"
560
+ say "switching $from -> $target"
561
+ switch_to "$target"
562
+ systemctl restart freeholder
563
+ if wait_ready 300; then
564
+ say "updated to $target. To go back: sudo freeholder rollback"
565
+ ls -1 "$OPT/releases" | grep -vx -e "$target" -e "$from" | grep -v '\.partial$' | sort | head -n -1 | while read -r old; do rm -rf "${OPT:?}/releases/$old"; done
566
+ return 0
567
+ fi
568
+ warn "$target did not become ready; rolling back to $from"
569
+ rollback_to "$from" "$backup"
570
+ die "the update to $target failed and was rolled back to $from (database restored from backup $backup)"
571
+ }
572
+
573
+ rollback_to() {
574
+ # $1 version, $2 backup taken before the update. The newer release may have
575
+ # migrated the schema, so the database comes back from that backup too.
576
+ systemctl stop freeholder
577
+ switch_to "$1"
578
+ restore_database "$BACKUPS/$2/database.dump" || die "restoring $2 failed; the site is stopped. Run: sudo freeholder restore $2"
579
+ systemctl start freeholder
580
+ wait_ready 300 || die "$1 did not become ready after rollback; see journalctl -u freeholder"
581
+ }
582
+
583
+ cmd_rollback() {
584
+ need_root rollback
585
+ local from backup answer
586
+ [ -f "$STATE/last-update" ] || die "no update to roll back"
587
+ from=$(sed -n 's/^from=//p' "$STATE/last-update")
588
+ backup=$(sed -n 's/^backup=//p' "$STATE/last-update")
589
+ [ -x "$OPT/releases/$from/bin/freeholder" ] || die "release $from is no longer on this server"
590
+ if interactive; then
591
+ answer=$(prompt "Go back to $from and restore the database from backup $backup? Changes made since the update are lost (a safety backup is taken first). [y/N] " n)
592
+ case "$answer" in y|Y|yes) ;; *) die "cancelled" ;; esac
593
+ fi
594
+ cmd_backup --reason "before-rollback"
595
+ rollback_to "$from" "$backup"
596
+ rm -f "$STATE/last-update"
597
+ say "rolled back to $from"
598
+ }
599
+
600
+ # ---------------------------------------------------------------- domain
601
+
602
+ cmd_domain() {
603
+ need_root domain
604
+ [ $# -eq 1 ] || die "usage: sudo freeholder domain example.com"
605
+ valid_host "$1" || die "not a domain name: $1"
606
+ ip=$(public_ipv4 || true)
607
+ if [ -n "$ip" ] && ! resolves_here "$1" "$ip"; then
608
+ die "$1 does not resolve to this server ($ip) yet. Point its A record here, wait for DNS, and run this again."
609
+ fi
610
+ old=$(site_host)
611
+ env_set APP_URL "https://$1"
612
+ write_caddyfile "$1"
613
+ systemctl reload-or-restart caddy
614
+ systemctl restart freeholder
615
+ wait_ready 300 || die "Freeholder did not become ready; see journalctl -u freeholder"
616
+ if wait_for "https://$1/api/health" 180; then say "https://$1 is live (was $old)"; else warn "https://$1 is not answering yet; check ports 80/443 and DNS, then: sudo freeholder status"; fi
617
+ say "Passkeys are tied to a domain: sign in at https://$1 with your password and add a new passkey in Settings."
618
+ }
619
+
620
+ # ---------------------------------------------------------------- dispatch
621
+
622
+ usage() {
623
+ cat <<'USAGE'
624
+ Usage: sudo freeholder COMMAND
625
+
626
+ status Check the service, database, HTTPS certificate, storage and backups.
627
+ claim-link Print the private owner claim link (until the owner account exists).
628
+ backup Back up the database and media now (also runs nightly).
629
+ restore BACKUP Restore a backup by name, path or 'latest' (takes a safety backup first).
630
+ update VERSION Download, verify and switch to a release; rolls back if it fails.
631
+ update latest The same, for the newest published release.
632
+ rollback Undo the last update, restoring the database from its pre-update backup.
633
+ domain NAME Move the site to NAME (after its DNS points here).
634
+ setup Re-apply settings, units and Caddy configuration (install.sh runs it).
635
+ logs Follow the application log.
636
+ version Print the installed version.
637
+ USAGE
638
+ }
639
+
640
+ command=${1:-help}
641
+ [ $# -eq 0 ] || shift
642
+ case "$command" in
643
+ setup) cmd_setup "$@" ;;
644
+ status|check|doctor) need_root status; cmd_status ;;
645
+ claim-link) cmd_claim_link ;;
646
+ backup) cmd_backup "$@" ;;
647
+ restore) cmd_restore "$@" ;;
648
+ update) cmd_update "$@" ;;
649
+ rollback) cmd_rollback ;;
650
+ domain) cmd_domain "$@" ;;
651
+ logs) exec journalctl -u freeholder -f ;;
652
+ version) current_version ;;
653
+ help|--help|-h) usage ;;
654
+ *) usage >&2; exit 2 ;;
655
+ esac