Fix installer module loading

oggit
Vadim vtroshchinskiy 2025-05-07 09:18:01 +02:00
parent b8e50585cd
commit 4e645bd619
1 changed files with 31 additions and 6 deletions

View File

@ -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
@ -2269,12 +2270,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 +2319,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)