diff --git a/.gitea/workflows/continuous_integration.yaml b/.gitea/workflows/continuous_integration.yaml index f8a68cc..afe99a7 100644 --- a/.gitea/workflows/continuous_integration.yaml +++ b/.gitea/workflows/continuous_integration.yaml @@ -15,6 +15,14 @@ on: description: 'Deploy ASF test (Andrea Slop Fotografie)' type: boolean default: false + build_asf_production: + description: 'Build the ASF production artifact (win-x64, IIS); builds only, uploads nothing' + type: boolean + default: false + deploy_asf_production: + description: 'Deploy ASF production (FTPS to IIS): ONLY with Bryan''s explicit go, on a release/* branch, and when the variable ASF_PROD_DEPLOY_ENABLED is true' + type: boolean + default: false pull_request: types: [opened, synchronize, reopened] push: @@ -682,6 +690,127 @@ jobs: health_check_url: ${{ vars.ASF_TEST_HEALTH_URL }} run_db_backup: false + # ========================================================================================== + # ASF PRODUCTION on IIS (MijnHostingPartner). NOTHING here runs on a push or a pull request. + # + # publish-asf-production: builds the self-contained win-x64 host with the admin frontend and the web.config + # template (deploy/asf-iis/web.config), verifies the output (scripts/check-asf-publish.sh) and keeps it as a + # pipeline artifact for 30 days (that is also the "previous release" for a rollback, see + # docs/deploy-asf-productie.md). Only on a manual dispatch with build_asf_production or deploy_asf_production. + # deploy-asf-production: uploads that artifact over explicit FTPS. OFF by default and behind THREE locks: + # a manual dispatch with deploy_asf_production checked, a release/* branch, and the Gitea variable + # ASF_PROD_DEPLOY_ENABLED=true (set only on Bryan's explicit go). It never creates or replaces + # appsettings.Production.json, uploads/ or logs/, and never touches wwwroot/web (the website upload of the ASF repo). + # ========================================================================================== + publish-asf-production: + needs: [changes, backend-build, backend-test, vulnerability-scan, frontend-build, frontend-test, frontend-lint] + if: | + always() && + needs.changes.result == 'success' && + needs.backend-build.result != 'failure' && needs.backend-build.result != 'cancelled' && + needs.backend-test.result != 'failure' && needs.backend-test.result != 'cancelled' && + needs.vulnerability-scan.result != 'failure' && needs.vulnerability-scan.result != 'cancelled' && + needs.frontend-build.result != 'failure' && needs.frontend-build.result != 'cancelled' && + needs.frontend-test.result != 'failure' && needs.frontend-test.result != 'cancelled' && + needs.frontend-lint.result != 'failure' && needs.frontend-lint.result != 'cancelled' && + github.event_name == 'workflow_dispatch' && + (github.event.inputs.build_asf_production == 'true' || github.event.inputs.deploy_asf_production == 'true') + runs-on: linux + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-dotnet@v4 + with: + dotnet-version: ${{ env.DOTNET_VERSION }} + - uses: actions/setup-node@v3 + with: + node-version: ${{ env.NODE_VERSION }} + - uses: pnpm/action-setup@v4 + with: + version: ${{ env.PNPM_VERSION }} + + - name: Build admin frontend (ASF theme) + working-directory: frontend + env: + VITE_APP_ENV: production + VITE_APP_TITLE: Andrea Slop Fotografie + VITE_THEME: asf + VITE_DEFAULT_LANGUAGE: nl + VITE_SENTRY_DSN: ${{ vars.ASF_PROD_SENTRY_DSN }} + run: | + pnpm install --frozen-lockfile + pnpm build + mkdir -p ../src/SlpModularCms.Api.AndreaSlopFotografie/wwwroot/admin + cp -r dist/. ../src/SlpModularCms.Api.AndreaSlopFotografie/wwwroot/admin/ + + # Self-contained: the runtime ships with the app (the Hosting Bundle on the host is only the IIS module). + - name: Publish (ASF production, win-x64) + working-directory: src/SlpModularCms.Api.AndreaSlopFotografie + run: > + dotnet publish -c Release -r win-x64 --self-contained true + -o ${{ github.workspace }}/app-asf-production-win-x64 + + - name: Apply the IIS web.config template and drop development settings + run: | + out="${{ github.workspace }}/app-asf-production-win-x64" + cp deploy/asf-iis/web.config "$out/web.config" + rm -f "$out/appsettings.Development.json" + + - name: Check the artifact (no secrets or server-only files, required files present) + run: bash scripts/check-asf-publish.sh "${{ github.workspace }}/app-asf-production-win-x64" + + - name: Upload publish artifact + uses: actions/upload-artifact@v3 + with: + name: app-asf-production-win-x64 + path: app-asf-production-win-x64 + retention-days: 30 + + deploy-asf-production: + needs: [publish-asf-production] + if: | + github.event_name == 'workflow_dispatch' && + github.event.inputs.deploy_asf_production == 'true' && + startsWith(github.ref, 'refs/heads/release/') && + vars.ASF_PROD_DEPLOY_ENABLED == 'true' + runs-on: linux + steps: + - uses: actions/checkout@v4 + + - uses: actions/download-artifact@v3 + with: + name: app-asf-production-win-x64 + path: app-asf-production-win-x64 + + - name: Check settings + env: + ADDRESS: ${{ secrets.ASF_PROD_HOST_ADDRESS }} + USERNAME: ${{ secrets.ASF_PROD_HOST_USERNAME }} + PASSWORD: ${{ secrets.ASF_PROD_HOST_PASSWORD }} + DEPLOY_PATH: ${{ vars.ASF_PROD_DEPLOY_PATH }} + run: | + for v in ADDRESS USERNAME PASSWORD DEPLOY_PATH; do + if [ -z "${!v}" ]; then echo "ASF_PROD_* setting for $v is not set (secrets ASF_PROD_HOST_ADDRESS/USERNAME/PASSWORD, variable ASF_PROD_DEPLOY_PATH)."; exit 1; fi + done + + - name: Ensure lftp + run: | + if ! command -v lftp >/dev/null 2>&1; then + (sudo -n apt-get update -qq && sudo -n apt-get install -y -qq lftp) || apt-get install -y -qq lftp + fi + lftp --version | head -1 + + # Re-checks the artifact (it was built in another job), then uploads. The password only travels as + # the environment variable LFTP_PASSWORD, never on a command line. + - name: Upload over FTPS (app_offline.htm during the sync) + env: + FTP_HOST: ${{ secrets.ASF_PROD_HOST_ADDRESS }} + FTP_PORT: ${{ secrets.ASF_PROD_HOST_PORT }} + FTP_USER: ${{ secrets.ASF_PROD_HOST_USERNAME }} + LFTP_PASSWORD: ${{ secrets.ASF_PROD_HOST_PASSWORD }} + REMOTE_DIR: ${{ vars.ASF_PROD_DEPLOY_PATH }} + HEALTH_URL: ${{ vars.ASF_PROD_HEALTH_URL }} + run: bash scripts/deploy-asf-iis-ftps.sh app-asf-production-win-x64 + # --- Smoke tests of the deployed compositions (anonymous requests, see scripts/smoke-site.sh) --- # The CI tests the master CMS; this is the check that the composition of each site that was just deployed to test # is the intended one (modules, website mode, noindex, protected admin routes). A failing smoke test fails the run. diff --git a/deploy/asf-iis/app_offline.htm b/deploy/asf-iis/app_offline.htm new file mode 100644 index 0000000..657ac3e --- /dev/null +++ b/deploy/asf-iis/app_offline.htm @@ -0,0 +1,19 @@ + + + + + + + Even onderhoud + + +

Even onderhoud

+

De website wordt bijgewerkt en is over enkele minuten weer bereikbaar.

+ + + + + + + + diff --git a/deploy/asf-iis/web.config b/deploy/asf-iis/web.config new file mode 100644 index 0000000..76bc737 --- /dev/null +++ b/deploy/asf-iis/web.config @@ -0,0 +1,64 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/deploy-asf-productie.md b/docs/deploy-asf-productie.md new file mode 100644 index 0000000..fbda8b0 --- /dev/null +++ b/docs/deploy-asf-productie.md @@ -0,0 +1,92 @@ +# ASF productie op IIS: bouwen, uploaden, eerste uitrol, terugdraaien + +Status: de pijplijn is gebouwd maar staat **uit**. Er is niets naar productie gezet; dat gebeurt alleen met Bryans expliciete go. Voor wie: Bryan (eerste uitrol) en wie de pijplijn beheert. De configuratiesleutels, de `web.config` en de database staan in `aidlc-docs/features/andreaslop-site-hosting/asf-iis-production-config.md`; dit document gaat over de uitrol zelf. + +## 1. Wat er is + +| Onderdeel | Waar | Doet | +|---|---|---| +| Job `publish-asf-production` | `.gitea/workflows/continuous_integration.yaml` | Bouwt de ASF-host self-contained voor win-x64 (de runtime zit erbij; alleen de IIS-module uit de Hosting Bundle is op de server nodig), met de admin-frontend (ASF-thema, Nederlands) en het `web.config`-sjabloon. Bewaart het resultaat 30 dagen als pijplijnartefact `app-asf-production-win-x64`. | +| `deploy/asf-iis/web.config` | repo | Het sjabloon: handler inprocess, stdout-log aan voor de eerste start, uploadlimiet 30 MB, requestFiltering (configbestanden en programmabestanden nooit rechtstreeks downloadbaar), `ASPNETCORE_ENVIRONMENT=Production`, de hosterstandaard. Geen instellingen, geen geheimen. | +| `scripts/check-asf-publish.sh` | repo | Controle van het artefact; laat de job falen als er `appsettings.Production.json`, `uploads/`, `logs/`, `wwwroot/web`, andere `appsettings.*.json`, sleutel- of certificaatbestanden in zitten, of als het sjabloon-`web.config`, het win-x64-Magick-bestand of de admin ontbreekt, of als de `web.config` iets geheimachtigs bevat. | +| Job `deploy-asf-production` | idem | Uploadt het artefact via FTPS met `scripts/deploy-asf-iis-ftps.sh`. **Standaard uit** (zie 2). | +| `scripts/deploy-asf-iis-ftps.sh` | repo | De upload: expliciete FTPS (AUTH TLS), TLS afgedwongen voor besturing en data, certificaat gecontroleerd, passief, wachtwoord alleen via `LFTP_PASSWORD`; dezelfde stijl als de website-upload van het ASF-project. | + +Een push naar `master` of een pull request bouwt of uploadt hier **nooit** iets. + +## 2. Wanneer de upload draait (drie sloten) + +`deploy-asf-production` draait alleen als **alle drie** gelden: +1. een handmatige run (`workflow_dispatch`) met **`deploy_asf_production`** aangevinkt; +2. die run staat op een branch **`release/*`**; +3. de Gitea-variabele **`ASF_PROD_DEPLOY_ENABLED`** is `true` (standaard niet aanwezig; pas zetten op Bryans go en na afloop weer weghalen). + +Alleen bouwen (zonder upload): dispatch met **`build_asf_production`** aangevinkt, op elke branch. Zo is de zip te controleren zonder dat er iets naar een server gaat. + +Instellingen (de variabelen en secrets bestaan al in de CMS-repo; het hosterpad staat nu nog op een plaatshouder): + +| Naam | Soort | Betekenis | +|---|---|---| +| `ASF_PROD_HOST_ADDRESS`, `ASF_PROD_HOST_PORT` (optioneel), `ASF_PROD_HOST_USERNAME`, `ASF_PROD_HOST_PASSWORD` | secrets | FTPS-account van de hoster. | +| `ASF_PROD_DEPLOY_PATH` | variabele | De **hostmap** (de map met de `.exe`, de `web.config` en de `wwwroot`), relatief aan de FTP-thuismap. Alleen `A-Za-z0-9._/-`. **Niet** `wwwroot/web`. Nu `wwwroot` als plaatshouder: leg dit met Bryan vast zodra het echte hosterpad bekend is. Is de IIS-sitemap de `wwwroot` van het account, dan wordt de website-map `/wwwroot/wwwroot/web`; leg dat bij de eerste uitrol vast. | +| `ASF_PROD_HEALTH_URL` | variabele | `https://andreaslop.nl/health` (health-check na de upload). | +| `ASF_PROD_SENTRY_DSN` | variabele (optioneel) | DSN voor de admin-frontend. | +| `ASF_PROD_DEPLOY_ENABLED` | variabele | Het derde slot, zie boven. | + +## 3. Wat de upload doet en nooit doet + +Volgorde (`scripts/deploy-asf-iis-ftps.sh`): +1. Verbinding en doelmap controleren. +2. `app_offline.htm` uploaden: IIS zet de app stil en geeft de DLL's vrij; vijf seconden wachten. +3. Map voor map uploaden (één lftp-sessie per stap, stoppen bij een fout). **Alleen `wwwroot/admin` wordt opgeruimd** (verwijderen van bestanden die niet meer in de release zitten); nergens anders `--delete`. +4. De bestanden in de root, de `web.config` als laatste. +5. `app_offline.htm` verwijderen: de app start (op de eerste uitrol maakt de eerste start de database-tabellen, zie 5). +6. Health-check (tot ongeveer twee minuten, voor de eerste start). + +Nooit aangeraakt of geüpload: `appsettings.Production.json` (alleen op de server, door Bryan), `uploads/` (de mediamap), `logs/` (stdout-log) en `wwwroot/web` (de prerendered website; die uploadt het ASF-project zelf met `--delete` binnen die map). Het script weigert te lopen als de doelmap `wwwroot/web` is of als het artefact een van die dingen bevat. + +Faalt de upload halverwege, dan **blijft `app_offline.htm` staan**: bezoekers krijgen de onderhoudspagina in plaats van een half bijgewerkte app. Los het op en draai de upload opnieuw (hij is herhaalbaar), of draai terug (zie 6). + +Lokaal proberen zonder iets te wijzigen: `DRY_RUN=1 scripts/deploy-asf-iis-ftps.sh ` toont wat er zou worden geüpload (met dezelfde variabelen als in de job; `lftp` moet geïnstalleerd zijn). + +## 4. Samenwerking met de website-upload van het ASF-project + +Het ASF-project uploadt de prerendered website (inclusief `redirects.json`) met FTPS naar **`/wwwroot/web`**, in drie rondes met een eigen `--delete` alleen in die map. Afspraken: +- De host-upload sluit `wwwroot/web` uit; de website-upload raakt niets buiten `wwwroot/web` aan en zet er zelf geen `web.config` neer. +- **Nooit tegelijk** draaien: op dezelfde FTP-account en in dezelfde mapstructuur. Eerst de host, dan de website. +- Een host-release hoeft de website niet opnieuw te uploaden, en andersom ook niet. +- Beide gebruiken dezelfde FTPS-instellingen (zie het script); de host voegt `net:timeout 30` en `net:reconnect-interval-base 5` toe, de website-upload heeft die niet nodig. + +## 5. De eerste uitrol op een lege database + +Alleen na Bryans go. Vooraf, door Bryan: +1. **Back-up** van de WordPress-site en de database `andreaslop` (WordPress mag daarna weg; de database blijft ongemoeid). +2. De oude WordPress-bestanden uit de hostmap halen (`index.php`, `wp-*`, de oude `web.config` met rewrite-regels), **behalve** de nieuwe map `uploads`. Een oude WordPress-`web.config` zou botsen met de nieuwe. +3. In de hostmap een map `logs` maken (schrijfrechten voor de application pool-identiteit); schrijfrechten voor `uploads` zetten. +4. `appsettings.Production.json` met de hand naast de plek van de `.exe` zetten, volgens sectie 3 van de configuratiedoc (databasegegevens, JWT-sleutel, setup-token, mail, `Media:Directory`, Sentry). `Website:NoIndex` blijft `true`. +5. In Gitea de variabelen controleren (`ASF_PROD_DEPLOY_PATH` is nu het echte hosterpad; test eerst `DRY_RUN=1`), en `ASF_PROD_DEPLOY_ENABLED=true` zetten. + +De uitrol: +1. Release-branch `release/...` van `master`, dispatch met `deploy_asf_production` aangevinkt. +2. De pijplijn bouwt, controleert en uploadt (sectie 3). **De eerste start** maakt alle tabellen (migraties van Core, Identity, Availability, Offerings, Hero, Media, Contact) op de lege MariaDB. Controleer het stdout-log (`logs/stdout_*.log`) op fouten ("Fout bij het activeren van module"), op `Image library Magick.NET self-test passed` en op de Hero-melding; controleer daarna `/api/v1/System/capabilities`. +3. Open `https://andreaslop.nl/admin`: de setuppagina vraagt het **setup-token** en maakt de **eerste beheerder** aan. +4. Haal daarna `Identity:Setup:Token` uit `appsettings.Production.json` (herlaad of recycle de pool). +5. Test de mediamap: een foto uploaden, controleren dat er een bestand in `uploads` staat en dat de map niet via de browser bereikbaar is (zie 1e van de configuratiedoc). +6. De website-upload van het ASF-project naar `/wwwroot/web` laten draaien; controleer de homepage, `/sitemap.xml` en dat `/redirects.json` een 404 geeft. +7. Pas op het release-moment, na Bryans go: `Website:NoIndex` naar `false`, en pas dan de domeinwissel/DNS. Haal `ASF_PROD_DEPLOY_ENABLED` weer weg. + +**Het stdout-log uit:** na de eerste *succesvolle* start (stap 2 en 3 zijn gelukt) zet Bryan in de `web.config` op de server `stdoutLogEnabled="false"`. Anders groeien de bestanden in `logs/` onbeperkt. Let op: een volgende deploy zet de `web.config` uit het sjabloon terug en dus `stdoutLogEnabled` weer op `true`. Zet het sjabloon daarom na de eerste uitrol in een aparte, kleine PR op `false`; tot die PR er is, zet Bryan het na elke deploy handmatig uit. + +## 6. Terugdraaien + +- **De vorige release** is het artefact `app-asf-production-win-x64` van de vorige run (30 dagen bewaard in Gitea). Elke run bewaart zijn eigen artefact; noteer bij elke uitrol welke run live staat. +- **Terugzetten:** `app_offline.htm` uploaden (of de upload-job draaien met het oude artefact), de oude release uploaden met hetzelfde script (`scripts/deploy-asf-iis-ftps.sh ` met de FTPS-variabelen uit de job), daarna `app_offline.htm` verwijderen. Het script doet dit hele traject; je hoeft alleen het oude artefact te downloaden en uit te pakken. +- **Database:** de host draait migraties vooruit en niet terug. Voor een release met een migratie die iets verwijdert is vooraf een database-back-up (herstelpunt) verplicht; zie `production-rollout.md` van de mediabibliotheek. Een code-rollback van een release zonder migratie vraagt niets van de database. +- **Wat een rollback niet raakt:** `appsettings.Production.json`, `uploads/`, `logs/` en `wwwroot/web`. + +## 7. Open punten voor Bryan en SLS + +1. Het echte hosterpad voor `ASF_PROD_DEPLOY_PATH` (en daarmee of de website-map `wwwroot/wwwroot/web` wordt). +2. Heeft het FTP-account schrijfrechten in de hostmap voor `app_offline.htm`? Faalt de eerste proefupload hier, meld het. +3. Is `lftp` op de Gitea-runner aanwezig (of mag de job het installeren)? De job probeert `apt-get`; anders de runner-image aanpassen. +4. Wat doet de hoster al aan HTTP naar HTTPS en www naar apex (zie 2a van de configuratiedoc)? diff --git a/scripts/check-asf-publish.sh b/scripts/check-asf-publish.sh new file mode 100644 index 0000000..7607f51 --- /dev/null +++ b/scripts/check-asf-publish.sh @@ -0,0 +1,63 @@ +#!/usr/bin/env bash +# Guard for the ASF production publish output (win-x64, IIS). Fails (exit 1) when the directory +# contains something that must NEVER be in the artifact or misses something a working IIS host needs. +# +# Usage: scripts/check-asf-publish.sh +set -u + +dir="${1:-}" +[ -n "$dir" ] && [ -d "$dir" ] || { echo "Usage: $0 " >&2; exit 2; } + +fail=0 +bad() { echo "FAIL: $1" >&2; fail=1; } + +# --- Must NOT be present: server-only files, data folders, secrets --- +[ -e "$dir/appsettings.Production.json" ] && bad "appsettings.Production.json is in the artifact (server-only file)" +for d in uploads logs wwwroot/web; do + [ -e "$dir/$d" ] && bad "folder '$d' is in the artifact (owned by the server or by the website upload)" +done +# Any other environment file: only appsettings.json may ship. +while IFS= read -r f; do + bad "unexpected configuration file: ${f#"$dir"/}" +done < <(find "$dir" -type f \( -name 'appsettings.*.json' -o -name '.env' -o -name '.env.*' -o -name '*.pfx' -o -name '*.pem' -o -name '*.key' -o -name '*.p12' \) -print) +# An accidental redirect map would be overwritten by the website upload; it belongs to the website. +[ -e "$dir/wwwroot/redirects.json" ] && bad "wwwroot/redirects.json is in the artifact" + +# --- Must be present --- +[ -f "$dir/appsettings.json" ] || bad "appsettings.json is missing" +[ -f "$dir/SlpModularCms.Api.AndreaSlopFotografie.exe" ] || bad "SlpModularCms.Api.AndreaSlopFotografie.exe is missing (self-contained win-x64 publish expected)" +[ -f "$dir/SlpModularCms.Api.AndreaSlopFotografie.dll" ] || bad "SlpModularCms.Api.AndreaSlopFotografie.dll is missing" +[ -f "$dir/wwwroot/admin/index.html" ] || bad "wwwroot/admin/index.html is missing (admin frontend not built into the publish)" +[ -f "$dir/app_offline.htm" ] && bad "app_offline.htm is in the artifact (the deploy script manages it)" +# Self-contained: the runtime ships with the app; native image library for win-x64. +[ -f "$dir/coreclr.dll" ] || bad "coreclr.dll is missing: not a self-contained publish" +if ! find "$dir" -type f -iname 'Magick.Native-*x64*.dll' | grep -q .; then + bad "no win-x64 Magick.Native-*.dll found: uploads would stay switched off" +fi + +# --- web.config: the template, with its required fields --- +wc="$dir/web.config" +if [ ! -f "$wc" ]; then + bad "web.config is missing" +else + grep -q 'hostingModel="inprocess"' "$wc" || bad "web.config: hostingModel is not inprocess" + grep -q 'stdoutLogEnabled="true"' "$wc" || bad "web.config: stdoutLogEnabled must be \"true\" in the artifact (set to false by hand after the first start)" + grep -q 'stdoutLogFile="\.\\logs\\stdout"' "$wc" || bad "web.config: stdoutLogFile is not .\\logs\\stdout" + grep -q 'name="ASPNETCORE_ENVIRONMENT" value="Production"' "$wc" || bad "web.config: ASPNETCORE_ENVIRONMENT=Production is missing" + grep -q 'maxAllowedContentLength="31457280"' "$wc" || bad "web.config: upload limit is missing" + grep -q 'hiddenSegments' "$wc" || bad "web.config: requestFiltering hiddenSegments is missing" + grep -q 'SlpModularCms.Api.AndreaSlopFotografie.exe' "$wc" || bad "web.config: processPath does not point at the ASF host" + # Only ASPNETCORE_ENVIRONMENT may be set here; secrets and settings live in appsettings.Production.json. + n=$(grep -c '"); if(!i){l="";break} l=substr(l,i+3); c=0} else {i=index(l,""); if(j){l=pre substr(rest,j+3)} else {l=pre; c=1; break}} } print l}' "$wc" | grep -Eiq 'password|secret|token|dsn|connectionstring'; then + bad "web.config mentions a secret-like word (password/secret/token/dsn/connectionstring)" + fi +fi + +if [ "$fail" -ne 0 ]; then + echo "The ASF production artifact check FAILED." >&2 + exit 1 +fi +echo "ASF production artifact check passed ($(find "$dir" -type f | wc -l) files)." diff --git a/scripts/deploy-asf-iis-ftps.sh b/scripts/deploy-asf-iis-ftps.sh new file mode 100644 index 0000000..6596f6f --- /dev/null +++ b/scripts/deploy-asf-iis-ftps.sh @@ -0,0 +1,134 @@ +#!/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."