Compare commits

...

6 Commits

9 changed files with 238 additions and 44 deletions

View File

@ -38,21 +38,49 @@ import libarchive
import xattr
import posix1e
import blkid
from filesystem import *
from disk import *
from ntfs import *
import re
import uuid
from tqdm import tqdm
from kernel import parse_kernel_cmdline
class OgProgressPrinter(git.RemoteProgress):
from GitLib.filesystem import *
from GitLib.disk import *
from GitLib.ntfs import *
from GitLib.kernel import parse_kernel_cmdline
#import requests
def _git_op_to_string(op):
op = op & git.RemoteProgress.OP_MASK
if op == git.RemoteProgress.COMPRESSING:
return "Compressing", "Obj"
elif op == git.RemoteProgress.CHECKING_OUT:
return "Checking out", "Obj"
elif op == git.RemoteProgress.COUNTING:
return "Counting", "Obj"
elif op == git.RemoteProgress.RECEIVING:
return "Receiving", "B"
elif op == git.RemoteProgress.WRITING:
return "Writing", "B"
elif op == git.RemoteProgress.RESOLVING:
return "Resolving deltas", "Obj"
return "Unknown", "?"
class OgProgressPrinterWeb(git.RemoteProgress):
"""
A class to print progress updates for Git operations.
This class extends `git.RemoteProgress` to provide custom logging and
printing of progress updates to the standard error stream.
This version reports progress to the web UI.
Attributes:
logger (Logger): The logger instance used to log debug messages.
prev_len (int): The length of the previous status string printed.
@ -67,9 +95,89 @@ class OgProgressPrinter(git.RemoteProgress):
__del__():
Ensures a newline is printed when the instance is deleted.
"""
def __init__(self, parentLogger, use_tqdm = False):
def __init__(self):
super().__init__()
self.logger = parentLogger
#self.ogcore_ip = get_IPcore()
#self.endpoint_url = f"https://{ogcore_ip}:8443/og-repository/webhook"
self.logger = logging.getLogger(f"{__package__}.{self.__class__.__name__}")
#self.jobId = jobId
"""Total stages of the git process. This comes from Git itself."""
self.total_stages = 6
self.simple_progress = 0
self.prev_progress = -1
def _get_stage_number(self, op):
op = op & git.RemoteProgress.OP_MASK
if op == git.RemoteProgress.COUNTING:
return 0
elif op == git.RemoteProgress.COMPRESSING:
return 1
elif op == git.RemoteProgress.WRITING:
return 2
elif op == git.RemoteProgress.CHECKING_OUT:
return 3
elif op == git.RemoteProgress.RECEIVING:
return 4
elif op == git.RemoteProgress.RESOLVING:
return 5
def update(self, op_code, cur_count, max_count=None, message=""):
# This shouldn't normally fail, but if it ever does, we don't want to fail the entire
# Git operation just because we're unable to report the status.
stage_portion = (100 / self.total_stages)
if max_count:
self.simple_progress = int((stage_portion * self._get_stage_number(op_code)) + ((stage_portion / max_count) * cur_count))
if self.prev_progress != self.simple_progress:
print(f"[{self.simple_progress}]")
self.prev_progress = self.simple_progress
op_text, _ = _git_op_to_string(op_code)
self.logger.debug(f"Progress: {op_text} ({op_code}) {cur_count}/{max_count}: {message}")
#try:
# data = json.dumps({"job_id" : self.jobId, "progress" : progress})
# headers = {'Content-Type': 'application/json'}
#
# response = requests.post(self.endpoint_url, data=data, headers=headers, verify=False)
#except Exception as e:
# self.logger.error("Exception %s while trying to report progress", e)
class OgProgressPrinterConsole(git.RemoteProgress):
"""
A class to print progress updates for Git operations.
This class extends `git.RemoteProgress` to provide custom logging and
printing of progress updates to the standard error stream.
This version is made for command-line usage.
Attributes:
logger (Logger): The logger instance used to log debug messages.
prev_len (int): The length of the previous status string printed.
Methods:
__init__(parentLogger):
Initializes the OgProgressPrinter with a logger instance.
update(op_code, cur_count, max_count=None, message=""):
Updates the progress status and prints it to the standard error stream.
__del__():
Ensures a newline is printed when the instance is deleted.
"""
def __init__(self, use_tqdm = True):
super().__init__()
self.logger = logging.getLogger(f"{__package__}.{self.__class__.__name__}")
if sys.stdin.isatty() and use_tqdm:
self.progress = tqdm()
@ -82,30 +190,7 @@ class OgProgressPrinter(git.RemoteProgress):
op = op_code & git.RemoteProgress.OP_MASK
stage = op_code & git.RemoteProgress.STAGE_MASK
op_text = "Unknown"
op_unit = "?"
if op == git.RemoteProgress.COMPRESSING:
op_text = "Compressing"
op_unit = "Obj"
elif op == git.RemoteProgress.CHECKING_OUT:
op_text = "Checking out"
op_unit = "Obj"
elif op == git.RemoteProgress.COUNTING:
op_text = "Counting"
op_unit = "Obj"
elif op == git.RemoteProgress.RECEIVING:
op_text = "Receiving"
op_unit = "B"
elif op == git.RemoteProgress.WRITING:
op_text = "Writing"
op_unit = "B"
elif op == git.RemoteProgress.RESOLVING:
op_text = "Resolving deltas"
op_unit = "Obj"
op_text, op_unit = _git_op_to_string(op)
self.logger.debug(f"Progress: {op_text} ({op_code}) {cur_count}/{max_count}: {message}")
@ -281,6 +366,10 @@ class OpengnsysGitLibrary:
else:
self.logger.debug(f"Git repository: {self.repo_server}")
"""Which progress callback to use, console or web-targeted one"""
self.progress_callback = OgProgressPrinterConsole()
def _is_efi(self):
"""Determina si hemos arrancado con EFI
@ -1301,8 +1390,8 @@ class OpengnsysGitLibrary:
repo.index.commit("Initial commit")
repo.git.push("--force") # Obliterate whatever might have been there
self.logger.debug("Fetching origin")
origin.fetch(progress=OgProgressPrinter(self.logger))
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)
@ -1385,7 +1474,7 @@ class OpengnsysGitLibrary:
self.logger.info("Uploading to ogrepository")
origin.push(progress=OgProgressPrinter(self.logger))
origin.push(progress=self.progress_callback)
#repo.git.push("--set-upstream", "origin", repo.head.ref, "--force")
self.logger.info("initRepo done")
@ -1454,7 +1543,7 @@ 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=OgProgressPrinter(self.logger))
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)
@ -1547,7 +1636,7 @@ class OpengnsysGitLibrary:
origin = repo.remotes["origin"]
repo.heads.master.set_tracking_branch(origin.refs.master)
origin.push(progress=OgProgressPrinter(self.logger))
origin.push(progress=self.progress_callback)
#repo.git.push("--set-upstream", "origin", repo.head.ref, "--force") # force = True)
@ -1568,7 +1657,7 @@ class OpengnsysGitLibrary:
if origin:
self.logger.debug("Fetching from origin")
origin.fetch(progress=OgProgressPrinter(self.logger))
origin.fetch(progress=self.progress_callback)
else:
self.logger.error("Origin not found, can't fetch")

View File

@ -82,7 +82,13 @@ class DiskLibrary:
(disk, partno) = self.split_device_partition(device)
result = subprocess.run(["/usr/sbin/sfdisk", "--part-uuid", disk, str(partno)], check=True, capture_output=True, encoding='utf-8')
result = subprocess.run(["/usr/sbin/sfdisk", "--part-uuid", disk, str(partno)], check=False, capture_output=True, encoding='utf-8')
if result.returncode != 0:
# We're using a DOS partition table, no UUID.
if "failed to get partition UUID" in result.stderr:
return ""
raise subprocess.CalledProcessError(result.returncode, result.args)
return result.stdout.strip()
def set_partition_uuid(self, device, uuid):

View File

@ -6,7 +6,7 @@ import json
import blkid
import time
from ntfs import *
from GitLib.ntfs import *
@ -172,7 +172,7 @@ class FilesystemLibrary:
done = False
start_time = time.time()
timeout = 60
timeout = 60*15
while not done and (time.time() - start_time) < timeout:

View File

@ -43,13 +43,15 @@ import NetLib
from gitlib import OpengnsysGitLibrary, NTFSImplementation
from GitLib import OpengnsysGitLibrary, NTFSImplementation, OgProgressPrinterWeb
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
@ -72,7 +74,7 @@ def main():
opengnsys_log_dir = "/opt/opengnsys/log"
ip_address = NetLib.ogGetIpAddress()
logFilePath = f"{opengnsys_log_dir}/{ip_address}.CrearImagenGit.log"
logFilePath = f"{opengnsys_log_dir}/{ip_address}.log"
fileLog = logging.FileHandler(logFilePath)
fileLog.setLevel(logging.DEBUG)

View File

@ -0,0 +1,96 @@
#!/usr/bin/env python3
import os
import subprocess
import sys
import time
import resource
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
def commit_image(disk_num, partition_num, repo, image_name, 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.initRepo(device, image_name)
og_git.commit(device, msg)
og_git.push()
def main():
if len(sys.argv) != 6:
sys.exit(SystemLib.ogRaiseError([], ogGlobals.OG_ERR_FORMAT, "Incorrect number of arguments. Usage: ModificarImagenGit.py disk_num partition_num image_name repo msg"))
# 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, msg = sys.argv[1:6]
opengnsys_log_dir = "/opt/opengnsys/log"
ip_address = NetLib.ogGetIpAddress()
logFilePath = f"{opengnsys_log_dir}/{ip_address}.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 ModificarImagenGit")
retval = commit_image(disk_num, partition_num, repo, image_name, msg)
sys.exit(retval)
if __name__ == "__main__":
main()

View File

@ -39,7 +39,7 @@ import ogGlobals
import SystemLib
import DiskLib
from gitlib import OpengnsysGitLibrary, NTFSImplementation
from GitLib import OpengnsysGitLibrary, NTFSImplementation, OgProgressPrinterWeb
if __name__ == "__main__":
if len(sys.argv) < 6:
@ -55,7 +55,7 @@ if __name__ == "__main__":
opengnsys_log_dir = "/opt/opengnsys/log"
ip_address = NetLib.ogGetIpAddress()
logFilePath = f"{opengnsys_log_dir}/{ip_address}.RestaurarImagenGit.log"
logFilePath = f"{opengnsys_log_dir}/{ip_address}.log"
fileLog = logging.FileHandler(logFilePath)
fileLog.setLevel(logging.DEBUG)
@ -75,6 +75,7 @@ if __name__ == "__main__":
ntfs_impl = NTFSImplementation.NTFS3G
og_git = OpengnsysGitLibrary(ntfs_implementation = ntfs_impl)
og_git.progress_callback = OgProgressPrinterWeb()
device = DiskLib.ogDiskToDev(disk, partition)