Compare commits
3 Commits
b8e50585cd
...
2c869ea524
Author | SHA1 | Date |
---|---|---|
|
2c869ea524 | |
|
9bbd9de285 | |
|
4e645bd619 |
134
api/repo_api.py
134
api/repo_api.py
|
@ -56,6 +56,7 @@ config_file = '/opt/opengnsys/ogrepository/etc/ogAdmRepo.cfg'
|
|||
|
||||
REPOSITORIES_BASE_PATH = "/opt/opengnsys/ogrepository/oggit/git/oggit/"
|
||||
|
||||
import sys
|
||||
import git
|
||||
import pkgutil
|
||||
import importlib
|
||||
|
@ -2225,6 +2226,34 @@ def git_compact_repository_task(repo, 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_sync_repository_task(repo, remote_repository, 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)
|
||||
|
||||
# Recreate the remote every time, it might change
|
||||
if "backup" in git_repo.remotes:
|
||||
git_repo.delete_remote("backup")
|
||||
|
||||
backup_repo = git_repo.create_remote("backup", remote_repository)
|
||||
pushed_references = backup_repo.push("*:*")
|
||||
results = []
|
||||
|
||||
# This gets returned to the API
|
||||
for ref in pushed_references:
|
||||
results = results + [ {"local_ref" : ref.local_ref.name, "remote_ref" : ref.remote_ref.name, "summary" : ref.summary }]
|
||||
|
||||
data = {
|
||||
'job_id': job_id,
|
||||
'success': True,
|
||||
'updated_references' : results
|
||||
}
|
||||
|
||||
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():
|
||||
|
@ -2249,7 +2278,7 @@ def git_list_repositories():
|
|||
journal.send("Running endpoint 'Obtener Información de todos los repositorios Git'...", PRIORITY=journal.LOG_INFO, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
||||
|
||||
if not os.path.isdir(REPOSITORIES_BASE_PATH):
|
||||
journal.send(f"Can't list repositories. Repository storage at {REPOSITORIES_BASE_PATH} not found", PRIORITY=journal.LOG_ERROR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
||||
journal.send(f"Can't list 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
|
||||
|
||||
repos = []
|
||||
|
@ -2269,12 +2298,34 @@ def git_list_repositories():
|
|||
}), 200
|
||||
|
||||
def _load_module(module_name):
|
||||
import importlib
|
||||
return importlib.util.find_spec(module_name) is not None
|
||||
# module = importlib.util.find_spec(module_name)
|
||||
module = importlib.import_module(module_name)
|
||||
|
||||
if module is not None:
|
||||
journal.send(f"Module {module_name} loaded successfully. Got {module}", PRIORITY=journal.LOG_INFO, SYSLOG_IDENTIFIER="oggit-api_DEBUG")
|
||||
return module
|
||||
|
||||
journal.send(f"Module {module_name} failed to load, not found", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="oggit-api_DEBUG")
|
||||
return False
|
||||
|
||||
def _load_installer():
|
||||
return _load_module("opengnsys-git-installer")
|
||||
|
||||
journal.send(f"Loading oggit installer module", PRIORITY=journal.LOG_INFO, SYSLOG_IDENTIFIER="oggit-api_DEBUG")
|
||||
|
||||
script_dir = os.path.dirname(os.path.realpath(__file__))
|
||||
|
||||
system_lib_path = '/opt/opengnsys/oggit/bin/'
|
||||
devel_lib_path = os.path.join(script_dir, "../../oggit/installer")
|
||||
|
||||
if devel_lib_path not in sys.path and os.path.isdir(devel_lib_path):
|
||||
journal.send(f"Using {devel_lib_path} development library path", PRIORITY=journal.LOG_INFO, SYSLOG_IDENTIFIER="oggit-api_DEBUG")
|
||||
sys.path.append(devel_lib_path)
|
||||
|
||||
if system_lib_path not in sys.path:
|
||||
journal.send(f"Using {system_lib_path} system library path", PRIORITY=journal.LOG_INFO, SYSLOG_IDENTIFIER="oggit-api_DEBUG")
|
||||
sys.path.append(system_lib_path)
|
||||
|
||||
return _load_module("opengnsys_git_installer")
|
||||
|
||||
|
||||
@app.route("/ogrepository/v1/git/repositories", methods=['POST'])
|
||||
|
@ -2296,17 +2347,19 @@ def git_create_repository():
|
|||
data = request.json
|
||||
|
||||
if data is None:
|
||||
journal.send(f"Can't create repository, JSON post data missing", PRIORITY=journal.LOG_ERROR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
||||
journal.send(f"Can't create repository, JSON post data missing", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
||||
return jsonify({"error" : "Parameters missing"}), 400
|
||||
|
||||
repo = data["name"]
|
||||
|
||||
repo_path = os.path.join(REPOSITORIES_BASE_PATH, repo + ".git")
|
||||
if os.path.isdir(repo_path):
|
||||
journal.send(f"Can't create repository {repo}, already exists at {repo_path}", PRIORITY=journal.LOG_ERROR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
||||
journal.send(f"Can't create repository {repo}, already exists at {repo_path}", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
||||
return jsonify({"status": "Repository already exists"}), 200
|
||||
|
||||
_import_installer()
|
||||
module = _load_installer()
|
||||
print(f"Got {module}")
|
||||
OpengnsysGitInstaller = getattr(module, 'OpengnsysGitInstaller')
|
||||
installer = OpengnsysGitInstaller()
|
||||
installer.add_forgejo_repo(repo)
|
||||
|
||||
|
@ -2320,7 +2373,34 @@ def git_delete_repository():
|
|||
|
||||
@app.route("/ogrepository/v1/git/repositories/<string:repo>/sync", methods=['POST'])
|
||||
def git_sync_repository(repo):
|
||||
return jsonify({"error" : "Not implemented"}), 500
|
||||
journal.send("Running endpoint 'Sincronizar repositorio Git'...", PRIORITY=journal.LOG_INFO, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
||||
|
||||
if not os.path.isdir(REPOSITORIES_BASE_PATH):
|
||||
journal.send(f"Can't sync 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 sync repository, JSON post data missing", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
||||
return jsonify({"error" : "Parameters missing"}), 400
|
||||
|
||||
if "remote_repository" in data:
|
||||
remote_repository = data["remote_repository"]
|
||||
else:
|
||||
journal.send(f"Can't sync repository, JSON remote_repository parameter missing", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
||||
return jsonify({"error" : "Parameters missing"}), 400
|
||||
|
||||
|
||||
job_id = f"GitSync_{''.join(random.choice('0123456789abcdef') for char in range(8))}"
|
||||
|
||||
threading.Thread(target=git_sync_repository_task, args=(repo, remote_repository, job_id,)).start()
|
||||
|
||||
return jsonify({
|
||||
"success": True,
|
||||
"output": "Synchronizing...",
|
||||
"job_id": job_id
|
||||
}), 200
|
||||
|
||||
@app.route("/ogrepository/v1/git/repositories/<string:repo>/backup", methods=['POST'])
|
||||
def git_backup_repository(repo):
|
||||
|
@ -2331,7 +2411,7 @@ def git_compact_repository(repo):
|
|||
journal.send("Running endpoint 'Compactar repositorio Git'...", PRIORITY=journal.LOG_INFO, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
||||
|
||||
if not os.path.isdir(REPOSITORIES_BASE_PATH):
|
||||
journal.send(f"Can't list repositories. Repository storage at {REPOSITORIES_BASE_PATH} not found", PRIORITY=journal.LOG_ERROR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
||||
journal.send(f"Can't list 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
|
||||
|
||||
|
||||
|
@ -2360,7 +2440,7 @@ def git_get_branches(repo):
|
|||
"""
|
||||
repo_path = os.path.join(REPOSITORIES_BASE_PATH, repo + ".git")
|
||||
if not os.path.isdir(repo_path):
|
||||
journal.send(f"Can't list repositories. Repository storage at {REPOSITORIES_BASE_PATH} not found", PRIORITY=journal.LOG_ERROR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
||||
journal.send(f"Can't list repositories. Repository storage at {REPOSITORIES_BASE_PATH} not found", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
||||
return jsonify({"error": "Repository not found"}), 404
|
||||
|
||||
git_repo = git.Repo(repo_path)
|
||||
|
@ -2392,7 +2472,7 @@ def git_create_branch(repo, branch):
|
|||
|
||||
repo_path = os.path.join(REPOSITORIES_BASE_PATH, repo + ".git")
|
||||
if not os.path.isdir(repo_path):
|
||||
journal.send(f"Can't create branch. Repository storage at {REPOSITORIES_BASE_PATH} not found", PRIORITY=journal.LOG_ERROR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
||||
journal.send(f"Can't create branch. Repository storage at {REPOSITORIES_BASE_PATH} not found", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
||||
return jsonify({"error": "Repository not found"}), 404
|
||||
|
||||
git_repo = git.Repo(repo_path)
|
||||
|
@ -2400,20 +2480,20 @@ def git_create_branch(repo, branch):
|
|||
|
||||
data = request.json
|
||||
if data is None:
|
||||
journal.send(f"Can't create branch. JSON post data missing", PRIORITY=journal.LOG_ERROR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
||||
journal.send(f"Can't create branch. JSON post data missing", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
||||
return jsonify({"error" : "Parameters missing"}), 400
|
||||
|
||||
if not "commit" in data:
|
||||
journal.send(f"Can't create branch. Commit parameter missing", PRIORITY=journal.LOG_ERROR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
||||
journal.send(f"Can't create branch. Commit parameter missing", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
||||
return jsonify({"error" : "commit parameter missing"}), 400
|
||||
|
||||
if branch in git_repo.branches:
|
||||
journal.send(f"Can't create branch. Already found in repository {repo}", PRIORITY=journal.LOG_ERROR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
||||
journal.send(f"Can't create branch. Already found in repository {repo}", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
||||
return jsonify({"error": "Branch already exists"}), 409
|
||||
|
||||
git_repo.create_tag(branch, ref = data["commit"])
|
||||
|
||||
journal.send(f"Branch {branch} created in repo {repo}", PRIORITY=journal.LOG_ERROR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
||||
journal.send(f"Branch {branch} created in repo {repo}", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
||||
return jsonify({"status": "created"}), 201
|
||||
|
||||
@app.route("/ogrepository/v1/git/repositories/<string:repo>/branches/<string:branch>", methods=['DELETE'])
|
||||
|
@ -2431,7 +2511,7 @@ def git_delete_branch(repo, branch):
|
|||
|
||||
repo_path = os.path.join(REPOSITORIES_BASE_PATH, repo + ".git")
|
||||
if not os.path.isdir(repo_path):
|
||||
journal.send(f"Can't delete branch. Repository storage at {REPOSITORIES_BASE_PATH} not found", PRIORITY=journal.LOG_ERROR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
||||
journal.send(f"Can't delete branch. Repository storage at {REPOSITORIES_BASE_PATH} not found", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
||||
return {"error": "Repository not found"}, 404
|
||||
|
||||
git_repo = git.Repo(repo_path)
|
||||
|
@ -2439,11 +2519,11 @@ def git_delete_branch(repo, branch):
|
|||
|
||||
|
||||
if not branch in git_repo.branches:
|
||||
journal.send(f"Can't delete branch. Not found in repository {repo}", PRIORITY=journal.LOG_ERROR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
||||
journal.send(f"Can't delete branch. Not found in repository {repo}", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
||||
return jsonify({"error": "Branch not found"}), 404
|
||||
|
||||
git_repo.delete_head(branch)
|
||||
journal.send(f"Branch {branch} deleted in repo {repo}", PRIORITY=journal.LOG_ERROR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
||||
journal.send(f"Branch {branch} deleted in repo {repo}", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
||||
|
||||
return jsonify({"status": "deleted"}), 200
|
||||
|
||||
|
@ -2463,7 +2543,7 @@ def git_list_tags(repo):
|
|||
"""
|
||||
repo_path = os.path.join(REPOSITORIES_BASE_PATH, repo + ".git")
|
||||
if not os.path.isdir(repo_path):
|
||||
journal.send(f"Can't list repositories. Repository storage at {REPOSITORIES_BASE_PATH} not found", PRIORITY=journal.LOG_ERROR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
||||
journal.send(f"Can't list repositories. Repository storage at {REPOSITORIES_BASE_PATH} not found", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
||||
return jsonify({"error": "Repository not found"}), 404
|
||||
|
||||
git_repo = git.Repo(repo_path)
|
||||
|
@ -2496,7 +2576,7 @@ def git_create_tag(repo, tag):
|
|||
|
||||
repo_path = os.path.join(REPOSITORIES_BASE_PATH, repo + ".git")
|
||||
if not os.path.isdir(repo_path):
|
||||
journal.send(f"Can't create tag. Repository storage at {REPOSITORIES_BASE_PATH} not found", PRIORITY=journal.LOG_ERROR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
||||
journal.send(f"Can't create tag. Repository storage at {REPOSITORIES_BASE_PATH} not found", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
||||
return jsonify({"error": "Repository not found"}), 404
|
||||
|
||||
git_repo = git.Repo(repo_path)
|
||||
|
@ -2504,20 +2584,20 @@ def git_create_tag(repo, tag):
|
|||
|
||||
data = request.json
|
||||
if data is None:
|
||||
journal.send(f"Can't create tag. JSON post data missing", PRIORITY=journal.LOG_ERROR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
||||
journal.send(f"Can't create tag. JSON post data missing", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
||||
return jsonify({"error" : "Parameters missing"}), 400
|
||||
|
||||
if not "commit" in data:
|
||||
journal.send(f"Can't create tag. Commit parameter missing", PRIORITY=journal.LOG_ERROR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
||||
journal.send(f"Can't create tag. Commit parameter missing", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
||||
return jsonify({"error" : "commit parameter missing"}), 400
|
||||
|
||||
if tag in git_repo.tags:
|
||||
journal.send(f"Can't create tag. Already found in repository {repo}", PRIORITY=journal.LOG_ERROR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
||||
journal.send(f"Can't create tag. Already found in repository {repo}", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
||||
return jsonify({"error": "Tag already exists"}), 409
|
||||
|
||||
git_repo.create_tag(tag, ref = data["commit"])
|
||||
|
||||
journal.send(f"Tag {tag} created in repo {repo}", PRIORITY=journal.LOG_ERROR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
||||
journal.send(f"Tag {tag} created in repo {repo}", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
||||
return jsonify({"status": "created"}), 200
|
||||
|
||||
@app.route("/ogrepository/v1/git/repositories/<string:repo>/tags/<string:tag>", methods=['DELETE'])
|
||||
|
@ -2535,7 +2615,7 @@ def git_delete_tag(repo, tag):
|
|||
|
||||
repo_path = os.path.join(REPOSITORIES_BASE_PATH, repo + ".git")
|
||||
if not os.path.isdir(repo_path):
|
||||
journal.send(f"Can't delete tag. Repository storage at {REPOSITORIES_BASE_PATH} not found", PRIORITY=journal.LOG_ERROR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
||||
journal.send(f"Can't delete tag. Repository storage at {REPOSITORIES_BASE_PATH} not found", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
||||
return {"error": "Repository not found"}, 404
|
||||
|
||||
git_repo = git.Repo(repo_path)
|
||||
|
@ -2543,11 +2623,11 @@ def git_delete_tag(repo, tag):
|
|||
|
||||
|
||||
if not tag in git_repo.tags:
|
||||
journal.send(f"Can't delete tag. Not found in repository {repo}", PRIORITY=journal.LOG_ERROR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
||||
journal.send(f"Can't delete tag. Not found in repository {repo}", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
||||
return jsonify({"error": "Tag not found"}), 404
|
||||
|
||||
git_repo.delete_head(tag)
|
||||
journal.send(f"Tag {tag} deleted in repo {repo}", PRIORITY=journal.LOG_ERROR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
||||
journal.send(f"Tag {tag} deleted in repo {repo}", PRIORITY=journal.LOG_ERR, SYSLOG_IDENTIFIER="ogrepo-api_DEBUG")
|
||||
|
||||
return jsonify({"status": "deleted"}), 200
|
||||
|
||||
|
|
Loading…
Reference in New Issue