diff --git a/src/lxc/lxc_utils.py b/src/lxc/lxc_utils.py index bb1c822..2cfebaa 100644 --- a/src/lxc/lxc_utils.py +++ b/src/lxc/lxc_utils.py @@ -179,8 +179,6 @@ def generate_pct_command_for_lxc(lxc: LXC, create: bool = True): command_params = common_params pct_command = f"pct set {lxc.id} {' '.join(command_params)}" - # TODO: add gateway4 - # f"ip6={self.ipv6},gw6={self.gateway6},trunks={self.vlan} " \ # TODO return pct_command diff --git a/src/machine/machine.py b/src/machine/machine.py index a6e43d4..b5c2996 100644 --- a/src/machine/machine.py +++ b/src/machine/machine.py @@ -324,3 +324,23 @@ class LinuxMachine: else: self.run_command(f"sed {'-i' if case_sensitive else ''} 's/{search}/{replace}/g' {path}", return_status_code=True) + + def start_service(self, service: str): + self.run_command(machine_utils.get_services_command(self.get_os_name(), "start", service), + return_status_code=True) + + def stop_service(self, service: str): + self.run_command(machine_utils.get_services_command(self.get_os_name(), "stop", service), + return_status_code=True) + + def restart_service(self, service: str): + self.run_command(machine_utils.get_services_command(self.get_os_name(), "restart", service), + return_status_code=True) + + def enable_service(self, service: str): + self.run_command(machine_utils.get_services_command(self.get_os_name(), "enable", service), + return_status_code=True) + + def disable_service(self, service: str): + self.run_command(machine_utils.get_services_command(self.get_os_name(), "disable", service), + return_status_code=True) diff --git a/src/machine/machine_utils.py b/src/machine/machine_utils.py index e41c96e..6dcd468 100644 --- a/src/machine/machine_utils.py +++ b/src/machine/machine_utils.py @@ -66,13 +66,9 @@ def get_remove_package_command(distribution: str): distribution = distribution.lower() - if "debian" in distribution: + if "debian" in distribution or "ubuntu" in distribution or "devuan" in distribution: return "apt-get remove -y" - elif "ubuntu" in distribution: - return "apt-get remove -y" - elif "centos" in distribution: - return "yum remove -y" - elif "fedora" in distribution: + elif "centos" in distribution or "fedora" in distribution: return "yum remove -y" elif "gentoo" in distribution: return "emerge -C" @@ -80,11 +76,70 @@ def get_remove_package_command(distribution: str): return "apk del" elif "archlinux" in distribution: return "pacman -R --noconfirm" - elif "devuan" in distribution: - return "apt-get remove -y" elif "nixos" in distribution: return "nix-env -e" elif "opensuse" in distribution: return "zypper remove -y" else: raise Exception(f"Unsupported distribution: {distribution}") + + +def get_services_command(distribution, operation: str, service: str): + """Retrieve the correct command to start/stop/restart a service based on the distribution specified + It supports all the distribution supported by Proxmox VE (from the pct command documentation). + + Parameters + ---------- + distribution: str + Name of the distribution as specific in the proxmox pct command documentation + operation: str + Operation to perform on the service, it can be start, stop or restart + service: str + Name of the service to start/stop/restart + + See Also + -------- + https://pve.proxmox.com/pve-docs/pct.1.html + + Returns + ------- + str + Command to start/stop/restart the service + """ + + distribution = distribution.lower() + operation = operation.lower() + + if "debian" in distribution or \ + "ubuntu" in distribution or \ + "devuan" in distribution or \ + "centos" in distribution or \ + "fedora" in distribution or \ + "archlinux" in distribution or \ + "opensuse" in distribution or \ + "nixos" in distribution: + if operation == "start": + return f"systemctl start {service}" + elif operation == "stop": + return f"systemctl stop {service}" + elif operation == "restart": + return f"systemctl restart {service}" + elif operation == "enable": + return f"systemctl enable {service}" + elif operation == "disable": + return f"systemctl disable {service}" + else: + raise Exception(f"Unsupported operation: {operation}") + elif "gentoo" in distribution or "alpine" in distribution: + if operation == "start": + return f"rc-service {service} start" + elif operation == "stop": + return f"rc-service {service} stop" + elif operation == "restart": + return f"rc-service {service} restart" + elif operation == "enable": + return f"rc-update add {service} default" + elif operation == "disable": + return f"rc-update del {service} default" + else: + raise Exception(f"Unsupported operation: {operation}") diff --git a/src/utils/steps_utils.py b/src/utils/steps_utils.py index 2aa25f6..4d06fe2 100644 --- a/src/utils/steps_utils.py +++ b/src/utils/steps_utils.py @@ -39,8 +39,12 @@ def _run_folder_create_step(linux_machine, step): def _run_folder_copy_step(linux_machine, step): - linux_machine.pve.copy_folder_to_lxc(linux_machine, get_path(linux_machine, step["path"]), step["destination"], - step.get("permission", 644), step.get("owner", "root")) + if "LXC" in str(linux_machine.__class__): + + linux_machine.pve.copy_folder_to_lxc(linux_machine, get_path(linux_machine, step["path"]), step["destination"], + step.get("permission", 644), step.get("owner", "root")) + else: + logging.warning(f"Folder copy step only supported on LXCs") def _run_command_step(linux_machine, step): @@ -76,40 +80,42 @@ def _run_remove_package_step(linux_machine, step): linux_machine.remove_package(step["package"]) -def _run_start_step(linux_machine, step): +def _run_start_step(linux_machine): linux_machine.start() -def _run_stop_step(linux_machine, step): +def _run_stop_step(linux_machine): linux_machine.stop() -def _run_reboot_step(linux_machine, step): +def _run_reboot_step(linux_machine): linux_machine.reboot() def _run_service_start_step(linux_machine, step): - pass + linux_machine.start_service(step["service"]) def _run_service_stop_step(linux_machine, step): - pass + linux_machine.stop_service(step["service"]) def _run_service_restart_step(linux_machine, step): - pass + linux_machine.restart_service(step["service"]) def _run_service_enable_step(linux_machine, step): - pass + linux_machine.enable_service(step["service"]) def _run_service_disable_step(linux_machine, step): - pass + linux_machine.disable_service(step["service"]) def _run_replace_in_file_step(linux_machine, step): - pass + # TODO : improve this + linux_machine.replace_in_files(step["path"], step["search"], step["replace"], + case_sensitive=step.get("case_sensitive", False)) def _run_unzip_step(linux_machine, step): @@ -159,11 +165,11 @@ def run_steps(linux_machine: LinuxMachine, steps: dict): case "remove_package": _run_remove_package_step(linux_machine, step) case "start": - _run_start_step(linux_machine, step) + _run_start_step(linux_machine) case "stop": - _run_stop_step(linux_machine, step) + _run_stop_step(linux_machine) case "reboot": - _run_reboot_step(linux_machine, step) + _run_reboot_step(linux_machine) case "service_start": _run_service_start_step(linux_machine, step) case "service_stop":