101 lines
2.8 KiB
Python
101 lines
2.8 KiB
Python
import os
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
|
|
# 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 load_engine_config():
|
|
engine_config_path = "/opt/opengnsys/etc/engine.cfg"
|
|
if os.path.exists(engine_config_path):
|
|
with open(engine_config_path) as f:
|
|
exec(f.read(), globals())
|
|
|
|
def clear_temp_logs():
|
|
open(os.getenv('OGLOGSESSION'), 'w').close()
|
|
open(os.getenv('OGLOGCOMMAND'), 'w').close()
|
|
open(f"{os.getenv('OGLOGCOMMAND')}.tmp", 'w').close()
|
|
|
|
def log_session_start(script_name, args):
|
|
ogEcho("log session", f"{os.getenv('MSG_INTERFACE_START')} {script_name} {' '.join(args)}")
|
|
|
|
def log_session_end(retval):
|
|
ogEcho("log session", f"{os.getenv('MSG_INTERFACE_END')} {retval}")
|
|
|
|
def ogEcho(*args):
|
|
print(" ".join(args))
|
|
|
|
def ogRaiseError(error_code, message):
|
|
print(f"Error {error_code}: {message}", file=sys.stderr)
|
|
return error_code
|
|
|
|
def ogGetIpAddress():
|
|
return subprocess.check_output(["hostname", "-I"]).decode().strip()
|
|
|
|
def ogCheckIpAddress(ip):
|
|
try:
|
|
subprocess.check_call(["ping", "-c", "1", ip])
|
|
return 0
|
|
except subprocess.CalledProcessError:
|
|
return 1
|
|
|
|
def ogChangeRepo(repo, unit):
|
|
# Placeholder for actual implementation
|
|
return True
|
|
|
|
def CambiarAcceso(mode):
|
|
# Placeholder for actual implementation
|
|
return 0
|
|
|
|
def create_image(disk_num, partition_num, repo, image_name):
|
|
if subprocess.call(["which", "createImageCustom"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) == 0:
|
|
return subprocess.call(["createImageCustom", disk_num, partition_num, repo, f"/{image_name}"])
|
|
else:
|
|
return subprocess.call(["createImage", disk_num, partition_num, repo, f"/{image_name}"])
|
|
|
|
def main():
|
|
if len(sys.argv) != 5:
|
|
sys.exit(ogRaiseError(OG_ERR_FORMAT, "Incorrect number of arguments"))
|
|
|
|
disk_num, partition_num, image_name, repo = sys.argv[1:5]
|
|
|
|
start_time = time.time()
|
|
|
|
load_engine_config()
|
|
clear_temp_logs()
|
|
log_session_start(sys.argv[0], sys.argv[1:])
|
|
|
|
repo = repo if repo else "REPO"
|
|
if repo == ogGetIpAddress():
|
|
repo = "CACHE"
|
|
|
|
if ogCheckIpAddress(repo) == 0 or repo == "REPO":
|
|
ogunit = os.getenv('ogunit', "")
|
|
if not ogChangeRepo(repo, ogunit):
|
|
sys.exit(ogRaiseError(OG_ERR_NOTFOUND, f"{repo}"))
|
|
|
|
if repo == "REPO" and os.getenv('boot') != "admin":
|
|
retval = CambiarAcceso("admin")
|
|
if retval > 0:
|
|
sys.exit(retval)
|
|
|
|
retval = create_image(disk_num, partition_num, repo, image_name)
|
|
|
|
if repo == "REPO" and os.getenv('boot') != "admin":
|
|
CambiarAcceso("user")
|
|
|
|
log_session_end(retval)
|
|
sys.exit(retval)
|
|
|
|
if __name__ == "__main__":
|
|
main() |