refs #1245 add scripts/deployImage.py
parent
1de8e854ad
commit
f0c4f7e07a
|
@ -32,7 +32,7 @@ OGLOGSESSION='/tmp/session.log'
|
|||
OGLOGCOMMAND='/tmp/command.log'
|
||||
#OGWINCHKDISK=True #Hacer chkdisk tras la clonacion
|
||||
ACTIONCACHEFULL='NONE' #Que hacer cuando la cache no tenga espacio libre. [ NONE | FORMAT ] ]
|
||||
#RESTOREPROTOCOLNOTCACHE=None #Que protocolo de restauracion usar en el caso de que no exista cache o no exista espacio sufiente. [NONE | UNICAST | MULTICAST].NONE retorna error
|
||||
RESTOREPROTOCOLNOTCACHE=None #Que protocolo de restauracion usar en el caso de que no exista cache o no exista espacio sufiente. [NONE | UNICAST | MULTICAST].NONE retorna error
|
||||
#IMGPROG='partclone'
|
||||
#IMGCOMP='lzop'
|
||||
#IMGEXT='img'
|
||||
|
|
|
@ -1,143 +1,172 @@
|
|||
#!/usr/bin/python3
|
||||
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
import subprocess
|
||||
import shutil
|
||||
|
||||
import ogGlobals
|
||||
import SystemLib
|
||||
import DiskLib
|
||||
import NetLib
|
||||
import StringLib
|
||||
import FileLib
|
||||
import ImageLib
|
||||
|
||||
#!/usr/bin/env python3
|
||||
#Descripcion:
|
||||
# Si Repositorio es el global (REPO) realiza un deploy.
|
||||
# Si Repositorio es local (CACHE) realiza un restoreImage CACHE
|
||||
# El deploy, si detecta que el cliente no tiene una CACHE o no tiene espacio suficiente consulta el engine.cfg RESTOREPROTOCOLNOCACHE
|
||||
|
||||
def main():
|
||||
PROG = os.path.basename(__file__)
|
||||
if len(sys.argv) < 5:
|
||||
SystemLib.ogRaiseError("session", OG_ERR_FORMAT, f"{MSG_FORMAT}: {PROG} REPO imagen ndisco nparticion [ UNICAST-DIRECT|UNICAST|UNICAST-CACHE|MULTICAST-DIRECT|MULTICAST|MULTICAST-CACHE|TORRENT [opciones protocolo] ]")
|
||||
sys.exit(1)
|
||||
def main (repo, imgname, disk, par, proto='UNICAST', protoopt=''):
|
||||
time1 = time.time()
|
||||
|
||||
TIME1 = time.time()
|
||||
REPO = sys.argv[1].upper() or "REPO"
|
||||
IMGNAME = sys.argv[2]
|
||||
DISK = sys.argv[3]
|
||||
PART = sys.argv[4]
|
||||
PROTO = (sys.argv[5].upper() if len(sys.argv) > 5 else "UNICAST")
|
||||
PROTOOPT = sys.argv[6] if len(sys.argv) > 6 else ""
|
||||
OGUNIT = os.getenv("ogunit", "")
|
||||
# Clear temporary file used as log track by httpdlog
|
||||
# Limpia los ficheros temporales usados como log de seguimiento para httpdlog
|
||||
with open (ogGlobals.OGLOGCOMMAND, 'w') as fd:
|
||||
fd.write (' ')
|
||||
if 'EjecutarScript' != SystemLib.ogGetCaller():
|
||||
with open (ogGlobals.OGLOGSESSION, 'w') as fd:
|
||||
fd.write ('')
|
||||
|
||||
if not OGENGINECONFIGURATE:
|
||||
exec(open("/opt/opengnsys/etc/engine.cfg").read())
|
||||
# Registro de inicio de ejecución
|
||||
SystemLib.ogEcho (['log', 'session'], None, f'[1] {ogGlobals.lang.MSG_SCRIPTS_START} {__file__} {" ".join(sys.argv)}')
|
||||
|
||||
with open(OGLOGCOMMAND, 'w') as f:
|
||||
f.write(" ")
|
||||
if SystemLib.ogGetCaller() != "EjecutarScript":
|
||||
with open(OGLOGSESSION, 'w') as f:
|
||||
f.write("")
|
||||
# Si el origen(pariticion) esta bloqueada salir.
|
||||
if FileSystemLib.ogIsLocked (disk, par):
|
||||
SystemLib.ogRaiseError ('session', ogGlobals.OG_ERR_LOCKED, f'{ogGlobals.lang.MSG_PARTITION}, {disk} {par}')
|
||||
sys.exit (1)
|
||||
|
||||
SystemLib.ogEcho("log", "session", f"[1] {MSG_SCRIPTS_START} {PROG} {' '.join(sys.argv)}")
|
||||
SystemLib.ogEcho (['log', 'session'], None, f'{ogGlobals.lang.MSG_HELP_ogUnmount} {disk} {par}')
|
||||
FileSystemLib.ogUnmount (disk, par)
|
||||
|
||||
if FileSystemLib.ogIsLocked(DISK, PART):
|
||||
sys.exit(SystemLib.ogRaiseError("session", OG_ERR_LOCKED, f"{MSG_PARTITION}, {DISK} {PART}"))
|
||||
|
||||
SystemLib.ogEcho("log", "session", f"{MSG_HELP_FileSystemLib.ogUnmount} {DISK} {PART}")
|
||||
FileSystemLib.ogUnmount(DISK, PART)
|
||||
|
||||
if REPO == NetLib.ogGetIpAddress() or REPO == "CACHE":
|
||||
MODE = "CACHE"
|
||||
# Valor por defecto para el repositorio.
|
||||
if repo == NetLib.ogGetIpAddress() or 'CACHE' == repo:
|
||||
MODE = 'CACHE'
|
||||
else:
|
||||
if DiskLib.ogCheckIpAddress(REPO) == 0 or REPO == "REPO":
|
||||
if not NetLib.ogChangeRepo(REPO, OGUNIT):
|
||||
sys.exit(SystemLib.ogRaiseError(OG_ERR_NOTFOUND, f"{REPO} {OGUNIT}"))
|
||||
MODE = "REPO"
|
||||
if StringLib.ogCheckIpAddress (repo) or 'REPO' == repo:
|
||||
if not NetLib.ogChangeRepo (repo):
|
||||
SystemLib.ogRaiseError ([], ogGlobals.OG_ERR_NOTFOUND, repo)
|
||||
sys.exit (1)
|
||||
MODE = 'REPO'
|
||||
|
||||
IMGOS = ImageLib.ogGetImageInfo(ogGetPath(MODE, f"{IMGNAME}.img"))
|
||||
if IMGOS == 1:
|
||||
sys.exit(SystemLib.ogRaiseError("session", OG_ERR_NOTFOUND, f"{REPO} {IMGNAME}"))
|
||||
elif IMGOS == 5:
|
||||
sys.exit(SystemLib.ogRaiseError("session", OG_ERR_IMAGEFILE, f"{REPO} {IMGNAME}"))
|
||||
elif IMGOS != 0:
|
||||
sys.exit(SystemLib.ogRaiseError("session", OG_ERR_GENERIC))
|
||||
#Informacioin previa de la imagen
|
||||
imgpath = FileLib.ogGetPath (MODE, f'{imgname}.img')
|
||||
imgos = ImageLib.ogGetImageInfo (imgpath)
|
||||
#if imgos == 1: sys.exit(SystemLib.ogRaiseError("session", OG_ERR_NOTFOUND, f"{repo} {imgname}"))
|
||||
#elif imgos == 5: sys.exit(SystemLib.ogRaiseError("session", OG_ERR_IMAGEFILE, f"{repo} {imgname}"))
|
||||
#elif imgos != 0: sys.exit(SystemLib.ogRaiseError("session", OG_ERR_GENERIC))
|
||||
if not imgos:
|
||||
SystemLib.ogRaiseError ('session', ogGlobals.OG_ERR_GENERIC, '')
|
||||
sys.exit (1)
|
||||
|
||||
IMGSIZE = os.path.getsize(ogGetPath(MODE, f"{IMGNAME}.img")) // 1024
|
||||
SystemLib.ogEcho("log", "session", f"[1] REPO={REPO} IMG-FILE={IMGNAME}.img SIZE={IMGSIZE} (KB) METADATA={IMGOS}")
|
||||
imgsize = os.path.getsize (imgpath) // 1024
|
||||
SystemLib.ogEcho (['log', 'session'], None, f'[1] REPO={repo} IMG-FILE={imgname}.img SIZE={imgsize} (KB) METADATA={imgos}')
|
||||
|
||||
if MODE == "CACHE":
|
||||
NEXTOPERATION = "CACHE"
|
||||
elif MODE == "REPO":
|
||||
if PROTO in ["MULTICAST-DIRECT", "UNICAST-DIRECT"]:
|
||||
NEXTOPERATION = PROTO.split('-')[0]
|
||||
elif PROTO in ["TORRENT", "TORRENT-CACHE", "MULTICAST", "MULTICAST-CACHE", "UNICAST", "UNICAST-CACHE"]:
|
||||
PROTO = PROTO.split('-')[0]
|
||||
SystemLib.ogEcho("log", "session", f"[2] updateCache {REPO} /{IMGNAME}.img {PROTO} {PROTOOPT}")
|
||||
TIME2 = time.time()
|
||||
RETVAL = updateCache(REPO, f"/{IMGNAME}.img", PROTO, PROTOOPT)
|
||||
TIME2 = time.time() - TIME2
|
||||
SystemLib.ogEcho("log", "session", f" [ ] {MSG_SCRIPTS_TIME_PARTIAL} updateCache {TIME2 // 60}m {TIME2 % 60}s")
|
||||
if RETVAL == 0:
|
||||
SystemLib.ogEcho("log", "session", "[50] updateCache (OK)")
|
||||
NEXTOPERATION = "CACHE"
|
||||
elif RETVAL in [15, 16]:
|
||||
SystemLib.ogEcho("log", "session", f"[50] {MSG_ERR_NOTCACHE} ; {MSG_ERR_CACHESIZE}")
|
||||
SystemLib.ogEcho("log", "session", f"[50] {MSG_SCRIPTS_CHECK_ENGINE}: RESTOREPROTOCOLNOTCACHE={RESTOREPROTOCOLNOTCACHE}")
|
||||
if RESTOREPROTOCOLNOTCACHE == "MULTICAST":
|
||||
NEXTOPERATION = "MULTICAST" if PROTO == "MULTICAST" else "UNICAST"
|
||||
elif RESTOREPROTOCOLNOTCACHE == "UNICAST":
|
||||
NEXTOPERATION = "UNICAST"
|
||||
elif RESTOREPROTOCOLNOTCACHE == "NONE":
|
||||
if RETVAL == 15:
|
||||
SystemLib.ogEcho("log", "session", f"[100] {MSG_ERR_NOTCACHE}")
|
||||
sys.exit(SystemLib.ogRaiseError("session", OG_ERR_NOTCACHE, "NOT CACHE"))
|
||||
elif RETVAL == 16:
|
||||
SystemLib.ogEcho("log", "session", f"[100] {MSG_ERR_CACHESIZE}")
|
||||
sys.exit(SystemLib.ogRaiseError("session", OG_ERR_CACHESIZE, "CACHE FULL"))
|
||||
elif RETVAL in [57, 60]:
|
||||
sys.exit(RETVAL)
|
||||
# Procesar repositorio.
|
||||
if 'CACHE' == MODE:
|
||||
nextoperation = 'CACHE'
|
||||
elif 'REPO' == MODE:
|
||||
if 'MULTICAST-DIRECT' == proto:
|
||||
nextoperation = 'MULTICAST'
|
||||
elif 'UNICAST-DIRECT' == proto:
|
||||
nextoperation = 'UNICAST'
|
||||
# Si protocolo es torrent|torrent-cache o multicast|multicast-cache
|
||||
elif proto in ['TORRENT', 'TORRENT-CACHE', 'MULTICAST', 'MULTICAST-CACHE', 'UNICAST', 'UNICAST-CACHE']:
|
||||
# Eliminamos CACHE o DIRECT
|
||||
proto = proto.split ('-')[0]
|
||||
SystemLib.ogEcho (['log', 'session'], None, f'[2] updateCache {repo} "/{imgname}.img" {proto} {protoopt}')
|
||||
time2 = time.time()
|
||||
retval = subprocess.run (['updateCache', repo, f'/{imgname}.img', proto, protoopt]).returncode
|
||||
time2 = time.time() - time2
|
||||
SystemLib.ogEcho (['log', 'session'], None, f' [ ] {ogGlobals.lang.MSG_SCRIPTS_TIME_PARTIAL} updateCache {int (time2/60)}m {int (time2%60)}s')
|
||||
if 0 == retval:
|
||||
SystemLib.ogEcho (['log', 'session'], None, '[50] updateCache (OK)')
|
||||
nextoperation = 'CACHE'
|
||||
elif retval in [15, 16]:
|
||||
# no se permite usar la cache (no existe(15) o no espacio sufiente (16). Se consulta engine.cfg para RESTOREPROTOCOLNOCACHE [ multicast unicast none ]
|
||||
SystemLib.ogEcho (['log', 'session'], None, f'[50] {ogGlobals.lang.MSG_ERR_NOTCACHE} ; {ogGlobals.lang.MSG_ERR_CACHESIZE}')
|
||||
SystemLib.ogEcho (['log', 'session'], None, f'[50] {ogGlobals.lang.MSG_SCRIPTS_CHECK_ENGINE}: RESTOREPROTOCOLNOTCACHE={ogGlobals.RESTOREPROTOCOLNOTCACHE}')
|
||||
if 'MULTICAST' == RESTOREPROTOCOLNOTCACHE:
|
||||
if 'MULTICAST' == proto: nextoperation = 'MULTICAST'
|
||||
elif 'TORRENT' == proto: nextoperation = 'UNICAST'
|
||||
elif 'UNICAST' == proto: nextoperation = 'UNICAST'
|
||||
elif 'UNICAST' == RESTOREPROTOCOLNOTCACHE:
|
||||
nextoperation = 'UNICAST'
|
||||
elif RESTOREPROTOCOLNOTCACHE is None:
|
||||
if 15 == retval:
|
||||
SystemLib.ogEcho (['log', 'session'], None, f'[100] {ogGlobals.lang.MSG_ERR_NOTCACHE}')
|
||||
SystemLib.ogRaiseError ('session', ogGlobals.OG_ERR_NOTCACHE, 'NOT CACHE')
|
||||
sys.exit (1)
|
||||
elif 16 == retval:
|
||||
SystemLib.ogEcho (['log', 'session'], None, f'[100] {ogGlobals.lang.MSG_ERR_CACHESIZE}')
|
||||
SystemLib.ogRaiseError ('session', ogGlobals.OG_ERR_CACHESIZE, 'CACHE FULL')
|
||||
sys.exit (1)
|
||||
elif retval in [57, 60]:
|
||||
# Time-out en la transferencia multicast (El mensaje de error está enviado)
|
||||
sys.exit (retval)
|
||||
else:
|
||||
sys.exit(RETVAL)
|
||||
# Error desconocido
|
||||
sys.exit (retval)
|
||||
else:
|
||||
sys.exit(SystemLib.ogRaiseError("session", OG_ERR_FORMAT, f"{MSG_ERR_FORMAT}, {PROTO}"))
|
||||
SystemLib.ogRaiseError ('session', ogGlobals.OG_ERR_FORMAT, f'{ogGlobals.lang.MSG_ERR_FORMAT}, {proto}')
|
||||
sys.exit (1)
|
||||
else:
|
||||
sys.exit(SystemLib.ogRaiseError("session", OG_ERR_FORMAT, f"{MSG_ERR_FORMAT}, {REPO}"))
|
||||
SystemLib.ogRaiseError ('session', ogGlobals.OG_ERR_FORMAT, f'{ogGlobals.lang.MSG_ERR_FORMAT}, {repo}')
|
||||
sys.exit (1)
|
||||
|
||||
TIME3 = time.time()
|
||||
time3 = time.time()
|
||||
|
||||
if NEXTOPERATION == "CACHE":
|
||||
PARAMS = f"CACHE {IMGNAME} {DISK} {PART}"
|
||||
elif NEXTOPERATION == "UNICAST":
|
||||
PARAMS = f"{REPO} {IMGNAME} {DISK} {PART}"
|
||||
elif NEXTOPERATION == "MULTICAST":
|
||||
PARAMS = f"{REPO} {IMGNAME} {DISK} {PART} {PROTO} {PROTOOPT}"
|
||||
# Obtener parámetros de restauración.
|
||||
if 'CACHE' == nextoperation:
|
||||
params = ['CACHE', imgname, disk, par]
|
||||
elif 'UNICAST' == nextoperation:
|
||||
params = [repo, imgname, disk, par]
|
||||
elif 'MULTICAST' == nextoperation:
|
||||
params = [repo, imgname, disk, par, proto, protoopt]
|
||||
|
||||
if os.system("which restoreImageCustom") == 0:
|
||||
SystemLib.ogEcho("log", "session", f"[55] {MSG_HELP_ogRestoreImage}: restoreImageCustom {PARAMS}")
|
||||
os.system(f"restoreImageCustom {PARAMS}")
|
||||
# Si existe, ejecuta script personalizado "restoreImageCustom"; si no, llama al genérico "restoreImage".
|
||||
if shutil.which ('restoreImageCustom'):
|
||||
SystemLib.ogEcho (['log', 'session'], None, f'[55] {ogGlobals.lang.MSG_HELP_ogRestoreImage}: restoreImageCustom {params}')
|
||||
retval = subprocess.run (['restoreImageCustom'] + params).returncode
|
||||
else:
|
||||
SystemLib.ogEcho("log", "session", f"[55] {MSG_HELP_ogRestoreImage}: restoreImage {PARAMS}")
|
||||
os.system(f"restoreImage {PARAMS}")
|
||||
RETVAL = os.system(f"restoreImage {PARAMS}")
|
||||
SystemLib.ogEcho (['log', 'session'], None, f'[55] {ogGlobals.lang.MSG_HELP_ogRestoreImage}: restoreImage {params}')
|
||||
retval = subprocess.run (['restoreImage'] + params).returncode
|
||||
|
||||
RESUMERESTOREIMAGE = os.popen(f"grep -m 1 'Total Time:' {OGLOGCOMMAND}").read()
|
||||
SystemLib.ogEcho("log", "session", f" [ ] {RESUMERESTOREIMAGE}")
|
||||
if RETVAL != 0:
|
||||
SystemLib.ogRaiseError("session", OG_ERR_IMAGE, f"{REPO} {IMGNAME}")
|
||||
if SystemLib.ogGetCaller() != "EjecutarScript":
|
||||
SystemLib.ogEcho("log", "session", f"{MSG_INTERFACE_END} {OG_ERR_IMAGE}")
|
||||
sys.exit(OG_ERR_IMAGE)
|
||||
# Mostrar resultados.
|
||||
resumerestoreimage = subprocess.run (['grep', '--max-count', '1', 'Total Time:', ogGlobals.OGLOGCOMMAND], capture_output=True, text=True).stdout
|
||||
SystemLib.ogEcho (['log', 'session'], None, f' [ ] {resumerestoreimage} ')
|
||||
# Si la transferencia ha dado error me salgo.
|
||||
if retval:
|
||||
SystemLib.ogRaiseError ('session', ogGlobals.OG_ERR_IMAGE, f'{repo} {imgname}')
|
||||
if SystemLib.ogGetCaller() != 'EjecutarScript':
|
||||
SystemLib.ogEcho (['log', 'session'], None, f'{ogGlobals.lang.MSG_INTERFACE_END} {ogGlobals.OG_ERR_IMAGE}')
|
||||
sys.exit (ogGlobals.OG_ERR_IMAGE)
|
||||
|
||||
TIME3 = time.time() - TIME3
|
||||
SystemLib.ogEcho("log", "session", f" [ ] {MSG_SCRIPTS_TIME_PARTIAL} : {TIME3 // 60}m {TIME3 % 60}s")
|
||||
time3 = time.time() - time3
|
||||
SystemLib.ogEcho (['log', 'session'], None, f' [ ] {ogGlobals.lang.MSG_SCRIPTS_TIME_PARTIAL} : {int (time3/60)}m {int (time3%60)}s')
|
||||
|
||||
if os.system("which configureOsCustom") == 0:
|
||||
SystemLib.ogEcho("log", "session", "[90] configureOsCustom")
|
||||
os.system(f"configureOsCustom {DISK} {PART} {REPO} {IMGNAME}")
|
||||
# Si existe, ejecuta script personalizado de postconfiguración "configureOsCustom"; si no, llama al genérico "configureOs".
|
||||
if shutil.which ('configureOsCustom'):
|
||||
SystemLib.ogEcho (['log', 'session'], None, '[90] configureOsCustom')
|
||||
subprocess.run (['configureOsCustom', disk, par, repo, imgname])
|
||||
else:
|
||||
SystemLib.ogEcho("log", "session", f"[90] {MSG_SCRIPTS_OS_CONFIGURE}")
|
||||
os.system(f"configureOs {DISK} {PART}")
|
||||
SystemLib.ogEcho (['log', 'session'], None, f'[90] {ogGlobals.lang.MSG_SCRIPTS_OS_CONFIGURE}')
|
||||
subprocess.run (['configureOs', disk, par])
|
||||
|
||||
TIME = time.time() - TIME1
|
||||
SystemLib.ogEcho("log", "session", f"[100] {MSG_SCRIPTS_TIME_TOTAL} {TIME // 60}m {TIME % 60}s")
|
||||
time_total = time.time() - time1
|
||||
SystemLib.ogEcho (['log', 'session'], None, f'[100] {ogGlobals.lang.MSG_SCRIPTS_TIME_TOTAL} {int (time_total/60)}m {int (time_total%60)}s')
|
||||
|
||||
if SystemLib.ogGetCaller() != "EjecutarScript":
|
||||
SystemLib.ogEcho("log", "session", f"{MSG_INTERFACE_END} {RETVAL}")
|
||||
sys.exit(RETVAL)
|
||||
# Registro de fin de ejecución
|
||||
# Si se ha llamado desde ejecutar script no lo muestro para no repetir.
|
||||
if SystemLib.ogGetCaller() != 'EjecutarScript':
|
||||
SystemLib.ogEcho (['log', 'session'], None, f'{ogGlobals.lang.MSG_INTERFACE_END} {retval}')
|
||||
sys.exit (retval)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
if __name__ == '__main__':
|
||||
if len (sys.argv) < 4:
|
||||
SystemLib.ogRaiseError ('session', ogGlobals.OG_ERR_FORMAT, f'{ogGlobals.lang.MSG_FORMAT}: {__file__} REPO imagen ndisco nparticion [ UNICAST-DIRECT|UNICAST|UNICAST-CACHE|MULTICAST-DIRECT|MULTICAST|MULTICAST-CACHE|TORRENT [opciones protocolo] ]')
|
||||
sys.exit (1)
|
||||
sys.argv[0] = sys.argv[0].upper() if sys.argv[0] else 'REPO'
|
||||
main (*sys.argv)
|
||||
|
|
Loading…
Reference in New Issue