diff --git a/api/repo_api.py b/api/repo_api.py index d56dd55..b636e15 100644 --- a/api/repo_api.py +++ b/api/repo_api.py @@ -60,6 +60,7 @@ import sys import git import pkgutil import importlib +import paramiko # -------------------------------------------------------------------------------------------- @@ -2254,6 +2255,31 @@ def git_sync_repository_task(repo, remote_repository, job_id): journal.send(f"Calling function 'recall_ogcore' (JOB_ID: {job_id}, SUCCESS: True)", PRIORITY=journal.LOG_INFO, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG") recall_ogcore(data) +def git_backup_repository_task(repo, params, job_id): + journal.send("Running function 'git_sync_repository_task'...", PRIORITY=journal.LOG_INFO, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG") + + git_repo_path = os.path.join(REPOSITORIES_BASE_PATH, repo + ".git") + git_repo = git.Repo(git_repo_path) + git_repo.git.config('--global', '--add', 'safe.directory', git_repo_path) + + ssh = paramiko.SSHClient() + ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + + ssh.connect(params["ssh_server"], params["ssh_port"], params["ssh_user"]) + sftp = ssh.open_sftp() + + with sftp.file(params["filename"], mode='wb+') as remote_file: + git_repo.archive(remote_file, format="tar.gz") + + data = { + 'job_id': job_id, + 'success': True, + } + + journal.send(f"Calling function 'recall_ogcore' (JOB_ID: {job_id}, SUCCESS: True)", PRIORITY=journal.LOG_INFO, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG") + recall_ogcore(data) + + @app.route("/ogrepository/v1/git/repositories", methods=['GET']) def git_list_repositories(): @@ -2404,7 +2430,45 @@ def git_sync_repository(repo): @app.route("/ogrepository/v1/git/repositories//backup", methods=['POST']) def git_backup_repository(repo): - return jsonify({"error" : "Not implemented"}), 500 + journal.send("Running endpoint 'Backup de repositorio Git'...", PRIORITY=journal.LOG_INFO, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG") + + if not os.path.isdir(REPOSITORIES_BASE_PATH): + journal.send(f"Can't backup repositories. Repository storage at {REPOSITORIES_BASE_PATH} not found", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG") + return jsonify({"error": "Repository storage not found, git functionality may not be installed."}), 500 + + data = request.json + + if data is None: + journal.send(f"Can't backup repository, JSON post data missing", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG") + return jsonify({"error" : "Parameters missing"}), 400 + + if not "ssh_server" in data: + journal.send(f"Can't sync repository, JSON ssh_server parameter missing", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG") + return jsonify({"error" : "Parameters missing"}), 400 + + if not "ssh_port" in data: + journal.send(f"Can't sync repository, JSON ssh_port parameter missing", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG") + return jsonify({"error" : "Parameters missing"}), 400 + + if not "ssh_user" in data: + journal.send(f"Can't sync repository, JSON ssh_user parameter missing", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG") + return jsonify({"error" : "Parameters missing"}), 400 + + if not "filename" in data: + journal.send(f"Can't sync repository, JSON filename parameter missing", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG") + return jsonify({"error" : "Parameters missing"}), 400 + + + job_id = f"GitBackup_{''.join(random.choice('0123456789abcdef') for char in range(8))}" + + threading.Thread(target=git_backup_repository_task, args=(repo, data, job_id,)).start() + + return jsonify({ + "success": True, + "output": "Backing up...", + "job_id": job_id + }), 200 + @app.route("/ogrepository/v1/git/repositories//compact", methods=['POST']) def git_compact_repository(repo):