From 4ef29e9fca8f4461929effe439b9bf628f4899f7 Mon Sep 17 00:00:00 2001 From: Vadim Troshchinskiy Date: Thu, 23 Jan 2025 09:59:44 +0100 Subject: [PATCH] Fix ogrepository paths --- api/gitapi.py | 46 +++++++++++------------ installer/opengnsys_git_installer.py | 56 ++++++++++++++++------------ 2 files changed, 56 insertions(+), 46 deletions(-) diff --git a/api/gitapi.py b/api/gitapi.py index 64f3483..7ad5387 100755 --- a/api/gitapi.py +++ b/api/gitapi.py @@ -60,7 +60,7 @@ from flask_restx import Api, Resource, fields #from flasgger import Swagger import paramiko -REPOSITORIES_BASE_PATH = "/opt/opengnsys/images" +REPOSITORIES_BASE_PATH = "/opt/opengnsys/ogrepository/images/git/oggit/" start_time = time.time() tasks = {} @@ -197,7 +197,7 @@ class GitRepositories(Resource): """ if not os.path.isdir(REPOSITORIES_BASE_PATH): - return jsonify({"error": "Repository storage not found, git functionality may not be installed."}), 500 + return {"error": "Repository storage not found, git functionality may not be installed."}, 500 repos = [] for entry in os.scandir(REPOSITORIES_BASE_PATH): @@ -208,9 +208,9 @@ class GitRepositories(Resource): repos = repos + [name] - return jsonify({ + return { "repositories": repos - }) + } def post(self): """ @@ -230,13 +230,13 @@ class GitRepositories(Resource): data = request.json if data is None: - return jsonify({"error" : "Parameters missing"}), 400 + return {"error" : "Parameters missing"}, 400 repo = data["name"] repo_path = os.path.join(REPOSITORIES_BASE_PATH, repo + ".git") if os.path.isdir(repo_path): - return jsonify({"status": "Repository already exists"}), 200 + return {"status": "Repository already exists"}, 200 installer = OpengnsysGitInstaller() @@ -245,7 +245,7 @@ class GitRepositories(Resource): #installer.init_git_repo(repo + ".git") - return jsonify({"status": "Repository created"}), 201 + return {"status": "Repository created"}, 201 @git_ns.route('/repositories//sync') @@ -268,18 +268,18 @@ class GitRepoSync(Resource): """ repo_path = os.path.join(REPOSITORIES_BASE_PATH, repo + ".git") if not os.path.isdir(repo_path): - return jsonify({"error": "Repository not found"}), 404 + return {"error": "Repository not found"}, 404 data = request.json if data is None: - return jsonify({"error" : "Parameters missing"}), 400 + return {"error" : "Parameters missing"}, 400 future = executor.submit(do_repo_sync, repo, data) task_id = str(uuid.uuid4()) tasks[task_id] = future - return jsonify({"status": "started", "task_id" : task_id}), 200 + return {"status": "started", "task_id" : task_id}, 200 @@ -310,12 +310,12 @@ class GitRepoBackup(Resource): """ repo_path = os.path.join(REPOSITORIES_BASE_PATH, repo + ".git") if not os.path.isdir(repo_path): - return jsonify({"error": "Repository not found"}), 404 + return {"error": "Repository not found"}, 404 data = request.json if data is None: - return jsonify({"error" : "Parameters missing"}), 400 + return {"error" : "Parameters missing"}, 400 if not "ssh_port" in data: @@ -326,7 +326,7 @@ class GitRepoBackup(Resource): task_id = str(uuid.uuid4()) tasks[task_id] = future - return jsonify({"status": "started", "task_id" : task_id}), 200 + return {"status": "started", "task_id" : task_id}, 200 @git_ns.route('/repositories//compact', methods=['POST']) class GitRepoCompact(Resource): @@ -348,13 +348,13 @@ class GitRepoCompact(Resource): """ repo_path = os.path.join(REPOSITORIES_BASE_PATH, repo + ".git") if not os.path.isdir(repo_path): - return jsonify({"error": "Repository not found"}), 404 + return {"error": "Repository not found"}, 404 future = executor.submit(do_repo_gc, repo) task_id = str(uuid.uuid4()) tasks[task_id] = future - return jsonify({"status": "started", "task_id" : task_id}), 200 + return {"status": "started", "task_id" : task_id}, 200 @git_ns.route('/tasks//status') @@ -373,15 +373,15 @@ class GitTaskStatus(Resource): - If the task is still in progress, returns a 202 status indicating the task is in progress. """ if not task_id in tasks: - return jsonify({"error": "Task not found"}), 404 + return {"error": "Task not found"}, 404 future = tasks[task_id] if future.done(): result = future.result() - return jsonify({"status" : "completed", "result" : result}), 200 + return {"status" : "completed", "result" : result}, 200 else: - return jsonify({"status" : "in progress"}), 202 + return {"status" : "in progress"}, 202 @@ -405,11 +405,11 @@ class GitRepo(Resource): """ repo_path = os.path.join(REPOSITORIES_BASE_PATH, repo + ".git") if not os.path.isdir(repo_path): - return jsonify({"error": "Repository not found"}), 404 + return {"error": "Repository not found"}, 404 shutil.rmtree(repo_path) - return jsonify({"status": "Repository deleted"}), 200 + return {"status": "Repository deleted"}, 200 @@ -430,7 +430,7 @@ class GitRepoBranches(Resource): """ repo_path = os.path.join(REPOSITORIES_BASE_PATH, repo + ".git") if not os.path.isdir(repo_path): - return jsonify({"error": "Repository not found"}), 404 + return {"error": "Repository not found"}, 404 git_repo = git.Repo(repo_path) @@ -439,9 +439,9 @@ class GitRepoBranches(Resource): branches = branches + [branch.name] - return jsonify({ + return { "branches": branches - }) + } diff --git a/installer/opengnsys_git_installer.py b/installer/opengnsys_git_installer.py index b4353a0..73f71b1 100755 --- a/installer/opengnsys_git_installer.py +++ b/installer/opengnsys_git_installer.py @@ -36,6 +36,20 @@ FORGEJO_URL=f"https://codeberg.org/forgejo/forgejo/releases/download/v{FORGEJO_V +def download_with_progress(url, output_file): + + with requests.get(url, stream=True, timeout=60) as req: + progress = tqdm.tqdm() + progress.total = int(req.headers["Content-Length"]) + progress.unit_scale = True + progress.desc = "Downloading" + + for chunk in req.iter_content(chunk_size=8192): + output_file.write(chunk) + progress.n = progress.n + len(chunk) + progress.refresh() + + progress.close() def show_error(*args): """ @@ -122,18 +136,7 @@ class OgliveMounter: self.tempfile = tempfile.NamedTemporaryFile(mode='wb') filename = self.tempfile.name - with requests.get(url, stream=True, timeout=60) as req: - progress = tqdm.tqdm() - progress.total = int(req.headers["Content-Length"]) - progress.unit_scale = True - progress.desc = "Downloading" - - for chunk in req.iter_content(chunk_size=8192): - self.tempfile.write(chunk) - progress.n = progress.n + len(chunk) - progress.refresh() - - progress.close() + download_with_progress(url, self.tempfile) else: self.logger.debug("We got a filename") filename = url @@ -206,6 +209,7 @@ class OpengnsysGitInstaller: self.__logger.debug("Inicializando") self.testmode = False self.base_path = "/opt/opengnsys" + self.ogrepository_base_path = os.path.join(self.base_path, "ogrepository") self.git_basedir = "base.git" self.email = "OpenGnsys@opengnsys.com" @@ -303,7 +307,7 @@ class OpengnsysGitInstaller: def init_git_repo(self, reponame): """Inicializa un repositorio Git""" # Creamos repositorio - ogdir_images = os.path.join(self.base_path, "images") + ogdir_images = os.path.join(self.ogrepository_base_path, "images") self.__logger.info("Creando repositorio de GIT %s", reponame) os.makedirs(os.path.join(ogdir_images, self.git_basedir), exist_ok=True) @@ -734,20 +738,23 @@ class OpengnsysGitInstaller: opengnsys_bin_path = os.path.join(self.base_path, "bin") - bin_path = os.path.join(opengnsys_bin_path, "forgejo") - conf_dir_path = os.path.join(self.base_path, "etc", "forgejo") + opengnsys_etc_path = os.path.join(self.base_path, "etc") + + forgejo_bin_path = os.path.join(self.ogrepository_base_path, "bin") + bin_path = os.path.join(forgejo_bin_path, "forgejo") + conf_dir_path = os.path.join(self.ogrepository_base_path, "etc", "forgejo") - lfs_dir_path = os.path.join(self.base_path, "images", "git-lfs") - git_dir_path = os.path.join(self.base_path, "images", "git") + lfs_dir_path = os.path.join(self.ogrepository_base_path, "images", "git-lfs") + git_dir_path = os.path.join(self.ogrepository_base_path, "images", "git") - forgejo_work_dir_path = os.path.join(self.base_path, "var", "lib", "forgejo/work") - forgejo_db_dir_path = os.path.join(self.base_path, "var", "lib", "forgejo/db") - forgejo_data_dir_path = os.path.join(self.base_path, "var", "lib", "forgejo/data") + forgejo_work_dir_path = os.path.join(self.ogrepository_base_path, "var", "lib", "forgejo/work") + forgejo_db_dir_path = os.path.join(self.ogrepository_base_path, "var", "lib", "forgejo/db") + forgejo_data_dir_path = os.path.join(self.ogrepository_base_path, "var", "lib", "forgejo/data") forgejo_db_path = os.path.join(forgejo_db_dir_path, "forgejo.db") - forgejo_log_dir_path = os.path.join(self.base_path, "log", "forgejo") + forgejo_log_dir_path = os.path.join(self.ogrepository_base_path, "log", "forgejo") conf_path = os.path.join(conf_dir_path, "app.ini") @@ -757,9 +764,11 @@ class OpengnsysGitInstaller: subprocess.run(["/usr/bin/systemctl", "stop", "opengnsys-forgejo"], check=False) self.__logger.debug("Downloading from %s into %s", FORGEJO_URL, bin_path) - pathlib.Path(opengnsys_bin_path).mkdir(parents=True, exist_ok=True) + pathlib.Path(forgejo_bin_path).mkdir(parents=True, exist_ok=True) + + with open(bin_path, "wb") as forgejo_bin: + download_with_progress(FORGEJO_URL, forgejo_bin) - urllib.request.urlretrieve(FORGEJO_URL, bin_path) os.chmod(bin_path, 0o755) if os.path.exists(forgejo_db_path): @@ -776,6 +785,7 @@ class OpengnsysGitInstaller: self.__logger.debug("Creating directories") + pathlib.Path(opengnsys_etc_path).mkdir(parents=True, exist_ok=True) pathlib.Path(conf_dir_path).mkdir(parents=True, exist_ok=True) pathlib.Path(git_dir_path).mkdir(parents=True, exist_ok=True) pathlib.Path(lfs_dir_path).mkdir(parents=True, exist_ok=True)