Deploy laedt atomar hoch: erst ins Staging ausserhalb des Web-Roots, dann umbenennen

This commit is contained in:
torsten 2026-10-31 11:53:21 +01:00
parent 3d72278e10
commit 9bc5e5b905
2 changed files with 65 additions and 14 deletions

View file

@ -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: