#!/usr/bin/env python3 import os import subprocess import sys import time import resource import argparse soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE) try: # Usamos el mínimo entre 65536 y el límite hard disponible new_limit = min(65536, hard) resource.setrlimit(resource.RLIMIT_NOFILE, (new_limit, hard)) print(f"RLIMIT_NOFILE establecido a: {resource.getrlimit(resource.RLIMIT_NOFILE)}") except ValueError as e: print(f"No se pudo aumentar el límite de archivos abiertos: {e}") # Añadir rutas personalizadas de forma segura extra_paths = [ "/opt/opengnsys/interfaceAdm/git/", "/opt/opengnsys/ogrepository/oggit/lib/" ] # Si estás completamente seguro de que esta ruta no interfiere con la stdlib # y contiene todos los módulos necesarios, puedes añadirla AL FINAL del path. path_maybe_problematic = "/opt/oglive/rootfs/opt/opengnsys/lib/python3/" if os.path.isdir(path_maybe_problematic): sys.path.append(path_maybe_problematic) for path in extra_paths: if os.path.isdir(path): sys.path.append(path) import NetLib import ogGlobals import SystemLib import logging import DiskLib import NetLib from GitLib import OpengnsysGitLibrary, NTFSImplementation, OgProgressPrinterWeb class OgLogger(logging.StreamHandler): def emit(self, record): log_types = ["command"] log_level = "info" match(record.levelno): case logging.DEBUG: log_level = None case logging.WARNING: log_level = "warning" case logging.INFO: log_level = "info" case logging.ERROR: log_level = "error" case logging.CRITICAL: SystemLib.ogRaiseError() SystemLib.ogEcho(log_types, log_level, record.getMessage()) 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) 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(): 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("--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") parser.add_help = True 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" ogLog = OgLogger() ogLog.setLevel(logging.DEBUG) fileLog = logging.FileHandler(logFilePath) fileLog.setLevel(logging.DEBUG) formatter = logging.Formatter('%(asctime)s - %(name)24s - [%(levelname)5s] - %(message)s') fileLog.setFormatter(formatter) logger = logging.getLogger(__package__) logger.setLevel(logging.DEBUG) logger.addHandler(fileLog) logger.addHandler(ogLog) logger.info("Starting ModificarImagenGit") retval = commit_image(disk_num, partition_num, repo, branch, options, msg) logger.info("ModificarImagenGit done, return code %i", retval) sys.exit(retval) if __name__ == "__main__": main()