From 6cb769e40c59df8a5ec65f4c2591918112e027ba Mon Sep 17 00:00:00 2001 From: Vadim Troshchinskiy Date: Thu, 17 Jul 2025 16:38:57 +0200 Subject: [PATCH 01/11] refs #2489 Set default branch to "main" --- ogclient/lib/python3/GitLib/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ogclient/lib/python3/GitLib/__init__.py b/ogclient/lib/python3/GitLib/__init__.py index d9665fd..dff1887 100755 --- a/ogclient/lib/python3/GitLib/__init__.py +++ b/ogclient/lib/python3/GitLib/__init__.py @@ -348,6 +348,7 @@ class OpengnsysGitLibrary: self.repo_server = self.kernel_args["ogrepo"] self.ip_address = self.kernel_args["ip"] + self.initial_branch = "main" if not self.repo_server: self.logger.warning("ogrepo kernel argument wasn't passed, or was empty. Defaulting to oglive.") @@ -1370,7 +1371,7 @@ class OpengnsysGitLibrary: os.symlink(real_git_dir, git_dir) - with git.Repo.init(path) as repo: + with git.Repo.init(path, initial_branch = self.initial_branch) as repo: # On NTFS, we have to unmount the filesystem to do secaudit. # Gitpython objects existing at that time may mean a dangling git process that prevents # the required unmounting. From 237ec3dbf60efa59450704a34456118d4af8d377 Mon Sep 17 00:00:00 2001 From: Vadim Troshchinskiy Date: Wed, 23 Jul 2025 19:10:34 +0200 Subject: [PATCH 02/11] refs #2506: Branch creation support --- ogclient/interfaceAdm/ModificarImagenGit | 32 +++++++-- ogclient/lib/python3/GitLib/__init__.py | 88 +++++++----------------- 2 files changed, 49 insertions(+), 71 deletions(-) diff --git a/ogclient/interfaceAdm/ModificarImagenGit b/ogclient/interfaceAdm/ModificarImagenGit index a3860d0..6e61866 100755 --- a/ogclient/interfaceAdm/ModificarImagenGit +++ b/ogclient/interfaceAdm/ModificarImagenGit @@ -67,30 +67,48 @@ class OgLogger(logging.StreamHandler): -def commit_image(disk_num, partition_num, image_name, msg): +def commit_image(disk_num, partition_num, repository, branch, options, msg): ntfs_impl = NTFSImplementation.NTFS3G og_git = OpengnsysGitLibrary(ntfs_implementation = ntfs_impl) og_git.progress_callback = OgProgressPrinterWeb() device = DiskLib.ogDiskToDev(disk_num, partition_num) - og_git.commit(device = device, message = msg) - og_git.push(device = device) + + force_push = False + opts = options.split(",") + if "force_push" in opts: + force_push = True + + try: + if branch: + if not og_git.create_branch(device = device, name = branch): + sys.exit(SystemLib.ogRaiseError([], ogGlobals.OG_ERR_FORMAT, f"Failed to create branch")) + + og_git.commit(device = device, message = msg) + og_git.push(device = device, force = force_push) + except Exception as ex: + sys.exit(SystemLib.ogRaiseError([], ogGlobals.OG_ERR_FORMAT, f"Exception during commit: {ex}")) + return 0 def main(): - if len(sys.argv) != 5: + if len(sys.argv) != 7: import json args = json.dumps(sys.argv) - sys.exit(SystemLib.ogRaiseError([], ogGlobals.OG_ERR_FORMAT, f"Incorrect number of arguments. Usage: ModificarImagenGit disk_num partition_num image_name repo msg. Received args: {args}")) + sys.exit(SystemLib.ogRaiseError([], ogGlobals.OG_ERR_FORMAT, f"Incorrect number of arguments. Got {len(sys.argv)}. Usage: ModificarImagenGit.py disk_num partition_num repo new_branch options msg. Received args: {args}")) # repo - repositorio, ip address. Opcional porque oglive lo recibe como parametro de kernel # tag - tag a crear automaticamente. Opcional, no necesitamos crear un tag siempre. - disk_num, partition_num, image_name, msg = sys.argv[1:5] + disk_num, partition_num, repo, branch, options, msg = sys.argv[1:7] + print(f"Repo : {repo}") + print(f"Branch : {branch}") + print(f"Options: {options}") + print(f"Msg : {msg}") opengnsys_log_dir = "/opt/opengnsys/log" ip_address = NetLib.ogGetIpAddress() @@ -114,7 +132,7 @@ def main(): logger.info("Starting ModificarImagenGit") - retval = commit_image(disk_num, partition_num, image_name, msg) + retval = commit_image(disk_num, partition_num, repo, branch, options, msg) logger.info("ModificarImagenGit done, return code %i", retval) diff --git a/ogclient/lib/python3/GitLib/__init__.py b/ogclient/lib/python3/GitLib/__init__.py index dff1887..e29edcf 100755 --- a/ogclient/lib/python3/GitLib/__init__.py +++ b/ogclient/lib/python3/GitLib/__init__.py @@ -47,6 +47,7 @@ from GitLib.disk import * from GitLib.ntfs import * from GitLib.kernel import parse_kernel_cmdline +import CacheLib #import requests @@ -286,7 +287,8 @@ class OpengnsysGitLibrary: self.repo_image_path = "oggit" self.ntfs_implementation = ntfs_implementation - self.cache_dir = self._runBashFunction("ogMountCache", []) + self.cache_dir = CacheLib.ogMountCache() + # Si no hay cache, se va a crear el .git en el FS directamente if (not self.cache_dir) and require_cache: @@ -603,64 +605,6 @@ class OpengnsysGitLibrary: except OSError as e: self.logger.warning('Failed to delete %s. Error: %s', file_path, e) - - def _runBashFunction(self, function, arguments): - """ - Executes an OpenGnsys bash function with given arguments. - - This method creates a temporary bash script that sources all `.lib` files in a specific directory, - writes the specified bash function and its arguments to the script, makes the script executable, - and then runs the script. The output and errors from the script execution are captured and logged. - - This is a temporary migration convenience function, it won't be present once the rest of the - code is migrated to Python. - - Args: - function (str): The name of the bash function to execute. - arguments (list): A list of arguments to pass to the bash function. - - Returns: - str: The standard output from the executed bash function. - - Logs: - - Debug information about the bash function and arguments being run. - - The path of the temporary file created. - - The command being executed. - - The standard output and standard error from the script execution. - """ - # Create a temporary file - self.logger.debug(f"Running bash function: {function} {arguments}") - - with tempfile.NamedTemporaryFile(mode='w', delete=False) as temp_file: - temp_file.write("#!/bin/bash\n") - temp_file.write("for lib in /opt/opengnsys/client/lib/engine/bin/*.lib ; do\n") - temp_file.write(" source $lib\n") - temp_file.write("done\n") - - #temp_file.write("source /opt/oglive/rootfs/opt/opengnsys/lib/engine/bin/Cache.lib") - #temp_file.write("source /opt/oglive/rootfs/opt/opengnsys/lib/engine/bin/Git.lib") - - temp_file.write(f"{function} \"$@\"\n") - - # Make the temporary file executable - os.chmod(temp_file.name, 0o755) - - self.logger.debug(f"File: {temp_file.name}") - - - # Run the temporary file - command = [temp_file.name] + arguments - self.logger.debug(f"Running: {command} {arguments}") - result = subprocess.run(command, shell=False, capture_output=True, text=True, check=True) - output = result.stdout.strip() - - self.logger.debug(f"STDOUT: {output}") - self.logger.debug(f"STDERR: {result.stderr}") - - return output - - - def _getOgRepository(self, name): return f"{self.repo_user}@{self.repo_server}:{self.repo_image_path}/{name}.git" @@ -1393,7 +1337,7 @@ class OpengnsysGitLibrary: self.logger.debug("Fetching origin, callback=%s", self.progress_callback) origin.fetch(progress=self.progress_callback) - repo.heads.master.set_tracking_branch(origin.refs.master) + repo.heads.main.set_tracking_branch(origin.refs.main) metadata_ret = self._create_metadata(path, initial_creation=True) @@ -1615,7 +1559,23 @@ class OpengnsysGitLibrary: # Restaurar cosas modificadas para git self._restore_metadata(path, destructive_only=True) - def push(self, path = None, device = None): + def create_branch(self, path = None, device = None, name = ""): + if path is None: + path = self.fs.ensure_mounted(device) + + repo = git.Repo(path) + if name in repo.branches: + self.logger.error("Can't create branch, it already exists") + return False + + self.logger.info("Creating branch %s", name) + + new_branch = repo.create_head(name) + repo.head.reference = new_branch + repo.head.reset(index=True, working_tree=True) + return True + + def push(self, path = None, device = None, force = False): """ Push local changes to ogrepository @@ -1634,9 +1594,10 @@ class OpengnsysGitLibrary: return origin = repo.remotes["origin"] - repo.heads.master.set_tracking_branch(origin.refs.master) + repo.heads.main.set_tracking_branch(origin.refs.main) - origin.push(progress=self.progress_callback) + ret = origin.push(progress=self.progress_callback, force = force) + ret.raise_if_error() #repo.git.push("--set-upstream", "origin", repo.head.ref, "--force") # force = True) @@ -1792,7 +1753,6 @@ if __name__ == '__main__': og_git = OpengnsysGitLibrary(ntfs_implementation = ntfs_impl) - # og_git._runBashFunction("ogMountCache", []) # if args.init_repo: From 33f7c6325c7a20233b8dca826e31fb69de6bf06a Mon Sep 17 00:00:00 2001 From: Vadim Trochinsky Date: Thu, 31 Jul 2025 09:54:42 +0200 Subject: [PATCH 03/11] refs #2553 Improve exception handling --- ogclient/lib/python3/GitLib/filesystem.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/ogclient/lib/python3/GitLib/filesystem.py b/ogclient/lib/python3/GitLib/filesystem.py index fb23619..b724f46 100644 --- a/ogclient/lib/python3/GitLib/filesystem.py +++ b/ogclient/lib/python3/GitLib/filesystem.py @@ -41,7 +41,7 @@ class FilesystemLibrary: try: with open(file, "r", encoding='utf-8') as f: data = f.read() - except IOError as io_err: + except Exception as io_err: self.logger.debug("IO Error reading file %s: %s", file, io_err) return data @@ -51,7 +51,7 @@ class FilesystemLibrary: try: data = os.readlink(file) - except IOError as io_err: + except Exception as io_err: self.logger.debug("IO Error reading link %s: %s", file, io_err) return data @@ -106,13 +106,16 @@ class FilesystemLibrary: self.logger.debug("PID %s (%s) is has a working directory within %s: %s", pid_dir, command_name, path, working_dir) pids_using_path[pid_dir] = { "cmd" : command_name, "args" : cmdline} else: - for fd_file in os.listdir(fd_path): - fd_file_full_path = os.path.join(fd_path, fd_file) - target = self._read_link(fd_file_full_path) + try: + for fd_file in os.listdir(fd_path): + fd_file_full_path = os.path.join(fd_path, fd_file) + target = self._read_link(fd_file_full_path) - if target.startswith(path): - self.logger.debug("PID %s (%s) is has an open file within %s: %s", pid_dir, command_name, path, target) - pids_using_path[pid_dir] = { "cmd" : command_name, "args" : cmdline} + if target.startswith(path): + self.logger.debug("PID %s (%s) is has an open file within %s: %s", pid_dir, command_name, path, target) + pids_using_path[pid_dir] = { "cmd" : command_name, "args" : cmdline} + except IOError as ioerr: + self.logger.warning("Exception during listdir: %s", ioerr) return pids_using_path From 363a253622e3b3ed2c04fe4035dc329f96c0ca99 Mon Sep 17 00:00:00 2001 From: Vadim Trochinsky Date: Thu, 31 Jul 2025 09:57:40 +0200 Subject: [PATCH 04/11] refs #2506 -- Initial implementation --- ogclient/interfaceAdm/CrearImagenGit | 28 +++++---- ogclient/interfaceAdm/ModificarImagenGit | 22 ++++++- ogclient/interfaceAdm/RestaurarImagenGit | 35 ++++++----- ogclient/lib/python3/GitLib/__init__.py | 75 ++++++++++++++++++++++-- 4 files changed, 130 insertions(+), 30 deletions(-) diff --git a/ogclient/interfaceAdm/CrearImagenGit b/ogclient/interfaceAdm/CrearImagenGit index 10ec270..facda41 100755 --- a/ogclient/interfaceAdm/CrearImagenGit +++ b/ogclient/interfaceAdm/CrearImagenGit @@ -59,19 +59,24 @@ def main(): print(f"No se pudo aumentar el límite de archivos abiertos: {e}") + parser = argparse.ArgumentParser( + prog = "OpenGnsys Git Image Create", + description = "Creates a git repository from a partition" + ) - if len(sys.argv) < 5: - sys.exit(SystemLib.ogRaiseError([], ogGlobals.OG_ERR_FORMAT, "Incorrect number of arguments. Usage: CrearImagenGit disk_num partition_num image_name repo")) + parser.add_argument("--disk", type=int, metavar="DISK", required=True, help="Disk ID") + parser.add_argument("--partition", type=int, metavar="PART", required=True, help="Disk partition") + parser.add_argument("--repo", type=str, metavar="REPO", required=True, help="Address of the Git repository to clone") + parser.add_argument("--tag", type=str, metavar="TAG", required=False, help="Tag to automatically create") - # repo - repositorio, ip address. Opcional porque oglive lo recibe como parametro de kernel + parser.add_help = True + + args = parser.parse_args() + + #disk_num, partition_num, image_name, repo, tag = sys.argv[1:6] - disk_num, partition_num, image_name, repo = sys.argv[1:5] - - - opengnsys_log_dir = "/opt/opengnsys/log" - ip_address = NetLib.ogGetIpAddress() - logFilePath = f"{opengnsys_log_dir}/{ip_address}.log" + logFilePath = "/var/log/opengnsys.CrearImagenGit.log" ogLog = OgLogger() ogLog.setLevel(logging.DEBUG) @@ -91,8 +96,11 @@ def main(): logger.info("Starting CrearImagenGit") + # TODO: + # repo = repositorio, oggit@server.com:/oggit + # image = nombre repo - retval = create_image(disk_num, partition_num, repo, image_name) + retval = create_image(args.disk, args.partition, repo, image_name, args.tag) diff --git a/ogclient/interfaceAdm/ModificarImagenGit b/ogclient/interfaceAdm/ModificarImagenGit index 6e61866..0c3055e 100755 --- a/ogclient/interfaceAdm/ModificarImagenGit +++ b/ogclient/interfaceAdm/ModificarImagenGit @@ -4,7 +4,7 @@ import subprocess import sys import time import resource - +import argparse soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE) @@ -94,6 +94,26 @@ def commit_image(disk_num, partition_num, repository, branch, options, msg): return 0 def main(): + + + parser = argparse.ArgumentParser( + prog = "OpenGnsys Git Image Modify", + description = "Commits changes to a partition to a git repository" + ) + + parser.add_argument("--disk", type=int, metavar="DISK", required=True, help="Disk ID") + parser.add_argument("--partition", type=int, metavar="PART", required=True, help="Disk partition") + parser.add_argument("--repo", type=str, metavar="REPO", required=True, help="Address of the Git repository to clone") + parser.add_argument("--branch", type=str, metavar="BRANCH", required=False, help="Branch to automatically create") + parser.add_argument("--options", type=str, metavar="OPTS", required=False, help="Options to branch creation (forcepush)") + parser.add_argument("--message", type=str, metavar="MSG", required=False, help="Commit message") + + parser.add_help = True + + args = parser.parse_args() + + + if len(sys.argv) != 7: import json args = json.dumps(sys.argv) diff --git a/ogclient/interfaceAdm/RestaurarImagenGit b/ogclient/interfaceAdm/RestaurarImagenGit index 6aa7ad7..66bda11 100755 --- a/ogclient/interfaceAdm/RestaurarImagenGit +++ b/ogclient/interfaceAdm/RestaurarImagenGit @@ -10,6 +10,10 @@ import DiskLib from GitLib import OpengnsysGitLibrary, NTFSImplementation, OgProgressPrinterWeb +import argparse + + + class OgLogger(logging.StreamHandler): def emit(self, record): log_types = ["command"] @@ -42,20 +46,23 @@ if __name__ == "__main__": print(f"No se pudo aumentar el límite de archivos abiertos: {e}") - if len(sys.argv) < 6: - print("Usage: python RestaurarImagenGit ") - sys.exit(1) - disk = sys.argv[1] - partition = sys.argv[2] - repo = sys.argv[3] - ipaddr = sys.argv[4] - gitref = sys.argv[5] - proto = sys.argv[6] + parser = argparse.ArgumentParser( + prog = "OpenGnsys Git Image Restore", + description = "Restores an image from Git" + ) - opengnsys_log_dir = "/opt/opengnsys/log" - ip_address = NetLib.ogGetIpAddress() - logFilePath = f"{opengnsys_log_dir}/{ip_address}.log" + parser.add_argument("--disk", type=int, metavar="DISK", required=True, help="Disk ID") + parser.add_argument("--partition", type=int, metavar="PART", required=True, help="Disk partition") + parser.add_argument("--repo", type=str, metavar="REPO", required=True, help="Address of the Git repository to clone") + parser.add_argument("--branch", type=str, metavar="BRANCH", required=True, help="Branch to check out") + parser.add_argument("--commit", type=str, metavar="COMMIT_ID", required=True, help="Commit to check out") + parser.add_help = True + + args = parser.parse_args() + + + logFilePath = "/var/log/opengnsys.RestaurarImagenGit.log" ogLog = OgLogger() ogLog.setLevel(logging.DEBUG) @@ -81,9 +88,9 @@ if __name__ == "__main__": og_git = OpengnsysGitLibrary(ntfs_implementation = ntfs_impl) og_git.progress_callback = OgProgressPrinterWeb() - device = DiskLib.ogDiskToDev(disk, partition) + device = DiskLib.ogDiskToDev(args.disk, args.partition) - og_git.cloneRepo(repo, device, device) + og_git.cloneRepo(args.repo, destination = device, boot_device = device, ref = args.commit, branch = args.branch) logger.info("RestaurarImagenGit Finished.") diff --git a/ogclient/lib/python3/GitLib/__init__.py b/ogclient/lib/python3/GitLib/__init__.py index e29edcf..b0a8352 100755 --- a/ogclient/lib/python3/GitLib/__init__.py +++ b/ogclient/lib/python3/GitLib/__init__.py @@ -1424,7 +1424,7 @@ class OpengnsysGitLibrary: self.logger.info("initRepo done") return True - def cloneRepo(self, repo_name, destination, boot_device): + def cloneRepo(self, repo_name, destination, boot_device, ref = None, branch = None): """ Clones a repository to a specified destination and sets up the bootloader. @@ -1487,12 +1487,71 @@ class OpengnsysGitLibrary: self.logger.info("Cloning repository from %s", repo_url) - repo = git.Repo.clone_from(repo_url, destination_dir, multi_options = [f"--separate-git-dir={real_git_dir}"], progress=self.progress_callback) - if repo_is_efi: - self._efi_install(root_directory=destination_dir) + + + if branch: + repo = git.Repo.clone_from(repo_url, destination_dir, branch = branch, multi_options = [f"--separate-git-dir={real_git_dir}"], progress=self.progress_callback) + + self.logger.debug("Checking out indicated branch %s", branch) + remote_branch_ref = repo.heads[branch] + + if branch in repo.heads: + self.logger.debug("Removing existing local branch %s", branch) + repo.delete_head(branch) + + if ref: + self.logger.debug("Local branch adjusted to ref %s", ref) + local_ref = repo.create_head(branch, ref) + else: + self.logger.debug("Local branch is set to remote branch %s", remote_branch_ref) + local_ref = repo.create_head(branch, remote_branch_ref) + local_ref.set_tracking_branch(remote_branch_ref) + + self.logger.debug("Checking out local branch %s", branch) + local_ref.checkout() + else: - self._grub_install(root_directory=destination_dir, boot_device=boot_device) + repo = git.Repo.clone_from(repo_url, destination_dir, multi_options = [f"--separate-git-dir={real_git_dir}"], progress=self.progress_callback) + + branches_with_commit = repo.git.branch("-r", "--contains", ref).split("\n") + self.logger.debug("Branches with commit: %s", branches_with_commit) + + if len(branches_with_commit) > 0: + remote_branch = branches_with_commit[0].strip() + if "->" in remote_branch: + # Git returned something like: + # origin/HEAD -> origin/main + # + # so take the second part. + remote_branch = remote_branch.split("->")[1].strip() + + parts = remote_branch.split("/") + self.logger.info(f"Branch: {remote_branch}, parts: {parts}") + local_branch_name = parts[1] + + if local_branch_name in repo.heads: + #self.logger.debug("Removing existing local branch %s", local_branch_name) + #repo.git.reset("--hard") + #repo.delete_head(local_branch_name) + self.logger.debug("Local branch %s exists, checking it out first", local_branch_name) + repo.git.checkout(local_branch_name) + + self.logger.debug("Resetting branch %s to ref %s", local_branch_name, ref) + repo.git.reset("--hard", ref) + else: + self.logger.info("Checking out containing remote branch %s, as %s", remote_branch, local_branch_name) + repo.git.checkout("-b", local_branch_name, remote_branch) + + try: + self.logger.info("Setting upstream: %s", local_branch_name) + repo.git.branch(f"--set-upstream-to={remote_branch}", local_branch_name) + except Exception as ex: + self.logger.error("Setting upstream failed: %s", ex) + + else: + self.logger.info("Checking out REF %s", ref) + repo.git.checkout(ref) self.fs.mklostandfound(destination_dir) self._restore_metadata(destination_dir, set_device_uuids=True) @@ -1500,6 +1559,12 @@ class OpengnsysGitLibrary: if self.fs.filesystem_type(mountpoint = destination_dir) == "ntfs": self._ntfs_restore_secaudit(destination_dir) + if repo_is_efi: + self._efi_install(root_directory=destination_dir) + else: + self._grub_install(root_directory=destination_dir, boot_device=boot_device) + + self.logger.info("Clone completed.") From bf1620d0adcb8e6e2a417e8df206956cfb92b505 Mon Sep 17 00:00:00 2001 From: Vadim Troshchinskiy Date: Thu, 14 Aug 2025 13:36:49 +0200 Subject: [PATCH 05/11] refs #2506 rename repo argument --- ogclient/interfaceAdm/CrearImagenGit | 3 ++- ogclient/interfaceAdm/ModificarImagenGit | 2 +- ogclient/interfaceAdm/RestaurarImagenGit | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/ogclient/interfaceAdm/CrearImagenGit b/ogclient/interfaceAdm/CrearImagenGit index facda41..7c2ed0f 100755 --- a/ogclient/interfaceAdm/CrearImagenGit +++ b/ogclient/interfaceAdm/CrearImagenGit @@ -66,8 +66,9 @@ def main(): parser.add_argument("--disk", type=int, metavar="DISK", required=True, help="Disk ID") parser.add_argument("--partition", type=int, metavar="PART", required=True, help="Disk partition") - parser.add_argument("--repo", type=str, metavar="REPO", required=True, help="Address of the Git repository to clone") + parser.add_argument("--repository", type=str, metavar="REPO", required=True, help="Address of the Git repository to clone") parser.add_argument("--tag", type=str, metavar="TAG", required=False, help="Tag to automatically create") + parser.add_argument("--message", type=str, metavar="MSG", required=False, help="Commit message") parser.add_help = True diff --git a/ogclient/interfaceAdm/ModificarImagenGit b/ogclient/interfaceAdm/ModificarImagenGit index 0c3055e..c54d1e9 100755 --- a/ogclient/interfaceAdm/ModificarImagenGit +++ b/ogclient/interfaceAdm/ModificarImagenGit @@ -103,7 +103,7 @@ def main(): parser.add_argument("--disk", type=int, metavar="DISK", required=True, help="Disk ID") parser.add_argument("--partition", type=int, metavar="PART", required=True, help="Disk partition") - parser.add_argument("--repo", type=str, metavar="REPO", required=True, help="Address of the Git repository to clone") + parser.add_argument("--repository", type=str, metavar="REPO", required=True, help="Address of the Git repository to clone") parser.add_argument("--branch", type=str, metavar="BRANCH", required=False, help="Branch to automatically create") parser.add_argument("--options", type=str, metavar="OPTS", required=False, help="Options to branch creation (forcepush)") parser.add_argument("--message", type=str, metavar="MSG", required=False, help="Commit message") diff --git a/ogclient/interfaceAdm/RestaurarImagenGit b/ogclient/interfaceAdm/RestaurarImagenGit index 66bda11..1feb29b 100755 --- a/ogclient/interfaceAdm/RestaurarImagenGit +++ b/ogclient/interfaceAdm/RestaurarImagenGit @@ -54,7 +54,7 @@ if __name__ == "__main__": parser.add_argument("--disk", type=int, metavar="DISK", required=True, help="Disk ID") parser.add_argument("--partition", type=int, metavar="PART", required=True, help="Disk partition") - parser.add_argument("--repo", type=str, metavar="REPO", required=True, help="Address of the Git repository to clone") + parser.add_argument("--repository", type=str, metavar="REPO", required=True, help="Address of the Git repository to clone") parser.add_argument("--branch", type=str, metavar="BRANCH", required=True, help="Branch to check out") parser.add_argument("--commit", type=str, metavar="COMMIT_ID", required=True, help="Commit to check out") parser.add_help = True From 1d4573438b22f988248d6986d5ad18b495b12d27 Mon Sep 17 00:00:00 2001 From: Vadim Troshchinskiy Date: Thu, 14 Aug 2025 16:09:08 +0200 Subject: [PATCH 06/11] refs #2506 argument fixes --- ogclient/interfaceAdm/CrearImagenGit | 6 ++++-- ogclient/interfaceAdm/ModificarImagenGit | 1 + ogclient/interfaceAdm/RestaurarImagenGit | 4 +++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/ogclient/interfaceAdm/CrearImagenGit b/ogclient/interfaceAdm/CrearImagenGit index 7c2ed0f..157c774 100755 --- a/ogclient/interfaceAdm/CrearImagenGit +++ b/ogclient/interfaceAdm/CrearImagenGit @@ -2,6 +2,7 @@ import sys import resource import logging +import argparse import ogGlobals @@ -59,7 +60,7 @@ def main(): print(f"No se pudo aumentar el límite de archivos abiertos: {e}") - parser = argparse.ArgumentParser( + parser = argparse.ArgumentParser( prog = "OpenGnsys Git Image Create", description = "Creates a git repository from a partition" ) @@ -67,6 +68,7 @@ def main(): parser.add_argument("--disk", type=int, metavar="DISK", required=True, help="Disk ID") parser.add_argument("--partition", type=int, metavar="PART", required=True, help="Disk partition") parser.add_argument("--repository", type=str, metavar="REPO", required=True, help="Address of the Git repository to clone") + parser.add_argument("--image-name", type=str, metavar="REPO", required=True, help="Name of the new image at the repository") parser.add_argument("--tag", type=str, metavar="TAG", required=False, help="Tag to automatically create") parser.add_argument("--message", type=str, metavar="MSG", required=False, help="Commit message") @@ -101,7 +103,7 @@ def main(): # repo = repositorio, oggit@server.com:/oggit # image = nombre repo - retval = create_image(args.disk, args.partition, repo, image_name, args.tag) + retval = create_image(args.disk, args.partition, args.repository, args.image_name, args.tag) diff --git a/ogclient/interfaceAdm/ModificarImagenGit b/ogclient/interfaceAdm/ModificarImagenGit index c54d1e9..b59ab3b 100755 --- a/ogclient/interfaceAdm/ModificarImagenGit +++ b/ogclient/interfaceAdm/ModificarImagenGit @@ -104,6 +104,7 @@ def main(): parser.add_argument("--disk", type=int, metavar="DISK", required=True, help="Disk ID") parser.add_argument("--partition", type=int, metavar="PART", required=True, help="Disk partition") parser.add_argument("--repository", type=str, metavar="REPO", required=True, help="Address of the Git repository to clone") + parser.add_argument("--branch", type=str, metavar="BRANCH", required=False, help="Branch to automatically create") parser.add_argument("--options", type=str, metavar="OPTS", required=False, help="Options to branch creation (forcepush)") parser.add_argument("--message", type=str, metavar="MSG", required=False, help="Commit message") diff --git a/ogclient/interfaceAdm/RestaurarImagenGit b/ogclient/interfaceAdm/RestaurarImagenGit index 1feb29b..8896703 100755 --- a/ogclient/interfaceAdm/RestaurarImagenGit +++ b/ogclient/interfaceAdm/RestaurarImagenGit @@ -55,6 +55,8 @@ if __name__ == "__main__": parser.add_argument("--disk", type=int, metavar="DISK", required=True, help="Disk ID") parser.add_argument("--partition", type=int, metavar="PART", required=True, help="Disk partition") parser.add_argument("--repository", type=str, metavar="REPO", required=True, help="Address of the Git repository to clone") + parser.add_argument("--image-name", type=str, metavar="REPO", required=True, help="Name of the new image at the repository") + parser.add_argument("--branch", type=str, metavar="BRANCH", required=True, help="Branch to check out") parser.add_argument("--commit", type=str, metavar="COMMIT_ID", required=True, help="Commit to check out") parser.add_help = True @@ -90,7 +92,7 @@ if __name__ == "__main__": device = DiskLib.ogDiskToDev(args.disk, args.partition) - og_git.cloneRepo(args.repo, destination = device, boot_device = device, ref = args.commit, branch = args.branch) + og_git.cloneRepo(args.image_name, destination = device, boot_device = device, ref = args.commit, branch = args.branch) logger.info("RestaurarImagenGit Finished.") From 012b7a5dbbee4cd2a0711118c05348aaf4bd631a Mon Sep 17 00:00:00 2001 From: Vadim Trochinsky Date: Thu, 28 Aug 2025 09:51:16 +0200 Subject: [PATCH 07/11] refs #2506 -- fix indentation --- ogclient/interfaceAdm/ModificarImagenGit | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ogclient/interfaceAdm/ModificarImagenGit b/ogclient/interfaceAdm/ModificarImagenGit index b59ab3b..25aa943 100755 --- a/ogclient/interfaceAdm/ModificarImagenGit +++ b/ogclient/interfaceAdm/ModificarImagenGit @@ -96,7 +96,7 @@ def commit_image(disk_num, partition_num, repository, branch, options, msg): def main(): - parser = argparse.ArgumentParser( + parser = argparse.ArgumentParser( prog = "OpenGnsys Git Image Modify", description = "Commits changes to a partition to a git repository" ) From cf17d765a0359eb494030278059f0ffcd3096819 Mon Sep 17 00:00:00 2001 From: Vadim Troshchinskiy Date: Thu, 28 Aug 2025 09:55:22 +0200 Subject: [PATCH 08/11] refs #2506 -- Add error handling for the case where the disk can't be found --- ogclient/interfaceAdm/CrearImagenGit | 3 +++ ogclient/interfaceAdm/ModificarImagenGit | 2 ++ ogclient/interfaceAdm/RestaurarImagenGit | 2 ++ 3 files changed, 7 insertions(+) diff --git a/ogclient/interfaceAdm/CrearImagenGit b/ogclient/interfaceAdm/CrearImagenGit index 157c774..7aa879c 100755 --- a/ogclient/interfaceAdm/CrearImagenGit +++ b/ogclient/interfaceAdm/CrearImagenGit @@ -42,6 +42,9 @@ def create_image(disk_num, partition_num, repo, image_name): og_git.progress_callback = OgProgressPrinterWeb() device = DiskLib.ogDiskToDev(disk_num, partition_num) + if device is None: + sys.exit(SystemLib.ogRaiseError([], ogGlobals.OG_ERR_FORMAT, f"Failed to translate disk {disk_num} partition {partition_num} to a device")) + if og_git.initRepo(device, image_name): return 0 else: diff --git a/ogclient/interfaceAdm/ModificarImagenGit b/ogclient/interfaceAdm/ModificarImagenGit index 25aa943..c9ac305 100755 --- a/ogclient/interfaceAdm/ModificarImagenGit +++ b/ogclient/interfaceAdm/ModificarImagenGit @@ -74,6 +74,8 @@ def commit_image(disk_num, partition_num, repository, branch, options, msg): og_git.progress_callback = OgProgressPrinterWeb() device = DiskLib.ogDiskToDev(disk_num, partition_num) + if device is None: + sys.exit(SystemLib.ogRaiseError([], ogGlobals.OG_ERR_FORMAT, f"Failed to translate disk {disk_num} partition {partition_num} to a device")) force_push = False opts = options.split(",") diff --git a/ogclient/interfaceAdm/RestaurarImagenGit b/ogclient/interfaceAdm/RestaurarImagenGit index 8896703..0fd79f2 100755 --- a/ogclient/interfaceAdm/RestaurarImagenGit +++ b/ogclient/interfaceAdm/RestaurarImagenGit @@ -91,6 +91,8 @@ if __name__ == "__main__": og_git.progress_callback = OgProgressPrinterWeb() device = DiskLib.ogDiskToDev(args.disk, args.partition) + if device is None: + sys.exit(SystemLib.ogRaiseError([], ogGlobals.OG_ERR_FORMAT, f"Failed to translate disk {args.disk} partition {args.partition} to a device")) og_git.cloneRepo(args.image_name, destination = device, boot_device = device, ref = args.commit, branch = args.branch) From 36ed2a0b5a599fd31678f0fe89d02e6662f504ed Mon Sep 17 00:00:00 2001 From: Vadim Troshchinskiy Date: Thu, 28 Aug 2025 09:57:01 +0200 Subject: [PATCH 09/11] refs #2506 -- allow options to be empty --- ogclient/interfaceAdm/ModificarImagenGit | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/ogclient/interfaceAdm/ModificarImagenGit b/ogclient/interfaceAdm/ModificarImagenGit index c9ac305..9058fb8 100755 --- a/ogclient/interfaceAdm/ModificarImagenGit +++ b/ogclient/interfaceAdm/ModificarImagenGit @@ -78,9 +78,10 @@ def commit_image(disk_num, partition_num, repository, branch, options, msg): sys.exit(SystemLib.ogRaiseError([], ogGlobals.OG_ERR_FORMAT, f"Failed to translate disk {disk_num} partition {partition_num} to a device")) force_push = False - opts = options.split(",") - if "force_push" in opts: - force_push = True + if options: + opts = options.split(",") + if "force_push" in opts: + force_push = True try: if branch: From 7f6ec263e87f58be2c3b0e0db34a3577e152a271 Mon Sep 17 00:00:00 2001 From: Vadim Troshchinskiy Date: Thu, 28 Aug 2025 13:16:04 +0200 Subject: [PATCH 10/11] refs #2506 -- argument refactoring in Crear/Modificar imagen --- ogclient/interfaceAdm/CrearImagenGit | 2 +- ogclient/interfaceAdm/ModificarImagenGit | 24 ++---------------------- 2 files changed, 3 insertions(+), 23 deletions(-) diff --git a/ogclient/interfaceAdm/CrearImagenGit b/ogclient/interfaceAdm/CrearImagenGit index 7aa879c..87c66ee 100755 --- a/ogclient/interfaceAdm/CrearImagenGit +++ b/ogclient/interfaceAdm/CrearImagenGit @@ -106,7 +106,7 @@ def main(): # repo = repositorio, oggit@server.com:/oggit # image = nombre repo - retval = create_image(args.disk, args.partition, args.repository, args.image_name, args.tag) + retval = create_image(args.disk, args.partition, args.repository, args.image_name) diff --git a/ogclient/interfaceAdm/ModificarImagenGit b/ogclient/interfaceAdm/ModificarImagenGit index 9058fb8..4e99a03 100755 --- a/ogclient/interfaceAdm/ModificarImagenGit +++ b/ogclient/interfaceAdm/ModificarImagenGit @@ -116,27 +116,7 @@ def main(): args = parser.parse_args() - - - if len(sys.argv) != 7: - import json - args = json.dumps(sys.argv) - sys.exit(SystemLib.ogRaiseError([], ogGlobals.OG_ERR_FORMAT, f"Incorrect number of arguments. Got {len(sys.argv)}. Usage: ModificarImagenGit.py disk_num partition_num repo new_branch options msg. Received args: {args}")) - - # repo - repositorio, ip address. Opcional porque oglive lo recibe como parametro de kernel - # tag - tag a crear automaticamente. Opcional, no necesitamos crear un tag siempre. - - - disk_num, partition_num, repo, branch, options, msg = sys.argv[1:7] - - print(f"Repo : {repo}") - print(f"Branch : {branch}") - print(f"Options: {options}") - print(f"Msg : {msg}") - - opengnsys_log_dir = "/opt/opengnsys/log" - ip_address = NetLib.ogGetIpAddress() - logFilePath = f"{opengnsys_log_dir}/{ip_address}.log" + logFilePath = "/var/log/opengnsys.ModificarImagenGit.log" ogLog = OgLogger() ogLog.setLevel(logging.DEBUG) @@ -156,7 +136,7 @@ def main(): logger.info("Starting ModificarImagenGit") - retval = commit_image(disk_num, partition_num, repo, branch, options, msg) + retval = commit_image(args.disk, args.partition, args.repository, args.branch, args.options, args.message) logger.info("ModificarImagenGit done, return code %i", retval) From 91be3803b6a48229a880a6ba3e669be2cd955fda Mon Sep 17 00:00:00 2001 From: Vadim Troshchinskiy Date: Thu, 28 Aug 2025 15:18:05 +0200 Subject: [PATCH 11/11] refs #2506 -- refactoring --- ogclient/lib/python3/GitLib/__init__.py | 67 +++++++------------------ 1 file changed, 18 insertions(+), 49 deletions(-) diff --git a/ogclient/lib/python3/GitLib/__init__.py b/ogclient/lib/python3/GitLib/__init__.py index b0a8352..c86afc1 100755 --- a/ogclient/lib/python3/GitLib/__init__.py +++ b/ogclient/lib/python3/GitLib/__init__.py @@ -1491,67 +1491,36 @@ class OpengnsysGitLibrary: if branch: + # We've got a nearby branch, start from that + self.logger.debug("Cloning repo %s, branch %s", repo_url, branch) repo = git.Repo.clone_from(repo_url, destination_dir, branch = branch, multi_options = [f"--separate-git-dir={real_git_dir}"], progress=self.progress_callback) + else: + # Start from main instead + self.logger.debug("Cloning repo %s", repo_url) + repo = git.Repo.clone_from(repo_url, destination_dir, multi_options = [f"--separate-git-dir={real_git_dir}"], progress=self.progress_callback) + self.logger.debug("Checking out indicated branch %s", branch) remote_branch_ref = repo.heads[branch] if branch in repo.heads: - self.logger.debug("Removing existing local branch %s", branch) - repo.delete_head(branch) + self.logger.debug("Local branch %s exists, checking it out first", branch) + repo.git.checkout(branch) - if ref: - self.logger.debug("Local branch adjusted to ref %s", ref) - local_ref = repo.create_head(branch, ref) + self.logger.debug("Resetting branch %s to ref %s", branch, ref) + repo.git.reset("--hard", ref) else: - self.logger.debug("Local branch is set to remote branch %s", remote_branch_ref) - local_ref = repo.create_head(branch, remote_branch_ref) - local_ref.set_tracking_branch(remote_branch_ref) + if ref: + self.logger.debug("Local branch adjusted to ref %s", ref) + local_ref = repo.create_head(branch, ref) + else: + self.logger.debug("Local branch is set to remote branch %s", remote_branch_ref) + local_ref = repo.create_head(branch, remote_branch_ref) + local_ref.set_tracking_branch(remote_branch_ref) self.logger.debug("Checking out local branch %s", branch) local_ref.checkout() - else: - repo = git.Repo.clone_from(repo_url, destination_dir, multi_options = [f"--separate-git-dir={real_git_dir}"], progress=self.progress_callback) - - branches_with_commit = repo.git.branch("-r", "--contains", ref).split("\n") - self.logger.debug("Branches with commit: %s", branches_with_commit) - - if len(branches_with_commit) > 0: - remote_branch = branches_with_commit[0].strip() - if "->" in remote_branch: - # Git returned something like: - # origin/HEAD -> origin/main - # - # so take the second part. - remote_branch = remote_branch.split("->")[1].strip() - - parts = remote_branch.split("/") - self.logger.info(f"Branch: {remote_branch}, parts: {parts}") - local_branch_name = parts[1] - - if local_branch_name in repo.heads: - #self.logger.debug("Removing existing local branch %s", local_branch_name) - #repo.git.reset("--hard") - #repo.delete_head(local_branch_name) - self.logger.debug("Local branch %s exists, checking it out first", local_branch_name) - repo.git.checkout(local_branch_name) - - self.logger.debug("Resetting branch %s to ref %s", local_branch_name, ref) - repo.git.reset("--hard", ref) - else: - self.logger.info("Checking out containing remote branch %s, as %s", remote_branch, local_branch_name) - repo.git.checkout("-b", local_branch_name, remote_branch) - - try: - self.logger.info("Setting upstream: %s", local_branch_name) - repo.git.branch(f"--set-upstream-to={remote_branch}", local_branch_name) - except Exception as ex: - self.logger.error("Setting upstream failed: %s", ex) - - else: - self.logger.info("Checking out REF %s", ref) - repo.git.checkout(ref) self.fs.mklostandfound(destination_dir) self._restore_metadata(destination_dir, set_device_uuids=True)