feat: migrate secrets to Infisical
Validate and deploy production / validate (push) Canceled after 0s
Validate and deploy production / deploy (push) Canceled after 0s
Validate and deploy staging / validate (push) Failing after 22s
Validate and deploy staging / deploy (push) Skipped

This commit is contained in:
jb
2026-08-02 01:19:41 -04:00
parent 9089f4a257
commit 25d9241b51
34 changed files with 2353 additions and 691 deletions
+28 -6
View File
@@ -7,23 +7,45 @@ if [ -z "$bundle_path" ] || [ ! -f "$bundle_path" ]; then
echo "Usage: bootstrap-production.sh <bootstrap-bundle.json> [deployment-directory]" >&2
exit 2
fi
bundle_path=$(readlink -f "$bundle_path")
compose_file="$deployment_directory/compose.production.yml"
deploy_environment="$deployment_directory/.env.deploy"
volume_bundle_path=/app/data/.production-bootstrap-import.json
remove_volume_bundle=false
compose() {
docker compose --env-file "$deploy_environment" -f "$compose_file" "$@"
}
restart_services() {
compose up -d --no-build app scheduler
cleanup() {
status=$?
trap - EXIT HUP INT TERM
set +e
if [ "$remove_volume_bundle" = true ]; then
compose run --rm --no-deps --entrypoint rm --user root app -f "$volume_bundle_path"
fi
if ! compose up -d --no-build app scheduler; then status=1; fi
exit "$status"
}
trap restart_services EXIT
trap cleanup EXIT
trap 'exit 129' HUP
trap 'exit 130' INT
trap 'exit 143' TERM
echo "Stopping production before the one-time database bootstrap."
compose stop scheduler app || true
compose stop scheduler app
compose create app >/dev/null
app_container=$(compose ps --all --quiet app)
if [ -z "$app_container" ]; then
echo "The stopped production app container could not be found." >&2
exit 1
fi
docker cp "$bundle_path" "$app_container:$volume_bundle_path"
remove_volume_bundle=true
compose run --rm --no-deps --entrypoint sh --user root app -c \
'chown shd-runtime:node "$1" && chmod 600 "$1"' sh "$volume_bundle_path"
compose run --rm --no-deps \
-e ALLOW_PRODUCTION_BOOTSTRAP=true \
-v "$bundle_path:/import/production-bootstrap.json:ro" \
app node scripts/environment-bootstrap-import.mjs \
/import/production-bootstrap.json --target production --confirm-empty-target-replace
"$volume_bundle_path" --target production --confirm-empty-target-replace
echo "Production bootstrap completed."
+28 -6
View File
@@ -7,23 +7,45 @@ if [ -z "$bundle_path" ] || [ ! -f "$bundle_path" ]; then
echo "Usage: bootstrap-staging.sh <bootstrap-bundle.json> [deployment-directory]" >&2
exit 2
fi
bundle_path=$(readlink -f "$bundle_path")
compose_file="$deployment_directory/compose.staging.yml"
deploy_environment="$deployment_directory/.env.deploy"
volume_bundle_path=/app/data/.staging-bootstrap-import.json
remove_volume_bundle=false
compose() {
docker compose --env-file "$deploy_environment" -f "$compose_file" "$@"
}
restart_services() {
compose up -d --no-build app scheduler
cleanup() {
status=$?
trap - EXIT HUP INT TERM
set +e
if [ "$remove_volume_bundle" = true ]; then
compose run --rm --no-deps --entrypoint rm --user root app -f "$volume_bundle_path"
fi
if ! compose up -d --no-build app scheduler; then status=1; fi
exit "$status"
}
trap restart_services EXIT
trap cleanup EXIT
trap 'exit 129' HUP
trap 'exit 130' INT
trap 'exit 143' TERM
echo "Stopping staging before the one-time database bootstrap."
compose stop scheduler app || true
compose stop scheduler app
compose create app >/dev/null
app_container=$(compose ps --all --quiet app)
if [ -z "$app_container" ]; then
echo "The stopped staging app container could not be found." >&2
exit 1
fi
docker cp "$bundle_path" "$app_container:$volume_bundle_path"
remove_volume_bundle=true
compose run --rm --no-deps --entrypoint sh --user root app -c \
'chown shd-runtime:node "$1" && chmod 600 "$1"' sh "$volume_bundle_path"
compose run --rm --no-deps \
-e ALLOW_STAGING_BOOTSTRAP=true \
-v "$bundle_path:/import/staging-bootstrap.json:ro" \
app node scripts/stage-bootstrap-import.mjs \
/import/staging-bootstrap.json --confirm-empty-staging-replace
"$volume_bundle_path" --confirm-empty-staging-replace
echo "Staging bootstrap completed."
+43 -17
View File
@@ -5,26 +5,29 @@ services:
image: ${SHD_IMAGE:-shd-build-production:local}
restart: unless-stopped
init: true
env_file:
- .env.production.runtime
environment:
HOST: 0.0.0.0
PORT: 3000
BODY_SIZE_LIMIT: 6M
SKIP_DEFAULT_USERS: 'true'
TDR_DATABASE_PATH: /app/data/shd-build-production.db
TDR_AVATAR_STORAGE_PATH: /app/data/avatars
TDR_GAME_ICON_STORAGE_PATH: /app/data/game-icons
INFISICAL_PROJECT_ID: 2ee94b27-e599-4194-9797-3372d42e53f6
INFISICAL_ENVIRONMENT: prod
INFISICAL_RUNTIME_SCOPE: app
INFISICAL_UNIVERSAL_AUTH_CLIENT_ID_FILE: /run/secrets/infisical_client_id
INFISICAL_UNIVERSAL_AUTH_CLIENT_SECRET_FILE: /run/secrets/infisical_client_secret
SHD_BUILD_NUMBER: ${SHD_BUILD_NUMBER:-0}
APP_ORIGIN: https://shd.build
ORIGIN: https://shd.build
PASSKEY_RP_ID: shd.build
DISCORD_REDIRECT_URI: https://shd.build/auth/discord/callback
UBISOFT_NEWS_IMPORT_ENABLED: 'true'
SHD_COMMIT_SHA: ${SHD_COMMIT_SHA:-}
ports:
- '0.0.0.0:${SHD_PRODUCTION_PORT:-3102}:3000'
volumes:
- shd_production_data:/app/data
secrets:
- source: infisical_app_client_id
target: infisical_client_id
uid: '0'
gid: '0'
mode: 0400
- source: infisical_app_client_secret
target: infisical_client_secret
uid: '0'
gid: '0'
mode: 0400
healthcheck:
test:
- CMD
@@ -43,10 +46,23 @@ services:
init: true
command:
- scheduler
env_file:
- .env.production.runtime
environment:
SCHEDULER_URL: http://app:3000/api/internal/scheduler
INFISICAL_PROJECT_ID: 2ee94b27-e599-4194-9797-3372d42e53f6
INFISICAL_ENVIRONMENT: prod
INFISICAL_RUNTIME_SCOPE: scheduler
INFISICAL_UNIVERSAL_AUTH_CLIENT_ID_FILE: /run/secrets/infisical_client_id
INFISICAL_UNIVERSAL_AUTH_CLIENT_SECRET_FILE: /run/secrets/infisical_client_secret
secrets:
- source: infisical_scheduler_client_id
target: infisical_client_id
uid: '0'
gid: '0'
mode: 0400
- source: infisical_scheduler_client_secret
target: infisical_client_secret
uid: '0'
gid: '0'
mode: 0400
depends_on:
app:
condition: service_healthy
@@ -55,3 +71,13 @@ services:
volumes:
shd_production_data:
name: shd-build-production-data
secrets:
infisical_app_client_id:
file: .infisical.app.client-id
infisical_app_client_secret:
file: .infisical.app.client-secret
infisical_scheduler_client_id:
file: .infisical.scheduler.client-id
infisical_scheduler_client_secret:
file: .infisical.scheduler.client-secret
+43 -13
View File
@@ -5,22 +5,29 @@ services:
image: ${SHD_IMAGE:-shd-build-stage:local}
restart: unless-stopped
init: true
env_file:
- .env.staging.runtime
environment:
HOST: 0.0.0.0
PORT: 3000
BODY_SIZE_LIMIT: 6M
SKIP_DEFAULT_USERS: 'true'
TDR_DATABASE_PATH: /app/data/shd-build-staging.db
TDR_AVATAR_STORAGE_PATH: /app/data/avatars
TDR_GAME_ICON_STORAGE_PATH: /app/data/game-icons
INFISICAL_PROJECT_ID: 2ee94b27-e599-4194-9797-3372d42e53f6
INFISICAL_ENVIRONMENT: staging
INFISICAL_RUNTIME_SCOPE: app
INFISICAL_UNIVERSAL_AUTH_CLIENT_ID_FILE: /run/secrets/infisical_client_id
INFISICAL_UNIVERSAL_AUTH_CLIENT_SECRET_FILE: /run/secrets/infisical_client_secret
SHD_BUILD_NUMBER: ${SHD_BUILD_NUMBER:-0}
UBISOFT_NEWS_IMPORT_ENABLED: 'false'
SHD_COMMIT_SHA: ${SHD_COMMIT_SHA:-}
ports:
- '0.0.0.0:${SHD_STAGE_PORT:-5569}:3000'
volumes:
- shd_stage_data:/app/data
secrets:
- source: infisical_app_client_id
target: infisical_client_id
uid: '0'
gid: '0'
mode: 0400
- source: infisical_app_client_secret
target: infisical_client_secret
uid: '0'
gid: '0'
mode: 0400
healthcheck:
test:
- CMD
@@ -39,10 +46,23 @@ services:
init: true
command:
- scheduler
env_file:
- .env.staging.runtime
environment:
SCHEDULER_URL: http://app:3000/api/internal/scheduler
INFISICAL_PROJECT_ID: 2ee94b27-e599-4194-9797-3372d42e53f6
INFISICAL_ENVIRONMENT: staging
INFISICAL_RUNTIME_SCOPE: scheduler
INFISICAL_UNIVERSAL_AUTH_CLIENT_ID_FILE: /run/secrets/infisical_client_id
INFISICAL_UNIVERSAL_AUTH_CLIENT_SECRET_FILE: /run/secrets/infisical_client_secret
secrets:
- source: infisical_scheduler_client_id
target: infisical_client_id
uid: '0'
gid: '0'
mode: 0400
- source: infisical_scheduler_client_secret
target: infisical_client_secret
uid: '0'
gid: '0'
mode: 0400
depends_on:
app:
condition: service_healthy
@@ -51,3 +71,13 @@ services:
volumes:
shd_stage_data:
name: shd-build-stage-data
secrets:
infisical_app_client_id:
file: .infisical.app.client-id
infisical_app_client_secret:
file: .infisical.app.client-secret
infisical_scheduler_client_id:
file: .infisical.scheduler.client-id
infisical_scheduler_client_secret:
file: .infisical.scheduler.client-secret
+111 -6
View File
@@ -1,13 +1,118 @@
#!/bin/sh
set -eu
if [ "${1:-}" = "serve" ]; then
node scripts/db-migrate.mjs
exec node build
project_id=${INFISICAL_PROJECT_ID:?INFISICAL_PROJECT_ID is required}
environment=${INFISICAL_ENVIRONMENT:?INFISICAL_ENVIRONMENT is required}
runtime_scope=${INFISICAL_RUNTIME_SCOPE:?INFISICAL_RUNTIME_SCOPE is required}
client_id_file=${INFISICAL_UNIVERSAL_AUTH_CLIENT_ID_FILE:?INFISICAL_UNIVERSAL_AUTH_CLIENT_ID_FILE is required}
client_secret_file=${INFISICAL_UNIVERSAL_AUTH_CLIENT_SECRET_FILE:?INFISICAL_UNIVERSAL_AUTH_CLIENT_SECRET_FILE is required}
build_number=${SHD_BUILD_NUMBER:-0}
commit_sha=${SHD_COMMIT_SHA:-}
secret_file=
shared_secret_file=
cleanup() {
if [ -n "$secret_file" ]; then rm -f "$secret_file"; fi
if [ -n "$shared_secret_file" ]; then rm -f "$shared_secret_file"; fi
}
trap cleanup EXIT
trap 'exit 129' HUP
trap 'exit 130' INT
trap 'exit 143' TERM
if [ ! -r "$client_id_file" ] || [ ! -r "$client_secret_file" ]; then
echo "Infisical Universal Auth credential files are not readable." >&2
exit 1
fi
if [ "${1:-}" = "scheduler" ]; then
exec node scripts/scheduler-client.mjs
if [ "$(id -u)" -eq 0 ]; then
runtime_uid=$(id -u shd-runtime)
for credential_file in "$client_id_file" "$client_secret_file"; do
credential_uid=$(stat -c '%u' "$credential_file")
credential_mode=$(stat -c '%a' "$credential_file")
if [ "$credential_uid" -eq "$runtime_uid" ] || [ $((credential_mode % 100)) -ne 0 ]; then
echo "Infisical credential files must not be readable by the application runtime user." >&2
exit 1
fi
done
if [ "$runtime_scope" = app ] && ! { [ "${1:-}" = node ] && [ "${2:-}" = scripts/validate-runtime-environment.mjs ]; }; then
chgrp -R node /app/data
chmod -R g+rwX /app/data
fi
fi
exec "$@"
client_id=$(cat "$client_id_file")
client_secret=$(cat "$client_secret_file")
if [ -z "$client_id" ] || [ -z "$client_secret" ]; then
echo "Infisical Universal Auth credential files must not be empty." >&2
exit 1
fi
unset INFISICAL_TOKEN INFISICAL_UNIVERSAL_AUTH_ACCESS_TOKEN TOKEN
access_token=$(
INFISICAL_UNIVERSAL_AUTH_CLIENT_ID="$client_id" \
INFISICAL_UNIVERSAL_AUTH_CLIENT_SECRET="$client_secret" \
infisical login --method=universal-auth --silent --plain
)
unset client_id client_secret
if [ -z "$access_token" ]; then
echo "Infisical Universal Auth returned an empty access token." >&2
exit 1
fi
umask 077
secret_file=$(mktemp /tmp/shd-infisical-app.XXXXXX)
case "$runtime_scope" in
app)
INFISICAL_UNIVERSAL_AUTH_ACCESS_TOKEN="$access_token" infisical export \
--projectId="$project_id" \
--env="$environment" \
--path=/app \
--format=json \
--output-file="$secret_file" \
--silent
shared_secret_file=$(mktemp /tmp/shd-infisical-shared.XXXXXX)
INFISICAL_UNIVERSAL_AUTH_ACCESS_TOKEN="$access_token" infisical export \
--projectId="$project_id" \
--env="$environment" \
--path=/shared/scheduler \
--format=json \
--output-file="$shared_secret_file" \
--silent
;;
scheduler)
INFISICAL_UNIVERSAL_AUTH_ACCESS_TOKEN="$access_token" infisical export \
--projectId="$project_id" \
--env="$environment" \
--path=/shared/scheduler \
--format=json \
--output-file="$secret_file" \
--silent
;;
*)
echo "Unsupported INFISICAL_RUNTIME_SCOPE: $runtime_scope" >&2
exit 2
;;
esac
unset access_token \
INFISICAL_TOKEN \
INFISICAL_UNIVERSAL_AUTH_ACCESS_TOKEN \
INFISICAL_UNIVERSAL_AUTH_CLIENT_ID \
INFISICAL_UNIVERSAL_AUTH_CLIENT_SECRET
if [ "$runtime_scope" = app ]; then
if [ "$(id -u)" -eq 0 ]; then
chown shd-runtime:node "$secret_file" "$shared_secret_file"
exec gosu shd-runtime node scripts/runtime-launcher.mjs app "$environment" "$build_number" "$commit_sha" "$secret_file" "$shared_secret_file" -- "$@"
fi
exec node scripts/runtime-launcher.mjs app "$environment" "$build_number" "$commit_sha" "$secret_file" "$shared_secret_file" -- "$@"
fi
if [ "$(id -u)" -eq 0 ]; then
chown shd-runtime:node "$secret_file"
exec gosu shd-runtime node scripts/runtime-launcher.mjs scheduler "$environment" "$build_number" "$commit_sha" "$secret_file" -- "$@"
fi
exec node scripts/runtime-launcher.mjs scheduler "$environment" "$build_number" "$commit_sha" "$secret_file" -- "$@"
+131 -25
View File
@@ -4,48 +4,154 @@ set -eu
deployment_directory=${1:-/opt/shd-build-production}
source_directory=${2:-}
candidate_image=${3:-}
if [ -z "$source_directory" ] || [ ! -f "$source_directory/Dockerfile" ] || [ -z "$candidate_image" ]; then
echo "Usage: deploy-production.sh <deployment-directory> <source-directory> <candidate-image>" >&2
candidate_directory=${4:-}
if [ -z "$source_directory" ] || [ ! -f "$source_directory/Dockerfile" ] || [ -z "$candidate_image" ] || [ ! -f "$candidate_directory/compose.production.yml" ] || [ ! -f "$candidate_directory/.env.deploy" ]; then
echo "Usage: deploy-production.sh <deployment-directory> <source-directory> <candidate-image> <candidate-directory>" >&2
exit 2
fi
compose_file="$deployment_directory/compose.production.yml"
deploy_environment="$deployment_directory/.env.deploy"
case "$candidate_directory" in
"$deployment_directory"/candidate-*) ;;
*) echo "Candidate directory must be a candidate-* child of the deployment directory." >&2; exit 2 ;;
esac
candidate_compose_file="$candidate_directory/compose.production.yml"
candidate_environment="$candidate_directory/.env.deploy"
canonical_compose_file="$deployment_directory/compose.production.yml"
canonical_environment="$deployment_directory/.env.deploy"
previous_directory="$deployment_directory/.previous-release"
healthy_marker="$deployment_directory/.deployment-healthy"
release_files="compose.production.yml .env.deploy .infisical.app.client-id .infisical.app.client-secret .infisical.scheduler.client-id .infisical.scheduler.client-secret deploy-production.sh bootstrap-production.sh sync-production-isac-to-staging.sh"
rollback_required=false
candidate_promoted=false
had_previous_release=false
compose() {
docker compose --env-file "$deploy_environment" -f "$compose_file" "$@"
candidate_compose() {
docker compose --env-file "$candidate_environment" -f "$candidate_compose_file" "$@"
}
canonical_compose() {
docker compose --env-file "$canonical_environment" -f "$canonical_compose_file" "$@"
}
release_is_healthy() {
health_environment=$1
health_compose_file=$2
health_attempt=0
while [ "$health_attempt" -lt 30 ]; do
health_app_container=$(docker compose --env-file "$health_environment" -f "$health_compose_file" ps -q app)
health_scheduler_container=$(docker compose --env-file "$health_environment" -f "$health_compose_file" ps -q scheduler)
health_app_status=missing
health_scheduler_status=missing
if [ -n "$health_app_container" ]; then
health_app_status=$(docker inspect --format '{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}' "$health_app_container" 2>/dev/null || echo missing)
fi
if [ -n "$health_scheduler_container" ]; then
health_scheduler_status=$(docker inspect --format '{{.State.Status}}' "$health_scheduler_container" 2>/dev/null || echo missing)
fi
if [ "$health_app_status" = healthy ] && [ "$health_scheduler_status" = running ]; then return 0; fi
if [ "$health_app_status" = unhealthy ] || [ "$health_app_status" = exited ] || [ "$health_app_status" = dead ] || [ "$health_scheduler_status" = exited ] || [ "$health_scheduler_status" = dead ]; then return 1; fi
health_attempt=$((health_attempt + 1))
sleep 2
done
return 1
}
copy_release() {
copy_source=$1
copy_target=$2
copy_status=0
for release_file in $release_files; do
if [ -f "$copy_source/$release_file" ]; then
copy_temporary=$(mktemp "$copy_target/.release.XXXXXX") || return 1
if cp -p "$copy_source/$release_file" "$copy_temporary"; then
mv "$copy_temporary" "$copy_target/$release_file" || copy_status=1
else
rm -f "$copy_temporary"
copy_status=1
fi
else
rm -f "$copy_target/$release_file" || copy_status=1
fi
done
return "$copy_status"
}
cleanup_candidate() {
case "$candidate_directory" in
"$deployment_directory"/candidate-*) rm -rf "$candidate_directory" ;;
esac
}
rollback() {
rollback_exit_status=$?
trap - EXIT HUP INT TERM
set +e
if [ "$rollback_required" = true ]; then
echo "Restoring the previous healthy production release." >&2
restore_status=0
candidate_compose stop scheduler app >/dev/null 2>&1 || restore_status=1
if [ "$candidate_promoted" = true ]; then copy_release "$previous_directory" "$deployment_directory" || restore_status=1; fi
if [ "$had_previous_release" = true ]; then
canonical_compose up -d --no-build app scheduler || restore_status=1
if [ "$restore_status" -eq 0 ] && ! release_is_healthy "$canonical_environment" "$canonical_compose_file"; then restore_status=1; fi
else
echo "No previous production release is available." >&2
restore_status=1
fi
if [ "$restore_status" -ne 0 ]; then
echo "The previous production release could not be restored." >&2
if [ "$rollback_exit_status" -eq 0 ]; then rollback_exit_status=1; fi
fi
fi
cleanup_candidate
exit "$rollback_exit_status"
}
trap rollback EXIT
trap 'exit 129' HUP
trap 'exit 130' INT
trap 'exit 143' TERM
echo "Building the candidate image while production remains online."
docker build --label org.opencontainers.image.title=shd-build -t "$candidate_image" "$source_directory"
echo "Validating Infisical access and the candidate runtime configuration."
candidate_compose run --rm --no-deps app node scripts/validate-runtime-environment.mjs app
candidate_compose run --rm --no-deps scheduler node scripts/validate-runtime-environment.mjs scheduler
rm -rf "$previous_directory"
mkdir -p "$previous_directory"
if [ -f "$canonical_compose_file" ] && [ -f "$canonical_environment" ]; then
had_previous_release=true
copy_release "$deployment_directory" "$previous_directory"
fi
echo "Stopping only SHD production processes before accessing the embedded database."
compose stop scheduler app || true
rollback_required=true
candidate_compose stop scheduler app
echo "Creating a timestamped production backup when the database exists."
compose run --rm --no-deps app sh -c \
candidate_compose run --rm --no-deps app sh -c \
'if [ -f "$TDR_DATABASE_PATH" ]; then node scripts/db-backup.mjs; else echo "No production database exists yet; skipping backup."; fi'
echo "Applying additive schema migrations."
compose run --rm --no-deps app node scripts/db-migrate.mjs
candidate_compose run --rm --no-deps app node scripts/db-migrate.mjs
candidate_promoted=true
copy_release "$candidate_directory" "$deployment_directory"
echo "Starting production."
compose up -d --no-build --remove-orphans app scheduler
canonical_compose up -d --no-build --remove-orphans app scheduler
container_id=$(compose ps -q app)
attempt=0
while [ "$attempt" -lt 30 ]; do
status=$(docker inspect --format '{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}' "$container_id")
if [ "$status" = "healthy" ]; then
echo "SHD production deployment is healthy."
exit 0
fi
if [ "$status" = "unhealthy" ] || [ "$status" = "exited" ] || [ "$status" = "dead" ]; then
break
fi
attempt=$((attempt + 1))
sleep 2
done
if release_is_healthy "$canonical_environment" "$canonical_compose_file"; then
echo "SHD production deployment is healthy."
rollback_required=false
umask 077
: > "$healthy_marker"
cleanup_candidate
trap - EXIT HUP INT TERM
exit 0
fi
echo "SHD production failed its health check."
compose logs --tail=150 app
canonical_compose logs --tail=150 app scheduler
exit 1
+131 -25
View File
@@ -4,48 +4,154 @@ set -eu
deployment_directory=${1:-/opt/shd-build-stage}
source_directory=${2:-}
candidate_image=${3:-}
if [ -z "$source_directory" ] || [ ! -f "$source_directory/Dockerfile" ] || [ -z "$candidate_image" ]; then
echo "Usage: deploy-staging.sh <deployment-directory> <source-directory> <candidate-image>" >&2
candidate_directory=${4:-}
if [ -z "$source_directory" ] || [ ! -f "$source_directory/Dockerfile" ] || [ -z "$candidate_image" ] || [ ! -f "$candidate_directory/compose.staging.yml" ] || [ ! -f "$candidate_directory/.env.deploy" ]; then
echo "Usage: deploy-staging.sh <deployment-directory> <source-directory> <candidate-image> <candidate-directory>" >&2
exit 2
fi
compose_file="$deployment_directory/compose.staging.yml"
deploy_environment="$deployment_directory/.env.deploy"
case "$candidate_directory" in
"$deployment_directory"/candidate-*) ;;
*) echo "Candidate directory must be a candidate-* child of the deployment directory." >&2; exit 2 ;;
esac
candidate_compose_file="$candidate_directory/compose.staging.yml"
candidate_environment="$candidate_directory/.env.deploy"
canonical_compose_file="$deployment_directory/compose.staging.yml"
canonical_environment="$deployment_directory/.env.deploy"
previous_directory="$deployment_directory/.previous-release"
healthy_marker="$deployment_directory/.deployment-healthy"
release_files="compose.staging.yml .env.deploy .infisical.app.client-id .infisical.app.client-secret .infisical.scheduler.client-id .infisical.scheduler.client-secret deploy-staging.sh bootstrap-staging.sh"
rollback_required=false
candidate_promoted=false
had_previous_release=false
compose() {
docker compose --env-file "$deploy_environment" -f "$compose_file" "$@"
candidate_compose() {
docker compose --env-file "$candidate_environment" -f "$candidate_compose_file" "$@"
}
canonical_compose() {
docker compose --env-file "$canonical_environment" -f "$canonical_compose_file" "$@"
}
release_is_healthy() {
health_environment=$1
health_compose_file=$2
health_attempt=0
while [ "$health_attempt" -lt 30 ]; do
health_app_container=$(docker compose --env-file "$health_environment" -f "$health_compose_file" ps -q app)
health_scheduler_container=$(docker compose --env-file "$health_environment" -f "$health_compose_file" ps -q scheduler)
health_app_status=missing
health_scheduler_status=missing
if [ -n "$health_app_container" ]; then
health_app_status=$(docker inspect --format '{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}' "$health_app_container" 2>/dev/null || echo missing)
fi
if [ -n "$health_scheduler_container" ]; then
health_scheduler_status=$(docker inspect --format '{{.State.Status}}' "$health_scheduler_container" 2>/dev/null || echo missing)
fi
if [ "$health_app_status" = healthy ] && [ "$health_scheduler_status" = running ]; then return 0; fi
if [ "$health_app_status" = unhealthy ] || [ "$health_app_status" = exited ] || [ "$health_app_status" = dead ] || [ "$health_scheduler_status" = exited ] || [ "$health_scheduler_status" = dead ]; then return 1; fi
health_attempt=$((health_attempt + 1))
sleep 2
done
return 1
}
copy_release() {
copy_source=$1
copy_target=$2
copy_status=0
for release_file in $release_files; do
if [ -f "$copy_source/$release_file" ]; then
copy_temporary=$(mktemp "$copy_target/.release.XXXXXX") || return 1
if cp -p "$copy_source/$release_file" "$copy_temporary"; then
mv "$copy_temporary" "$copy_target/$release_file" || copy_status=1
else
rm -f "$copy_temporary"
copy_status=1
fi
else
rm -f "$copy_target/$release_file" || copy_status=1
fi
done
return "$copy_status"
}
cleanup_candidate() {
case "$candidate_directory" in
"$deployment_directory"/candidate-*) rm -rf "$candidate_directory" ;;
esac
}
rollback() {
rollback_exit_status=$?
trap - EXIT HUP INT TERM
set +e
if [ "$rollback_required" = true ]; then
echo "Restoring the previous healthy staging release." >&2
restore_status=0
candidate_compose stop scheduler app >/dev/null 2>&1 || restore_status=1
if [ "$candidate_promoted" = true ]; then copy_release "$previous_directory" "$deployment_directory" || restore_status=1; fi
if [ "$had_previous_release" = true ]; then
canonical_compose up -d --no-build app scheduler || restore_status=1
if [ "$restore_status" -eq 0 ] && ! release_is_healthy "$canonical_environment" "$canonical_compose_file"; then restore_status=1; fi
else
echo "No previous staging release is available." >&2
restore_status=1
fi
if [ "$restore_status" -ne 0 ]; then
echo "The previous staging release could not be restored." >&2
if [ "$rollback_exit_status" -eq 0 ]; then rollback_exit_status=1; fi
fi
fi
cleanup_candidate
exit "$rollback_exit_status"
}
trap rollback EXIT
trap 'exit 129' HUP
trap 'exit 130' INT
trap 'exit 143' TERM
echo "Building the candidate image while the current staging service remains online."
docker build --label org.opencontainers.image.title=shd-build -t "$candidate_image" "$source_directory"
echo "Validating Infisical access and the candidate runtime configuration."
candidate_compose run --rm --no-deps app node scripts/validate-runtime-environment.mjs app
candidate_compose run --rm --no-deps scheduler node scripts/validate-runtime-environment.mjs scheduler
rm -rf "$previous_directory"
mkdir -p "$previous_directory"
if [ -f "$canonical_compose_file" ] && [ -f "$canonical_environment" ]; then
had_previous_release=true
copy_release "$deployment_directory" "$previous_directory"
fi
echo "Stopping SHD staging processes before accessing the embedded database."
compose stop scheduler app || true
rollback_required=true
candidate_compose stop scheduler app
echo "Creating a timestamped database backup when a staging database exists."
compose run --rm --no-deps app sh -c \
candidate_compose run --rm --no-deps app sh -c \
'if [ -f "$TDR_DATABASE_PATH" ]; then node scripts/db-backup.mjs; else echo "No staging database exists yet; skipping backup."; fi'
echo "Applying additive schema migrations."
compose run --rm --no-deps app node scripts/db-migrate.mjs
candidate_compose run --rm --no-deps app node scripts/db-migrate.mjs
candidate_promoted=true
copy_release "$candidate_directory" "$deployment_directory"
echo "Starting the application and notification scheduler."
compose up -d --no-build --remove-orphans app scheduler
canonical_compose up -d --no-build --remove-orphans app scheduler
container_id=$(compose ps -q app)
attempt=0
while [ "$attempt" -lt 30 ]; do
status=$(docker inspect --format '{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}' "$container_id")
if [ "$status" = "healthy" ]; then
echo "SHD staging deployment is healthy."
exit 0
fi
if [ "$status" = "unhealthy" ] || [ "$status" = "exited" ] || [ "$status" = "dead" ]; then
break
fi
attempt=$((attempt + 1))
sleep 2
done
if release_is_healthy "$canonical_environment" "$canonical_compose_file"; then
echo "SHD staging deployment is healthy."
rollback_required=false
umask 077
: > "$healthy_marker"
cleanup_candidate
trap - EXIT HUP INT TERM
exit 0
fi
echo "SHD staging failed its health check."
compose logs --tail=150 app
canonical_compose logs --tail=150 app scheduler
exit 1
+393
View File
@@ -0,0 +1,393 @@
import { spawnSync } from 'node:child_process';
import { existsSync } from 'node:fs';
import {
copyFile,
mkdir,
mkdtemp,
readFile,
rm,
writeFile,
} from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join, resolve } from 'node:path';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
const repositoryRoot = resolve(import.meta.dirname, '..');
const schedulerToken = 'scheduler-token-with-at-least-32-characters';
const infisicalStub = `#!/bin/sh
set -eu
case "\${1:-}" in
login)
printf '%s\n' "$@" > "$STUB_LOGIN_LOG"
if [ "\${INFISICAL_UNIVERSAL_AUTH_CLIENT_ID:-}" != "$STUB_EXPECTED_CLIENT_ID" ] || \
[ "\${INFISICAL_UNIVERSAL_AUTH_CLIENT_SECRET:-}" != "$STUB_EXPECTED_CLIENT_SECRET" ]; then
printf '%s\n' 'credentials=incorrect' >> "$STUB_LOGIN_LOG"
exit 26
fi
printf '%s\n' 'credentials=ok' >> "$STUB_LOGIN_LOG"
case "\${STUB_LOGIN_RESULT:-success}" in
fail) exit 23 ;;
empty) exit 0 ;;
*) printf '%s\n' stub-access-token ;;
esac
;;
export)
[ "\${INFISICAL_UNIVERSAL_AUTH_ACCESS_TOKEN:-}" = stub-access-token ] || exit 27
secret_path=
output_file=
for argument in "$@"; do
case "$argument" in
--path=*) secret_path=\${argument#--path=} ;;
--output-file=*) output_file=\${argument#--output-file=} ;;
esac
done
[ -n "$secret_path" ] && [ -n "$output_file" ] || exit 28
printf '%s|%s\n' "$secret_path" "$output_file" >> "$STUB_EXPORT_LOG"
case "$secret_path" in
/app) cp "$STUB_APP_EXPORT" "$output_file" ;;
/shared/scheduler) cp "$STUB_SHARED_EXPORT" "$output_file" ;;
*) exit 29 ;;
esac
;;
*) exit 25 ;;
esac
`;
const environmentCapture = `#!/bin/sh
env > "$CAPTURE_ENV_PATH"
`;
const gosuStub = `#!/bin/sh
set -eu
[ "\${1:-}" = shd-runtime ] || exit 30
printf '%s\n' 'target=shd-runtime' > "$STUB_GOSU_LOG"
shift
exec "$@"
`;
const idStub = `#!/bin/sh
if [ "\${1:-}" = -u ] && [ "\${2:-}" = shd-runtime ]; then
printf '%s\n' 10001
exit 0
fi
exec /usr/bin/id "$@"
`;
const chownStub = `#!/bin/sh
exit 0
`;
describe('container runtime startup', () => {
let temporaryRoot: string;
let clientIdFile: string;
let clientSecretFile: string;
let loginLog: string;
let exportLog: string;
let gosuLog: string;
let capturedEnvironment: string;
let appExport: string;
let sharedExport: string;
beforeEach(async () => {
temporaryRoot = await mkdtemp(join(tmpdir(), 'shd-runtime-start-'));
const deployDirectory = join(temporaryRoot, 'deploy');
const scriptsDirectory = join(temporaryRoot, 'scripts');
await mkdir(deployDirectory);
await mkdir(scriptsDirectory);
await Promise.all([
copyFile(
join(repositoryRoot, 'deploy/container-entrypoint.sh'),
join(deployDirectory, 'container-entrypoint.sh'),
),
copyFile(
join(repositoryRoot, 'scripts/runtime-launcher.mjs'),
join(scriptsDirectory, 'runtime-launcher.mjs'),
),
copyFile(
join(repositoryRoot, 'scripts/runtime-environment.js'),
join(scriptsDirectory, 'runtime-environment.js'),
),
]);
clientIdFile = join(temporaryRoot, 'client-id');
clientSecretFile = join(temporaryRoot, 'client-secret');
loginLog = join(temporaryRoot, 'login.log');
exportLog = join(temporaryRoot, 'export.log');
gosuLog = join(temporaryRoot, 'gosu.log');
capturedEnvironment = join(temporaryRoot, 'child.env');
appExport = join(temporaryRoot, 'app-export.json');
sharedExport = join(temporaryRoot, 'shared-export.json');
await Promise.all([
writeFile(clientIdFile, 'universal-client-id\n', { mode: 0o600 }),
writeFile(clientSecretFile, 'universal-client-secret\n', { mode: 0o600 }),
writeFile(join(temporaryRoot, 'infisical'), infisicalStub, {
mode: 0o755,
}),
writeFile(
join(temporaryRoot, 'capture-environment'),
environmentCapture,
{
mode: 0o755,
},
),
writeFile(join(temporaryRoot, 'gosu'), gosuStub, { mode: 0o755 }),
writeFile(join(temporaryRoot, 'id'), idStub, { mode: 0o755 }),
writeFile(join(temporaryRoot, 'chown'), chownStub, { mode: 0o755 }),
writeExport(appExport, [
{ key: 'APP_ORIGIN', value: 'https://exported.invalid' },
{ key: 'DISCORD_CLIENT_ID', value: 'exported-discord-client' },
{ key: 'DISCORD_CLIENT_SECRET', value: 'exported-discord-secret' },
]),
writeExport(sharedExport, [
{ key: 'SCHEDULER_TOKEN', value: schedulerToken },
]),
]);
});
afterEach(async () => {
await rm(temporaryRoot, { recursive: true, force: true });
});
function runtimeEnvironment(
scope: 'app' | 'scheduler',
): Record<string, string> {
return {
PATH: `${temporaryRoot}:${process.env.PATH ?? ''}`,
INFISICAL_PROJECT_ID: 'project-id',
INFISICAL_ENVIRONMENT: 'staging',
INFISICAL_RUNTIME_SCOPE: scope,
INFISICAL_UNIVERSAL_AUTH_CLIENT_ID_FILE: clientIdFile,
INFISICAL_UNIVERSAL_AUTH_CLIENT_SECRET_FILE: clientSecretFile,
INFISICAL_UNIVERSAL_AUTH_CLIENT_ID: 'bootstrap-client-id',
INFISICAL_UNIVERSAL_AUTH_CLIENT_SECRET: 'bootstrap-client-secret',
INFISICAL_UNIVERSAL_AUTH_ACCESS_TOKEN: 'bootstrap-access-token',
INFISICAL_TOKEN: 'bootstrap-token',
TOKEN: 'generic-bootstrap-token',
STUB_EXPECTED_CLIENT_ID: 'universal-client-id',
STUB_EXPECTED_CLIENT_SECRET: 'universal-client-secret',
STUB_LOGIN_LOG: loginLog,
STUB_EXPORT_LOG: exportLog,
STUB_GOSU_LOG: gosuLog,
STUB_APP_EXPORT: appExport,
STUB_SHARED_EXPORT: sharedExport,
CAPTURE_ENV_PATH: capturedEnvironment,
SHD_BUILD_NUMBER: '42',
SHD_COMMIT_SHA: 'abc123',
APP_ORIGIN: 'https://parent.invalid',
SCHEDULER_URL: 'https://parent.invalid/scheduler',
BASH_ENV: '',
CDPATH: temporaryRoot,
ENV: '',
IFS: 'hostile-ifs',
LD_LIBRARY_PATH: temporaryRoot,
LD_PRELOAD: '',
NODE_OPTIONS: '--no-warnings',
NODE_PATH: temporaryRoot,
PYTHONPATH: temporaryRoot,
};
}
function start(environment: Record<string, string>) {
return spawnSync(
'/bin/sh',
['./deploy/container-entrypoint.sh', 'capture-environment'],
{
cwd: temporaryRoot,
env: environment,
encoding: 'utf8',
},
);
}
it('exports app and shared secrets into a scrubbed app child environment', async () => {
const result = start(runtimeEnvironment('app'));
expect(result.status, result.stderr).toBe(0);
expect(await exportPaths()).toEqual(['/app', '/shared/scheduler']);
expect(await readFile(loginLog, 'utf8')).toBe(
'login\n--method=universal-auth\n--silent\n--plain\ncredentials=ok\n',
);
const childEnvironment = parseEnvironment(
await readFile(capturedEnvironment, 'utf8'),
);
expect(childEnvironment).toMatchObject({
APP_ORIGIN: 'https://stage.shd.build',
DISCORD_CLIENT_ID: 'exported-discord-client',
DISCORD_CLIENT_SECRET: 'exported-discord-secret',
ORIGIN: 'https://stage.shd.build',
PASSKEY_RP_ID: 'shd.build',
SCHEDULER_TOKEN: schedulerToken,
SHD_BUILD_NUMBER: '42',
SHD_COMMIT_SHA: 'abc123',
TDR_AVATAR_STORAGE_PATH: '/app/data/avatars',
TDR_DATABASE_PATH: '/app/data/shd-build-staging.db',
TDR_GAME_ICON_STORAGE_PATH: '/app/data/game-icons',
});
for (const name of removedEnvironmentNames) {
expect(childEnvironment).not.toHaveProperty(name);
}
await expectSecretFilesRemoved();
await expectRootPrivilegeDrop();
});
it('exports only shared secrets into the scheduler child environment', async () => {
const result = start(runtimeEnvironment('scheduler'));
expect(result.status, result.stderr).toBe(0);
expect(await exportPaths()).toEqual(['/shared/scheduler']);
expect(
parseEnvironment(await readFile(capturedEnvironment, 'utf8')),
).toMatchObject({
SCHEDULER_TOKEN: schedulerToken,
SCHEDULER_URL: 'http://app:3000/api/internal/scheduler',
});
await expectSecretFilesRemoved();
await expectRootPrivilegeDrop();
});
it('fails closed on an unknown secret before launching the child', async () => {
await writeExport(appExport, [
{ key: 'DISCORD_CLIENT_ID', value: 'exported-discord-client' },
{ key: 'UNKNOWN_SECRET', value: 'must-not-reach-child' },
]);
const result = start(runtimeEnvironment('app'));
expect(result.status).not.toBe(0);
expect(result.stderr).toContain(
'Infisical secret UNKNOWN_SECRET is not allowed in this runtime scope.',
);
expect(existsSync(capturedEnvironment)).toBe(false);
await expectSecretFilesRemoved();
});
it('fails closed when a credential file setting is missing', () => {
const environment = runtimeEnvironment('app');
delete environment.INFISICAL_UNIVERSAL_AUTH_CLIENT_ID_FILE;
const result = start(environment);
expect(result.status).not.toBe(0);
expect(result.stderr).toContain(
'INFISICAL_UNIVERSAL_AUTH_CLIENT_ID_FILE is required',
);
expect(existsSync(exportLog)).toBe(false);
expect(existsSync(capturedEnvironment)).toBe(false);
});
it('fails closed when either credential is empty', async () => {
for (const credentialFile of [clientIdFile, clientSecretFile]) {
await writeFile(credentialFile, '');
const result = start(runtimeEnvironment('app'));
expect(result.status).not.toBe(0);
expect(result.stderr).toContain('credential files must not be empty');
expect(existsSync(exportLog)).toBe(false);
expect(existsSync(capturedEnvironment)).toBe(false);
await writeFile(
credentialFile,
credentialFile === clientIdFile
? 'universal-client-id\n'
: 'universal-client-secret\n',
);
}
});
it.each(['fail', 'empty'])(
'fails closed when login returns %s',
(loginResult) => {
const result = start({
...runtimeEnvironment('app'),
STUB_LOGIN_RESULT: loginResult,
});
expect(result.status).not.toBe(0);
expect(existsSync(exportLog)).toBe(false);
expect(existsSync(capturedEnvironment)).toBe(false);
},
);
it('fails closed on a malformed export and removes every secret file', async () => {
await writeFile(appExport, '{not-json');
const result = start(runtimeEnvironment('app'));
expect(result.status).not.toBe(0);
expect(result.stderr).toContain(
'Infisical returned an unreadable secret export.',
);
expect(existsSync(capturedEnvironment)).toBe(false);
await expectSecretFilesRemoved();
});
async function exportPaths() {
return (await exportRecords()).map(([path]) => path);
}
async function expectSecretFilesRemoved() {
for (const [, file] of await exportRecords()) {
expect(existsSync(file)).toBe(false);
}
}
async function exportRecords() {
return (await readFile(exportLog, 'utf8'))
.trim()
.split('\n')
.map((line) => line.split('|'));
}
async function expectRootPrivilegeDrop() {
if (typeof process.getuid === 'function' && process.getuid() === 0) {
expect(await readFile(gosuLog, 'utf8')).toBe('target=shd-runtime\n');
} else {
expect(existsSync(gosuLog)).toBe(false);
}
}
});
const removedEnvironmentNames = [
'BASH_ENV',
'CDPATH',
'ENV',
'IFS',
'INFISICAL_ENVIRONMENT',
'INFISICAL_PROJECT_ID',
'INFISICAL_RUNTIME_SCOPE',
'INFISICAL_TOKEN',
'INFISICAL_UNIVERSAL_AUTH_ACCESS_TOKEN',
'INFISICAL_UNIVERSAL_AUTH_CLIENT_ID',
'INFISICAL_UNIVERSAL_AUTH_CLIENT_ID_FILE',
'INFISICAL_UNIVERSAL_AUTH_CLIENT_SECRET',
'INFISICAL_UNIVERSAL_AUTH_CLIENT_SECRET_FILE',
'LD_LIBRARY_PATH',
'LD_PRELOAD',
'NODE_OPTIONS',
'NODE_PATH',
'PYTHONPATH',
'TOKEN',
];
function writeExport(
file: string,
entries: Array<{ key: string; value: string }>,
) {
return writeFile(file, `${JSON.stringify(entries)}\n`);
}
function parseEnvironment(value: string) {
return Object.fromEntries(
value
.trim()
.split('\n')
.map((line) => {
const separator = line.indexOf('=');
return [line.slice(0, separator), line.slice(separator + 1)];
}),
);
}
+46 -10
View File
@@ -14,7 +14,11 @@ production_environment="$production_directory/.env.deploy"
staging_compose_file="$staging_directory/compose.staging.yml"
staging_environment="$staging_directory/.env.deploy"
snapshot_name="isac-production-$(date -u +%Y-%m-%dT%H-%M-%SZ).json"
snapshot_path="/tmp/$snapshot_name"
snapshot_path=$(mktemp "/tmp/$snapshot_name.XXXXXX")
production_snapshot_path="/app/data/exports/$snapshot_name"
staging_snapshot_path="/app/data/$snapshot_name"
remove_production_snapshot=false
remove_staging_snapshot=false
production_compose() {
docker compose --env-file "$production_environment" -f "$production_compose_file" "$@"
@@ -22,22 +26,54 @@ production_compose() {
staging_compose() {
docker compose --env-file "$staging_environment" -f "$staging_compose_file" "$@"
}
restart_services() {
production_compose up -d --no-build app scheduler || true
staging_compose up -d --no-build app scheduler || true
rm -f "$snapshot_path"
cleanup() {
exit_status=$?
trap - EXIT
if [ "$remove_production_snapshot" = true ]; then
production_compose run --rm --no-deps app rm -f "$production_snapshot_path" || true
fi
if [ "$remove_staging_snapshot" = true ]; then
staging_compose run --rm --no-deps app rm -f "$staging_snapshot_path" || true
fi
rm -f "$snapshot_path" || true
if ! production_compose up -d --no-build app scheduler; then
if [ "$exit_status" -eq 0 ]; then exit_status=1; fi
fi
if ! staging_compose up -d --no-build app scheduler; then
if [ "$exit_status" -eq 0 ]; then exit_status=1; fi
fi
exit "$exit_status"
}
trap restart_services EXIT
trap cleanup EXIT
echo "Stopping production writers for a consistent canonical export."
production_compose stop scheduler app
production_compose run --rm --no-deps -v "/tmp:/export" app \
node scripts/isac-data-export.mjs "/export/$snapshot_name"
production_app_container=$(production_compose ps --all --quiet app)
if [ -z "$production_app_container" ]; then
echo "The stopped production app container could not be found." >&2
exit 1
fi
remove_production_snapshot=true
production_compose run --rm --no-deps app \
node scripts/isac-data-export.mjs "$production_snapshot_path"
docker cp "$production_app_container:$production_snapshot_path" "$snapshot_path"
production_compose run --rm --no-deps app rm -f "$production_snapshot_path"
remove_production_snapshot=false
production_compose up -d --no-build app scheduler
echo "Stopping staging writers before its automatic full and ISAC safety backups."
staging_compose stop scheduler app
staging_compose run --rm --no-deps -v "/tmp:/import:ro" app \
node scripts/isac-data-import.mjs "/import/$snapshot_name" --confirm-replace-isac-data
staging_app_container=$(staging_compose ps --all --quiet app)
if [ -z "$staging_app_container" ]; then
echo "The stopped staging app container could not be found." >&2
exit 1
fi
remove_staging_snapshot=true
docker cp "$snapshot_path" "$staging_app_container:$staging_snapshot_path"
staging_compose run --rm --no-deps app \
node scripts/isac-data-import.mjs "$staging_snapshot_path" --confirm-replace-isac-data
staging_compose run --rm --no-deps app rm -f "$staging_snapshot_path"
remove_staging_snapshot=false
rm -f "$snapshot_path"
echo "Production ISAC data was safely synchronized into staging."