From d98d0ae545db5cc718bebfced7a0c5ad89ec4cf2 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 18 Sep 2026 06:54:33 +0000 Subject: [PATCH] feat: require live post-deploy health probe Resolve the probe from DEPLOY_HEALTH_URL or APP_URL + /api/_info/health-check. Live refuses without a URL unless --allow-no-deploy-health / ALLOW_NO_DEPLOY_HEALTH=1. Rename ROLLBACK_ON_SMOKE_FAIL to ROLLBACK_ON_FAIL (no alias). Do not read SMOKE_URL. Co-authored-by: ROBJkE --- README.md | 12 +- docs/command-matrix.md | 39 ++-- src/cli.rs | 51 +++++- src/shopware/env.rs | 21 ++- src/shopware/release.rs | 26 +-- src/shopware/rollback.rs | 20 ++- src/shopware/rollout.rs | 377 ++++++++++++++++++++++++++++++++++++--- tests/release.rs | 196 +++++++++++++++++++- tests/rollback.rs | 78 +++++++- 9 files changed, 747 insertions(+), 73 deletions(-) diff --git a/README.md b/README.md index 3f2d6d2..8829bff 100644 --- a/README.md +++ b/README.md @@ -212,9 +212,17 @@ fyrst-cli shopware deploy rollback --dry-run fyrst-cli shopware deploy rollback --skip-pull ``` -On live smoke failure, release prints +After a successful compose recreate, release/rollback GET a health URL +(`curl -fsS`, 30 attempts, 2s sleep). Default probe: `APP_URL` with trailing +slash stripped + `/api/_info/health-check`. Override with `DEPLOY_HEALTH_URL` +(full URL, used as-is). Live (`SHOPWARE_DEPLOY_ENV=live`) requires a probe URL; +ops escape only: `--allow-no-deploy-health` or `ALLOW_NO_DEPLOY_HEALTH=1`. +Non-live skips the probe when no URL can be resolved. + +On live deploy-health failure, release prints `IMAGE_TAG=$(cat .previous-tag) fyrst-cli shopware deploy rollback` and may -auto-rollback; it still exits 1. Live with empty `COMPOSE_PROFILES` warns +auto-rollback (`ROLLBACK_ON_FAIL`; unset → on for live); it still +exits 1. Live with empty `COMPOSE_PROFILES` warns (recommended: `redis,worker,scheduler`). ### sync capture / apply / pull / local diff --git a/docs/command-matrix.md b/docs/command-matrix.md index 8e1c41f..87872a1 100644 --- a/docs/command-matrix.md +++ b/docs/command-matrix.md @@ -30,8 +30,8 @@ those scripts. Use the CLI column. | Historical overlay (removed) | CLI | Nested verbs | Flags | Status | | --- | --- | --- | --- | --- | | `deploy/init-env.sh` | `fyrst-cli shopware env init` | `init` | `--shop-id`, `--env`, `--image`, `--dry-run` | **implemented** | -| `deploy/vps-release.sh` | `fyrst-cli shopware deploy release` | `release` | `--dry-run`, `--skip-pull` | **implemented** | -| `deploy/vps-rollback.sh` | `fyrst-cli shopware deploy rollback` | `rollback` | `--dry-run`, `--skip-pull` | **implemented** | +| `deploy/vps-release.sh` | `fyrst-cli shopware deploy release` | `release` | `--dry-run`, `--skip-pull`, `--allow-no-deploy-health` | **implemented** | +| `deploy/vps-rollback.sh` | `fyrst-cli shopware deploy rollback` | `rollback` | `--dry-run`, `--skip-pull`, `--allow-no-deploy-health` | **implemented** | | `restore_db_*` (sync-runtime) | `fyrst-cli shopware db import` | `import` | `--file`, `--dry-run`, `--allow-live` | **implemented** | | `deploy/sync-runtime.sh` snapshot | `fyrst-cli shopware sync capture` | `capture` | `--from`, `--data`, `--snapshot-dir`, `--dry-run`, `--skip-db`, `--skip-volumes` | **volumes implemented** (bind-mount trees → `--snapshot-dir/data//`; named-volume tar fallback). **Not a dump command:** `--data db` exits 2 and tells operators to run `shopware-cli project dump` | | `deploy/sync-runtime.sh` restore | `fyrst-cli shopware sync apply` | `apply` | same flags | **implemented**: DB via same import module; bind-mount volumes from `data//` or `volumes/.tar.gz`; stop/start `web`/`worker`/`scheduler`; rewrite from `APP_URL` via compose `web`; live refuse unless `SHOPWARE_ALLOW_LIVE_RESTORE=1`; rewrite skipped on live | @@ -59,6 +59,7 @@ for apply, pull, import, and backup recover (also in `shopware --help`). | `sync apply` | `SHOPWARE_ALLOW_LIVE_RESTORE=1` only | | `sync pull` | `SHOPWARE_ALLOW_LIVE_RESTORE=1` only | | `backup recover` | confirm flag (`--i-understand-this-restores-this-host` or `BACKUP_CONFIRM_RESTORE=1`) **and** `SHOPWARE_ALLOW_LIVE_RESTORE=1` when `SHOPWARE_DEPLOY_ENV=live` | +| `deploy release` / `deploy rollback` | post-deploy health probe required (`DEPLOY_HEALTH_URL` or `APP_URL`). Ops escape: `--allow-no-deploy-health` or `ALLOW_NO_DEPLOY_HEALTH=1` | URL rewrite after apply uses `APP_URL` and is **skipped** on live (never applied). Staging / playground / dev need no extra live flag for backup @@ -96,7 +97,7 @@ fyrst-cli shopware env init [--shop-id SLUG] [--env live|staging|playground|dev] ## Exact deploy release CLI ```text -fyrst-cli shopware deploy release [--dry-run] [--skip-pull] +fyrst-cli shopware deploy release [--dry-run] [--skip-pull] [--allow-no-deploy-health] ``` ## `shopware deploy release` (implemented) @@ -125,11 +126,16 @@ compile flags stay in the setup helper image. 4. One-shot `--profile setup run --rm --pull never setup`. 5. `up -d --no-build` (optional `--pull never`) `--remove-orphans web`. 6. Extra `COMPOSE_PROFILES` (must not include `setup`). -7. Optional `SMOKE_URL` (curl, 30 attempts). Write `.deployed-tag` only after - setup/web succeed and smoke passes. -8. On smoke failure: print +7. Post-deploy health probe (`curl -fsS`, 30 attempts, 2s sleep). Default URL: + `APP_URL` with trailing slash stripped + `/api/_info/health-check`. Override + with `DEPLOY_HEALTH_URL` (full URL, used as-is). Write `.deployed-tag` only after + setup/web succeed and the probe passes (or is skipped). + Live (`SHOPWARE_DEPLOY_ENV=live`) **requires** a probe URL; refuse before + mutating unless `--allow-no-deploy-health` / `ALLOW_NO_DEPLOY_HEALTH=1` (ops + escape only). Non-live skips the probe when no URL can be resolved. +8. On deploy-health failure: print `IMAGE_TAG=$(cat .previous-tag) fyrst-cli shopware deploy rollback`. Auto-run the - shared rollout helper at the previous tag when `ROLLBACK_ON_SMOKE_FAIL` is + shared rollout helper at the previous tag when `ROLLBACK_ON_FAIL` is on (unset → **on for live**, off otherwise). Release still **exits 1** after a successful auto-rollback. First deploy with no `.previous-tag` cannot auto-rollback. @@ -140,8 +146,9 @@ compile flags stay in the setup helper image. Loud warning (does not auto-enable) when `SHOPWARE_DEPLOY_ENV=live` and `COMPOSE_PROFILES` is empty. Recommended live: `redis,worker,scheduler`. -Passwords / `DATABASE_URL` are never logged. Auto-rollback on smoke uses the -shared compose/rollout helper (`fyrst-cli shopware deploy rollback`). +Passwords / `DATABASE_URL` are never logged. Auto-rollback on deploy-health +failure uses the shared compose/rollout helper +(`fyrst-cli shopware deploy rollback`). ## Exact import CLI @@ -214,13 +221,13 @@ passwords). ## Exact deploy rollback CLI ```text -fyrst-cli shopware deploy rollback [--dry-run] [--skip-pull] +fyrst-cli shopware deploy rollback [--dry-run] [--skip-pull] [--allow-no-deploy-health] ``` ## `shopware deploy rollback` (implemented) Resolves shop root (`COMPOSE_DIR` or walk from cwd for `.env` + `deploy/`), -loads `.env` then `.env.local` then `.env.prod`. Process-env wins for `IMAGE`, `SMOKE_URL`, +loads `.env` then `.env.local` then `.env.prod`. Process-env wins for `IMAGE`, `DEPLOY_HEALTH_URL`, `COMPOSE_PROFILES`, `SKIP_PULL`, `PULL_POLICY` when non-empty. **`IMAGE_TAG` from the process environment is ignored** — the only tag is shop-root `.previous-tag` (overlay `vps_read_previous_tag`). Missing or empty @@ -239,8 +246,10 @@ Rollout order reuses the release compose/rollout helper (never `--build`; 3. One-shot `compose --profile setup run --rm --pull never setup`. 4. `up -d --no-build` (optional `--pull never`) `--remove-orphans web`. 5. Extra `COMPOSE_PROFILES` (must not include `setup`). -6. Optional `SMOKE_URL`. Write `.deployed-tag` **only after** success (and - smoke, if set). Smoke failure does not update `.deployed-tag`. +6. Post-deploy health probe (same URL rules as release). Write `.deployed-tag` + **only after** success (and the probe, if a URL was resolved). Probe failure + does not update `.deployed-tag`. Live requires a probe URL unless the ops + escape `--allow-no-deploy-health` / `ALLOW_NO_DEPLOY_HEALTH=1` is set. `--dry-run` prints that sequence and does not pull or recreate containers. `--skip-pull` sets `PULL_POLICY=never` (same-host / air-gap). @@ -586,8 +595,8 @@ This CLI reads shop identity and secrets from the environment / shop-root | Restore volumes | `SHOPWARE_DATA_ROOT`, `SHOPWARE_DATA_BASE` | | Rewrite | `APP_URL`, `IMAGE` | | Dump (shopware-cli only) | Use `shopware-cli project dump`. fyrst-cli does not dump. | -| Release | `IMAGE`, `IMAGE_TAG` (release / `.env`; **ignored on rollback**), `COMPOSE_PROFILES`, `SMOKE_URL`, `PULL_POLICY`, `SKIP_PULL`, `ROLLBACK_ON_SMOKE_FAIL` | -| Rollback | `.previous-tag` is the only `IMAGE_TAG`; same optional env as release except `ROLLBACK_ON_SMOKE_FAIL` | +| Release | `IMAGE`, `IMAGE_TAG` (release / `.env`; **ignored on rollback**), `COMPOSE_PROFILES`, `DEPLOY_HEALTH_URL` (default from `APP_URL` + `/api/_info/health-check`), `ALLOW_NO_DEPLOY_HEALTH`, `PULL_POLICY`, `SKIP_PULL`, `ROLLBACK_ON_FAIL` | +| Rollback | `.previous-tag` is the only `IMAGE_TAG`; same optional env as release except `ROLLBACK_ON_FAIL` | | SSH / remote live data | `SHOPWARE_SSH_HOST`, `SHOPWARE_SSH_USER`, `SHOPWARE_SSH_KEY`, `SHOPWARE_REMOTE_DATA_ROOT` | | Live gate | `SHOPWARE_ALLOW_LIVE_RESTORE` | | Backup | `BACKUP_TARGET` (default `local`), `BACKUP_KEEP_DAYS` (default 14, `0` = forever), `BACKUP_DB_DUMP`, `BACKUP_CONFIRM_RESTORE` | diff --git a/src/cli.rs b/src/cli.rs index 39b3ef4..3ebd0be 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -33,6 +33,9 @@ Live policy (refuse a live consumer unless): sync pull SHOPWARE_ALLOW_LIVE_RESTORE=1 only backup recover confirm flag + SHOPWARE_ALLOW_LIVE_RESTORE=1 (--i-understand-this-restores-this-host or BACKUP_CONFIRM_RESTORE=1) + deploy release post-deploy health probe (DEPLOY_HEALTH_URL or APP_URL); + ops escape: --allow-no-deploy-health / ALLOW_NO_DEPLOY_HEALTH=1 + deploy rollback same probe require as release Dump = shopware-cli project dump only (fyrst-cli does not dump). Import = fyrst-cli shopware db import (also used by sync apply --data db and sync pull). @@ -49,6 +52,16 @@ backup recover = disaster recovery onto this host (confirmation required). See docs/command-matrix.md. "; +pub const DEPLOY_HEALTH_AFTER_HELP: &str = "\ +Post-deploy health probe (curl -fsS, 30 attempts, 2s sleep):\n \ + Default: APP_URL with trailing slash stripped + /api/_info/health-check\n \ + Override: DEPLOY_HEALTH_URL (full URL, used as-is)\n \ + Live (SHOPWARE_DEPLOY_ENV=live): a probe URL is required.\n \ + Ops escape only: --allow-no-deploy-health or ALLOW_NO_DEPLOY_HEALTH=1\n \ + Non-live: skip the probe when no URL can be resolved.\n \ + Probe failure auto-rollback still uses ROLLBACK_ON_FAIL\n \ + (unset → on for live, off otherwise).\n"; + #[derive(Debug, Parser)] #[command( name = "fyrst-cli", @@ -113,7 +126,9 @@ import `db.sql.gz` from `--snapshot-dir` if `--data` includes db (does not dump) database, never local SHOPWARE_DATA_ROOT). `--data all` is refused (all includes db on sync pull / backup create). \ Dumps stay with `shopware-cli project dump` — this CLI does not wrap dump.\n\n\ `deploy rollback` is implemented: same compose files and order as `vps-release.sh`, with \ -IMAGE_TAG only from `.previous-tag` (process-env IMAGE_TAG is ignored).\n\n\ +IMAGE_TAG only from `.previous-tag` (process-env IMAGE_TAG is ignored). Live requires a \ +post-deploy health probe (`DEPLOY_HEALTH_URL` or `APP_URL` + `/api/_info/health-check`); \ +`--allow-no-deploy-health` / `ALLOW_NO_DEPLOY_HEALTH=1` is an ops escape only.\n\n\ `backup create` copies bind-mount trees (and an operator-provided db.sql.gz) into \ BACKUP_TARGET; it is allowed on live. \ `backup prune` is implemented: stamp-based retention under BACKUP_TARGET \ @@ -220,6 +235,7 @@ pub struct InitEnvArgs { } #[derive(Debug, Args)] +#[command(after_help = DEPLOY_HEALTH_AFTER_HELP)] pub struct ReleaseArgs { /// Print the compose sequence; do not pull or recreate containers #[arg(long)] @@ -228,9 +244,14 @@ pub struct ReleaseArgs { /// Same-host / air-gap: skip registry pull, PULL_POLICY=never #[arg(long = "skip-pull")] pub skip_pull: bool, + + /// Ops escape only: skip the live require for a post-deploy health probe (or set ALLOW_NO_DEPLOY_HEALTH=1) + #[arg(long = "allow-no-deploy-health")] + pub allow_no_deploy_health: bool, } #[derive(Debug, Args)] +#[command(after_help = DEPLOY_HEALTH_AFTER_HELP)] pub struct RollbackArgs { /// Print the compose sequence; do not pull or recreate containers #[arg(long)] @@ -239,6 +260,10 @@ pub struct RollbackArgs { /// Same-host / air-gap: skip registry pull, PULL_POLICY=never #[arg(long = "skip-pull")] pub skip_pull: bool, + + /// Ops escape only: skip the live require for a post-deploy health probe (or set ALLOW_NO_DEPLOY_HEALTH=1) + #[arg(long = "allow-no-deploy-health")] + pub allow_no_deploy_health: bool, } #[derive(Debug, Subcommand)] @@ -588,6 +613,29 @@ mod tests { }) => { assert!(op.dry_run); assert!(op.skip_pull); + assert!(!op.allow_no_deploy_health); + } + other => panic!("unexpected parse: {other:?}"), + } + } + + #[test] + fn deploy_release_parses_allow_no_deploy_health() { + let cli = Cli::try_parse_from([ + "fyrst-cli", + "shopware", + "deploy", + "release", + "--allow-no-deploy-health", + ]) + .unwrap(); + match cli.command { + Command::Shopware(ShopwareArgs { + command: ShopwareCommand::Deploy(DeployCommand::Release(op)), + }) => { + assert!(op.allow_no_deploy_health); + assert!(!op.dry_run); + assert!(!op.skip_pull); } other => panic!("unexpected parse: {other:?}"), } @@ -635,6 +683,7 @@ mod tests { }) => { assert!(op.dry_run); assert!(op.skip_pull); + assert!(!op.allow_no_deploy_health); } other => panic!("unexpected parse: {other:?}"), } diff --git a/src/shopware/env.rs b/src/shopware/env.rs index 87643bb..b32244b 100644 --- a/src/shopware/env.rs +++ b/src/shopware/env.rs @@ -30,9 +30,10 @@ const PROCESS_WINS: &[&str] = &[ "SHOPWARE_SSH_KEY", "SHOPWARE_ALLOW_LIVE_RESTORE", "APP_URL", - "SMOKE_URL", + "DEPLOY_HEALTH_URL", + "ALLOW_NO_DEPLOY_HEALTH", "COMPOSE_PROFILES", - "ROLLBACK_ON_SMOKE_FAIL", + "ROLLBACK_ON_FAIL", "SKIP_PULL", "PULL_POLICY", "BACKUP_TARGET", @@ -599,7 +600,7 @@ services: .unwrap(); fs::write( shop.join(".env.prod"), - "APP_URL=http://from-prod\nSMOKE_URL=http://smoke\n", + "APP_URL=http://from-prod\nDEPLOY_HEALTH_URL=http://health\n", ) .unwrap(); let mut process = HashMap::new(); @@ -609,7 +610,7 @@ services: assert_eq!(env.get("APP_URL"), Some("http://from-prod")); assert_eq!(env.get("SHOPWARE_SSH_HOST"), Some("vps.local")); assert_eq!(env.get("BACKUP_TARGET"), Some("/from-local")); - assert_eq!(env.get("SMOKE_URL"), Some("http://smoke")); + assert_eq!(env.get("DEPLOY_HEALTH_URL"), Some("http://health")); let _ = fs::remove_dir_all(&shop); } @@ -649,7 +650,7 @@ SHOPWARE_SHOP_ID=acme SHOPWARE_DEPLOY_ENV=live IMAGE=ghcr.io/from-file/shop IMAGE_TAG=latest -SMOKE_URL=http://from-file +DEPLOY_HEALTH_URL=http://from-file-health COMPOSE_PROFILES=redis ", ) @@ -657,11 +658,17 @@ COMPOSE_PROFILES=redis let mut process = HashMap::new(); process.insert("IMAGE".into(), "ghcr.io/from-ci/shop".into()); process.insert("IMAGE_TAG".into(), "abc123deadbeef".into()); - process.insert("SMOKE_URL".into(), "http://127.0.0.1:8000".into()); + process.insert( + "DEPLOY_HEALTH_URL".into(), + "http://127.0.0.1:8000/api/_info/health-check".into(), + ); let env = ShopEnv::load(shop.clone(), &process).unwrap(); assert_eq!(env.get("IMAGE"), Some("ghcr.io/from-ci/shop")); assert_eq!(env.get("IMAGE_TAG"), Some("abc123deadbeef")); - assert_eq!(env.get("SMOKE_URL"), Some("http://127.0.0.1:8000")); + assert_eq!( + env.get("DEPLOY_HEALTH_URL"), + Some("http://127.0.0.1:8000/api/_info/health-check") + ); assert_eq!(env.get("COMPOSE_PROFILES"), Some("redis")); let _ = fs::remove_dir_all(&shop); } diff --git a/src/shopware/release.rs b/src/shopware/release.rs index 4645453..de265ae 100644 --- a/src/shopware/release.rs +++ b/src/shopware/release.rs @@ -1,14 +1,14 @@ //! `fyrst-cli shopware deploy release` — VPS image pull + Compose recreate. //! //! Matches recipes `deploy/vps-release.sh`. Never builds images or compiles -//! themes/assets. Auto-rollback on smoke failure uses the shared rollout +//! themes/assets. Auto-rollback on deploy-health failure uses the shared rollout //! helper (`fyrst-cli shopware deploy rollback` is the operator command). use super::env::{resolve_compose_dir, ShopEnv}; use super::error::Error; use super::rollout::{ - bootstrap, docker_log_checked, ensure_env_prod, execute_rollout, read_tag_file, - record_previous_tag, rollout_to_tag, smoke, smoke_fail_message, write_tag_file, + bootstrap, deploy_health, deploy_health_fail_message, docker_log_checked, ensure_env_prod, + execute_rollout, read_tag_file, record_previous_tag, rollout_to_tag, write_tag_file, ROLLBACK_ONE_LINER, }; use crate::cli::ReleaseArgs; @@ -21,6 +21,9 @@ pub fn run(args: ReleaseArgs) -> Result<(), Error> { if args.skip_pull { process_env.insert("SKIP_PULL".into(), "1".into()); } + if args.allow_no_deploy_health { + process_env.insert("ALLOW_NO_DEPLOY_HEALTH".into(), "1".into()); + } let cwd = std::env::current_dir().map_err(|e| Error::fail(format!("cannot read cwd: {e}")))?; execute_release(&process_env, &cwd, args.skip_pull, args.dry_run) } @@ -78,12 +81,15 @@ pub fn execute_release( execute_rollout(&ctx, &plan)?; - if let Err(_e) = smoke(&ctx) { - let url = ctx.smoke_url.as_deref().unwrap_or("SMOKE_URL"); - let mut msg = smoke_fail_message(url); + if let Err(_e) = deploy_health(&ctx) { + let url = ctx + .deploy_health_url + .as_deref() + .unwrap_or("DEPLOY_HEALTH_URL"); + let mut msg = deploy_health_fail_message(url); if ctx.should_auto_rollback() { println!( - "==> ROLLBACK_ON_SMOKE_FAIL on (SHOPWARE_DEPLOY_ENV={}; live default is on, others off unless ROLLBACK_ON_SMOKE_FAIL=1)", + "==> ROLLBACK_ON_FAIL on (SHOPWARE_DEPLOY_ENV={}; live default is on, others off unless ROLLBACK_ON_FAIL=1)", ctx.deploy_env ); let Some(tag) = previous.filter(|t| !t.is_empty()) else { @@ -101,12 +107,12 @@ pub fn execute_release( return Err(Error::fail(msg)); } msg.push_str( - "\nERROR: Rolled back after smoke failure. Release still exits 1 so CI does not treat the new tag as live.", + "\nERROR: Rolled back after deploy health failure. Release still exits 1 so CI does not treat the new tag as live.", ); return Err(Error::fail(msg)); } println!( - "==> Auto-rollback skipped (SHOPWARE_DEPLOY_ENV={}; set ROLLBACK_ON_SMOKE_FAIL=1 to enable)", + "==> Auto-rollback skipped (SHOPWARE_DEPLOY_ENV={}; set ROLLBACK_ON_FAIL=1 to enable)", ctx.deploy_env ); return Err(Error::fail(msg)); @@ -175,7 +181,7 @@ fn print_dry_run( format!("==> DRY-RUN extra profiles: {}", plan.profiles_raw), )?; } - if let Some(url) = &ctx.smoke_url { + if let Some(url) = &ctx.deploy_health_url { emit(ctx, format!("==> DRY-RUN would GET {url}"))?; } Ok(()) diff --git a/src/shopware/rollback.rs b/src/shopware/rollback.rs index f908dcf..070c568 100644 --- a/src/shopware/rollback.rs +++ b/src/shopware/rollback.rs @@ -2,14 +2,14 @@ //! //! `IMAGE` stays from env / `.env`. `IMAGE_TAG` is **only** `.previous-tag` //! (process-env `IMAGE_TAG` is ignored). Reuses the release compose/rollout -//! helper. Writes `.deployed-tag` only after a successful rollout (and smoke, -//! if set). +//! helper. Writes `.deployed-tag` only after a successful rollout (and deploy +//! health probe, if set). use super::env::{resolve_compose_dir, ShopEnv}; use super::error::Error; use super::mysql::require_docker; use super::rollout::{ - bootstrap, docker_log_checked, ensure_env_prod, execute_rollout, read_tag_file, smoke, + bootstrap, deploy_health, docker_log_checked, ensure_env_prod, execute_rollout, read_tag_file, write_tag_file, VpsContext, }; use crate::cli::RollbackArgs; @@ -22,6 +22,9 @@ pub fn run(args: RollbackArgs) -> Result<(), Error> { if args.skip_pull { process.insert("SKIP_PULL".into(), "1".into()); } + if args.allow_no_deploy_health { + process.insert("ALLOW_NO_DEPLOY_HEALTH".into(), "1".into()); + } let cwd = std::env::current_dir().map_err(|e| Error::fail(format!("cannot read cwd: {e}")))?; execute_rollback(&process, &cwd, args.skip_pull, args.dry_run) } @@ -76,7 +79,7 @@ pub fn execute_rollback( if dry_run { print_dry_run(&ctx, &plan)?; - if let Some(url) = &ctx.smoke_url { + if let Some(url) = &ctx.deploy_health_url { emit(&ctx, format!("==> DRY-RUN would GET {url}"))?; } emit( @@ -93,10 +96,13 @@ pub fn execute_rollback( require_docker()?; execute_rollout(&ctx, &plan)?; - if let Err(_e) = smoke(&ctx) { - let url = ctx.smoke_url.as_deref().unwrap_or("SMOKE_URL"); + if let Err(_e) = deploy_health(&ctx) { + let url = ctx + .deploy_health_url + .as_deref() + .unwrap_or("DEPLOY_HEALTH_URL"); return Err(Error::fail(format!( - "Smoke check failed for {url} after rollback. Stack is on {}:{} but .deployed-tag was not updated", + "Deploy health check failed for {url} after rollback. Stack is on {}:{} but .deployed-tag was not updated", ctx.image, ctx.image_tag ))); } diff --git a/src/shopware/rollout.rs b/src/shopware/rollout.rs index 2da3f63..4f3abe7 100644 --- a/src/shopware/rollout.rs +++ b/src/shopware/rollout.rs @@ -15,14 +15,21 @@ use std::process::{Command, Stdio}; use std::thread; use std::time::Duration; -pub const SMOKE_ATTEMPTS: u32 = 30; -pub const SMOKE_SLEEP: Duration = Duration::from_secs(2); +pub const DEPLOY_HEALTH_ATTEMPTS: u32 = 30; +pub const DEPLOY_HEALTH_SLEEP: Duration = Duration::from_secs(2); +/// Shopware Core health path (same as compose container healthcheck). +pub const DEPLOY_HEALTH_PATH: &str = "/api/_info/health-check"; -/// Operator one-liner printed on smoke failure (fyrst-cli equivalent of overlay +/// Operator one-liner printed on deploy-health failure (fyrst-cli equivalent of overlay /// `IMAGE_TAG=$(cat .previous-tag) bash deploy/vps-rollback.sh`). pub const ROLLBACK_ONE_LINER: &str = "IMAGE_TAG=$(cat .previous-tag) fyrst-cli shopware deploy rollback"; +const LIVE_DEPLOY_HEALTH_REQUIRED: &str = "\ +Live deploy requires a post-deploy health probe. Set DEPLOY_HEALTH_URL (full URL) \ +or APP_URL (probe is APP_URL with trailing slash stripped + /api/_info/health-check). \ +Ops escape only: --allow-no-deploy-health or ALLOW_NO_DEPLOY_HEALTH=1."; + pub fn env_truthy(v: &str) -> bool { matches!( v.trim().to_ascii_lowercase().as_str(), @@ -74,7 +81,51 @@ pub fn parse_profiles(raw: Option<&str>) -> Result, Error> { Ok(out) } -/// `ROLLBACK_ON_SMOKE_FAIL`: explicit 1/0 wins. Unset → on for live, off otherwise. +fn trimmed_non_empty(v: Option<&str>) -> Option<&str> { + v.map(str::trim).filter(|s| !s.is_empty()) +} + +/// Join `APP_URL` to the Shopware Core health path. Trailing slashes on the +/// base are stripped. Empty / whitespace-only `APP_URL` cannot produce a URL. +pub fn deploy_health_url_from_app_url(app_url: &str) -> Option { + let base = app_url.trim().trim_end_matches('/'); + if base.is_empty() { + None + } else { + Some(format!("{base}{DEPLOY_HEALTH_PATH}")) + } +} + +/// Resolved post-deploy probe: `DEPLOY_HEALTH_URL` as-is, else `APP_URL` + +/// `/api/_info/health-check`. `SMOKE_URL` is not read. +pub fn resolve_deploy_health_probe( + deploy_health_url: Option<&str>, + app_url: Option<&str>, +) -> Option { + if let Some(url) = trimmed_non_empty(deploy_health_url) { + return Some(url.to_string()); + } + trimmed_non_empty(app_url).and_then(deploy_health_url_from_app_url) +} + +pub fn live_deploy_health_required(deploy_env: &str) -> bool { + deploy_env.eq_ignore_ascii_case("live") +} + +/// Live must have a probe URL unless the ops escape hatch is set. +pub fn refuse_live_without_deploy_health( + deploy_env: &str, + url: Option<&str>, + allow_no: bool, +) -> Result<(), Error> { + if url.is_some() || !live_deploy_health_required(deploy_env) || allow_no { + Ok(()) + } else { + Err(Error::fail(LIVE_DEPLOY_HEALTH_REQUIRED)) + } +} + +/// `ROLLBACK_ON_FAIL`: explicit 1/0 wins. Unset → on for live, off otherwise. pub fn should_auto_rollback(flag: Option<&str>, deploy_env: &str) -> bool { if let Some(f) = flag.filter(|s| !s.is_empty()) { if env_truthy(f) { @@ -229,10 +280,10 @@ pub struct VpsContext { pub data_root: String, pub profiles: Vec, pub profiles_raw: String, - pub smoke_url: Option, + pub deploy_health_url: Option, pub pull_policy: String, pub skip_pull: bool, - pub rollback_on_smoke_fail: Option, + pub rollback_on_fail: Option, pub dry_run: bool, pub notes: Vec, pub warnings: Vec, @@ -275,7 +326,7 @@ impl VpsContext { } pub fn should_auto_rollback(&self) -> bool { - should_auto_rollback(self.rollback_on_smoke_fail.as_deref(), &self.deploy_env) + should_auto_rollback(self.rollback_on_fail.as_deref(), &self.deploy_env) } pub fn log_contains_secret(&self, text: &str) -> bool { @@ -322,8 +373,29 @@ pub fn bootstrap(env: &ShopEnv, skip_pull_flag: bool, dry_run: bool) -> Result Result Result<(), Error Ok(()) } -pub fn smoke(ctx: &VpsContext) -> Result<(), Error> { - let Some(url) = ctx.smoke_url.as_deref() else { +pub fn deploy_health(ctx: &VpsContext) -> Result<(), Error> { + let Some(url) = ctx.deploy_health_url.as_deref() else { return Ok(()); }; - println!("==> Smoke {url}"); + println!("==> Deploy health {url}"); if ctx.dry_run { println!("==> DRY-RUN would GET {url}"); return Ok(()); @@ -457,10 +529,10 @@ pub fn smoke(ctx: &VpsContext) -> Result<(), Error> { .unwrap_or(false); if !curl_ok { return Err(Error::fail(format!( - "Smoke check failed for {url}: curl is not available" + "Deploy health check failed for {url}: curl is not available" ))); } - for _ in 0..SMOKE_ATTEMPTS { + for _ in 0..DEPLOY_HEALTH_ATTEMPTS { let ok = Command::new("curl") .args(["-fsS", url]) .stdout(Stdio::null()) @@ -469,16 +541,16 @@ pub fn smoke(ctx: &VpsContext) -> Result<(), Error> { .map(|s| s.success()) .unwrap_or(false); if ok { - println!("==> Smoke OK"); + println!("==> Deploy health OK"); return Ok(()); } - thread::sleep(SMOKE_SLEEP); + thread::sleep(DEPLOY_HEALTH_SLEEP); } - Err(Error::fail(format!("Smoke check failed for {url}"))) + Err(Error::fail(format!("Deploy health check failed for {url}"))) } -pub fn smoke_fail_message(url: &str) -> String { - format!("Smoke check failed for {url}\nERROR: Rollback command: {ROLLBACK_ONE_LINER}") +pub fn deploy_health_fail_message(url: &str) -> String { + format!("Deploy health check failed for {url}\nERROR: Rollback command: {ROLLBACK_ONE_LINER}") } /// Re-run the same compose order at `image_tag` (release auto-rollback). @@ -487,7 +559,7 @@ pub fn rollout_to_tag(ctx: &mut VpsContext, image_tag: String) -> Result<(), Err ctx.image_tag = image_tag; let plan = ctx.plan(); execute_rollout(ctx, &plan)?; - smoke(ctx)?; + deploy_health(ctx)?; write_tag_file(&ctx.compose_dir.join(".deployed-tag"), &ctx.image_tag)?; Ok(()) } @@ -585,6 +657,32 @@ mod tests { assert!(!should_auto_rollback(Some("off"), "live")); } + #[test] + fn bootstrap_reads_rollback_on_fail_not_old_name() { + let shop = TempShop::new("rb-old"); + write_vps_files(&shop.0); + write_boot_env( + &shop.0, + "SHOPWARE_DEPLOY_ENV=live\nAPP_URL=https://shop.example.com/\nROLLBACK_ON_SMOKE_FAIL=0\n", + ); + let env = ShopEnv::load(shop.0.clone(), &HashMap::new()).unwrap(); + let ctx = bootstrap(&env, false, true).unwrap(); + assert!( + ctx.should_auto_rollback(), + "old ROLLBACK_ON_SMOKE_FAIL must not disable live default" + ); + + let shop = TempShop::new("rb-new"); + write_vps_files(&shop.0); + write_boot_env( + &shop.0, + "SHOPWARE_DEPLOY_ENV=live\nAPP_URL=https://shop.example.com/\nROLLBACK_ON_FAIL=0\n", + ); + let env = ShopEnv::load(shop.0.clone(), &HashMap::new()).unwrap(); + let ctx = bootstrap(&env, false, true).unwrap(); + assert!(!ctx.should_auto_rollback()); + } + #[test] fn live_empty_profiles_warns() { let w = live_empty_profiles_warnings("live", None); @@ -596,14 +694,82 @@ mod tests { } #[test] - fn smoke_fail_prints_rollback_one_liner() { - let m = smoke_fail_message("http://127.0.0.1:8000"); + fn deploy_health_fail_prints_rollback_one_liner() { + let m = deploy_health_fail_message("http://127.0.0.1:8000"); assert!(m.contains("http://127.0.0.1:8000"), "{m}"); assert!(m.contains(ROLLBACK_ONE_LINER), "{m}"); assert!(m.contains("fyrst-cli shopware deploy rollback"), "{m}"); + assert!(m.contains("Deploy health check failed"), "{m}"); assert!(!m.contains("DATABASE_URL"), "{m}"); } + #[test] + fn app_url_trailing_slash_joins_health_path() { + assert_eq!( + deploy_health_url_from_app_url("https://shop.example.com"), + Some("https://shop.example.com/api/_info/health-check".into()) + ); + assert_eq!( + deploy_health_url_from_app_url("https://shop.example.com/"), + Some("https://shop.example.com/api/_info/health-check".into()) + ); + assert_eq!( + deploy_health_url_from_app_url("https://shop.example.com///"), + Some("https://shop.example.com/api/_info/health-check".into()) + ); + assert_eq!( + deploy_health_url_from_app_url(" https://shop.example.com/ "), + Some("https://shop.example.com/api/_info/health-check".into()) + ); + assert_eq!(deploy_health_url_from_app_url(""), None); + assert_eq!(deploy_health_url_from_app_url(" "), None); + assert_eq!(deploy_health_url_from_app_url("/"), None); + } + + #[test] + fn resolve_prefers_deploy_health_url_then_app_url() { + assert_eq!( + resolve_deploy_health_probe( + Some("http://127.0.0.1:8000/api/_info/health-check"), + Some("https://shop.example.com/"), + ) + .as_deref(), + Some("http://127.0.0.1:8000/api/_info/health-check") + ); + assert_eq!( + resolve_deploy_health_probe(None, Some("https://shop.example.com/")).as_deref(), + Some("https://shop.example.com/api/_info/health-check") + ); + assert_eq!( + resolve_deploy_health_probe(Some(" "), Some(" ")).as_deref(), + None + ); + assert!(resolve_deploy_health_probe(None, None).is_none()); + } + + #[test] + fn live_refuses_without_probe_unless_escape_hatch() { + let err = refuse_live_without_deploy_health("live", None, false).unwrap_err(); + assert!(err.to_string().contains("DEPLOY_HEALTH_URL"), "{err}"); + assert!(err.to_string().contains("APP_URL"), "{err}"); + assert!( + err.to_string().contains("--allow-no-deploy-health"), + "{err}" + ); + assert!(err.to_string().contains("ALLOW_NO_DEPLOY_HEALTH"), "{err}"); + + assert!(refuse_live_without_deploy_health("LIVE", None, false).is_err()); + assert!(refuse_live_without_deploy_health("live", None, true).is_ok()); + assert!(refuse_live_without_deploy_health( + "live", + Some("http://127.0.0.1:8000/api/_info/health-check"), + false + ) + .is_ok()); + assert!(refuse_live_without_deploy_health("staging", None, false).is_ok()); + assert!(refuse_live_without_deploy_health("dev", None, false).is_ok()); + } + #[test] fn setup_run_never_uses_no_build() { let (_shop, p) = plan(false, ""); @@ -706,6 +872,171 @@ DATABASE_URL=mysql://alice:s3cret-value@db.example.com/shop assert!(!ctx.log_contains_secret(&web)); assert!(!web.contains("s3cret-value")); assert!(!web.contains("super-secret-pass")); + assert!(ctx.deploy_health_url.is_none()); + } + + fn write_boot_env(shop: &Path, extra: &str) { + fs::write( + shop.join(".env"), + format!( + "\ +SHOPWARE_SHOP_ID=acme +IMAGE=ghcr.io/file/shop +IMAGE_TAG=latest +{extra}" + ), + ) + .unwrap(); + } + + #[test] + fn bootstrap_app_url_trailing_slash_becomes_health_path() { + let shop = TempShop::new("app-url-join"); + write_vps_files(&shop.0); + write_boot_env( + &shop.0, + "SHOPWARE_DEPLOY_ENV=staging\nAPP_URL=https://shop.example.com/\n", + ); + let env = ShopEnv::load(shop.0.clone(), &HashMap::new()).unwrap(); + let ctx = bootstrap(&env, false, true).unwrap(); + assert_eq!( + ctx.deploy_health_url.as_deref(), + Some("https://shop.example.com/api/_info/health-check") + ); + } + + #[test] + fn bootstrap_smoke_url_does_not_drive_probe() { + let shop = TempShop::new("smoke-ignored"); + write_vps_files(&shop.0); + write_boot_env( + &shop.0, + "SHOPWARE_DEPLOY_ENV=staging\nSMOKE_URL=http://127.0.0.1:8000\n", + ); + let env = ShopEnv::load(shop.0.clone(), &HashMap::new()).unwrap(); + let ctx = bootstrap(&env, false, true).unwrap(); + assert!(ctx.deploy_health_url.is_none()); + assert!(!ctx.notes.iter().any(|n| n.contains("SMOKE_URL"))); + + let shop = TempShop::new("smoke-ignored-app"); + write_vps_files(&shop.0); + write_boot_env( + &shop.0, + "SHOPWARE_DEPLOY_ENV=staging\nSMOKE_URL=http://127.0.0.1:8000\nAPP_URL=https://shop.example.com/\n", + ); + let env = ShopEnv::load(shop.0.clone(), &HashMap::new()).unwrap(); + let ctx = bootstrap(&env, false, true).unwrap(); + assert_eq!( + ctx.deploy_health_url.as_deref(), + Some("https://shop.example.com/api/_info/health-check") + ); + } + + #[test] + fn bootstrap_live_smoke_url_alone_does_not_satisfy_require() { + let shop = TempShop::new("live-smoke-only"); + write_vps_files(&shop.0); + write_boot_env( + &shop.0, + "SHOPWARE_DEPLOY_ENV=live\nSMOKE_URL=http://127.0.0.1:8000\n", + ); + let env = ShopEnv::load(shop.0.clone(), &HashMap::new()).unwrap(); + let err = match bootstrap(&env, false, true) { + Err(e) => e, + Ok(_) => panic!("SMOKE_URL must not satisfy the live health require"), + }; + assert!(err.to_string().contains("DEPLOY_HEALTH_URL"), "{err}"); + } + + #[test] + fn bootstrap_deploy_health_url_used_as_is() { + let shop = TempShop::new("health-override"); + write_vps_files(&shop.0); + write_boot_env( + &shop.0, + "SHOPWARE_DEPLOY_ENV=live\nDEPLOY_HEALTH_URL=http://127.0.0.1:8000/api/_info/health-check\nAPP_URL=https://shop.example.com/\nSMOKE_URL=http://ignored\n", + ); + let env = ShopEnv::load(shop.0.clone(), &HashMap::new()).unwrap(); + let ctx = bootstrap(&env, false, true).unwrap(); + assert_eq!( + ctx.deploy_health_url.as_deref(), + Some("http://127.0.0.1:8000/api/_info/health-check") + ); + } + + #[test] + fn bootstrap_live_refuses_without_probe_url() { + let shop = TempShop::new("live-no-health"); + write_vps_files(&shop.0); + write_boot_env(&shop.0, "SHOPWARE_DEPLOY_ENV=live\n"); + let env = ShopEnv::load(shop.0.clone(), &HashMap::new()).unwrap(); + let err = match bootstrap(&env, false, true) { + Err(e) => e, + Ok(_) => panic!("expected live refuse without probe URL"), + }; + assert!(err.to_string().contains("DEPLOY_HEALTH_URL"), "{err}"); + assert!( + err.to_string().contains("--allow-no-deploy-health"), + "{err}" + ); + } + + #[test] + fn bootstrap_live_empty_and_whitespace_app_url_refuse() { + let shop = TempShop::new("live-empty-app"); + write_vps_files(&shop.0); + write_boot_env(&shop.0, "SHOPWARE_DEPLOY_ENV=live\nAPP_URL=\n"); + let env = ShopEnv::load(shop.0.clone(), &HashMap::new()).unwrap(); + assert!(env.get("APP_URL").is_none()); + let err = match bootstrap(&env, false, true) { + Err(e) => e, + Ok(_) => panic!("expected live refuse without probe URL"), + }; + assert!(err.to_string().contains("Live deploy requires"), "{err}"); + + // File parser trims values, so APP_URL= in a file is empty. + // Process-env whitespace is non-empty for ShopEnv::get / PROCESS_WINS + // but must not produce a probe URL (and must not satisfy live require). + let shop = TempShop::new("live-ws-app"); + write_vps_files(&shop.0); + write_boot_env(&shop.0, "SHOPWARE_DEPLOY_ENV=live\n"); + let mut process = HashMap::new(); + process.insert("APP_URL".into(), " ".into()); + let env = ShopEnv::load(shop.0.clone(), &process).unwrap(); + assert_eq!(env.get("APP_URL"), Some(" ")); + let err = match bootstrap(&env, false, true) { + Err(e) => e, + Ok(_) => panic!("expected live refuse without probe URL"), + }; + assert!(err.to_string().contains("Live deploy requires"), "{err}"); + } + + #[test] + fn bootstrap_live_escape_hatch_skips_probe() { + let shop = TempShop::new("live-hatch"); + write_vps_files(&shop.0); + write_boot_env( + &shop.0, + "SHOPWARE_DEPLOY_ENV=live\nALLOW_NO_DEPLOY_HEALTH=1\n", + ); + let env = ShopEnv::load(shop.0.clone(), &HashMap::new()).unwrap(); + let ctx = bootstrap(&env, false, true).unwrap(); + assert!(ctx.deploy_health_url.is_none()); + assert!(ctx + .warnings + .iter() + .any(|w| w.contains("ops escape only") && w.contains("ALLOW_NO_DEPLOY_HEALTH"))); + } + + #[test] + fn bootstrap_staging_skips_probe_without_url() { + let shop = TempShop::new("staging-skip"); + write_vps_files(&shop.0); + write_boot_env(&shop.0, "SHOPWARE_DEPLOY_ENV=staging\n"); + let env = ShopEnv::load(shop.0.clone(), &HashMap::new()).unwrap(); + let ctx = bootstrap(&env, false, true).unwrap(); + assert!(ctx.deploy_health_url.is_none()); + assert!(!ctx.warnings.iter().any(|w| w.contains("ops escape"))); } #[test] diff --git a/tests/release.rs b/tests/release.rs index da9992b..27b5fcd 100644 --- a/tests/release.rs +++ b/tests/release.rs @@ -79,9 +79,12 @@ const LEAK_KEYS: &[&str] = &[ "IMAGE_TAG", "COMPOSE_PROFILES", "SMOKE_URL", + "DEPLOY_HEALTH_URL", + "ALLOW_NO_DEPLOY_HEALTH", + "APP_URL", "PULL_POLICY", "SKIP_PULL", - "ROLLBACK_ON_SMOKE_FAIL", + "ROLLBACK_ON_FAIL", "MYSQL_PASSWORD", "MYSQL_ROOT_PASSWORD", "DATABASE_URL", @@ -126,15 +129,35 @@ fn release_help_documents_flags() { .unwrap(); assert!(out.status.success()); let help = String::from_utf8_lossy(&out.stdout); - for needle in ["--dry-run", "--skip-pull"] { + for needle in ["--dry-run", "--skip-pull", "--allow-no-deploy-health"] { assert!(help.contains(needle), "missing {needle} in:\n{help}"); } + assert!( + help.contains("DEPLOY_HEALTH_URL"), + "help should document DEPLOY_HEALTH_URL:\n{help}" + ); + assert!( + help.contains("Ops escape"), + "help should mark the live hatch as ops escape:\n{help}" + ); + assert!( + help.contains("ROLLBACK_ON_FAIL"), + "help should document ROLLBACK_ON_FAIL:\n{help}" + ); + assert!( + !help.contains("ROLLBACK_ON_SMOKE_FAIL"), + "old env name still in help:\n{help}" + ); + assert!( + !help.contains("SMOKE_URL"), + "SMOKE_URL should not appear in help:\n{help}" + ); } #[test] fn dry_run_prints_compose_sequence_without_passwords() { let shop = TempShop::new("dry"); - shop.write_env("SMOKE_URL=http://127.0.0.1:8000\nCOMPOSE_PROFILES=redis,worker,scheduler\nCOMPOSE_PROJECT_NAME=shopware-acme\n"); + shop.write_env("DEPLOY_HEALTH_URL=http://127.0.0.1:8000/api/_info/health-check\nCOMPOSE_PROFILES=redis,worker,scheduler\nCOMPOSE_PROJECT_NAME=shopware-acme\n"); fs::write(shop.path().join(".deployed-tag"), "oldtag\n").unwrap(); let out = release(shop.path(), &["--dry-run"], &[]); assert_eq!( @@ -176,7 +199,7 @@ fn dry_run_prints_compose_sequence_without_passwords() { "{log}" ); assert!( - log.contains("DRY-RUN would GET http://127.0.0.1:8000"), + log.contains("DRY-RUN would GET http://127.0.0.1:8000/api/_info/health-check"), "{log}" ); assert!( @@ -286,6 +309,7 @@ SHOPWARE_SHOP_ID=acme SHOPWARE_DEPLOY_ENV=live IMAGE=ghcr.io/fyrst-dev/acme IMAGE_TAG=deadbeef +APP_URL=https://shop.example.com/ ", ) .unwrap(); @@ -298,6 +322,11 @@ IMAGE_TAG=deadbeef err.contains("COMPOSE_PROFILES=redis,worker,scheduler"), "{err}" ); + let log = stdout(&out); + assert!( + log.contains("DRY-RUN would GET https://shop.example.com/api/_info/health-check"), + "{log}" + ); } #[test] @@ -349,3 +378,162 @@ fn missing_image_tag_exits_1() { assert_eq!(out.status.code(), Some(1), "stderr={}", stderr(&out)); assert!(stderr(&out).contains("IMAGE_TAG"), "{}", stderr(&out)); } + +#[test] +fn smoke_url_alone_does_not_drive_probe() { + let shop = TempShop::new("smoke-ignored"); + shop.write_env("SMOKE_URL=http://127.0.0.1:8000\n"); + let out = release(shop.path(), &["--dry-run"], &[]); + assert_eq!(out.status.code(), Some(0), "stderr={}", stderr(&out)); + let log = stdout(&out); + assert!(!log.contains("DRY-RUN would GET"), "{log}"); + assert!(!log.contains("http://127.0.0.1:8000"), "{log}"); + assert!(!combined(&out).contains("deprecated"), "{}", combined(&out)); +} + +#[test] +fn smoke_url_does_not_override_app_url_probe() { + let shop = TempShop::new("smoke-not-override"); + shop.write_env("SMOKE_URL=http://127.0.0.1:8000\nAPP_URL=https://staging.example.com/\n"); + let out = release(shop.path(), &["--dry-run"], &[]); + assert_eq!(out.status.code(), Some(0), "stderr={}", stderr(&out)); + let log = stdout(&out); + assert!( + log.contains("DRY-RUN would GET https://staging.example.com/api/_info/health-check"), + "{log}" + ); + assert!(!log.contains("http://127.0.0.1:8000"), "{log}"); +} + +#[test] +fn app_url_default_probe_strips_trailing_slash() { + let shop = TempShop::new("app-url-join"); + shop.write_env("APP_URL=https://staging.example.com/\n"); + let out = release(shop.path(), &["--dry-run"], &[]); + assert_eq!(out.status.code(), Some(0), "stderr={}", stderr(&out)); + let log = stdout(&out); + assert!( + log.contains("DRY-RUN would GET https://staging.example.com/api/_info/health-check"), + "{log}" + ); +} + +#[test] +fn live_refuses_without_probe_url() { + let shop = TempShop::new("live-no-probe"); + fs::write( + shop.path().join(".env"), + "\ +SHOPWARE_SHOP_ID=acme +SHOPWARE_DEPLOY_ENV=live +IMAGE=ghcr.io/fyrst-dev/acme +IMAGE_TAG=deadbeef +", + ) + .unwrap(); + shop.write_vps_compose(); + let out = release(shop.path(), &["--dry-run"], &[]); + assert_eq!(out.status.code(), Some(1), "stderr={}", stderr(&out)); + let err = stderr(&out); + assert!(err.contains("DEPLOY_HEALTH_URL"), "{err}"); + assert!(err.contains("APP_URL"), "{err}"); + assert!(err.contains("--allow-no-deploy-health"), "{err}"); + assert!(!shop.path().join(".previous-tag").is_file()); +} + +#[test] +fn live_smoke_url_alone_does_not_satisfy_require() { + let shop = TempShop::new("live-smoke-only"); + fs::write( + shop.path().join(".env"), + "\ +SHOPWARE_SHOP_ID=acme +SHOPWARE_DEPLOY_ENV=live +IMAGE=ghcr.io/fyrst-dev/acme +IMAGE_TAG=deadbeef +SMOKE_URL=http://127.0.0.1:8000 +", + ) + .unwrap(); + shop.write_vps_compose(); + let out = release(shop.path(), &["--dry-run"], &[]); + assert_eq!(out.status.code(), Some(1), "stderr={}", stderr(&out)); + let err = stderr(&out); + assert!(err.contains("DEPLOY_HEALTH_URL"), "{err}"); + assert!(!err.contains("http://127.0.0.1:8000"), "{err}"); +} + +#[test] +fn live_whitespace_app_url_does_not_satisfy_require() { + let shop = TempShop::new("live-ws-app"); + fs::write( + shop.path().join(".env"), + "\ +SHOPWARE_SHOP_ID=acme +SHOPWARE_DEPLOY_ENV=live +IMAGE=ghcr.io/fyrst-dev/acme +IMAGE_TAG=deadbeef +", + ) + .unwrap(); + shop.write_vps_compose(); + let out = release(shop.path(), &["--dry-run"], &[("APP_URL", " ")]); + assert_eq!(out.status.code(), Some(1), "stderr={}", stderr(&out)); + assert!( + stderr(&out).contains("Live deploy requires"), + "{}", + stderr(&out) + ); +} + +#[test] +fn live_allow_no_deploy_health_flag_skips_probe() { + let shop = TempShop::new("live-hatch-flag"); + fs::write( + shop.path().join(".env"), + "\ +SHOPWARE_SHOP_ID=acme +SHOPWARE_DEPLOY_ENV=live +IMAGE=ghcr.io/fyrst-dev/acme +IMAGE_TAG=deadbeef +", + ) + .unwrap(); + shop.write_vps_compose(); + let out = release(shop.path(), &["--dry-run", "--allow-no-deploy-health"], &[]); + assert_eq!(out.status.code(), Some(0), "stderr={}", stderr(&out)); + let all = combined(&out); + assert!(all.contains("ops escape only"), "{all}"); + assert!( + !stdout(&out).contains("DRY-RUN would GET"), + "{}", + stdout(&out) + ); +} + +#[test] +fn live_allow_no_deploy_health_env_skips_probe() { + let shop = TempShop::new("live-hatch-env"); + fs::write( + shop.path().join(".env"), + "\ +SHOPWARE_SHOP_ID=acme +SHOPWARE_DEPLOY_ENV=live +IMAGE=ghcr.io/fyrst-dev/acme +IMAGE_TAG=deadbeef +", + ) + .unwrap(); + shop.write_vps_compose(); + let out = release( + shop.path(), + &["--dry-run"], + &[("ALLOW_NO_DEPLOY_HEALTH", "1")], + ); + assert_eq!(out.status.code(), Some(0), "stderr={}", stderr(&out)); + assert!( + combined(&out).contains("ALLOW_NO_DEPLOY_HEALTH"), + "{}", + combined(&out) + ); +} diff --git a/tests/rollback.rs b/tests/rollback.rs index 5d595ae..901ebb1 100644 --- a/tests/rollback.rs +++ b/tests/rollback.rs @@ -98,8 +98,12 @@ const LEAK_KEYS: &[&str] = &[ "IMAGE_TAG", "COMPOSE_PROFILES", "SMOKE_URL", + "DEPLOY_HEALTH_URL", + "ALLOW_NO_DEPLOY_HEALTH", + "APP_URL", "PULL_POLICY", "SKIP_PULL", + "ROLLBACK_ON_FAIL", ]; fn rollback(shop: &Path, extra: &[&str], extra_env: &[(&str, &str)]) -> Output { @@ -153,9 +157,17 @@ fn rollback_help_lists_flags() { .unwrap(); assert!(out.status.success()); let help = String::from_utf8_lossy(&out.stdout); - for needle in ["--dry-run", "--skip-pull"] { + for needle in ["--dry-run", "--skip-pull", "--allow-no-deploy-health"] { assert!(help.contains(needle), "missing {needle} in:\n{help}"); } + assert!( + help.contains("ROLLBACK_ON_FAIL"), + "help should document ROLLBACK_ON_FAIL:\n{help}" + ); + assert!( + !help.contains("ROLLBACK_ON_SMOKE_FAIL") && !help.contains("SMOKE_URL"), + "old smoke names still in help:\n{help}" + ); } #[test] @@ -326,7 +338,7 @@ fn skip_pull_via_env() { } #[test] -fn smoke_url_dry_run_does_not_curl() { +fn smoke_url_does_not_drive_probe() { let shop = TempShop::new("smoke"); shop.write_min_shop(); shop.write_previous("t1\n"); @@ -339,12 +351,31 @@ fn smoke_url_dry_run_does_not_curl() { ); assert_eq!(out.status.code(), Some(0), "stderr={}", stderr(&out)); let log = stdout(&out); + assert!(!log.contains("DRY-RUN would GET"), "{log}"); + assert!(!log.contains("http://127.0.0.1:9/"), "{log}"); assert!( - log.contains("DRY-RUN would GET http://127.0.0.1:9/"), + log.contains("DRY-RUN would write .deployed-tag=t1"), "{log}" ); + assert!(!shop.path().join("trap-invoked").is_file()); +} + +#[test] +fn deploy_health_url_dry_run_does_not_curl() { + let shop = TempShop::new("health-url"); + shop.write_min_shop(); + shop.write_previous("t1\n"); + let trap = shop.install_docker_trap(); + let out = rollback_with_path( + shop.path(), + &["--dry-run"], + &[("DEPLOY_HEALTH_URL", "http://127.0.0.1:9/")], + Some(&trap), + ); + assert_eq!(out.status.code(), Some(0), "stderr={}", stderr(&out)); + let log = stdout(&out); assert!( - log.contains("DRY-RUN would write .deployed-tag=t1"), + log.contains("DRY-RUN would GET http://127.0.0.1:9/"), "{log}" ); assert!(!shop.path().join("trap-invoked").is_file()); @@ -363,3 +394,42 @@ fn setup_in_profiles_refused() { assert_eq!(out.status.code(), Some(1), "stderr={}", stderr(&out)); assert!(stderr(&out).contains("setup"), "{}", stderr(&out)); } + +#[test] +fn live_refuses_without_probe_url() { + let shop = TempShop::new("live-no-probe"); + shop.write_min_shop(); + shop.write_previous("t1\n"); + let out = rollback( + shop.path(), + &["--dry-run"], + &[("SHOPWARE_DEPLOY_ENV", "live")], + ); + assert_eq!(out.status.code(), Some(1), "stderr={}", stderr(&out)); + let err = stderr(&out); + assert!(err.contains("DEPLOY_HEALTH_URL"), "{err}"); + assert!(err.contains("--allow-no-deploy-health"), "{err}"); + assert!(!shop.path().join(".deployed-tag").is_file()); +} + +#[test] +fn live_allow_no_deploy_health_flag_skips_probe() { + let shop = TempShop::new("live-hatch"); + shop.write_min_shop(); + shop.write_previous("t1\n"); + let out = rollback( + shop.path(), + &["--dry-run", "--allow-no-deploy-health"], + &[("SHOPWARE_DEPLOY_ENV", "live")], + ); + assert_eq!(out.status.code(), Some(0), "stderr={}", stderr(&out)); + assert!( + combined_rb(&out).contains("ops escape only"), + "{}", + combined_rb(&out) + ); +} + +fn combined_rb(out: &Output) -> String { + format!("{}{}", stdout(out), stderr(out)) +}