Native install: mandatory install directory, UI delivered from the repository

- install.sh always asks for the install directory (--prefix or a prompt
  with confirmation, no default); refuses system directories, the
  repository checkout itself, paths inside/containing it and non-empty
  directories (a resumed install is allowed). A preflight refusal says
  nothing was changed.
- UI is part of the repository: committed build in deploy/ui-dist with
  SOURCE_HASH and a staleness guard (check-ui.sh, loud warning). install.sh
  and update.sh resolve the UI: --ui-dist FILE|DIR, --build-ui, committed
  build; install stops without a UI unless --no-ui. update.sh swaps the UI
  when its hash differs from the installed one (.ui-source-hash).
- build-ui.sh: --docker, --local (host Node >= 20.19, temporary copy),
  --node-download (portable Node, SHA-256 verified), --publish.
- Verified on Alpine 3.23: install without UI options installs the real UI;
  local build equals the Docker build; update skips an unchanged UI and
  warns on a stale build. --node-download is untested (needs network).
- Docs: Scenario 2 guide, change summary, plan, README.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
ayurishchevandClaude Sonnet 5.5 committed 2026-10-05 12:38:18 +03:00
1 parent 4abd2044f0
commit 8371d889a4
31 files changed
+920 -114

No files matched your search

+155 -8
View File
@@ -714,19 +714,166 @@ ensure_runtime_files() {
fi
}
# check_ui_archive FILE: a readable tarball with index.html
check_ui_archive() {
[ -f "$1" ] || die "UI archive not found: $1 (build it with deploy/native/build-ui.sh)"
tar -tzf "$1" >/dev/null 2>&1 || die "UI archive is not a readable .tar.gz: $1"
tar -tzf "$1" | grep -q -E '^(\./)?index\.html$' || die "UI archive has no index.html at its top level: $1"
# ---- UI: sources, committed build, staging ----
# The UI is part of the repository: deploy/ui-dist holds a build of APP_UI together with SOURCE_HASH, the hash
# of the sources it was built from (ui_source_hash). The installed web root records the hash it came from in
# $UI_HASH_FILE, so update.sh can tell when the UI changed. See DOCS/Deployment/Scenario2_System_Services.md
UI_HASH_FILE=.ui-source-hash
# sha256_run [FILE...]: sha256sum, or shasum -a 256 (macOS); "hash name" lines, stdin when no file is given
sha256_run() {
if command -v sha256sum >/dev/null 2>&1; then sha256sum "$@"
elif command -v shasum >/dev/null 2>&1; then shasum -a 256 "$@"
else die "neither sha256sum nor shasum found"
fi
}
# stage_ui FILE DIR: extract into a fresh DIR (never into the live web root)
# valid_hash VALUE: 64 hexadecimal digits
valid_hash() {
case "$1" in
""|*[!0-9a-f]*) return 1 ;;
esac
[ "${#1}" -eq 64 ]
}
# ui_source_hash [APP_UI_DIR]: SHA-256 over the UI sources (default: this repository's APP_UI), deterministic and
# the same on busybox and GNU: relative paths of src/, public/ and the build configuration files in C-locale
# order, the sha256 of every file with its path, then the sha256 of that list. node_modules and dist are never
# part of it. Prints the hash; fails when there are no sources.
ui_source_hash() {
local d
d="${1:-$ROOT_DIR/APP_UI}"
[ -f "$d/package.json" ] && [ -d "$d/src" ] || return 1
command -v sha256sum >/dev/null 2>&1 || command -v shasum >/dev/null 2>&1 || return 1
(
cd "$d" || exit 1
{
for f in index.html package.json package-lock.json vite.config.js jsconfig.json; do
if [ -f "$f" ]; then echo "$f"; fi
done
find src public -type f ! -name .DS_Store 2>/dev/null
} | LC_ALL=C sort | while IFS= read -r f; do sha256_run "$f"; done | sha256_run | awk '{ print $1 }'
)
}
# ui_hash_of SOURCE: the SOURCE_HASH stored in a UI directory or tarball, "unknown" when there is none
ui_hash_of() {
local h
h=""
if [ -d "$1" ]; then
h=$(tr -d ' \t\r\n' 2>/dev/null < "$1/SOURCE_HASH" || true)
elif [ -f "$1" ]; then
h=$(tar -xzOf "$1" ./SOURCE_HASH 2>/dev/null || tar -xzOf "$1" SOURCE_HASH 2>/dev/null || true)
h=$(printf '%s' "$h" | tr -d ' \t\r\n')
fi
if valid_hash "$h"; then printf '%s' "$h"; else printf 'unknown'; fi
}
# ui_repo_state REPO_DIR: sets UI_STATE (fresh | stale | missing), UI_BUILT_HASH (SOURCE_HASH of the committed
# build) and UI_CUR_HASH (hash of the APP_UI sources now). A build without a valid SOURCE_HASH counts as stale
# (it cannot be shown to match). Returns 1 when the hash of the sources cannot be computed.
ui_repo_state() {
local dir
dir="$1/deploy/ui-dist"
UI_STATE=missing; UI_BUILT_HASH=""; UI_CUR_HASH=""
[ -f "$dir/index.html" ] || return 0
UI_CUR_HASH=$(ui_source_hash "$1/APP_UI") || { UI_CUR_HASH=""; return 1; }
UI_BUILT_HASH=$(ui_hash_of "$dir")
if [ "$UI_BUILT_HASH" = "$UI_CUR_HASH" ]; then UI_STATE=fresh; else UI_STATE=stale; fi
}
# ui_stale_lines: the explanation printed when the committed build is older than the sources (one line each)
ui_stale_lines() {
echo "the committed UI build deploy/ui-dist is OLDER than the APP_UI sources (build: ${UI_BUILT_HASH:-none}, sources: $UI_CUR_HASH)."
echo "It does NOT contain the latest UI changes."
echo "Maintainers: run deploy/native/build-ui.sh --publish and commit deploy/ui-dist."
echo "On this host: add --build-ui to build the current UI now (Docker, or Node 20.19+ / --node-download; see build-ui.sh --help)."
}
# ui_build_check / ui_build_run REPO OUT: prerequisites of the build, the build into the tarball OUT (build-ui.sh
# picks Docker when it works, otherwise the local Node; UI_NODE_DL=1 adds --node-download)
ui_build_check() {
[ -f "$SCRIPT_DIR/build-ui.sh" ] || die "$SCRIPT_DIR/build-ui.sh not found"
if [ "${UI_NODE_DL:-0}" = 1 ]; then sh "$SCRIPT_DIR/build-ui.sh" --check --node-download; else sh "$SCRIPT_DIR/build-ui.sh" --check; fi \
|| die "the UI cannot be built on this host (see above)"
}
ui_build_run() {
log "building the UI from $1/APP_UI (--build-ui); this takes a few minutes"
if [ "${UI_NODE_DL:-0}" = 1 ]; then sh "$SCRIPT_DIR/build-ui.sh" --repo "$1" -o "$2" --node-download; else sh "$SCRIPT_DIR/build-ui.sh" --repo "$1" -o "$2"; fi \
|| die "the UI build failed (see above)"
}
# check_ui_source FILE|DIR: a readable .tar.gz with index.html at its top level, or a directory with index.html
check_ui_source() {
if [ -d "$1" ]; then
[ -f "$1/index.html" ] && [ -r "$1/index.html" ] || die "UI directory has no readable index.html: $1"
elif [ -f "$1" ]; then
tar -tzf "$1" >/dev/null 2>&1 || die "UI archive is not a readable .tar.gz: $1"
tar -tzf "$1" | grep -q -E '^(\./)?index\.html$' || die "UI archive has no index.html at its top level: $1"
else
die "UI source not found: $1 (a .tar.gz from deploy/native/build-ui.sh, or a directory with index.html)"
fi
}
# ui_resolve REPO_DIR TMP_DIR: picks the UI source. Input: UI_DIST (explicit FILE|DIR), UI_BUILD=1 (--build-ui),
# UI_FALLBACK (optional tarball of last resort). Order: explicit, build now, committed deploy/ui-dist, fallback.
# Output: UI_SRC (path, empty when nothing was found), UI_KIND (explicit|build|repo|fallback), UI_ORIGIN (text).
# A stale committed build is used with a loud warning; --build-ui always builds fresh from the sources.
ui_resolve() {
local repo tmp line
repo="$1"; tmp="$2"
UI_SRC=""; UI_KIND=""; UI_ORIGIN=""
if [ -n "${UI_DIST:-}" ]; then
check_ui_source "$UI_DIST"
UI_SRC="$UI_DIST"; UI_KIND=explicit; UI_ORIGIN="$UI_DIST (--ui-dist)"
return 0
fi
if [ "${UI_BUILD:-0}" = 1 ]; then
ui_build_check
ui_build_run "$repo" "$tmp/ui-build.tar.gz"
check_ui_source "$tmp/ui-build.tar.gz"
UI_SRC="$tmp/ui-build.tar.gz"; UI_KIND=build; UI_ORIGIN="built now from APP_UI (--build-ui)"
return 0
fi
if ui_repo_state "$repo"; then
case "$UI_STATE" in
fresh)
UI_SRC="$repo/deploy/ui-dist"; UI_KIND=repo; UI_ORIGIN="deploy/ui-dist of the repository (up to date with APP_UI)" ;;
stale)
ui_stale_lines | while IFS= read -r line; do warn "$line"; done
UI_SRC="$repo/deploy/ui-dist"; UI_KIND=repo; UI_ORIGIN="deploy/ui-dist of the repository (STALE: older than APP_UI)" ;;
*)
log "no committed UI build in $repo/deploy/ui-dist" ;;
esac
else
warn "cannot compute the hash of $repo/APP_UI (sources missing or no sha256sum/shasum): the committed UI build cannot be checked for staleness"
if [ -f "$repo/deploy/ui-dist/index.html" ]; then
UI_SRC="$repo/deploy/ui-dist"; UI_KIND=repo; UI_ORIGIN="deploy/ui-dist of the repository (not verified)"
fi
fi
if [ -z "$UI_SRC" ] && [ -n "${UI_FALLBACK:-}" ] && [ -f "$UI_FALLBACK" ]; then
check_ui_source "$UI_FALLBACK"
UI_SRC="$UI_FALLBACK"; UI_KIND=fallback; UI_ORIGIN="$UI_FALLBACK (archive found next to the scripts)"
fi
return 0
}
# stage_ui FILE|DIR DIR: a validated copy in a fresh DIR (never the live web root). Records the hash of the
# sources the build came from in DIR/$UI_HASH_FILE (SOURCE_HASH of the build, "unknown" for foreign archives).
stage_ui() {
local h
check_ui_source "$1"
h=$(ui_hash_of "$1")
rm -rf "$2"
mkdir -p "$2"
tar -xzf "$1" -C "$2" --no-same-owner || die "cannot extract the UI archive"
[ -f "$2/index.html" ] || die "index.html missing after extracting the UI archive"
if [ -d "$1" ]; then
( cd "$1" && tar -cf - . ) | ( cd "$2" && tar -xf - --no-same-owner ) || die "cannot copy the UI directory $1"
else
tar -xzf "$1" -C "$2" --no-same-owner || die "cannot extract the UI archive"
fi
[ -f "$2/index.html" ] && [ -r "$2/index.html" ] || die "index.html missing in the staged UI files"
rm -f "$2/SOURCE_HASH"
printf '%s\n' "$h" > "$2/$UI_HASH_FILE"
chown -R root:root "$2"
chmod -R go-w "$2"
chmod -R a+rX "$2"