|
459 | 459 |
|
460 | 460 | _ft_log "msg='slot resolved' active=$ACTIVE active_port=$ACTIVE_PORT inactive=$INACTIVE inactive_port=$INACTIVE_PORT" |
461 | 461 |
|
| 462 | +# --------------------------------------------------------------------------- |
| 463 | +# INITIAL DEPLOYMENT DETECTION -- no containers exist yet |
| 464 | +# --------------------------------------------------------------------------- |
| 465 | +if ! docker ps -a --format '{{.Names}}' | grep -Eq '^api-(blue|green)$'; then |
| 466 | + _ft_log "msg='initial deployment detected — no existing containers'" |
| 467 | + INITIAL_DEPLOY=true |
| 468 | +fi |
| 469 | + |
462 | 470 | # --------------------------------------------------------------------------- |
463 | 471 | # IDEMPOTENCY GUARD -- skip deploy if this exact SHA is already the active container |
464 | 472 | # --------------------------------------------------------------------------- |
@@ -790,18 +798,22 @@ unset _STABLE |
790 | 798 | # --------------------------------------------------------------------------- |
791 | 799 | _ft_state "CLEANUP" "msg='validating active container exists before cleanup' name=$ACTIVE_NAME" |
792 | 800 |
|
793 | | -# ACTIVE CONTAINER GUARD -- prevent edge-case race corruption |
| 801 | +# ACTIVE CONTAINER GUARD -- handle missing container gracefully (e.g., first deploy or crash) |
794 | 802 | if ! docker ps --format '{{.Names}}' | grep -q "^$ACTIVE_NAME$"; then |
795 | | - _ft_log "level=ERROR msg='active container missing before cleanup -- cannot safely proceed (possible race condition or crash)' name=$ACTIVE_NAME" |
796 | | - _ft_snapshot |
797 | | - _ft_exit 3 "DEPLOY_FAILED_FATAL" "reason=active_container_missing_before_cleanup" |
| 803 | + _ft_log "msg='active container missing — treating as first deploy, skipping cleanup' name=$ACTIVE_NAME" |
| 804 | + SKIP_CLEANUP=true |
| 805 | +else |
| 806 | + _ft_log "msg='active container guard passed' name=$ACTIVE_NAME" |
798 | 807 | fi |
799 | | -_ft_log "msg='active container guard passed' name=$ACTIVE_NAME" |
800 | 808 |
|
801 | 809 | # Graceful shutdown: allow in-flight requests to drain before forcing removal. |
802 | | -docker stop --time 10 "$ACTIVE_NAME" 2>/dev/null || true |
803 | | -docker rm "$ACTIVE_NAME" || true |
804 | | -_ft_log "msg='previous container removed (graceful)' name=$ACTIVE_NAME" |
| 810 | +if [ "${SKIP_CLEANUP:-false}" != "true" ]; then |
| 811 | + docker stop --time 10 "$ACTIVE_NAME" 2>/dev/null || true |
| 812 | + docker rm "$ACTIVE_NAME" || true |
| 813 | + _ft_log "msg='previous container removed (graceful)' name=$ACTIVE_NAME" |
| 814 | +else |
| 815 | + _ft_log "msg='cleanup skipped (first deploy scenario or container already removed)'" |
| 816 | +fi |
805 | 817 |
|
806 | 818 | _ft_state "SUCCESS" "msg='deployment complete' container=$INACTIVE_NAME sha=$IMAGE_SHA slot=$INACTIVE port=$INACTIVE_PORT" |
807 | 819 |
|
|
0 commit comments