import os import sys import subprocess import time #!/usr/bin/env python3 # Error codes OG_ERR_NOTEXEC = 1 OG_ERR_LOCKED = 4 OG_ERR_FORMAT = 1 OG_ERR_PARTITION = 3 OG_ERR_IMAGE = 5 OG_ERR_NOTWRITE = 14 OG_ERR_NOTCACHE = 15 OG_ERR_CACHESIZE = 16 OG_ERR_REDUCEFS = 17 OG_ERR_EXTENDFS = 18 def main(): if len(sys.argv) != 5: og_raise_error(OG_ERR_FORMAT, "Incorrect number of arguments") disk_num = sys.argv[1] partition_num = sys.argv[2] image_name = sys.argv[3] repo = sys.argv[4] if sys.argv[4] else "REPO" start_time = time.time() # Load engine configurator og_engine_configurate = os.getenv('OGENGINECONFIGURATE') if not og_engine_configurate: exec(open("/opt/opengnsys/etc/engine.cfg").read()) # Clear temporary log files with open(os.getenv('OGLOGSESSION'), 'w') as f: f.write(" ") with open(os.getenv('OGLOGCOMMAND'), 'w') as f: f.write(" ") with open(f"{os.getenv('OGLOGCOMMAND')}.tmp", 'w') as f: f.write(" ") # Log start of execution og_echo("log session", f"Start {sys.argv[0]} {' '.join(sys.argv)}") # Check if called by OpenGnsys Client caller = og_get_caller() if caller != "ogAdmClient": og_raise_error(OG_ERR_NOTEXEC, f"{caller} -> {sys.argv[0]}") # Default repository value if repo == og_get_ip_address(): repo = "CACHE" if og_check_ip_address(repo) or repo == "REPO": og_unit = os.getenv('ogunit') if og_unit: og_unit = og_unit if not og_change_repo(repo, og_unit): og_raise_error(OG_ERR_NOTFOUND, f"{repo}") if repo == "REPO" and os.getenv('boot') != "admin": if cambiar_acceso("admin") > 0: sys.exit(1) og_echo("createImage", f"{disk_num} {partition_num} {repo} /{image_name}") if subprocess.call(["which", "createImageCustom"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) == 0: retval = create_image_custom(disk_num, partition_num, repo, image_name) else: retval = create_image(disk_num, partition_num, repo, image_name) if repo == "REPO" and os.getenv('boot') != "admin": cambiar_acceso("user") # Log end of execution og_echo("log session", f"End {retval}") sys.exit(retval) if __name__ == "__main__": main()