From 9bc5e5b9056b99fc0c35fd2085a306bc4d555ece Mon Sep 17 00:00:00 2001 From: torsten Date: Sat, 31 Oct 2026 11:53:21 +0100 Subject: [PATCH] Deploy laedt atomar hoch: erst ins Staging ausserhalb des Web-Roots, dann umbenennen --- README.md | 6 ++++ tools/deploy_live.py | 73 +++++++++++++++++++++++++++++++++++--------- 2 files changed, 65 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index d379782..4d74a04 100644 --- a/README.md +++ b/README.md @@ -339,6 +339,12 @@ mysql -h -u -p < tools/dumps/tippspiel_live_<...>.sql und live gepflegt sind. Hat Live mehr, warnt das Script explizit vor Verlust. - Die `.htaccess` wird mit korrekt gequoteten `SetEnv`-Werten erzeugt (wichtig bei Leerzeichen, z. B. `MAIL_FROM_NAME`). +- **Uploads sind atomar:** Jede Datei wird zuerst nach `/tippspiel/.staging/` + (außerhalb des Web-Roots) geladen und dann per `RNFR`/`RNTO` an ihren Platz + umbenannt. Ein Besucher sieht dadurch nie eine halb geschriebene Datei – + ohne diesen Zwischenschritt würde `STOR` die Zieldatei erst leeren, und ein + Request in genau diesem Moment bekäme bei `index.php` einen Parse-Fehler (500). + Das Staging-Verzeichnis wird nach dem Deploy entfernt, sofern es leer ist. - Beim DB-Sync werden nach dem Import die Zeilenzahlen aller Tabellen geprüft. ## Nützliche Befehle diff --git a/tools/deploy_live.py b/tools/deploy_live.py index 820d693..82efa94 100755 --- a/tools/deploy_live.py +++ b/tools/deploy_live.py @@ -39,6 +39,9 @@ CRED_FILE = os.path.join(REPO_ROOT, "docs", "live_credentials.txt") DEFAULT_BACKUP_DIR = os.path.expanduser("~/backups/tippspiel-live") REMOTE_ROOT = "tippspiel" +# Zwischenablage für atomare Uploads: liegt neben public/ und damit ausserhalb +# des Web-Roots - Dateien dort sind während des Uploads nicht abrufbar. +STAGING_DIR = "/%s/.staging" % REMOTE_ROOT FTP_PORT = 21 DEFAULT_APP_URL = "https://hoschis-tippspiel.de" @@ -393,6 +396,40 @@ def mkd_ignore_exists(ftp, name): raise +def staging_path(remote_path): + """Zwischenablage ausserhalb des Web-Roots (nicht per Browser erreichbar).""" + name = remote_path.strip("/").replace("/", "__") + return "%s/%s.tmp" % (STAGING_DIR, name) + + +def store_atomic(ftp, remote_path, source): + """Lädt eine Datei hoch und benennt sie erst dann an ihren Platz um. + + Ein direktes STOR in die Zieldatei trunkiert sie zuerst; ein Request in + genau diesem Moment könnte eine halb geschriebene Datei lesen (bei + index.php: Parse-Fehler, also 500 für den Besucher). rename() ist auf + demselben Dateisystem atomar - die Datei ist entweder vollständig alt + oder vollständig neu. + + `source` ist ein lokaler Pfad oder ein bereits geöffnetes Dateiobjekt. + """ + tmp_path = staging_path(remote_path) + if hasattr(source, "read"): + ftp.storbinary("STOR " + tmp_path, source) + else: + with open(source, "rb") as handle: + ftp.storbinary("STOR " + tmp_path, handle) + + try: + ftp.rename(tmp_path, remote_path) + except Exception: + try: + ftp.delete(tmp_path) + except error_perm: + pass + raise + + def upload_dir(ftp, local_dir, remote_dir, skip=None): mkd_ignore_exists(ftp, remote_dir) skip = skip or set() @@ -401,15 +438,12 @@ def upload_dir(ftp, local_dir, remote_dir, skip=None): if name in skip or name in FTP_SKIP: continue local_path = os.path.join(local_dir, name) + remote_path = remote_dir.rstrip("/") + "/" + name if os.path.isfile(local_path): - with open(local_path, "rb") as handle: - ftp.storbinary("STOR " + name, handle) + store_atomic(ftp, remote_path, local_path) count += 1 elif os.path.isdir(local_path): - mkd_ignore_exists(ftp, name) - ftp.cwd(name) - count += upload_dir(ftp, local_path, remote_dir + "/" + name) - ftp.cwd("..") + count += upload_dir(ftp, local_path, remote_path, skip) return count @@ -579,25 +613,36 @@ def main(): try: ftp.login(creds["ftp_user"], creds["ftp_pw"]) mkd_ignore_exists(ftp, REMOTE_ROOT) - ftp.cwd(REMOTE_ROOT) + mkd_ignore_exists(ftp, STAGING_DIR) for sub in ("public", "src"): local_sub = os.path.join(APP_DIR, sub) if not os.path.isdir(local_sub): raise DeployError("Lokales Verzeichnis fehlt: %s" % local_sub) log("") log("== Lade %s/ hoch ==" % sub) - mkd_ignore_exists(ftp, sub) - ftp.cwd(sub) skip = {"img"} if (sub == "public" and args.skip_img_upload) else None - uploaded = upload_dir(ftp, local_sub, "%s/%s" % (REMOTE_ROOT, sub), skip=skip) + uploaded = upload_dir( + ftp, + local_sub, + "/%s/%s" % (REMOTE_ROOT, sub), + skip=skip, + ) log(" %d Dateien hochgeladen." % uploaded) - ftp.cwd("..") log("") log("== Schreibe public/.htaccess ==") - ftp.cwd("public") - ftp.storbinary("STOR .htaccess", io.BytesIO(htaccess.encode("utf-8"))) - ftp.cwd("..") + store_atomic( + ftp, + "/%s/public/.htaccess" % REMOTE_ROOT, + io.BytesIO(htaccess.encode("utf-8")), + ) + + # Zwischenablage nur entfernen, wenn sie leer ist (rmd schlaegt + # sonst fehl - Reste eines abgebrochenen Uploads bleiben liegen). + try: + ftp.rmd(STAGING_DIR) + except error_perm: + pass ftp.quit() except Exception: try: