from flask import Flask, jsonify import os.path import os import git import shutil from opengnsys_git_installer import OpengnsysGitInstaller from flask import request repositories_base_path = "/opt/opengnsys/images" # Create an instance of the Flask class app = Flask(__name__) # Define a route for the root URL @app.route('/') def home(): return jsonify({ "message": "OpenGnsys Git API" }) # Define another route for /hello/ @app.route('/repositories') def get_repositories(): if not os.path.isdir(repositories_base_path): return jsonify({"error": "Repository storage not found, git functionality may not be installed."}), 500 repos = [] for entry in os.scandir(repositories_base_path): if entry.is_dir(follow_symlinks=False) and os.path.isfile(os.path.join(entry.path, "HEAD")): name = entry.name if name.endswith(".git"): name = name[:-4] repos = repos + [name] return jsonify({ "repositories": repos }) @app.route('/repositories/', methods=['PUT']) def create_repo(repo): repo_path = os.path.join(repositories_base_path, repo + ".git") if os.path.isdir(repo_path): return jsonify({"status": "Repository already exists"}), 200 installer = OpengnsysGitInstaller() installer._init_git_repo(repo + ".git") return jsonify({"status": "Repository created"}), 201 @app.route('/repositories//sync', methods=['POST']) def sync_repo(repo): repo_path = os.path.join(repositories_base_path, repo + ".git") if not os.path.isdir(repo_path): return jsonify({"error": "Repository not found"}), 404 data = request.json dest_repo = data["remote_repository"] return jsonify({"status": "Started synchronization", "repository" : repo, "destination_repository" : dest_repo}), 200 @app.route('/repositories//backup', methods=['POST']) def backup_repo(repo): repo_path = os.path.join(repositories_base_path, repo + ".git") if not os.path.isdir(repo_path): return jsonify({"error": "Repository not found"}), 404 data = request.json dest_server = data["ssh_server"] dest_user = data["ssh_user"] dest_file = data["filename"] return jsonify({"status": "Started backup", "repository" : repo, "ssh_server" : dest_server, "ssh_user" : dest_user, "filename" : dest_file}), 200 @app.route('/repositories/', methods=['DELETE']) def delete_repo(repo): repo_path = os.path.join(repositories_base_path, repo + ".git") if not os.path.isdir(repo_path): return jsonify({"error": "Repository not found"}), 404 shutil.rmtree(repo_path) return jsonify({"status": "Repository deleted"}), 200 @app.route('/repositories//branches') def get_repository_branches(repo): repo_path = os.path.join(repositories_base_path, repo + ".git") if not os.path.isdir(repo_path): return jsonify({"error": "Repository not found"}), 404 gitRepo = git.Repo(repo_path) branches = [] for branch in gitRepo.branches: branches = branches + [branch.name] return jsonify({ "branches": branches }) # Define a route for health check @app.route('/health') def health_check(): return jsonify({ "status": "OK" }) # Run the Flask app if __name__ == '__main__': app.run(debug=True)