diff --git a/client/lib/engine/bin/CacheLib.py b/client/lib/engine/bin/CacheLib.py index 0a9f8c7..180684f 100755 --- a/client/lib/engine/bin/CacheLib.py +++ b/client/lib/engine/bin/CacheLib.py @@ -64,8 +64,9 @@ def ogCreateCache (ndsk=1, part=4, sizecache=0): if ENDPREVPART: break i += 1 if not ENDPREVPART: - SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_FORMAT, ndsk) - return None + ENDPREVPART=0 + #SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_FORMAT, ndsk) + #return None # Error si tamaño no está entre límites permitidos o si se solapa con la partición anterior. MINSIZE = 25000 MAXSIZE = END @@ -245,7 +246,7 @@ def ogGetCacheSize(): cachepart = ogFindCache() if cachepart is None: SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_PARTITION, ogGlobals.lang.MSG_NOCACHE) - return + return None disk, par = cachepart.split() return DiskLib.ogGetPartitionSize (disk, par) @@ -321,7 +322,7 @@ def ogMountCache(): m = FileSystemLib.ogMountFs (c[0], c[1]) if not m: SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_PARTITION, ogGlobals.lang.MSG_NOCACHE) - #return + return None return m diff --git a/client/shared/scripts/initCache.py b/client/shared/scripts/initCache.py index e60f784..9fb4ed8 100755 --- a/client/shared/scripts/initCache.py +++ b/client/shared/scripts/initCache.py @@ -1,94 +1,123 @@ +#!/usr/bin/python3 +# Scirpt de iniciación de la caché local de disco. +# Nota: se usa como base para el programa de configuración de equipos de OpenGnsys Admin). +# Formato: initCache [int_ndisk [int_npart]] {-1 | 0 | int_size} [NOMOUNT] + import sys import time import subprocess + +import ogGlobals import SystemLib import FileSystemLib import CacheLib import DiskLib -#!/usr/bin/env python3 - -def main(): +def main (NDISK, NPART, SIZE, MOUNT): TIME1 = time.time() - PROG = sys.argv[0] - EXECFORMAT = f"{PROG} [int_ndisk [int_npart]] {{-1 | 0 | int_size}} [NOMOUNT]" - args = sys.argv[1:] - if args and args[-1].upper() == "NOMOUNT": - MOUNT = 0 - args = args[:-1] - else: - MOUNT = 1 - - PARAMS = len(args) - if PARAMS == 1: - NDISK = 1 - NPART = 4 - SIZE = int(args[0]) - elif PARAMS == 2: - NDISK = int(args[0]) - NPART = 4 - SIZE = int(args[1]) - elif PARAMS == 3: - NDISK = int(args[0]) - NPART = int(args[1]) - SIZE = int(args[2]) - else: - SystemLib.ogRaiseError("OG_ERR_FORMAT", f"{EXECFORMAT}") - - if NDISK < 1 or NPART < 1: - SystemLib.ogRaiseError("OG_ERR_FORMAT", f"{EXECFORMAT}") - - if SIZE < -1: - SystemLib.ogRaiseError("OG_ERR_FORMAT", f"{EXECFORMAT}") - - if SIZE == 0: +# Si tamaño=0, no hacer nada. + if 0 == SIZE: print("No modificar la caché local.") - return - - if SIZE == -1: + return True + +# Si tamaño=-1, borrar caché. + if -1 == SIZE: print("[10] Trabajar sin caché local.") CacheLib.ogUnmountCache() CacheLib.ogDeleteCache() else: + # Si la caché actual está definida en otro disco y partición, se elimina. current_cache = CacheLib.ogFindCache() if current_cache and f"{NDISK} {NPART}" != current_cache: print("[10] Detectada otra caché, eliminarla") CacheLib.ogUnmountCache() CacheLib.ogDeleteCache() - - try: - OLDSIZE = CacheLib.ogGetCacheSize() - except ValueError: - OLDSIZE = 0 - + + # Tomamos el tamaño actual. Si no existe cache será 0. + CACHESIZE = CacheLib.ogGetCacheSize() + if not CACHESIZE: CACHESIZE = 0 ## may be None + + # Error si tamaño definido no es >0. if SIZE <= 0: - SystemLib.ogRaiseError("OG_ERR_FORMAT", f"!({SIZE}>0)") - - if SIZE != OLDSIZE: + SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_FORMAT, f"!({SIZE}>0)") + return False + + # Si no existe caché o si cambia su tamaño, crearla. + if SIZE != CACHESIZE: print("[10] Crear partición de caché local.") CacheLib.ogUnmountCache() - CacheLib.ogCreateCache(NDISK, NPART, SIZE) - DiskLib.ogUpdatePartitionTable(NDISK) - + CacheLib.ogCreateCache (NDISK, NPART, SIZE) + DiskLib.ogUpdatePartitionTable() + + # Si caché no montada y no formateada o cambia el tamaño: formatear. cache = CacheLib.ogFindCache() - if not FileSystemLib.ogIsFormated(cache) or SIZE != OLDSIZE: + if not cache: + SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_NOTCACHE, '') + return False + cache = cache.split() + if not FileSystemLib.ogIsFormated (cache[0], cache[1]) or SIZE != CACHESIZE: print("[50] Formatear caché local.") CacheLib.ogFormatCache() - print("[70] Comprobar montaje de caché local.") - if CacheLib.ogMountCache() != 0: + # Si error al montar, chequear sistema de archivos y volver a montar. + if not CacheLib.ogMountCache(): print("[80] Comprobar consistencia y volver a montar caché local.") - FileSystem.ogCheckFs(cache) - if CacheLib.ogMountCache() != 0: - sys.exit(1) - - if MOUNT == 0: + FileSystem.ogCheckFs (cache[0], cache[1]) + if not CacheLib.ogMountCache(): + return False + + # Dejar desmontada la caché si se ha solicitado. + if not MOUNT: print("[90] Dejar desmontada la caché local.") CacheLib.ogUnmountCache() - + +# Duración del proceso. TIME = time.time() - TIME1 - print(f"[100] Duración de la operación {int(TIME // 60)}m {int(TIME % 60)}s") + print (f"[100] Duración de la operación {int(TIME // 60)}m {int(TIME % 60)}s") + return True if __name__ == "__main__": - main() + PROG = sys.argv[0] + EXECFORMAT = f"{PROG} [int_ndisk [int_npart]] {{-1 | 0 | int_size}} [NOMOUNT]" + + args = sys.argv[1:] + if args and 'NOMOUNT' == args[-1].upper(): + MOUNT = False + args = args[:-1] + else: + MOUNT = True + + PARAMS = len (args) + try: + if 1 == PARAMS: + NDISK = 1 + NPART = 4 + SIZE = int (args[0]) + elif 2 == PARAMS: + NDISK = int (args[0]) + NPART = 4 + SIZE = int (args[1]) + elif 3 == PARAMS: + NDISK = int (args[0]) + NPART = int (args[1]) + SIZE = int (args[2]) + else: + SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_FORMAT, EXECFORMAT) + sys.exit (1) + except ValueError: + SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_FORMAT, EXECFORMAT) + sys.exit (1) + + # Si disco o partición no son mayores o iguales que 1, error. + if NDISK < 1 or NPART < 1: + SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_FORMAT, EXECFORMAT) + sys.exit (1) + + # Si tamaño no es numérico o tamaño<-1, error. + if SIZE < -1: + SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_FORMAT, EXECFORMAT) + sys.exit (1) + + rc = main (NDISK, NPART, SIZE, MOUNT) + sys.exit (not rc)