From 9fe806882ef95f17c5e73f0a3f99070b662b9ca1 Mon Sep 17 00:00:00 2001 From: Mathieu Broillet Date: Fri, 16 Jun 2023 16:06:03 +0200 Subject: [PATCH] added scripts support --- src/lxc/lxc.py | 18 ++++++++----- src/lxc/lxc_utils.py | 63 +++++++++++++++++++++++++++++++++----------- src/utils/machine.py | 2 +- 3 files changed, 59 insertions(+), 24 deletions(-) diff --git a/src/lxc/lxc.py b/src/lxc/lxc.py index c507af9..e30a0dc 100644 --- a/src/lxc/lxc.py +++ b/src/lxc/lxc.py @@ -1,7 +1,6 @@ import logging from . import creation_utils, lxc_utils -from ..utils import commands_utils from ..utils.machine import LinuxMachine from ..utils.proxmox import ProxmoxHost @@ -333,8 +332,7 @@ class LXC(LinuxMachine): self.install_package("bash") logging.info("Setting up SSH for LXC") - # self.run_script(script_path="protected/scripts/install-config-ssh.sh") - commands_utils.run_script(lxc=self, step={"local_path": "protected/scripts/install-config-ssh.sh"}) + lxc_utils.run_protected_script(lxc=self, script_path="protected/scripts/install-config-ssh.sh") def run_creation(self): """ @@ -364,7 +362,7 @@ class LXC(LinuxMachine): :param script_path: :return: """ - return commands_utils.run_script(self, {"lxc_path": script_path}) + return self.run_command(command=f"chmod +x {script_path} && bash {script_path}") def run_command(self, command: str, return_status_code: bool = False, exception_on_exit: bool = False, @@ -374,6 +372,11 @@ class LXC(LinuxMachine): Run command on LXC :param command: command to run :return: command output + + return_status_code: bool = False, + exception_on_exit: bool = False, + exception_on_empty_stdout: bool = False, + working_directory: str = None """ # logging.debug(f"Running command {command} on LXC {self.lxc_id}") @@ -381,6 +384,7 @@ class LXC(LinuxMachine): if working_directory: command = f"cd {working_directory} && {command}" - self.pve.run_command(command=f"pct exec {self.lxc_id} -- {command}", return_status_code=return_status_code, - exception_on_exit=exception_on_exit, - exception_on_empty_stdout=exception_on_empty_stdout) + return self.pve.run_command(command=f"pct exec {self.lxc_id} -- {command}", + return_status_code=return_status_code, + exception_on_exit=exception_on_exit, + exception_on_empty_stdout=exception_on_empty_stdout) diff --git a/src/lxc/lxc_utils.py b/src/lxc/lxc_utils.py index a9fd7da..6173b01 100644 --- a/src/lxc/lxc_utils.py +++ b/src/lxc/lxc_utils.py @@ -1,7 +1,9 @@ import json from .lxc import LXC +from ..utils import utils from ..utils.proxmox import ProxmoxHost +from ..utils.resources_utils import get_path lxcs = [] @@ -94,7 +96,7 @@ def generate_pct_command_for_lxc(lxc: LXC, create: bool = True): f"--cores {lxc.get_cpu()} " \ f"--memory {lxc.get_memory()} " \ f"--swap {lxc.get_swap()} " \ - f"--net0 name=eth0,bridge={lxc.get_bridge()},ip={lxc.get_ipv4()},hwaddr={lxc.get_mac()},type=veth " \ + f"--net0 name=eth0,bridge={lxc.get_bridge()},ip={lxc.get_ipv4(netmask=True)},hwaddr={lxc.get_mac()},type=veth " \ f"--onboot {int(lxc.is_start_on_boot())} " \ f"--ostype {lxc.get_os_name()} " \ f"--password {lxc.get_password()} " \ @@ -110,7 +112,7 @@ def generate_pct_command_for_lxc(lxc: LXC, create: bool = True): f"--cores {lxc.get_cpu()} " \ f"--memory {lxc.get_memory()} " \ f"--swap {lxc.get_memory()} " \ - f"--net0 name=eth0,bridge={lxc.get_bridge()},ip={lxc.get_ipv4()},hwaddr={lxc.get_mac()},type=veth " \ + f"--net0 name=eth0,bridge={lxc.get_bridge()},ip={lxc.get_ipv4(netmask=True)},hwaddr={lxc.get_mac()},type=veth " \ f"--onboot {int(lxc.is_start_on_boot())} " \ f"--ostype {lxc.get_os_name()} " @@ -119,22 +121,51 @@ def generate_pct_command_for_lxc(lxc: LXC, create: bool = True): return pct_command -def run_script_parser(lxc: LXC, step: dict): +def run_script_step_parser(lxc: LXC, step: dict): # Install bash if not installed # Sometimes only ash or sh are installed, which doesn't work for some scripts if not lxc.has_program("bash"): lxc.install_package("bash") - # # Run local script - # if "path" in step: - # path = get_path(lxc, step["local_path"]) - # _run_local_script_on_lxc(lxc, path) - # - # # Run remote script - # elif "url" in step: - # _run_remote_script_on_lxc(lxc, step["url"]) - # - # # Run script in LXC - # elif "lxc_path" in step: - # path = get_path(lxc, step["lxc_path"]) - # _run_script_on_lxc(lxc, path) + # Run local script + if "path" in step: + if "protected/" in step["path"]: + run_protected_script(lxc, step["path"]) + else: + run_repo_script(lxc, step["path"]) + + # Run remote script + elif "url" in step: + run_remote_script(lxc, step["url"]) + + # Run script in LXC + elif "lxc_path" in step: + lxc.run_script(step["lxc_path"]) + + +def run_repo_script(lxc: LXC, script_path: str): + # Run local script + script_path = get_path(lxc, script_path) + + lxc.pve.copy_file_to_lxc(lxc, script_path, f"/tmp/pdj-temp/{script_path.name}") + lxc.run_script(f"/tmp/pdj-temp/{script_path.name}") + lxc.delete_file(f"/tmp/pdj-temp/{script_path.name}") + + +def run_protected_script(lxc: LXC, script_path: str): + script_path = get_path(lxc, script_path) + + utils.copy_local_file_to_pve(lxc.pve, script_path, f"/tmp/pdj-temp/{script_path.name}") + lxc.pve.copy_file_to_lxc(lxc, f"/tmp/pdj-temp/{script_path.name}", f"/tmp/pdj-temp/{script_path.name}") + lxc.run_script(f"/tmp/pdj-temp/{script_path.name}") + lxc.delete_file(f"/tmp/pdj-temp/{script_path.name}") + lxc.pve.delete_file(f"/tmp/pdj-temp/{script_path.name}") + + +def run_remote_script(lxc: LXC, url: str): + # Install curl if not installed + if not lxc.has_program("curl"): + lxc.install_package("curl") + + # Run remote script + lxc.run_command(f"curl -sSL {url} | bash") diff --git a/src/utils/machine.py b/src/utils/machine.py index 8603c90..8225061 100644 --- a/src/utils/machine.py +++ b/src/utils/machine.py @@ -89,7 +89,7 @@ class LinuxMachine(): pass def run_script(self, script: str or Path): - pass + return self.run_command(f"bash {script}", return_status_code=True) def list_dir(self, directory: str or Path): pass