diff --git a/.dockerignore b/.dockerignore index d62a49eb..4124ab80 100644 --- a/.dockerignore +++ b/.dockerignore @@ -12,3 +12,7 @@ uploads **/uploads *.pdf graphify-out +**/.revalidation.env* +**/.support*.env* +**/*runtime.env +**/release.override.json diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index 75ed8358..89aac4b6 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -13,6 +13,8 @@ jobs: - name: Run deploy.sh from within the scripts folder env: BRANCH: ${{ github.ref_name }} + DEPLOY_SHA: ${{ github.sha }} + GITHUB_ACTIONS: 'true' SUPPORT_CHAT_KEY: ${{ secrets.SUPPORT_CHAT_KEY }} run: | # 1. رفع مشکل DNS و غیرفعال کردن پروکسی برای دامنه محلی @@ -25,7 +27,8 @@ jobs: # 2. دانلود و اجرای اسکریپت deploy.sh بر اساس برنچ جاری mkdir -p scripts - wget --no-proxy -qO scripts/deploy.sh https://git.parsaaghayi.ir/parsa/canina/raw/branch/${BRANCH}/scripts/deploy.sh + wget --no-proxy -qO scripts/deploy.sh https://git.parsaaghayi.ir/parsa/canina/raw/commit/${DEPLOY_SHA}/scripts/deploy.sh + wget --no-proxy -qO scripts/release-cache-stage.sh https://git.parsaaghayi.ir/parsa/canina/raw/commit/${DEPLOY_SHA}/scripts/release-cache-stage.sh chmod +x scripts/deploy.sh cd scripts diff --git a/scripts/deploy.sh b/scripts/deploy.sh index b0f52388..1d617b8e 100644 --- a/scripts/deploy.sh +++ b/scripts/deploy.sh @@ -1,6 +1,11 @@ #!/bin/bash set -e +if [ "${1:-develop}" = develop ] && [ "${2:-}" = "" ]; then + SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd) + exec bash "$SCRIPT_DIR/release-cache-stage.sh" +fi + # Explicit no-data-write stage rollout. Run only after testing and pushing HEAD. if [ "${2:-}" = "stage-backend-only" ]; then [ "${1:-}" = "develop" ] || exit 2 @@ -105,12 +110,16 @@ if [ ! -s .support-chat.env ]; then exit 1 fi -# Ensure .revalidation.env exists and is properly provisioned with valid secret -if [ -f "scripts/provision-revalidation-env.py" ]; then - python3 scripts/provision-revalidation-env.py --environment "${BRANCH:-stage}" --source .revalidation.env --output .revalidation.env.tmp 2>/dev/null && mv .revalidation.env.tmp .revalidation.env && chmod 600 .revalidation.env || touch .revalidation.env -else - touch .revalidation.env -fi +# Legacy main path must fail closed rather than deploy an empty secret file. +ENVIRONMENT=prod +[ "$BRANCH" = develop ] && ENVIRONMENT=stage +exec 9>"${TARGET_DIR}/../${ENVIRONMENT}-revalidation-deploy.lock" +flock -n 9 || exit 1 +[ -f scripts/provision-revalidation-env.py ] || exit 1 +python3 scripts/provision-revalidation-env.py --environment "$ENVIRONMENT" \ + --source .revalidation.env --output .revalidation.env.tmp +# The operational source remains unchanged; this validated copy is disposable. +rm .revalidation.env.tmp # ✅ فایل‌های کانفیگ را از پوشه‌ی scripts مخزن به TARGET_DIR کپی می‌کنیم echo "Copying configuration files in $TARGET_DIR..." @@ -138,7 +147,7 @@ BOT_ENV="${TARGET_DIR}/.support-bot.runtime.env" docker inspect -f '{{range .Config.Env}}{{println .}}{{end}}' canina-bot > "${BOT_ENV}" chmod 600 "${BOT_ENV}" echo "Starting containers..." -docker compose up -d --remove-orphans --force-recreate backend_stage frontend_stage +docker compose up -d --no-deps --force-recreate "backend_${ENVIRONMENT}" "frontend_${ENVIRONMENT}" echo "Starting support-chat bot container..." docker rm -f canina-bot >/dev/null 2>&1 || true docker run -d --name canina-bot --restart unless-stopped --env-file "${BOT_ENV}" -e HERMES_HOME=/opt/data -e HERMES_RUNTIME_DIR=/opt/hermes/tools -e API_SERVER_HOST=0.0.0.0 -e API_SERVER_PORT=8642 --network traefik_public -v canina_bot_data:/opt/data "${BOT_IMAGE}" gateway run diff --git a/scripts/provision-revalidation-env.py b/scripts/provision-revalidation-env.py index e3cfec37..47dc71c4 100644 --- a/scripts/provision-revalidation-env.py +++ b/scripts/provision-revalidation-env.py @@ -12,10 +12,24 @@ import secrets import stat -def provision(source: Path, output: Path) -> None: +def provision(source: Path, output: Path, repair_empty: bool = False) -> None: for path in (source, output): if any(parent.is_symlink() for parent in (path, *path.parents)): raise ValueError('Unsafe runtime path') + if repair_empty and source.exists(): + info = source.lstat() + if not stat.S_ISREG(info.st_mode) or info.st_nlink != 1: + raise ValueError('Unsafe runtime file') + if info.st_size == 0: + backup = source.with_name(source.name + '.empty-before-provision') + os.link(source, backup) + temporary = source.with_name(source.name + '.new-private') + fd = os.open(temporary, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600) + with os.fdopen(fd, 'w') as stream: + stream.write('REVALIDATION_SECRET=' + secrets.token_urlsafe(48) + '\n') + stream.flush() + os.fsync(stream.fileno()) + os.replace(temporary, source) if not source.exists(): fd = os.open(source, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600) with os.fdopen(fd, 'w') as stream: @@ -41,13 +55,14 @@ def provision(source: Path, output: Path) -> None: def main() -> None: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--environment', choices=('stage', 'prod'), required=True) + parser.add_argument('--repair-empty', action='store_true') parser.add_argument('--source', type=Path, required=True) parser.add_argument('--output', type=Path, required=True) args = parser.parse_args() if os.environ.get('GITHUB_ACTIONS', os.environ.get('GITEA_ACTIONS')) != 'true': parser.error('Provisioning requires serialized CI deployment') try: - provision(args.source, args.output) + provision(args.source, args.output, repair_empty=args.repair_empty) except (ValueError, OSError): parser.exit(1, 'ERROR: revalidation provisioning failed; no secret values logged\n') diff --git a/scripts/release-cache-stage.sh b/scripts/release-cache-stage.sh new file mode 100644 index 00000000..7184aefd --- /dev/null +++ b/scripts/release-cache-stage.sh @@ -0,0 +1,55 @@ +#!/usr/bin/env bash +# Narrow code-only Stage release in the existing CI runner/daemon boundary. +set -euo pipefail +set +x +umask 077 +[[ ${GITHUB_ACTIONS:-${GITEA_ACTIONS:-}} == true && ${DEPLOY_SHA:-} =~ ^[0-9a-f]{40}$ ]] || exit 2 +STAGE_DIR=/opt/services/canina/stage +exec 9>/opt/services/canina/stage-backend-deploy.lock +flock -n 9 || { printf 'Stage deployment locked\n' >&2; exit 1; } +cd "$STAGE_DIR" +git fetch origin develop +[[ $(git rev-parse origin/develop) == "$DEPLOY_SHA" ]] || exit 2 +# Never reset/clean the operational checkout or overwrite its generated configs. +TARGET_DIR="/opt/services/canina/cache-releases/$DEPLOY_SHA" +[[ ! -e "$TARGET_DIR" ]] || { printf 'Release directory already exists\n' >&2; exit 2; } +mkdir -p "$TARGET_DIR" +git archive "$DEPLOY_SHA" | tar -x -C "$TARGET_DIR" +[[ $(git rev-parse HEAD) == "$DEPLOY_SHA" ]] || printf 'Operational HEAD intentionally preserved\n' +command -v python3 >/dev/null || apk add --no-cache python3 +cd "$TARGET_DIR" +python3 scripts/provision-revalidation-env.py --environment stage --repair-empty \ + --source "$STAGE_DIR/.revalidation.env" --output "$TARGET_DIR/.revalidation.env" +# Build with the existing operational nginx configuration, without changing it. +cp "$STAGE_DIR/nginx.conf" "$TARGET_DIR/nginx.conf" +# Compose uses the existing operational runtime configuration plus a private, +# release-specific override. Neither the database nor Redis is recreated. +export STAGE_DIR TARGET_DIR DEPLOY_SHA +python3 - <<'PY' +import json, os +from pathlib import Path +root=Path(os.environ['TARGET_DIR']); sha=os.environ['DEPLOY_SHA'] +services={} +for kind in ('backend','frontend'): + name=kind+'_stage' + services[name]={ + 'build': {'context': str(root/('backend' if kind=='backend' else '.')), + 'labels': {'org.opencontainers.image.revision': sha}, + 'args': {'NPM_REGISTRY': 'https://registry.parsaaghayi.ir/repository/npm/'}}, + 'labels': {'ir.canina.release.sha': sha}, + 'env_file': [{'path': str(root/'.revalidation.env'), 'required': True}], + } +services['backend_stage']['environment']={'FRONTEND_INTERNAL_URL':'http://frontend_stage:3000'} +services['backend_stage']['command']=['node','dist/main.js'] +services['frontend_stage']['environment']={'INTERNAL_API_URL':'http://backend_stage:3000/api'} +Path('release.override.json').write_text(json.dumps({'services':services})) +PY +compose=(docker compose -p stage --project-directory "$STAGE_DIR" -f "$STAGE_DIR/docker-compose.yml" -f "$TARGET_DIR/release.override.json") +"${compose[@]}" config --quiet +"${compose[@]}" --progress plain build backend_stage frontend_stage +"${compose[@]}" up -d --no-deps --force-recreate backend_stage frontend_stage +for name in canino_backend_stage canino_frontend_stage; do + [[ $(docker inspect -f '{{index .Config.Labels "ir.canina.release.sha"}}' "$name") == "$DEPLOY_SHA" ]] + docker inspect -f '{{.Name}} image={{.Image}} release={{index .Config.Labels "ir.canina.release.sha"}}' "$name" +done +printf 'Code-only Stage release complete: %s\n' "$DEPLOY_SHA" diff --git a/scripts/tests/test_cache_stage_release.py b/scripts/tests/test_cache_stage_release.py new file mode 100644 index 00000000..593e502d --- /dev/null +++ b/scripts/tests/test_cache_stage_release.py @@ -0,0 +1,45 @@ +import os +from pathlib import Path +import subprocess +import tempfile +import unittest + +ROOT = Path(__file__).resolve().parents[2] + +class CacheStageReleaseTests(unittest.TestCase): + def test_develop_uses_isolated_code_only_release(self): + deploy = (ROOT/'scripts/deploy.sh').read_text() + self.assertIn('exec bash "$SCRIPT_DIR/release-cache-stage.sh"', deploy) + workflow = (ROOT/'.gitea/workflows/deploy.yml').read_text() + self.assertIn('/raw/commit/${DEPLOY_SHA}/scripts/', workflow) + self.assertIn('DEPLOY_SHA: ${{ github.sha }}', workflow) + + def test_release_is_serialized_and_fail_closed(self): + source = (ROOT/'scripts/release-cache-stage.sh').read_text() + self.assertLess(source.index('flock'), source.index('provision-revalidation-env.py --environment stage')) + self.assertIn('git archive "$DEPLOY_SHA"', source) + self.assertIn('--force-recreate backend_stage frontend_stage', source) + self.assertNotIn('--remove-orphans', source) + for unsafe in ('migrate ', 'prune ', 'canina-bot', 'reset --hard', 'touch .revalidation.env'): + self.assertNotIn(unsafe, source) + self.assertIn('git rev-parse HEAD', source) + self.assertIn('org.opencontainers.image.revision', source) + + def test_runtime_secrets_never_enter_frontend_build_context(self): + ignore = (ROOT/'.dockerignore').read_text().splitlines() + self.assertIn('**/.revalidation.env*', ignore) + self.assertIn('**/.support*.env*', ignore) + + def test_legacy_empty_secret_requires_explicit_repair(self): + import importlib.util + spec = importlib.util.spec_from_file_location('provision', ROOT/'scripts/provision-revalidation-env.py') + module = importlib.util.module_from_spec(spec); spec.loader.exec_module(module) + with tempfile.TemporaryDirectory() as d: + source, output = Path(d)/'source', Path(d)/'output' + source.touch() + with self.assertRaises(ValueError): module.provision(source, output) + module.provision(source, output, repair_empty=True) + self.assertEqual(source.read_bytes(), output.read_bytes()) + self.assertEqual((Path(d)/'source.empty-before-provision').read_bytes(), b'') + +if __name__ == '__main__': unittest.main()