From 424248b35394af20da95f0fb7ef0514f267321db Mon Sep 17 00:00:00 2001 From: Natalia Serrano Date: Tue, 28 Jan 2025 18:05:37 +0100 Subject: [PATCH] refs #1333 add ogCreateImage --- client/lib/engine/bin/FileSystemLib.py | 8 +++- client/lib/engine/bin/ImageLib.py | 58 ++++++++++++++++++++++++++ client/shared/functions/ogCreateImage | 37 ++++++++++++++++ client/shared/functions/ogUnlockImage | 30 +++++++++++++ 4 files changed, 131 insertions(+), 2 deletions(-) create mode 100755 client/shared/functions/ogCreateImage create mode 100755 client/shared/functions/ogUnlockImage diff --git a/client/lib/engine/bin/FileSystemLib.py b/client/lib/engine/bin/FileSystemLib.py index 250efcb..aed53e7 100755 --- a/client/lib/engine/bin/FileSystemLib.py +++ b/client/lib/engine/bin/FileSystemLib.py @@ -516,7 +516,10 @@ def ogLockPartition (disk, par): if not PART: return LOCKFILE = f"/var/lock/lock{PART.replace('/', '-')}" - open(LOCKFILE, 'w').close() + try: + open (LOCKFILE, 'w').close() + except: + return False return True @@ -781,7 +784,8 @@ def ogUnlockPartition (disk, par): if not PART: return LOCKFILE = f"/var/lock/lock{PART.replace('/', '-')}" - os.remove(LOCKFILE) + if os.path.exists (LOCKFILE): + os.remove (LOCKFILE) #/** diff --git a/client/lib/engine/bin/ImageLib.py b/client/lib/engine/bin/ImageLib.py index fbdeb74..c8af204 100644 --- a/client/lib/engine/bin/ImageLib.py +++ b/client/lib/engine/bin/ImageLib.py @@ -13,6 +13,7 @@ import SystemLib import ogGlobals import FileLib import CacheLib +import NetLib ## ProtocolLib.ogUcastSyntax() calls ogCreateImageSyntax() ## in ogCreateImageSyntax(), param2 may contain a pipe or may be empty @@ -216,6 +217,52 @@ def ogRestoreImageSyntax (imgfile, part, tool=None, level=None): #@exception OG_ERR_IMAGE error al crear la imagen del sistema. #@todo Comprobaciones, control de errores, definir parámetros, etc. #*/ ## +def ogCreateImage (disk, par, container, imgfile, tool='partclone', level='gzip'): + PART = DiskLib.ogDiskToDev (disk, par) + if not PART: return None + + if FileSystemLib.ogIsLocked (disk, par): + SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_LOCKED, f'{ogGlobals.lang.MSG_ERR_LOCKED} {disk}, {par}') + return None + + imgtype = 'img' # Extensión genérica de imágenes. + imgdir = FileLib.ogGetParentPath (src=container, file=imgfile) + if not imgdir: + SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_NOTFOUND, f'{container} {imgfile}') + return None + + bn = os.path.basename (imgfile) + IMGFILE = f'{imgdir}/{bn}.{imgtype}' + if ogIsImageLocked (IMGFILE): + SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_LOCKED, f'{ogGlobals.lang.MSG_IMAGE} {container}, {imgfile}') + return None + +# Generar la instruccion a ejecutar antes de aplicar los bloqueos. + program = ogCreateImageSyntax (PART, IMGFILE, tool=tool, level=level) +# Desmontar partición, bloquear partición e imagen. + FileSystemLib.ogUnmount (disk, par) + if not FileSystemLib.ogLock (disk, par): + return None + if not ogLockImage (container, f'{imgfile}.{imgtype}'): + return None + +# Crear Imagen. + #trap + p = subprocess.run (program, shell=True, check=True) + errcode = p.returncode + if 0 == errcode: + i = ogGetImageInfo (IMGFILE) + h = NetLib.ogGetHostname() + with open (f'{IMGFILE}.info', 'w') as fd: + fd.write (f'{i}:{h}\n') + else: + SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_IMAGE, f'{disk} {par} {IMGFILE}') + if os.path.exists (IMGFILE): + os.unlink (IMGFILE) + + FileSystemLib.ogUnlock (disk, par) + ogUnlockImage (container, f'{imgfile}.{imgtype}') + return errcode #/** @@ -371,6 +418,7 @@ def ogLockImage (container=None, imgfile=None): try: bn = os.path.basename (imgfile) open (f'{imgdir}/{bn}.lock', 'w').close() + return True except: SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_NOTWRITE, f'{container} {imgfile}') return None @@ -507,6 +555,16 @@ def ogRestoreImage (repo, imgpath, disk, par): #@note Se elimina el fichero de bloqueo con extensión .lock #@exception OG_ERR_FORMAT formato incorrecto. #*/ ## +#ogUnlockImage REPO /cucu.img +def ogUnlockImage (container=None, imgfile=None): + f = f'{imgfile}.lock' + if container: + p = FileLib.ogGetPath (src=container, file=f) + else: + p = FileLib.ogGetPath (file=f) + + if os.path.exists (p): + os.unlink (p) #/** diff --git a/client/shared/functions/ogCreateImage b/client/shared/functions/ogCreateImage new file mode 100755 index 0000000..14c9709 --- /dev/null +++ b/client/shared/functions/ogCreateImage @@ -0,0 +1,37 @@ +#!/usr/bin/python3 + +import sys +import argparse +from SystemLib import ogHelp +from ImageLib import ogCreateImage + +if 2 == len (sys.argv) and 'help' == sys.argv[1]: + #parser.print_help() sale en inglés aunque la locale indique otra cosa + ogHelp ('ogCreateImage', 'ogCreateImage int_ndisk int_npart str_repo path_image', ['ogCreateImage 1 1 REPO /aula1/win7']) + sys.exit (0) + +parser = argparse.ArgumentParser (add_help=False) +if 5 == len (sys.argv): + parser.add_argument ('disk') + parser.add_argument ('par') + parser.add_argument ('container') + parser.add_argument ('imgfile') +elif 7 == len (sys.argv): + parser.add_argument ('disk') + parser.add_argument ('par') + parser.add_argument ('container') + parser.add_argument ('imgfile') + parser.add_argument ('tool') + parser.add_argument ('level') + +args = parser.parse_args() + +if 5 == len (sys.argv): + ret = ogCreateImage (args.disk, args.par, args.container, args.imgfile) +elif 7 == len (sys.argv): + ret = ogCreateImage (args.disk, args.par, args.container, args.imgfile, args.tool, args.level) + +if ret is not None: + if ret == True: sys.exit (0) + elif ret == False: sys.exit (1) + else: print (ret) diff --git a/client/shared/functions/ogUnlockImage b/client/shared/functions/ogUnlockImage new file mode 100755 index 0000000..c0f5196 --- /dev/null +++ b/client/shared/functions/ogUnlockImage @@ -0,0 +1,30 @@ +#!/usr/bin/python3 + +import sys +import argparse +from SystemLib import ogHelp +from ImageLib import ogUnlockImage + +if 2 == len (sys.argv) and 'help' == sys.argv[1]: + #parser.print_help() sale en inglés aunque la locale indique otra cosa + ogHelp ('ogUnlockImage', 'ogUnlockImage [str_repo] path_image', ['ogUnlockImage /opt/opengnsys/images/aula1/win7.img', 'ogUnlockImage REPO /aula1/win7.img']) + sys.exit (0) + +parser = argparse.ArgumentParser (add_help=False) +if 2 == len (sys.argv): + parser.add_argument ('file') +elif 3 == len (sys.argv): + parser.add_argument ('container') + parser.add_argument ('file') + +args = parser.parse_args() + +if 2 == len (sys.argv): + ret = ogUnlockImage (imgfile=args.file) +elif 3 == len (sys.argv): + ret = ogUnlockImage (container=args.container, imgfile=args.file) + +if ret is not None: + if ret == True: sys.exit (0) + elif ret == False: sys.exit (1) + else: print (ret)