Files
multica/scripts/selfhost-config.test.sh
YYClaw ee7ba83f53 fix(self-host): apply setup config to daemon (MUL-5269) (#5880)
Fixes two connected self-host setup failures in local worktree development.

Generated worktree environments now expose the backend HTTP origin through
MULTICA_PUBLIC_URL, and existing generated worktrees derive the missing value
at startup through both scripts/local-env.sh and the Makefile. Explicit
values, including an intentionally empty same-origin setting, are preserved.

setup and setup self-host now reconcile an existing same-profile daemon after
authentication so it loads the newly written server URL and token. An idle
daemon is restarted behind the existing restart preflight; when active tasks
exist, setup leaves the daemon running to avoid cancelling work and prints an
actionable profile-aware restart command instead.

Closes #5879
2026-07-30 15:10:40 +08:00

144 lines
4.1 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$ROOT_DIR"
require_config() {
local config=$1
local expected=$2
if ! grep -Fq "$expected" <<<"$config"; then
echo "Missing expected docker compose config value:"
echo " $expected"
exit 1
fi
}
require_env() {
local output=$1
local expected=$2
if ! grep -Fxq "$expected" <<<"$output"; then
echo "Missing expected derived env value:"
echo " $expected"
echo "Observed:"
echo "$output"
exit 1
fi
}
tmp_env="$(mktemp)"
tmp_dir="$(mktemp -d)"
trap 'rm -f "$tmp_env"; rm -rf "$tmp_dir"' EXIT
sed 's/^FRONTEND_PORT=.*/FRONTEND_PORT=3100/' .env.example >"$tmp_env"
printf '\nBACKEND_PORT=9100\n' >>"$tmp_env"
config="$(
docker compose \
--env-file "$tmp_env" \
-f docker-compose.selfhost.yml \
config
)"
require_config "$config" 'published: "3100"'
require_config "$config" 'published: "9100"'
require_config "$config" 'FRONTEND_ORIGIN: http://localhost:3100'
require_config "$config" 'GOOGLE_REDIRECT_URI: http://localhost:3100/auth/callback'
require_config "$config" 'MULTICA_APP_URL: http://localhost:3100'
for script in scripts/dev.sh scripts/check.sh; do
if ! grep -Fq '. scripts/local-env.sh' "$script"; then
echo "$script must source scripts/local-env.sh for shared local env derivation."
exit 1
fi
done
local_env="$(
env -i PATH="$PATH" bash -c '
set -euo pipefail
env_file=$1
set -a
# shellcheck disable=SC1090
. "$env_file"
set +a
# shellcheck disable=SC1091
. scripts/local-env.sh
printf "%s\n" \
"PORT=${PORT}" \
"FRONTEND_PORT=${FRONTEND_PORT}" \
"FRONTEND_ORIGIN=${FRONTEND_ORIGIN}" \
"MULTICA_APP_URL=${MULTICA_APP_URL}" \
"GOOGLE_REDIRECT_URI=${GOOGLE_REDIRECT_URI}" \
"MULTICA_SERVER_URL=${MULTICA_SERVER_URL}" \
"LOCAL_UPLOAD_BASE_URL=${LOCAL_UPLOAD_BASE_URL}" \
"PLAYWRIGHT_BASE_URL=${PLAYWRIGHT_BASE_URL}"
' _ "$tmp_env"
)"
require_env "$local_env" 'PORT=9100'
require_env "$local_env" 'FRONTEND_PORT=3100'
require_env "$local_env" 'FRONTEND_ORIGIN=http://localhost:3100'
require_env "$local_env" 'MULTICA_APP_URL=http://localhost:3100'
require_env "$local_env" 'GOOGLE_REDIRECT_URI=http://localhost:3100/auth/callback'
require_env "$local_env" 'MULTICA_SERVER_URL=ws://localhost:9100/ws'
require_env "$local_env" 'LOCAL_UPLOAD_BASE_URL=http://localhost:9100'
require_env "$local_env" 'PLAYWRIGHT_BASE_URL=http://localhost:3100'
worktree_env="$tmp_dir/.env.worktree"
WORKTREE_NAME=selfhost-config-test bash scripts/init-worktree-env.sh "$worktree_env" >/dev/null
worktree_backend_port="$(sed -n 's/^PORT=//p' "$worktree_env")"
require_env "$(cat "$worktree_env")" "MULTICA_PUBLIC_URL=http://localhost:${worktree_backend_port}"
resolve_local_public_url() {
env -i PATH="$PATH" bash -c '
set -euo pipefail
env_file=$1
set -a
# shellcheck disable=SC1090
. "$env_file"
set +a
# shellcheck disable=SC1091
. scripts/local-env.sh
printf "%s\n" "$MULTICA_PUBLIC_URL"
' _ "$1"
}
make_env_probe="$tmp_dir/print-public-url.mk"
printf '%s\n' \
'.PHONY: print-public-url' \
'print-public-url:' \
' @printf "%s\n" "$$MULTICA_PUBLIC_URL"' \
>"$make_env_probe"
resolve_make_public_url() {
make \
--no-print-directory \
-s \
-f Makefile \
-f "$make_env_probe" \
ENV_FILE="$1" \
print-public-url
}
old_worktree_env="$tmp_dir/.env.worktree.old"
grep -v '^MULTICA_PUBLIC_URL=' "$worktree_env" >"$old_worktree_env"
require_env \
"$(resolve_local_public_url "$old_worktree_env")" \
"http://localhost:${worktree_backend_port}"
require_env \
"$(resolve_make_public_url "$old_worktree_env")" \
"http://localhost:${worktree_backend_port}"
explicit_worktree_env="$tmp_dir/.env.worktree.explicit"
cp "$old_worktree_env" "$explicit_worktree_env"
printf '\nMULTICA_PUBLIC_URL=https://api.explicit.example\n' >>"$explicit_worktree_env"
require_env \
"$(resolve_local_public_url "$explicit_worktree_env")" \
"https://api.explicit.example"
require_env \
"$(resolve_make_public_url "$explicit_worktree_env")" \
"https://api.explicit.example"
echo "self-host env derivation ok"