#!/usr/bin/env bash # Uploads the ASF production publish output (win-x64, IIS) over explicit FTPS, directory by directory, # with app_offline.htm in place while the files change (IIS then releases the locked DLLs). # # Same FTPS style as the website upload of the ASF repo (upload-production.sh): explicit FTPS (AUTH TLS), # TLS forced for control and data, certificate checked, passive mode, password only via LFTP_PASSWORD. # # What this script NEVER touches on the server: appsettings.Production.json, uploads/, logs/, wwwroot/web/ # (the website upload owns that one). The only place where files are deleted is wwwroot/admin (the admin # frontend is entirely owned by this artifact and its hashed files would otherwise pile up). There is no # mirror --delete anywhere else, in particular never over the root or over wwwroot. # # Usage: scripts/deploy-asf-iis-ftps.sh # Env: FTP_HOST (required), FTP_USER (required), LFTP_PASSWORD (required), FTP_PORT (optional, numeric), # REMOTE_DIR (required: the host folder, relative to the FTP home, characters A-Za-z0-9._/- only), # HEALTH_URL (optional: checked after the upload), DRY_RUN=1 (list what would be uploaded, change nothing). # # Failure: when the upload fails, app_offline.htm is LEFT in place (visitors get the maintenance page rather than # a half-updated app). See docs/deploy-asf-productie.md for recovery. set -euo pipefail src="${1:-}" [ -n "$src" ] && [ -d "$src" ] || { echo "Usage: $0 " >&2; exit 2; } here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" : "${FTP_HOST:?FTP_HOST is not set}" : "${FTP_USER:?FTP_USER is not set}" : "${LFTP_PASSWORD:?LFTP_PASSWORD is not set}" : "${REMOTE_DIR:?REMOTE_DIR is not set}" export LFTP_PASSWORD port_args=() if [ -n "${FTP_PORT:-}" ]; then [[ "$FTP_PORT" =~ ^[0-9]{1,5}$ ]] || { echo "FTP_PORT must be numeric." >&2; exit 2; } port_args=(-p "$FTP_PORT") fi [[ "$FTP_HOST" =~ ^[A-Za-z0-9.:-]+$ ]] || { echo "FTP_HOST contains unexpected characters." >&2; exit 2; } # Path validation: only [A-Za-z0-9._/-], no '.', '..' or empty segments. validate_path() { local p="$1" seg [[ "$p" =~ ^[A-Za-z0-9._/-]+$ ]] || { echo "Unsafe path: '$p'" >&2; exit 2; } IFS='/' read -ra segs <<< "$p" for seg in "${segs[@]}"; do [ -n "$seg" ] && [ "$seg" != "." ] && [ "$seg" != ".." ] || { echo "Unsafe path segment in '$p'" >&2; exit 2; } done } validate_path "$REMOTE_DIR" remote="${REMOTE_DIR%/}" case "$remote" in *wwwroot/web|*wwwroot/web/*) echo "REMOTE_DIR must be the host folder, not the website folder (wwwroot/web)." >&2; exit 2;; esac # Never upload what must not be there (same rules as the artifact check; this also covers a local run). bash "$here/check-asf-publish.sh" "$src" settings='set ftp:ssl-allow yes; set ftp:ssl-auth TLS; set ftp:ssl-force yes; set ftp:ssl-protect-data yes; set ftp:ssl-protect-list yes; set ssl:verify-certificate yes; set ftp:passive-mode on; set net:max-retries 3; set net:timeout 30; set net:reconnect-interval-base 5; set xfer:make-backup no;' dry="${DRY_RUN:-0}" # One lftp session per step (a failing step stops the run). lftp_run() { lftp --env-password -u "$FTP_USER" ${port_args[@]+"${port_args[@]}"} "ftp://$FTP_HOST" -e "$settings $1 quit" } excl="--exclude-glob appsettings.Production.json --exclude-glob uploads --exclude-glob logs" mirror_opts="--reverse --verbose=1 --no-perms --parallel=2 $excl" [ "$dry" = "1" ] && mirror_opts="$mirror_opts --dry-run" echo "== Target: ftp://$FTP_HOST/$remote (dry run: $dry)" echo "== 1/6 Check connection and remote folder" lftp_run "cd '$remote'; cls -1" if [ "$dry" != "1" ]; then echo "== 2/6 app_offline.htm (IIS stops the app and releases the DLLs)" lftp_run "cd '$remote'; put -O . '$here/../deploy/asf-iis/app_offline.htm' -o app_offline.htm" sleep 5 else echo "== 2/6 (dry run) skipping app_offline.htm" fi echo "== 3/6 Directories (no deletes except wwwroot/admin)" while IFS= read -r d; do name="$(basename "$d")" validate_path "$name" case "$name" in uploads|logs) echo "refusing to upload '$name'" >&2; exit 1;; wwwroot) # wwwroot: only the admin is owned by this artifact. Everything else in it is mirrored without deleting; # wwwroot/web belongs to the website upload and is never touched. while IFS= read -r sd; do sname="$(basename "$sd")" validate_path "$sname" [ "$sname" = "web" ] && { echo "refusing to upload wwwroot/web" >&2; exit 1; } if [ "$sname" = "admin" ]; then lftp_run "mirror $mirror_opts --delete '$sd' '$remote/wwwroot/admin'" else lftp_run "mirror $mirror_opts '$sd' '$remote/wwwroot/$sname'" fi done < <(find "$d" -mindepth 1 -maxdepth 1 -type d | sort) lftp_run "mirror $mirror_opts --no-recursion '$d' '$remote/wwwroot'" ;; *) lftp_run "mirror $mirror_opts '$d' '$remote/$name'" ;; esac done < <(find "$src" -mindepth 1 -maxdepth 1 -type d | sort) echo "== 4/6 Files in the root (program files; web.config last)" lftp_run "mirror $mirror_opts --no-recursion --exclude-glob web.config '$src' '$remote'" if [ "$dry" != "1" ]; then lftp_run "cd '$remote'; put -O . '$src/web.config' -o web.config" fi if [ "$dry" = "1" ]; then echo "== Dry run finished; nothing was changed." exit 0 fi echo "== 5/6 Remove app_offline.htm (the app starts: migrations run on the first start)" lftp_run "cd '$remote'; rm app_offline.htm" echo "== 6/6 Health check" if [ -n "${HEALTH_URL:-}" ]; then # The first start can take a while (migrations, image library self-test). if ! curl -fsS --max-time 30 --retry 12 --retry-delay 10 --retry-all-errors "$HEALTH_URL" >/dev/null; then echo "Health check FAILED for $HEALTH_URL. app_offline.htm has been removed; see the stdout log (logs/stdout_*.log) on the host and docs/deploy-asf-productie.md (recovery)." >&2 exit 1 fi echo "Health check passed: $HEALTH_URL" else echo "No HEALTH_URL set; skipped." fi echo "Done."