add services steps support
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Mathieu Broillet 2023-06-23 10:01:52 +02:00
parent a503ad16c0
commit 6ae2d4ded1
No known key found for this signature in database
GPG Key ID: 7D4F25BC50A0AA32
4 changed files with 103 additions and 24 deletions

View File

@ -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

View File

@ -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)

View File

@ -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}")

View File

@ -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":