From 4a2411d2e3066cebc3c514d8ea5aec09b3725a5b Mon Sep 17 00:00:00 2001 From: ZekaDeploy Date: Thu, 17 Sep 2026 17:24:16 +0300 Subject: [PATCH] fix(deploy): a two-file --env-file dropped the operator file; single channel + env In podman-compose 1.6.0, `--env-file` is a SINGLE-valued argument and only the LAST file is resolved (podman_compose.py:2941-2946, :2559). The unit's and the deploy's two-file call (operator first, then stack.env) therefore never read the operator file at all: the stack ran with the compose defaults (DATABASE_URL password `hezarfen`, bridge token `change-me`) and the deploy's "cannot be empty" gate only inspected the file, so nothing went red (hit in production, 2026-09-17). - unit: the operator file enters the unit's environment via `EnvironmentFile=`, and the single `--env-file` passed to compose is stack.env (ExecStart + ExecStop). - deploy remote script: the operator file is read line by line into the process (no shell expansion), and the compose calls use a single `--env-file`. - new gate: once the stack is up, the container's environment must carry the operator file's VALUES verbatim (AI_SHARED_TOKEN, POSTGRES_PASSWORD, AI_QUIC_ADDR, BUILDER_USERNAME); otherwise rollback. - AI_SHARED_TOKEN and AI_QUIC_ADDR were added to the required-key list. The environment variable overrides --env-file values (podman_compose.py:2562), so the two channels cannot clash; Docker Compose v2's "the later file wins on a clashing key" behaviour does not apply to this provider. --- .github/workflows/main.yml | 54 ++++++++++++++++++++++--- deploy/hezarfen_backend_compose.service | 28 ++++++++++--- 2 files changed, 72 insertions(+), 10 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index edd4ad2..213463e 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -376,13 +376,37 @@ jobs: exit 1 fi chmod 0600 -- "$env_file" - for key in POSTGRES_PASSWORD BUILDER_PASSWORD ZO_ROOT_USER_EMAIL ZO_ROOT_USER_PASSWORD; do + for key in POSTGRES_PASSWORD BUILDER_PASSWORD ZO_ROOT_USER_EMAIL ZO_ROOT_USER_PASSWORD AI_SHARED_TOKEN AI_QUIC_ADDR; do grep -Eq "^${key}=.+" "$env_file" || { echo "no non-empty ${key}= line in $env_file — prod must not run on the compose defaults" >&2 exit 1 } done + # OPERATOR ORTAMI SURECE ALINIR. podman-compose interpolasyonu ORTAM + # DEGISKENLERINE bakar ve `--env-file` YALNIZ SON dosyayi cozer + # (podman_compose.py:2941-2946, :2559): iki dosyali cagri operator + # dosyasini tamamen dusurur ve stack SESSIZCE compose + # varsayilanlariyla kosar (DATABASE_URL parolasi `hezarfen`, kopru + # tokeni `change-me`) — yukaridaki "bos olamaz" kapisi bunu + # yakalamaz, cunku o dosyayi denetler, compose'un onu kullanip + # kullanmadigini degil (canlida yasandi, 2026-09-17). Bu yuzden + # dosya satir satir okunur: kabuk genisletmesi YOK (deger icindeki + # `$` oldugu gibi kalir), bicim duz KEY=value oldugu icin + # tirnak/`export` aranmaz. + while IFS= read -r satir || [ -n "$satir" ]; do + case "$satir" in ''|'#'*) continue ;; esac + anahtar=${satir%%=*} + deger=${satir#*=} + case "$anahtar" in + ''|*[!A-Za-z0-9_]*) + echo "operator env: gecersiz anahtar adi -- harf/rakam/alt cizgi beklenirdi" >&2 + exit 1 + ;; + esac + export "$anahtar=$deger" + done < "$env_file" + # Rootless podman plus a compose provider; fail with the fix, not # a bare command-not-found three steps in. command -v podman >/dev/null || { @@ -407,8 +431,10 @@ jobs: printf '%s\n' "$tag" > "$deploy_path/current_tag" # stack.env is deploy-owned (unlike the secrets file): it carries - # only the image tag, and --env-file order makes it win over the - # operator's file for HEZARFEN_TAG. + # only the image tag, and it is the ONE `--env-file` compose gets — + # the operator's values are in the process environment (above). A + # two-file call would drop the operator file entirely + # (podman_compose.py:2941-2946, :2559). printf 'HEZARFEN_TAG=%s\n' "$tag" > "$deploy_path/stack.env" # The freshly extracted bundle: compose file and unit are @@ -420,7 +446,7 @@ jobs: rm -f -- "$deploy_path/hezarfen_backend.tar.gz" compose_up() { - podman compose --env-file "$env_file" --env-file "$deploy_path/stack.env" up -d --no-build + podman compose --env-file "$deploy_path/stack.env" up -d --no-build } rollback() { if [ -f "$deploy_path/previous_tag" ]; then @@ -433,7 +459,7 @@ jobs: # First deploy: nothing to roll back to, so stop the # half-brought-up stack. Plain down — the volumes hold the # operator's data and postgres's first-boot password. - podman compose --env-file "$env_file" --env-file "$deploy_path/stack.env" down + podman compose --env-file "$deploy_path/stack.env" down echo 'first deploy failed; stack stopped, volumes kept' >&2 fi } @@ -488,6 +514,24 @@ jobs: exit 1 ;; esac + # OPERATOR SIRLARI KONTEYNERDE MI -- bu iddia olmadan iki dosyali + # `--env-file` sinifi sessizce geri gelebilir: stack compose + # varsayilanlariyla ayaga kalkar ve hicbir sey kirmizi olmaz. En + # guclu hal: konteynerin ortamindaki DEGER, operator dosyasindaki + # DEGERLE birebir ayni olmali. Deger YAZDIRILMAZ. + container_env=$(podman inspect -f '{{range .Config.Env}}{{println .}}{{end}}' "$container") + for cift in "AI_SHARED_TOKEN=$AI_SHARED_TOKEN" "POSTGRES_PASSWORD=$POSTGRES_PASSWORD" "AI_QUIC_ADDR=$AI_QUIC_ADDR" "BUILDER_USERNAME=$BUILDER_USERNAME"; do + ad=${cift%%=*} + case "$container_env" in + *"$cift"*) ;; + *) + echo "container env: $ad operator dosyasindaki degeri TASIMIYOR -- operator env compose'a ulasmamis; rolling back" >&2 + rollback + exit 1 + ;; + esac + done + echo 'ok: operator sirlari konteynerde (AI_SHARED_TOKEN / POSTGRES_PASSWORD / AI_QUIC_ADDR / BUILDER_USERNAME birebir)' echo 'hezarfen_backend_compose is up' REMOTE diff --git a/deploy/hezarfen_backend_compose.service b/deploy/hezarfen_backend_compose.service index 898da66..fb94ce2 100644 --- a/deploy/hezarfen_backend_compose.service +++ b/deploy/hezarfen_backend_compose.service @@ -36,11 +36,29 @@ Wants=network-online.target Type=oneshot RemainAfterExit=yes WorkingDirectory=%h/hezarfen_backend -# Later --env-file wins for duplicate keys: the operator's secrets file -# first, the deploy-owned stack.env (HEZARFEN_TAG) second. -ExecStart=/usr/bin/podman compose --env-file %h/hezarfen_backend/hezarfen_backend.env --env-file %h/hezarfen_backend/stack.env up -d --no-build -# No -v: named volumes must survive stop. -ExecStop=/usr/bin/podman compose down +# ORTAM IKI KANALDAN GELIR -- bu bir tercih degil, saglayicinin siniridir: +# * operatorun ayar dosyasi -> `EnvironmentFile=` (systemd dosyayi unit'in +# ORTAMINA koyar; compose interpolasyonu ORTAM DEGISKENLERINE bakar) +# * deploy sahipli stack.env (HEZARFEN_TAG) -> tek `--env-file` +# +# NEDEN: `podman compose --env-file A --env-file B` bu saglayicida A'yi HIC +# OKUMAZ. `--env-file` tek degerli bir argumandir (podman_compose.py:2941-2946) +# ve yalniz SON dosya cozulur (:2559 `dotenv_path = os.path.realpath(args.env_file)`). +# Iki dosyali eski cagri operator dosyasini tamamen dusuruyordu: stack compose +# varsayilanlariyla kosuyordu (`DATABASE_URL` parolasi `hezarfen`, kopru tokeni +# `change-me`) ve dosyadaki degerler HIC okunmuyordu -- deploy'un +# "POSTGRES_PASSWORD bos olamaz" kapisi dosyayi denetlerken compose onu +# kullanmadigi icin bu sessiz kaliyordu (canlida yasandi, 2026-09-17). Docker +# Compose v2'nin "sonraki dosya cakisan anahtarda kazanir" davranisi bu +# saglayici icin GECERLI DEGIL. Ortam degiskeni --env-file degerlerini EZER +# (:2562), yani iki kanal cakismaz. +EnvironmentFile=%h/hezarfen_backend/hezarfen_backend.env +# Tek --env-file: yalniz deploy sahipli stack.env (HEZARFEN_TAG). +ExecStart=/usr/bin/podman compose --env-file %h/hezarfen_backend/stack.env up -d --no-build +# No -v: named volumes must survive stop. (`EnvironmentFile=` yukarida oldugu +# icin burada da gerekli degiskenler ortamdadir; saglayici eksik env dosyasini +# bos sozluk sayar -- podman_compose.py:2247-2250.) +ExecStop=/usr/bin/podman compose --env-file %h/hezarfen_backend/stack.env down TimeoutStartSec=180 [Install]