refs #1675 add interfaceAdm/EjecutarScript.py

bootlib
Natalia Serrano 2025-03-10 12:49:24 +01:00
parent d7fc4fde40
commit f8eaf8859a
1 changed files with 33 additions and 45 deletions

View File

@ -1,62 +1,50 @@
#!/usr/bin/env python3 #!/usr/bin/python3
import os import os
import time import time
import subprocess import subprocess
import sys import sys
import SystemLib
import ogGlobals import ogGlobals
from SystemLib import ogEcho, ogRaiseError
#sys.path.append('/opt/opengnsys/lib/engine/bin')
def main(script_path): prog = sys.argv[0]
start_time = time.time() if len (sys.argv) != 2:
print(f"Ejecutando:",script_path) print (f'Usage: {prog} <script_path>')
sys.exit (1)
# Load engine configurator from engine.cfg file. TIME1 = time.time()
engine_config_path = '/opt/opengnsys/etc/engine.cfg' script_path = sys.argv[1]
# if 'OGENGINECONFIGURATE' not in os.environ:
# with open(engine_config_path) as f:
# exec(f.read(), globals())
# Clear temporary file used as log track by httpdlog # Clear temporary file used as log track by httpdlog
with open(os.environ['OGLOGSESSION'], 'w') as f: # Limpia los ficheros temporales usados como log de seguimiento para httpdlog
f.write("") open (ogGlobals.OGLOGSESSION, 'w').close()
with open(os.environ['OGLOGCOMMAND'], 'w') as f: open (ogGlobals.OGLOGCOMMAND, 'w').close()
f.write("")
# Registro de inicio de ejecución # Registro de inicio de ejecución
SystemLib.ogEcho('log session', f"{os.environ['MSG_INTERFACE_START']} {sys.argv[0]} {' '.join(sys.argv[1:])}") ogEcho (['log', 'session'], None, f'{ogGlobals.lang.MSG_INTERFACE_START} {prog} {script_path}')
with open(os.environ['OGLOGFILE'], 'a') as log_file: with open (ogGlobals.OGLOGFILE, 'a') as logfd:
log_file.write("\n Instrucciones a ejecutar: *****************************\n" with open (script_path) as scriptfd:
with open(script_path.split()[1]) as script_file: # Obtener solo el nombre del script logfd.write ('\n Instrucciones a ejecutar: *****************************\n')
log_file.write(script_file.read()) ) logfd.write (scriptfd.read())
log_file.write("\n Salida de las instrucciones: *****************************\n") logfd.write ('\n Salida de las instrucciones: *****************************\n')
# Cambiar permisos y ejecutar el script os.chmod (script_path, 0o755)
os.chmod(script_path.split()[1], 0o755) # Si mandamos la salida a OGLOGCOMMAND reescribimos lo que manda el comando.
result = subprocess.run([sys.executable] + script_path.split(), capture_output=True, text=True) RETVAL = subprocess.run (script_path, capture_output=True, text=True).returncode
ret_val = result.returncode
with open(os.environ['OGLOGCOMMAND'], 'a') as log_command_file:
log_command_file.write(result.stdout)
log_command_file.write(result.stderr)
elapsed_time = time.time() - start_time TIME = time.time() - TIME1
if ret_val == 0: if 0 == RETVAL:
SystemLib.ogEcho('log session', f"[100] Duracion de la operacion {int(elapsed_time // 60)}m {int(elapsed_time % 60)}s") ogEcho (['log', 'session'], None, f'[100] Duracion de la operacion {int(TIME // 60)}m {int(TIME % 60)}s')
else: else:
SystemLib.ogRaiseError('log session', ret_val) ogRaiseError (['log', 'session'], RETVAL, '')
SystemLib.ogEcho('log session', 'error "Operacion no realizada"') ogEcho (['log', 'session'], 'error', 'Operacion no realizada')
# Registro de fin de ejecución # Registro de fin de ejecución
SystemLib.ogEcho('log session', f"{os.environ['MSG_INTERFACE_END']} {ret_val}") ogEcho (['log', 'session'], None, f'{ogGlobals.lang.MSG_INTERFACE_END} {RETVAL}')
sys.exit(ret_val) sys.exit (RETVAL)
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python EjecutarScript.py <script_path>")
sys.exit(1)
main(sys.argv[1])