diff --git a/src/utils/creation_utils.py b/src/utils/creation_utils.py index d5a747a..3c13f46 100644 --- a/src/utils/creation_utils.py +++ b/src/utils/creation_utils.py @@ -34,7 +34,7 @@ def are_all_conditions_met(lxc): return all(result) -def run_creations_steps(lxc): +def run_steps(lxc): """ Run creation steps for an LXC @@ -46,6 +46,51 @@ def run_creations_steps(lxc): for step in creation_steps: match step["type"]: - case "file": - if type(step["path"]) is list: - pass + + # Support for scripts + case "script": + run_script(step, lxc) + + +def run_script(step, lxc): + # Run local script + if "path" in step: + path = step["path"] + + with open(path, "r") as file: + script = file.read() + lxc.run_command("'bash -s' <<'ENDSSH'\n" + script) + + # Run remote script + elif "url" in step: + url = step["url"] + lxc.has + pass + + # Run script in LXC + elif "lxc_path" in step: + lxc_path = step["lxc_path"] + pass + + path = step["path"] + if path.startswith("/global/"): + # Use global folder + full_path = "resources/scripts" + path[len("/global/"):] + else: + # Use VM/LXC folder + lxc_id = lxc.get_id() + full_path = f"resources/lxc/{lxc_id}/{path}" + + if os.path.isfile(full_path): + lxc.run_command(f"bash {full_path}") + + +def has_program(lxc, program): + """ + Check if program is installed in LXC + + :param lxc: lxc + :param program: program + :return: is program installed + """ + return lxc.run_command("which " + program, only_code=True) == 0 diff --git a/src/utils/lxc_utils.py b/src/utils/lxc_utils.py index 3139ba8..279255a 100644 --- a/src/utils/lxc_utils.py +++ b/src/utils/lxc_utils.py @@ -292,8 +292,8 @@ class LXC: def get_deploy(self): """ - Get deployements - :return: deployements + Get deployments + :return: deployments """ return self.deploy @@ -311,20 +311,6 @@ class LXC: """ return proxmox_utils.run_command_on_pve(f"pct list | awk '/running/ && /{self.lxc_id}/'") != "" - def create(self): - """ - Create LXC - - :return: - """ - if proxmox_utils.does_lxc_exist(self.lxc_id): - logging.info(f"LXC {self.lxc_id} already exists, skipping creation") - proxmox_utils.run_command_on_pve(self.get_pct_command(create=False), True) - return - else: - logging.info(f"Creating LXC {self.lxc_id}") - proxmox_utils.run_command_on_pve(self.get_pct_command(create=True), True) - def start(self): """ Start LXC @@ -336,6 +322,41 @@ class LXC: logging.info(f"Starting LXC {self.lxc_id}") proxmox_utils.run_command_on_pve(f"pct start {self.lxc_id}", True) + def create(self): + """ + Create LXC + """ + if proxmox_utils.does_lxc_exist(self.lxc_id): + logging.info(f"LXC {self.lxc_id} already exists, skipping creation") + proxmox_utils.run_command_on_pve(self.get_pct_command(create=False), True) + else: + logging.info(f"Creating LXC {self.lxc_id}") + proxmox_utils.run_command_on_pve(self.get_pct_command(create=True), True) + + def creation(self): + """ + Run the creations checks and steps + """ + + # Check if LXC is running + if not self.is_running(): + self.start() + + # Check if all creation conditions are met + if not creation_utils.creation_conditions_met(self): + logging.info(f"Not all creation conditions met for LXC {self.lxc_id}, running creation steps...") + + # Run creation steps + creation_utils.run_creation_steps(self) + + + + def deploy(self): + pass + + def has_program(self, program): + return creation_utils.has_program(self, program) + def run_command(self, command, warn_exit_status=False, only_code=False): """ Run command on LXC @@ -349,12 +370,6 @@ class LXC: return proxmox_utils.run_command_on_pve(f"pct exec {self.lxc_id} -- {command}", warn_exit_status) - def deploy(self): - pass - - def check_creation_conditions(self): - return creation_utils.are_all_conditions_met(self) - def get_pct_command(self, create=True): """ Get pct command to create/edit LXC @@ -362,6 +377,7 @@ class LXC: """ if create: + # Create command pct_command = f"pct create {self.lxc_id} {self.get_os_template()} " \ f"--hostname {self.lxc_hostname} " \ f"--cores {self.cpu} " \ @@ -376,6 +392,7 @@ class LXC: f"--rootfs volume={self.storage}:{self.disk},size={self.disk} " \ f"--unprivileged {not self.privileged}" else: + # Update command pct_command = f"pct set {self.lxc_id} " \ f"--hostname {self.lxc_hostname} " \ f"--cores {self.cpu} " \ @@ -393,6 +410,8 @@ class LXC: def get_tteck_env_variables(self): """ Get TTECK environment variables to run scripts silently + ! Deprecated for now ! + :return: environment variables """ diff --git a/src/utils/proxmox_utils.py b/src/utils/proxmox_utils.py index d373814..7de00d7 100644 --- a/src/utils/proxmox_utils.py +++ b/src/utils/proxmox_utils.py @@ -109,4 +109,4 @@ def run_command_on_pve(command, warn_exit_status=False, only_code=False): if only_code: return command.returncode - return command.stdout.rstrip() + return command.stdout.rstrip() \ No newline at end of file