80 lines
2.2 KiB
Python
80 lines
2.2 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
IWZ Ferienwohnungen - Deploy Script
|
|
Baut die Hugo-Seite und deployt sie per FTP zur Bunny.net Storage Zone.
|
|
"""
|
|
|
|
import ftplib
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
PROJECT_ROOT = Path(__file__).resolve().parent
|
|
PUBLIC_DIR = PROJECT_ROOT / "public"
|
|
FTP_PORT = 21
|
|
|
|
|
|
def load_env(path):
|
|
env = {}
|
|
if not path.exists():
|
|
return env
|
|
for line in path.read_text(encoding="utf-8").splitlines():
|
|
line = line.strip()
|
|
if not line or line.startswith("#") or "=" not in line:
|
|
continue
|
|
key, value = line.split("=", 1)
|
|
env[key.strip()] = value.strip()
|
|
return env
|
|
|
|
|
|
def hugo_build():
|
|
print("\n-> Hugo Build...")
|
|
result = subprocess.run("hugo --gc --minify", shell=True)
|
|
if result.returncode != 0:
|
|
print("\nFEHLER: Hugo Build fehlgeschlagen!")
|
|
sys.exit(1)
|
|
|
|
|
|
def ftp_upload_dir(ftp, local_dir, remote_dir=""):
|
|
for entry in sorted(local_dir.iterdir()):
|
|
remote_path = f"{remote_dir}/{entry.name}" if remote_dir else entry.name
|
|
if entry.is_dir():
|
|
try:
|
|
ftp.mkd(remote_path)
|
|
except ftplib.error_perm:
|
|
pass # existiert bereits
|
|
ftp_upload_dir(ftp, entry, remote_path)
|
|
else:
|
|
with open(entry, "rb") as f:
|
|
ftp.storbinary(f"STOR {remote_path}", f)
|
|
print(f" {remote_path}")
|
|
|
|
|
|
def deploy_to_bunny(host, zone, password):
|
|
print(f"\n-> Deploy zu Bunny.net Storage Zone '{zone}'...")
|
|
with ftplib.FTP() as ftp:
|
|
ftp.connect(host, FTP_PORT)
|
|
ftp.login(zone, password)
|
|
ftp.set_pasv(True)
|
|
ftp_upload_dir(ftp, PUBLIC_DIR)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print("=== IWZ Deploy ===")
|
|
|
|
env = {**load_env(PROJECT_ROOT / ".env"), **os.environ}
|
|
zone = env.get("BUNNY_STORAGE_ZONE")
|
|
host = env.get("BUNNY_STORAGE_HOST", "storage.bunnycdn.com")
|
|
password = env.get("BUNNY_STORAGE_PASSWORD")
|
|
|
|
if not zone or not password:
|
|
print("\nFEHLER: BUNNY_STORAGE_ZONE / BUNNY_STORAGE_PASSWORD fehlen.")
|
|
print("Siehe .env.example -> .env kopieren und befuellen.")
|
|
sys.exit(1)
|
|
|
|
hugo_build()
|
|
deploy_to_bunny(host, zone, password)
|
|
|
|
print("\nFertig! Seite ist live.")
|