fix: add debugging & change s3 mount
This commit is contained in:
parent
ac0ee58889
commit
a3ca94549f
@ -15,7 +15,7 @@ MINIO_HOST = os.getenv("MINIO_HOST", "minio.minio.svc.cluster.local:9000")
|
|||||||
MINIO_REGION = os.getenv("MINIO_REGION", "us-east-1")
|
MINIO_REGION = os.getenv("MINIO_REGION", "us-east-1")
|
||||||
MINIO_MOUNT_PATH = "/mnt"
|
MINIO_MOUNT_PATH = "/mnt"
|
||||||
N8N_WEBHOOK_URL = os.getenv("N8N_WEBHOOK_URL", "https://n8n.n8n.svc.kube.ia86.cc/webhook/7950310f-e526-475a-82d1-63818da79339")
|
N8N_WEBHOOK_URL = os.getenv("N8N_WEBHOOK_URL", "https://n8n.n8n.svc.kube.ia86.cc/webhook/7950310f-e526-475a-82d1-63818da79339")
|
||||||
DEBUG = bool(os.getenv("DEBUG", False))
|
DEBUG = bool(os.getenv("DEBUG", True))
|
||||||
|
|
||||||
S3_ACCESS_KEY = os.getenv("S3_ACCESS_KEY")
|
S3_ACCESS_KEY = os.getenv("S3_ACCESS_KEY")
|
||||||
S3_SECRET_KEY = os.getenv("S3_SECRET_KEY")
|
S3_SECRET_KEY = os.getenv("S3_SECRET_KEY")
|
||||||
@ -35,10 +35,13 @@ def log_debug(msg):
|
|||||||
print(f"🔍 DEBUG: {msg}")
|
print(f"🔍 DEBUG: {msg}")
|
||||||
|
|
||||||
def mount_s3():
|
def mount_s3():
|
||||||
|
log_debug("🔧 Tentative de montage S3...")
|
||||||
with local_lock:
|
with local_lock:
|
||||||
if not os.path.ismount(MINIO_MOUNT_PATH):
|
if not os.path.ismount(MINIO_MOUNT_PATH):
|
||||||
|
log_debug("📂 Chemin non monté, création répertoire si besoin...")
|
||||||
os.makedirs(MINIO_MOUNT_PATH, exist_ok=True)
|
os.makedirs(MINIO_MOUNT_PATH, exist_ok=True)
|
||||||
credentials_file = "/etc/passwd-s3fs"
|
credentials_file = "/etc/passwd-s3fs"
|
||||||
|
log_debug(f"🔑 Création credentials S3 dans {credentials_file}")
|
||||||
with open(credentials_file, "w") as f:
|
with open(credentials_file, "w") as f:
|
||||||
f.write(f"{S3_ACCESS_KEY}:{S3_SECRET_KEY}\n")
|
f.write(f"{S3_ACCESS_KEY}:{S3_SECRET_KEY}\n")
|
||||||
os.chmod(credentials_file, 0o600)
|
os.chmod(credentials_file, 0o600)
|
||||||
@ -51,16 +54,17 @@ def mount_s3():
|
|||||||
"-o allow_other"
|
"-o allow_other"
|
||||||
)
|
)
|
||||||
|
|
||||||
log_debug(f"🚀 Montage S3: {cmd}")
|
log_debug(f"🚀 Commande montage S3: {cmd}")
|
||||||
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
|
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
|
||||||
if result.returncode != 0:
|
if result.returncode != 0:
|
||||||
log_debug(f"❌ Erreur montage: {result.stderr}")
|
log_debug(f"❌ Erreur montage S3: {result.stderr}")
|
||||||
raise RuntimeError(f"Erreur montage: {result.stderr}")
|
raise RuntimeError(f"Erreur montage S3: {result.stderr}")
|
||||||
log_debug("✅ Montage réussi.")
|
log_debug("✅ Montage S3 réussi.")
|
||||||
else:
|
else:
|
||||||
log_debug("✅ Montage S3 déjà actif.")
|
log_debug("✅ Montage S3 déjà actif, rien à faire.")
|
||||||
|
|
||||||
def unmount_s3():
|
def unmount_s3():
|
||||||
|
log_debug("🔧 Tentative de démontage S3...")
|
||||||
with local_lock:
|
with local_lock:
|
||||||
if os.path.ismount(MINIO_MOUNT_PATH):
|
if os.path.ismount(MINIO_MOUNT_PATH):
|
||||||
cmd = f"fusermount -u {MINIO_MOUNT_PATH}"
|
cmd = f"fusermount -u {MINIO_MOUNT_PATH}"
|
||||||
@ -69,64 +73,78 @@ def unmount_s3():
|
|||||||
log_debug("✅ Démontage S3 réussi.")
|
log_debug("✅ Démontage S3 réussi.")
|
||||||
else:
|
else:
|
||||||
log_debug(f"⚠️ Échec démontage S3: {result.stderr}")
|
log_debug(f"⚠️ Échec démontage S3: {result.stderr}")
|
||||||
|
else:
|
||||||
|
log_debug("✅ Aucun montage actif à démonter.")
|
||||||
|
|
||||||
def unmount_checker():
|
def unmount_checker():
|
||||||
while True:
|
while True:
|
||||||
|
log_debug("⏰ Vérification périodique pour démontage S3 en cours...")
|
||||||
time.sleep(S3_UNMOUNT_CHECK_INTERVAL)
|
time.sleep(S3_UNMOUNT_CHECK_INTERVAL)
|
||||||
log_debug("⏰ Vérification périodique pour démontage S3...")
|
|
||||||
active_locks = redis_client.keys(f"{lock_prefix}*")
|
active_locks = redis_client.keys(f"{lock_prefix}*")
|
||||||
if not active_locks:
|
if not active_locks:
|
||||||
log_debug("🟢 Aucun build actif, démontage S3...")
|
log_debug("🟢 Aucun build actif détecté, démontage S3...")
|
||||||
unmount_s3()
|
unmount_s3()
|
||||||
else:
|
else:
|
||||||
log_debug("🔴 Builds en cours détectés, S3 reste monté.")
|
log_debug("🔴 Des builds actifs existent encore, S3 reste monté.")
|
||||||
|
|
||||||
@app.route("/build", methods=["POST"])
|
@app.route("/build", methods=["POST"])
|
||||||
def build_mkdocs():
|
def build_mkdocs():
|
||||||
|
log_debug("📥 Nouvelle requête POST reçue sur /build")
|
||||||
data = request.json
|
data = request.json
|
||||||
website = data.get("WEBSITE")
|
website = data.get("WEBSITE")
|
||||||
error_callback = data.get("ERROR_CALLBACK", N8N_WEBHOOK_URL)
|
error_callback = data.get("ERROR_CALLBACK", N8N_WEBHOOK_URL)
|
||||||
|
|
||||||
if not website:
|
if not website:
|
||||||
|
log_debug("❌ Erreur : WEBSITE manquant dans la requête.")
|
||||||
return jsonify({"error": "WEBSITE manquant"}), 400
|
return jsonify({"error": "WEBSITE manquant"}), 400
|
||||||
|
|
||||||
lock_key = f"{lock_prefix}{website}"
|
lock_key = f"{lock_prefix}{website}"
|
||||||
|
log_debug(f"🔒 Tentative d'acquisition du verrou Redis pour: {lock_key}")
|
||||||
lock_acquired = redis_client.set(lock_key, "locked", nx=True, ex=60)
|
lock_acquired = redis_client.set(lock_key, "locked", nx=True, ex=60)
|
||||||
|
|
||||||
if not lock_acquired:
|
if not lock_acquired:
|
||||||
log_debug(f"⚠️ Build déjà actif: {website}")
|
log_debug(f"⚠️ Build déjà actif pour: {website}")
|
||||||
return jsonify({"status": "busy", "message": f"Build déjà actif: {website}"}), 429
|
return jsonify({"status": "busy", "message": f"Build déjà actif: {website}"}), 429
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
log_debug("🔧 Début du montage S3 pour build...")
|
||||||
mount_s3()
|
mount_s3()
|
||||||
|
|
||||||
src = f"{MINIO_MOUNT_PATH}/files/sites/@{website}/mkdocs.yml"
|
src = f"{MINIO_MOUNT_PATH}/files/sites/@{website}/mkdocs.yml"
|
||||||
tmp = f"/srv/{website}"
|
tmp = f"/srv/{website}"
|
||||||
|
|
||||||
if not os.path.exists(src):
|
log_debug(f"🔎 Vérification existence fichier : {src}")
|
||||||
log_debug(f"❌ Config introuvable: {src}")
|
|
||||||
return jsonify({"error": f"{src} introuvable"}), 404
|
|
||||||
|
|
||||||
|
timeout = 10
|
||||||
|
start_time = time.time()
|
||||||
|
while not os.path.exists(src):
|
||||||
|
elapsed = time.time() - start_time
|
||||||
|
if elapsed > timeout:
|
||||||
|
log_debug(f"❌ Timeout ({timeout}s): {src} introuvable après montage")
|
||||||
|
return jsonify({"error": f"{src} introuvable après montage"}), 404
|
||||||
|
log_debug(f"⏳ Attente du fichier {src}... ({elapsed:.1f}s écoulées)")
|
||||||
|
time.sleep(0.5)
|
||||||
|
|
||||||
|
log_debug(f"🚀 Lancement build MkDocs : {src} → {tmp}")
|
||||||
cmd = f"mkdocs build --quiet --no-strict --config-file {src} --site-dir {tmp}"
|
cmd = f"mkdocs build --quiet --no-strict --config-file {src} --site-dir {tmp}"
|
||||||
log_debug(f"🚀 Build MkDocs: {cmd}")
|
|
||||||
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
|
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
|
||||||
|
|
||||||
if result.returncode != 0:
|
if result.returncode != 0:
|
||||||
build_error = base64.b64encode(result.stderr.encode()).decode()
|
build_error = base64.b64encode(result.stderr.encode()).decode()
|
||||||
json_payload = {"site": website, "erreur": build_error}
|
json_payload = {"site": website, "erreur": build_error}
|
||||||
log_debug(f"❌ Échec build: {result.stderr}")
|
log_debug(f"❌ Échec du build MkDocs: {result.stderr}")
|
||||||
requests.post(error_callback, json=json_payload, headers={"Content-Type": "application/json"})
|
requests.post(error_callback, json=json_payload, headers={"Content-Type": "application/json"})
|
||||||
return jsonify({"status": "error", "message": "Build échoué", "error": result.stderr}), 500
|
return jsonify({"status": "error", "message": "Build échoué", "error": result.stderr}), 500
|
||||||
|
|
||||||
log_debug(f"✅ Build réussi: {website}")
|
log_debug(f"✅ Build MkDocs réussi pour: {website}")
|
||||||
return jsonify({"status": "success", "message": "Build réussi"}), 200
|
return jsonify({"status": "success", "message": "Build réussi"}), 200
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
redis_client.delete(lock_key)
|
redis_client.delete(lock_key)
|
||||||
log_debug(f"🔓 Verrou libéré: {website}")
|
log_debug(f"🔓 Verrou Redis libéré pour: {website}")
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
log_debug("🚀 Flask démarré (0.0.0.0:80)")
|
log_debug("🚀 Démarrage serveur Flask (0.0.0.0:80)")
|
||||||
checker_thread = Thread(target=unmount_checker, daemon=True)
|
checker_thread = Thread(target=unmount_checker, daemon=True)
|
||||||
checker_thread.start()
|
checker_thread.start()
|
||||||
app.run(host="0.0.0.0", port=80)
|
app.run(host="0.0.0.0", port=80)
|
Loading…
x
Reference in New Issue
Block a user