Fix ogrepository paths

ogrepository-fixes
Vadim vtroshchinskiy 2025-01-23 09:59:44 +01:00
parent 6491757535
commit 4ef29e9fca
2 changed files with 56 additions and 46 deletions

View File

@ -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/<repo>/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/<repo>/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/<task_id>/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
})
}

View File

@ -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)