Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
e9905ca
Refactor code structure for improved readability and maintainability
rajashish147 Apr 3, 2026
d125282
feat(ci): add guard against forbidden docker exec curl usage and impr…
rajashish147 Apr 3, 2026
f264a82
fix(ci): enhance guard against forbidden docker exec curl usage in wo…
rajashish147 Apr 3, 2026
e409cac
fix(ci): refine guard against forbidden docker exec curl usage in wor…
rajashish147 Apr 3, 2026
f3200c9
feat: enhance CI/CD workflow with production simulation and infra con…
rajashish147 Apr 4, 2026
6f1110b
fix: enforce canonical redis URL in env.example + scope guard to prod…
rajashish147 Apr 4, 2026
9967c4c
fix(ci): docker-exec curl guard ignores path-prefixed lines and self-doc
rajashish147 Apr 4, 2026
46f2abd
fix(ci): remove api-ci-test container before docker rmi in bootstrap …
rajashish147 Apr 4, 2026
16d804e
fix(ci): infra leakage grep pipeline must not fail on zero matches (p…
rajashish147 Apr 4, 2026
b337e47
Merge branch 'master' into beta
rajashish147 Apr 4, 2026
13c6881
fix(ci): path-aware grep filters for -r output; harden .env API_BASE_…
rajashish147 Apr 4, 2026
7d76800
fix(vps): port 80/443 readiness use ss and allow docker-proxy (not ls…
rajashish147 Apr 4, 2026
5a225d1
fix(deploy): writable state dir for lock/slot; chown /var/lib/fieldtr…
rajashish147 Apr 4, 2026
36c592c
fix(deploy): sudo mkdir+chown for INFRA nginx live/backup; readiness …
rajashish147 Apr 4, 2026
8c39a1a
Merge branch 'master' into beta
rajashish147 Apr 4, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions scripts/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
# Deploy state (slot, lock, last-good):
# - FIELDTRACK_STATE_DIR or /var/lib/fieldtrack when writable (sudo chown if needed)
# - Otherwise $DEPLOY_ROOT/.fieldtrack; existing /var/lib/fieldtrack/* is migrated once
#
# Nginx config paths (INFRA_ROOT, default /opt/infra):
# - deploy user must write $INFRA_ROOT/nginx/live and nginx/backup (sudo mkdir+chown if needed)
# =============================================================================
set -euo pipefail
if [ "${DEBUG:-false}" = "true" ]; then set -x; fi
Expand Down Expand Up @@ -456,14 +459,40 @@ preflight() {
_ft_log "msg='port-leak guard passed'"
}

# ---------------------------------------------------------------------------
# Infra nginx directories β€” deploy must write live + backup (not root-only /opt)
# ---------------------------------------------------------------------------
_ft_ensure_infra_nginx_dirs() {
local d
for d in "$NGINX_LIVE_DIR" "$NGINX_BACKUP_DIR"; do
if [ ! -d "$d" ]; then
_ft_log "msg='nginx dir missing, creating' path=$d"
if ! sudo mkdir -p "$d" 2>/dev/null; then
_ft_log "level=ERROR msg='cannot create nginx directory' path=$d"
return 1
fi
fi
if [ ! -w "$d" ]; then
_ft_log "msg='nginx dir not writable; fixing ownership' path=$d user=$(id -un)"
sudo chown "$(id -un):$(id -gn)" "$d" 2>/dev/null || true
sudo chmod u+rwx "$d" 2>/dev/null || true
fi
if [ ! -w "$d" ]; then
_ft_log "level=ERROR msg='nginx directory not writable after chown' path=$d user=$(id -un)"
return 1
fi
done
return 0
}

# ---------------------------------------------------------------------------
# ensure_network β€” create api_network if absent (idempotent)
# ---------------------------------------------------------------------------
ensure_network() {
docker network create --driver bridge "$NETWORK" 2>/dev/null \
&& _ft_log "msg='api_network created'" \
|| _ft_log "msg='api_network already exists'"
mkdir -p "$NGINX_LIVE_DIR" "$NGINX_BACKUP_DIR"
_ft_ensure_infra_nginx_dirs || _ft_exit 1 "DEPLOY_FAILED_SAFE" "reason=infra_nginx_dirs_not_writable"
}

# ---------------------------------------------------------------------------
Expand Down Expand Up @@ -780,7 +809,7 @@ switch_nginx() {
_ft_state "SWITCH_NGINX" "msg='switching nginx upstream' container=$INACTIVE_NAME"
sleep 2 # brief stabilization window before touching nginx

mkdir -p "$NGINX_BACKUP_DIR"
_ft_ensure_infra_nginx_dirs || _ft_exit 1 "DEPLOY_FAILED_SAFE" "reason=infra_nginx_dirs_not_writable"
local backup tmp
backup="$NGINX_BACKUP_DIR/api.conf.bak.$(date +%s)"
tmp="$(mktemp /tmp/api-nginx.XXXXXX.conf)"
Expand Down Expand Up @@ -1157,7 +1186,7 @@ main() {
health_check_internal
# Write nginx config directly for first deploy, but keep the current
# maintenance config as a rollback target for the routed verification.
mkdir -p "$NGINX_LIVE_DIR" "$NGINX_BACKUP_DIR"
_ft_ensure_infra_nginx_dirs || _ft_exit 1 "DEPLOY_FAILED_SAFE" "reason=infra_nginx_dirs_not_writable"
if [ -f "$NGINX_CONF" ]; then
cp "$NGINX_CONF" "$NGINX_BACKUP"
fi
Expand Down
26 changes: 26 additions & 0 deletions scripts/vps-readiness-check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ DEPLOY_ROOT="${DEPLOY_ROOT:-$HOME/api}"
NETWORK="api_network"
RUNTIME_DIR="/var/lib/fieldtrack"
LOG_DIR="/var/log/api"
INFRA_ROOT="${INFRA_ROOT:-/opt/infra}"

# ── Colour helpers ─────────────────────────────────────────────────────────────
GREEN='\033[0;32m'
Expand Down Expand Up @@ -205,6 +206,31 @@ for dir in "$RUNTIME_DIR" "$LOG_DIR"; do
fi
done

# ── CHECK 7b: Infra nginx paths (deploy.sh writes live config + backups) ─────
echo ""
echo "--- CHECK 7b: Infra nginx directories (\$INFRA_ROOT=$INFRA_ROOT) ---"
for d in "$INFRA_ROOT/nginx/live" "$INFRA_ROOT/nginx/backup"; do
if [ ! -d "$d" ]; then
warn "Missing $d β€” creating with sudo."
sudo install -d -m 755 "$d" 2>/dev/null || { record_failure "Cannot create directory: $d"; continue; }
fi
if [ ! -w "$d" ]; then
warn "Not writable by deploy user: $d β€” fixing ownership."
sudo chown "$(id -un):$(id -gn)" "$d" 2>/dev/null || true
sudo chmod u+rwx "$d" 2>/dev/null || true
fi
if [ ! -w "$d" ]; then
record_failure "Cannot write to $d β€” deploy will fail when updating nginx. Run: sudo chown -R $(id -un):$(id -gn) $d"
else
ok "Infra path ready (writable): $d"
fi
done
if [ ! -f "$INFRA_ROOT/nginx/api.conf" ]; then
record_failure "Missing nginx template: $INFRA_ROOT/nginx/api.conf (infra bootstrap required)"
else
ok "Nginx template present: $INFRA_ROOT/nginx/api.conf"
fi

# ── CHECK 8: Network attachment enforcement ───────────────────────────────────
#
# If nginx is running, it MUST be
Expand Down
Loading