diff --git a/script_creator.py b/script_creator.py index 3fde7ad..99e3ecc 100644 --- a/script_creator.py +++ b/script_creator.py @@ -3,6 +3,7 @@ import os import subprocess import redis from threading import Lock +import shutil app = Flask(__name__) @@ -53,21 +54,17 @@ encoding = Slash,BackSlash,Colon,Dot return config_path -def mount_nextcloud(website, mount_path="/mnt"): +def rclone_sync(website, local_path): config_file = write_rclone_config() remote_path = f"nextcloud:{NEXTCLOUD_PATH}/@{website}" - log_debug(f"Mounting {remote_path} to {mount_path}") - os.makedirs(mount_path, exist_ok=True) + if os.path.exists(local_path): + shutil.rmtree(local_path) + os.makedirs(local_path, exist_ok=True) - # Try to unmount first in case of stale mount - subprocess.run(f"fusermount -u {mount_path}", shell=True, stderr=subprocess.DEVNULL) - - cmd = f"rclone --config {config_file} mount {remote_path} {mount_path} --vfs-cache-mode full --daemon" - result = subprocess.run(cmd, shell=True, capture_output=True, text=True) - - if result.returncode != 0: - raise RuntimeError(f"Failed to mount: {result.stderr}") + cmd = f"rclone --config {config_file} sync {remote_path} {local_path} --verbose" + log_debug(f"Syncing from {remote_path} to {local_path}") + return stream_command(cmd) def stream_command(cmd): log_debug(f"Executing: {cmd}") @@ -100,18 +97,18 @@ def build_mkdocs(): log_debug(f"Build already active for website: {website}") return jsonify({"status": "busy", "message": f"Build already active: {website}"}), 429 - mount_path = "/mnt" - compile_path = f"/tmp/{website}#compile" + tmp_path = f"/tmp/{website}" + compile_path = f"{tmp_path}#compile" final_path = f"/srv/{website}" - src = os.path.join(mount_path, "mkdocs.yml") + src = os.path.join(tmp_path, "mkdocs.yml") try: - mount_nextcloud(website, mount_path) + if rclone_sync(website, tmp_path) != 0: + return jsonify({"status": "error", "message": "rclone sync failed"}), 500 - log_debug(f"Checking if mkdocs.yml exists at {src}") if not os.path.exists(src): - log_debug(f"{src} not found after mount") - return jsonify({"error": f"{src} not found after mount"}), 404 + log_debug(f"{src} not found after rclone sync") + return jsonify({"error": f"{src} not found after sync"}), 404 log_debug(f"Running MkDocs build: {src} -> {compile_path}") mkdocs_cmd = f"mkdocs build --no-strict --config-file {src} --site-dir {compile_path}" @@ -128,8 +125,7 @@ def build_mkdocs(): finally: redis_client.delete(lock_key) - subprocess.run(f"fusermount -u {mount_path}", shell=True, stderr=subprocess.DEVNULL) - log_debug(f"Redis lock released and mount cleaned for website: {website}") + log_debug(f"Redis lock released for website: {website}") if __name__ == "__main__": log_debug("Starting Flask server on 0.0.0.0:80")