#!/usr/bin/env python3 import os import sys import subprocess import SystemLib # Load engine configurator from engine.cfg file. og_engine_configurate = os.getenv('OGENGINECONFIGURATE') if not og_engine_configurate: with open('/opt/opengnsys/client/etc/engine.cfg') as f: for line in f: if '=' in line: key, value = line.strip().split('=', 1) os.environ[key] = value print(f"{key}={value}") # Clear temporary file used as log track by httpdlog # Limpia los ficheros temporales usados como log de seguimiento para httpdlog og_log_session = os.getenv('OGLOGSESSION') og_log_command = os.getenv('OGLOGCOMMAND') if og_log_session: # Check if the file exists, if not create it if not os.path.exists(og_log_session): os.makedirs(os.path.dirname(og_log_session), exist_ok=True) with open(og_log_session, 'w') as f: f.write(" ") else: with open(og_log_session, 'w') as f: f.write(" ") print("og_log_command", og_log_command) if og_log_command: with open(og_log_command, 'w') as f: f.write(" ") with open(f"{og_log_command}.tmp", 'w') as f: f.write(" ") print("og_log_session", og_log_session) # Registro de inicio de ejecución def SystemLib.ogEcho(log_type, message): # Implement the logging function here pass print("os.getenv('MSG_INTERFACE_START')", os.getenv('MSG_INTERFACE_START')) msg_interface_start = os.getenv('MSG_INTERFACE_START') if msg_interface_start: SystemLib.ogEcho('log', f"session {msg_interface_start} {__name__} {' '.join(os.sys.argv[1:])}") # Solo ejecutable por OpenGnsys Client. path = os.getenv('PATH') if path: os.environ['PATH'] = f"{path}:{os.path.dirname(__name__)}" prog = os.path.basename(__name__) print("prog", prog) # Captura de parámetros (se ignora el 1er parámetro y se eliminan espacios y tabuladores). param = ''.join(sys.argv[2:]).replace(' ', '').replace('\t', '') print("param", param) # Activate browser to see progress browser_command = ["/opt/opengnsys/bin/browser", "-qws", "http://localhost/cgi-bin/httpd-log.sh"] coproc = subprocess.Popen(browser_command) # Read the two blocks of parameters, separated by '!' tbprm = param.split('!') pparam = tbprm[0] # General disk parameters sparam = tbprm[1] # Partitioning and formatting parameters # Take disk and cache values, separated by '*' tbprm = pparam.split('*') for item in tbprm: if '=' in item: key, value = item.split('=', 1) os.environ[key] = value # Error if disk parameter (dis) is not defined if 'dis' not in os.environ: sys.exit(int(os.getenv('OG_ERR_FORMAT', 1))) # Take partition distribution values, separated by '%' cfg = [] # Configuration values tbp = [] # Partition table tbf = [] # Formatting table tbprm = sparam.split('%') maxp = 0 for item in tbprm: cfg = item.split('*') for c in cfg: if '=' in c: key, value = c.split('=', 1) os.environ[key] = value if os.getenv('cpt') != "CACHE": tbp.append(f"{os.getenv('cpt')}:{os.getenv('tam')}") if os.getenv('ope') == '1': if os.getenv('cpt') not in ["EMPTY", "EXTENDED", "LINUX-LVM", "LVM", "ZPOOL"]: tbf.append(os.getenv('sfi')) maxp = max(maxp, int(os.getenv('par', 0))) # Process # Current cache size cache_size = subprocess.check_output(["ogGetCacheSize"]).strip() # Unmount all partitions and cache subprocess.run(["ogUnmountAll", os.getenv('dis')], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) subprocess.run(["ogUnmountCache"]) # Delete partition table if not MSDOS if subprocess.check_output(["ogGetPartitionTableType", "1"]).strip() != b'MSDOS': subprocess.run(["ogDeletePartitionTable", os.getenv('dis')]) subprocess.run(["ogExecAndLog", "COMMAND", "ogUpdatePartitionTable", os.getenv('dis')]) subprocess.run(["ogCreatePartitionTable", os.getenv('dis'), "MSDOS"]) # Initialize cache if "CACHE" in sparam: subprocess.run(["ogExecAndLog", "COMMAND", "initCache", os.getenv('tch')]) # Define partitioning subprocess.run(["ogExecAndLog", "COMMAND", "ogCreatePartitions", os.getenv('dis')] + tbp) if subprocess.run(["ogExecAndLog", "COMMAND", "ogUpdatePartitionTable", os.getenv('dis')]).returncode != 0: coproc.kill() sys.exit(int(subprocess.check_output(["ogRaiseError", "session", "log", os.getenv('OG_ERR_GENERIC', '1'), f"ogCreatePartitions {os.getenv('dis')} {' '.join(tbp)}"]))) # Format partitions for par in range(1, maxp + 1): if tbf[par] == "CACHE": if cache_size == os.getenv('tch'): subprocess.run(["ogExecAndLog", "COMMAND", "ogFormatCache"]) elif tbf[par]: if subprocess.run(["ogExecAndLog", "COMMAND", "ogFormatFs", os.getenv('dis'), str(par), tbf[par]]).returncode != 0: coproc.kill() sys.exit(int(subprocess.check_output(["ogRaiseError", "session", "log", os.getenv('OG_ERR_GENERIC', '1'), f"ogFormatFs {os.getenv('dis')} {par} {tbf[par]}"]))) retval = 0 subprocess.run(["ogEcho", "log", "session", f"{os.getenv('MSG_INTERFACE_END')} {retval}"]) # Return coproc.kill() sys.exit(0)