Repository navigation
Expand file tree
/
Copy pathbuzz-ctl
More file actions
executable file
·420 lines (376 loc) · 14.7 KB
/
Copy pathbuzz-ctl
File metadata and controls
executable file
·420 lines (376 loc) · 14.7 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
419
420
#!/bin/bash
# SPDX-License-Identifier: MIT
# SPDX-FileCopyrightText: 2026 Marcus Quinn
set -euo pipefail
readonly OWNER_FILE="/app/data/config/owner-public-key"
readonly ADMIN_HOST_FILE="/app/data/config/admin-host"
readonly FQDN_PATTERN='^([a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,63}$'
readonly RELAY_LOCAL_URL="http://127.0.0.1:3000"
readonly RELAY_PUBLIC_KEY_FILE="/app/data/config/relay-public-key"
readonly RELAY_PRIVATE_KEY_FILE="/app/data/config/relay-private-key"
readonly SUPERVISOR_CONFIG="/app/code/supervisord.conf"
readonly STORAGE_RECORD="/app/data/config/storage-migration.json"
readonly LEGACY_DIR="/app/data/minio"
readonly LEGACY_ACCESS_KEY="/app/data/config/minio-access-key"
readonly LEGACY_SECRET_KEY="/app/data/config/minio-secret-key"
fail() {
local message="$1"
printf "ERROR: %s\n" "$message" >&2
return 1
}
read_value() {
local file_path="$1"
local label="$2"
local value=""
if [[ -L "$file_path" || ! -s "$file_path" ]]; then
fail "${label} file is unavailable" || return 1
fi
value=$(<"$file_path")
printf "%s" "$value"
return 0
}
load_admin_environment() {
local relay_private_key=""
case "${CLOUDRON_POSTGRESQL_URL:-}" in
postgres://* | postgresql://*) ;;
*) fail "CLOUDRON_POSTGRESQL_URL is missing or invalid" || return 1 ;;
esac
case "${CLOUDRON_REDIS_URL:-}" in
redis://* | rediss://*) ;;
*) fail "CLOUDRON_REDIS_URL is missing or invalid" || return 1 ;;
esac
if [[ -z "${CLOUDRON_APP_DOMAIN:-}" ]]; then
fail "CLOUDRON_APP_DOMAIN is not set" || return 1
fi
relay_private_key=$(read_value "$RELAY_PRIVATE_KEY_FILE" "relay private key") || return 1
export DATABASE_URL="${CLOUDRON_POSTGRESQL_URL}"
export REDIS_URL="${CLOUDRON_REDIS_URL}"
export RELAY_URL="wss://${CLOUDRON_APP_DOMAIN}"
export BUZZ_RELAY_PRIVATE_KEY="$relay_private_key"
return 0
}
# Atomically replaces a private config file owned by the relay user.
write_config_file() {
local file_path="$1"
local value="$2"
local temporary_file=""
if [[ -L "$file_path" ]]; then
fail "Refusing symlinked ${file_path}" || return 1
fi
temporary_file=$(mktemp "${file_path}.tmp.XXXXXX") || return 1
chmod 0600 "$temporary_file"
printf "%s\n" "$value" >"$temporary_file"
chown cloudron:cloudron "$temporary_file"
mv -f "$temporary_file" "$file_path"
return 0
}
write_owner() {
local owner_pubkey="$1"
if [[ ! "$owner_pubkey" =~ ^[0-9a-f]{64}$ ]]; then
fail "Normalized owner key is invalid" || return 1
fi
write_config_file "$OWNER_FILE" "$owner_pubkey" || return 1
return 0
}
wait_for_relay() {
local attempt=0
local max_attempts=90
while ((attempt < max_attempts)); do
if curl --fail --silent --show-error http://127.0.0.1:3000/_readiness >/dev/null 2>&1; then
return 0
fi
((attempt += 1))
sleep 1
done
fail "Buzz did not become ready within ${max_attempts} seconds" || return 1
}
add_member() {
local member_pubkey="$1"
local member_role="${2:-member}"
if [[ "$member_role" != "member" && "$member_role" != "admin" ]]; then
fail "Role must be member or admin" || return 1
fi
load_admin_environment || return 1
/app/code/bin/buzz-admin add-member --pubkey "$member_pubkey" --role "$member_role"
return 0
}
remove_member() {
local member_pubkey="$1"
local member_role="${2:-}"
load_admin_environment || return 1
if [[ -n "$member_role" ]]; then
if [[ "$member_role" != "member" && "$member_role" != "admin" ]]; then
fail "Role must be member or admin" || return 1
fi
/app/code/bin/buzz-admin remove-member --pubkey "$member_pubkey" --role "$member_role"
else
/app/code/bin/buzz-admin remove-member --pubkey "$member_pubkey"
fi
return 0
}
set_owner() {
local requested_pubkey="$1"
local output=""
local normalized_pubkey=""
local previous_owner=""
local relay_public_key=""
# Do not restart the relay while storage setup has quiesced it.
exec 8>/run/buzz/storage-migration.lock
flock -n 8 || { fail "Storage setup, legacy purge, or owner change is in progress" || return 1; }
load_admin_environment || return 1
previous_owner=$(read_value "$OWNER_FILE" "owner public key") || return 1
relay_public_key=$(read_value "$RELAY_PUBLIC_KEY_FILE" "relay public key") || return 1
if ! output=$(/app/code/bin/buzz-admin add-member --pubkey "$requested_pubkey" --role admin 2>&1); then
printf "%s\n" "$output" >&2
return 1
fi
printf "%s\n" "$output"
if [[ "$output" =~ (added|already[[:space:]]a[[:space:]]member:)[[:space:]]+([0-9a-f]{64}) ]]; then
normalized_pubkey="${BASH_REMATCH[2]}"
else
fail "Could not normalize the requested owner public key" || return 1
fi
write_owner "$normalized_pubkey" || return 1
/usr/bin/supervisorctl --configuration "$SUPERVISOR_CONFIG" restart buzz-relay
wait_for_relay || return 1
if [[ "$previous_owner" == "$relay_public_key" && "$normalized_pubkey" != "$relay_public_key" ]]; then
if ! /app/code/bin/buzz-admin remove-member --pubkey "$relay_public_key" --role admin; then
printf "WARNING: Human owner is active, but the bootstrap relay identity could not be removed from membership.\n" >&2
fi
fi
printf "Owner is now %s\n" "$normalized_pubkey"
return 0
}
show_owner() {
local owner_pubkey=""
local relay_pubkey=""
owner_pubkey=$(read_value "$OWNER_FILE" "owner public key") || return 1
relay_pubkey=$(read_value "$RELAY_PUBLIC_KEY_FILE" "relay public key") || return 1
printf "Owner public key: %s\n" "$owner_pubkey"
printf "Relay public key: %s\n" "$relay_pubkey"
return 0
}
list_members() {
load_admin_environment || return 1
/app/code/bin/buzz-admin list-members
return 0
}
status() {
# The exited one-shot storage-setup program is reported as EXITED (supervisor exit 3).
/usr/bin/supervisorctl --configuration "$SUPERVISOR_CONFIG" status || true
printf "\nRelay readiness:\n"
curl --fail --silent --show-error --max-time 5 http://127.0.0.1:3000/_readiness || return 1
printf "\n\nStorage status:\n"
storage_status || return 1
[[ "$(jq -er '.active' /run/buzz/storage.json)" == rustfs ]] || return 1
curl --fail --silent --show-error --max-time 5 http://127.0.0.1:9100/health/ready || return 1
printf "\n"
return 0
}
storage_status() {
jq '{active,state,source_count,destination_count,timestamp,package_version}' "$STORAGE_RECORD" || return 1
if [[ -e "$LEGACY_DIR" ]]; then
printf "Legacy Silo/MinIO data retained at %s (remove with storage-purge-legacy --confirm)\n" "$LEGACY_DIR"
fi
return 0
}
# The legacy Silo credentials protect nothing once the data is gone.
remove_legacy_credentials() {
rm -f -- "$LEGACY_ACCESS_KEY" "$LEGACY_SECRET_KEY" || { fail "Could not remove legacy Silo credentials" || return 1; }
return 0
}
# Deletes legacy Silo/MinIO data only after a verified migration (or when
# migration found no objects), and only with explicit operator confirmation.
storage_purge_legacy() {
local confirmation="$1"
local state=""
[[ "$EUID" == 0 ]] || { fail "storage-purge-legacy requires root" || return 1; }
exec 8>/run/buzz/storage-migration.lock
flock -n 8 || { fail "Storage setup, legacy purge, or owner change is in progress" || return 1; }
[[ ! -L "$STORAGE_RECORD" && -f "$STORAGE_RECORD" ]] || { fail "Storage migration record is unavailable" || return 1; }
state=$(jq -er '.state' "$STORAGE_RECORD") || { fail "Storage migration record is unreadable" || return 1; }
if [[ "$state" != migrated && "$state" != not-needed ]]; then
fail "Refusing to purge: storage state is '${state}', not 'migrated'" || return 1
fi
if [[ ! -e "$LEGACY_DIR" && ! -L "$LEGACY_DIR" ]]; then
if [[ ! -e "$LEGACY_ACCESS_KEY" && ! -e "$LEGACY_SECRET_KEY" ]]; then
printf "No legacy data at %s; nothing to purge.\n" "$LEGACY_DIR"
return 0
fi
if [[ "$confirmation" != --confirm ]]; then
printf "No legacy data at %s, but unused legacy Silo credentials remain. Re-run with --confirm to delete them.\n" "$LEGACY_DIR"
return 1
fi
remove_legacy_credentials || return 1
printf "Removed unused legacy Silo credentials; no legacy data at %s.\n" "$LEGACY_DIR"
return 0
fi
if [[ "$confirmation" != --confirm ]]; then
printf "This permanently deletes %s (%s of legacy Silo/MinIO data).\n" "$LEGACY_DIR" "$(du -sh "$LEGACY_DIR" 2>/dev/null | cut -f1)"
printf "RustFS at /app/data/rustfs is the active store (state: %s). Re-run with --confirm to delete.\n" "$state"
return 1
fi
rm -rf --one-file-system -- "$LEGACY_DIR" || { fail "Could not remove ${LEGACY_DIR}" || return 1; }
remove_legacy_credentials || return 1
printf "Removed legacy Silo/MinIO data at %s.\n" "$LEGACY_DIR"
return 0
}
is_alias_domain() {
local host="$1"
local alias_domain=""
local alias_domains=()
IFS=',' read -r -a alias_domains <<<"${CLOUDRON_ALIAS_DOMAINS:-}"
for alias_domain in "${alias_domains[@]}"; do
alias_domain="${alias_domain//[[:space:]]/}"
[[ "${alias_domain,,}" != "$host" ]] || return 0
done
return 1
}
# Prints the admin_api origin the running relay advertises in NIP-11, if any.
active_admin_api() {
curl --fail --silent --show-error --max-time 5 \
--header 'Accept: application/nostr+json' "${RELAY_LOCAL_URL}/" |
jq -r '.admin_api // empty' || return 1
return 0
}
restart_relay() {
/usr/bin/supervisorctl --configuration "$SUPERVISOR_CONFIG" restart buzz-relay || return 1
wait_for_relay || return 1
return 0
}
admin_enable() {
local requested_host="$1"
local host="${requested_host,,}"
local advertised=""
host="${host%.}"
if [[ ! "$host" =~ $FQDN_PATTERN ]]; then
fail "Admin host must be a fully qualified domain name" || return 1
fi
if [[ "$host" == "${CLOUDRON_APP_DOMAIN:-}" ]]; then
fail "The admin host must be a dedicated alias, not the relay domain ${host}" || return 1
fi
if ! is_alias_domain "$host"; then
fail "${host} is not a Cloudron alias of this app. Add it under the app's Location settings first; current aliases: ${CLOUDRON_ALIAS_DOMAINS:-none}" || return 1
fi
exec 8>/run/buzz/storage-migration.lock
flock -n 8 || { fail "Storage setup, legacy purge, owner or admin change is in progress" || return 1; }
write_config_file "$ADMIN_HOST_FILE" "$host" || return 1
restart_relay || return 1
advertised=$(active_admin_api) || return 1
if [[ "$advertised" != "https://${host}" ]]; then
fail "The relay does not advertise https://${host} (got '${advertised:-none}'); check the relay log" || return 1
fi
printf "Deployment-admin dashboard enabled at https://%s\n" "$host"
printf "Sign in with the owner identity through a NIP-07 browser extension or the Buzz desktop Admin Console.\n"
return 0
}
admin_disable() {
local advertised=""
exec 8>/run/buzz/storage-migration.lock
flock -n 8 || { fail "Storage setup, legacy purge, owner or admin change is in progress" || return 1; }
if [[ ! -e "$ADMIN_HOST_FILE" && ! -L "$ADMIN_HOST_FILE" ]]; then
# A previous attempt may have removed the file but failed to restart.
advertised=$(active_admin_api) || return 1
if [[ -z "$advertised" ]]; then
printf "Deployment-admin host is already disabled.\n"
return 0
fi
fi
rm -f -- "$ADMIN_HOST_FILE" || { fail "Could not remove ${ADMIN_HOST_FILE}" || return 1; }
restart_relay || return 1
advertised=$(active_admin_api) || return 1
if [[ -n "$advertised" ]]; then
fail "The relay still advertises ${advertised}; check the relay log" || return 1
fi
printf "Deployment-admin host disabled. You can now remove the Cloudron alias.\n"
return 0
}
admin_status() {
local configured=""
local advertised=""
if [[ -L "$ADMIN_HOST_FILE" ]]; then
configured="(invalid: symlink)"
elif [[ -f "$ADMIN_HOST_FILE" ]]; then
configured=$(<"$ADMIN_HOST_FILE")
fi
advertised=$(active_admin_api) || return 1
printf "Configured admin host: %s\n" "${configured:-none}"
printf "Cloudron alias domains: %s\n" "${CLOUDRON_ALIAS_DOMAINS:-none}"
printf "Active admin_api: %s\n" "${advertised:-disabled}"
if [[ -n "$configured" && "$advertised" != "https://${configured}" ]]; then
printf "The configured host is not active. Re-add it as an alias, or run buzz-ctl admin-disable.\n"
fi
return 0
}
feedback_list() {
local option="$1"
local limit="$2"
if [[ -n "$option" ]]; then
[[ "$option" == --limit ]] || { fail "Usage: feedback-list [--limit 1-1000]" || return 1; }
else
limit=100
fi
if [[ ! "$limit" =~ ^[0-9]{1,4}$ ]] || ((10#$limit < 1 || 10#$limit > 1000)); then
fail "--limit must be an integer from 1 to 1000" || return 1
fi
load_admin_environment || return 1
/app/code/bin/buzz-admin product-feedback list --limit "$((10#$limit))" || return 1
return 0
}
usage() {
printf "%s\n" \
"Usage: buzz-ctl <command> [arguments]" \
"" \
"Commands:" \
" owner Show owner and relay public keys" \
" set-owner <npub-or-hex> Set and activate the human owner" \
" add-member <npub-or-hex> [member|admin] Add a relay member" \
" remove-member <npub-or-hex> [role] Remove a non-owner member" \
" list-members List relay members" \
" admin-enable <alias-domain> Serve the deployment-admin dashboard on a Cloudron alias" \
" admin-disable Turn the deployment-admin dashboard off" \
" admin-status Show the configured and active admin host" \
" feedback-list [--limit N] List in-app product feedback (N: 1-1000, default 100)" \
" status Show process and dependency health" \
" storage-status Show active store and migration counts" \
" storage-purge-legacy [--confirm] Delete migrated legacy Silo data (root)"
return 0
}
main() {
local command_name="${1:-help}"
local first_argument="${2:-}"
local second_argument="${3:-}"
case "$command_name" in
owner) show_owner || return 1 ;;
set-owner)
[[ -n "$first_argument" ]] || { fail "set-owner requires an npub or hexadecimal public key" || return 1; }
set_owner "$first_argument" || return 1
;;
add-member)
[[ -n "$first_argument" ]] || { fail "add-member requires an npub or hexadecimal public key" || return 1; }
add_member "$first_argument" "${second_argument:-member}" || return 1
;;
remove-member)
[[ -n "$first_argument" ]] || { fail "remove-member requires an npub or hexadecimal public key" || return 1; }
remove_member "$first_argument" "$second_argument" || return 1
;;
list-members) list_members || return 1 ;;
admin-enable)
[[ -n "$first_argument" ]] || { fail "admin-enable requires a Cloudron alias domain" || return 1; }
admin_enable "$first_argument" || return 1
;;
admin-disable) admin_disable || return 1 ;;
admin-status) admin_status || return 1 ;;
feedback-list) feedback_list "$first_argument" "$second_argument" || return 1 ;;
status) status || return 1 ;;
storage-status) storage_status || return 1 ;;
storage-purge-legacy) storage_purge_legacy "$first_argument" || return 1 ;;
help | --help | -h) usage || return 1 ;;
*)
usage
fail "Unknown command: ${command_name}" || return 1
;;
esac
return 0
}
main "$@"