#!/usr/bin/env python3 import sys import resource import logging import NetLib import ogGlobals import SystemLib 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 create_image(disk_num, partition_num, repo, image_name, tagName): ntfs_impl = NTFSImplementation.NTFS3G og_git = OpengnsysGitLibrary(ntfs_implementation = ntfs_impl) og_git.progress_callback = OgProgressPrinterWeb() device = DiskLib.ogDiskToDev(disk_num, partition_num) if og_git.initRepo(device, image_name): return 0 #if tagName: # og_git.tag(device = device, tagName = tagName, commit = "HEAD", message = "Image created") else: return 1 def main(): 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}") parser = argparse.ArgumentParser( prog = "OpenGnsys Git Image Create", description = "Creates a git repository from a partition" ) 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("--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 args = parser.parse_args() #disk_num, partition_num, image_name, repo, tag = sys.argv[1:6] logFilePath = "/var/log/opengnsys.CrearImagenGit.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 CrearImagenGit") # TODO: # repo = repositorio, oggit@server.com:/oggit # image = nombre repo retval = create_image(args.disk, args.partition, repo, image_name, args.tag) sys.exit(retval) if __name__ == "__main__": main()