Compare commits

...

10 Commits

3 changed files with 192 additions and 13 deletions

View File

@ -67,14 +67,16 @@ class OgProgressPrinter(git.RemoteProgress):
__del__():
Ensures a newline is printed when the instance is deleted.
"""
def __init__(self, parentLogger):
def __init__(self, parentLogger, use_tqdm = False):
super().__init__()
self.logger = parentLogger
if sys.stdin.isatty():
if sys.stdin.isatty() and use_tqdm:
self.progress = tqdm()
self.progress.miniters = 1
#self.progress.ascii = False
else:
self.progress = None
def update(self, op_code, cur_count, max_count=None, message=""):
op = op_code & git.RemoteProgress.OP_MASK
@ -105,7 +107,7 @@ class OgProgressPrinter(git.RemoteProgress):
self.logger.debug(f"Progress: {op_code} {cur_count}/{max_count}: {message}")
self.logger.debug(f"Progress: {op_text} ({op_code}) {cur_count}/{max_count}: {message}")
if max_count is None:
return
@ -185,6 +187,11 @@ class OpengnsysGitLibrary:
self.logger.setLevel(logging.DEBUG)
self.logger.debug(f"Initializing. Cache = {require_cache}, ntfs = {ntfs_implementation}")
self.logger.debug("Ensuring SSH can't ask for passwords on the console")
os.environ["SSH_ASKPASS_REQUIRE"] = "force"
os.environ["SSH_ASKPASS"] = "/bin/false"
self.fs = FilesystemLibrary(ntfs_implementation = ntfs_implementation)
self.disk = DiskLibrary()
#self.ntfs = NTFSLibrary()
@ -1211,6 +1218,8 @@ class OpengnsysGitLibrary:
repo.config_writer().add_value("core", "filemode", "false").release()
repo.config_writer().add_value("push", "autoSetupRemote", "true").release()
repo.config_writer().add_value("maintenance", "autoDetach", "false").release()
repo.config_writer().add_value("push", "followTags", "true").release()
def initRepo(self, device, repo_name):
"""
@ -1235,7 +1244,7 @@ class OpengnsysGitLibrary:
if not self.check_remote_exists(repo_name):
self.logger.error("Specified repository can't be used, aborting.")
return
return False
path = self.fs.ensure_mounted(device)
self.logger.info("Initializing repository: %s", path)
@ -1375,10 +1384,13 @@ class OpengnsysGitLibrary:
# repo.heads.master.set_tracking_branch(origin.refs.master)
self.logger.info("Uploading to ogrepository")
origin.push(progress=OgProgressPrinter(self.logger))
origin.push(progress=OgProgressPrinter(self.logger))
#repo.git.push("--set-upstream", "origin", repo.head.ref, "--force")
self.logger.info("initRepo done")
return True
def cloneRepo(self, repo_name, destination, boot_device):
"""
Clones a repository to a specified destination and sets up the bootloader.
@ -1481,6 +1493,27 @@ class OpengnsysGitLibrary:
self._restore_metadata(path, destructive_only=True)
def tag(self, path = None, device = None, tagName = None, commit = None, message = None):
"""
Create a tag in the repository
"""
self.logger.info("Creating tag %s", tagName)
if path is None:
path = self.fs.ensure_mounted(device)
self.logger.info("Creating tag %s pointing at %s", tagName, commit)
repo = git.Repo(path)
self._configure_repo(repo)
if tagName in repo.tags:
self.logger.warning("Tag already existed")
return
repo.create_tag(tagName, ref = commit, message = message)
def restoreRepo(self, path):
"""
Restore the repository to the state it had before the non-committed modifications
@ -1513,6 +1546,7 @@ class OpengnsysGitLibrary:
origin = repo.remotes["origin"]
repo.heads.master.set_tracking_branch(origin.refs.master)
origin.push(progress=OgProgressPrinter(self.logger))
#repo.git.push("--set-upstream", "origin", repo.head.ref, "--force") # force = True)

View File

@ -3,34 +3,94 @@ import os
import subprocess
import sys
import time
import resource
sys.path.insert(0, "/opt/oglive/rootfs/opt/opengnsys/lib/python3/")
sys.path.insert(0, "/opt/opengnsys/interfaceAdm/git/")
sys.path.insert(0, "/opt/opengnsys/ogrepository/oggit/lib/")
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
def create_image(disk_num, partition_num, repo, image_name):
def create_image(disk_num, partition_num, repo, image_name, tagName):
ntfs_impl = NTFSImplementation.NTFS3G
og_git = OpengnsysGitLibrary(ntfs_implementation = ntfs_impl)
device = og_git._runBashFunction("ogDiskToDev", [str(disk_num), str(partition_num)])
og_git.initRepo(device, image_name)
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():
if len(sys.argv) != 6:
sys.exit(SystemLib.ogRaiseError(OG_ERR_FORMAT, "Incorrect number of arguments"))
sys.exit(SystemLib.ogRaiseError([], ogGlobals.OG_ERR_FORMAT, "Incorrect number of arguments. Usage: CrearImagenGit.py disk_num partition_num image_name repo tag"))
# 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, repo, tag = sys.argv[1:6]
retval = create_image(disk_num, partition_num, repo, image_name)
opengnsys_log_dir = "/opt/opengnsys/log"
ip_address = NetLib.ogGetIpAddress()
logFilePath = f"{opengnsys_log_dir}/{ip_address}.CrearImagenGit.log"
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.info("Starting CrearImagenGit")
retval = create_image(disk_num, partition_num, repo, image_name, tag)
sys.exit(retval)

View File

@ -0,0 +1,85 @@
#!/usr/bin/env python3
import sys
import subprocess
import resource
import os
import logging
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 DiskLib
from gitlib import OpengnsysGitLibrary, NTFSImplementation
if __name__ == "__main__":
if len(sys.argv) < 6:
print("Usage: python RestaurarImagenGit.py <disk> <partition> <repo> <ip> <ref> <protocol>")
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]
opengnsys_log_dir = "/opt/opengnsys/log"
ip_address = NetLib.ogGetIpAddress()
logFilePath = f"{opengnsys_log_dir}/{ip_address}.RestaurarImagenGit.log"
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.info("Starting RestaurarImagenGit")
ntfs_impl = NTFSImplementation.NTFS3G
og_git = OpengnsysGitLibrary(ntfs_implementation = ntfs_impl)
device = DiskLib.ogDiskToDev(disk, partition)
og_git.cloneRepo(repo, device, device)
logger.info("RestaurarImagenGit Finished.")