added scripts support
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Mathieu Broillet 2023-06-16 16:06:03 +02:00
parent fafa85c0e1
commit 9fe806882e
No known key found for this signature in database
GPG Key ID: 7D4F25BC50A0AA32
3 changed files with 59 additions and 24 deletions

View File

@ -1,7 +1,6 @@
import logging import logging
from . import creation_utils, lxc_utils from . import creation_utils, lxc_utils
from ..utils import commands_utils
from ..utils.machine import LinuxMachine from ..utils.machine import LinuxMachine
from ..utils.proxmox import ProxmoxHost from ..utils.proxmox import ProxmoxHost
@ -333,8 +332,7 @@ class LXC(LinuxMachine):
self.install_package("bash") self.install_package("bash")
logging.info("Setting up SSH for LXC") logging.info("Setting up SSH for LXC")
# self.run_script(script_path="protected/scripts/install-config-ssh.sh") lxc_utils.run_protected_script(lxc=self, script_path="protected/scripts/install-config-ssh.sh")
commands_utils.run_script(lxc=self, step={"local_path": "protected/scripts/install-config-ssh.sh"})
def run_creation(self): def run_creation(self):
""" """
@ -364,7 +362,7 @@ class LXC(LinuxMachine):
:param script_path: :param script_path:
:return: :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, def run_command(self, command: str, return_status_code: bool = False,
exception_on_exit: bool = False, exception_on_exit: bool = False,
@ -374,6 +372,11 @@ class LXC(LinuxMachine):
Run command on LXC Run command on LXC
:param command: command to run :param command: command to run
:return: command output :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}") # logging.debug(f"Running command {command} on LXC {self.lxc_id}")
@ -381,6 +384,7 @@ class LXC(LinuxMachine):
if working_directory: if working_directory:
command = f"cd {working_directory} && {command}" command = f"cd {working_directory} && {command}"
self.pve.run_command(command=f"pct exec {self.lxc_id} -- {command}", return_status_code=return_status_code, return self.pve.run_command(command=f"pct exec {self.lxc_id} -- {command}",
exception_on_exit=exception_on_exit, return_status_code=return_status_code,
exception_on_empty_stdout=exception_on_empty_stdout) exception_on_exit=exception_on_exit,
exception_on_empty_stdout=exception_on_empty_stdout)

View File

@ -1,7 +1,9 @@
import json import json
from .lxc import LXC from .lxc import LXC
from ..utils import utils
from ..utils.proxmox import ProxmoxHost from ..utils.proxmox import ProxmoxHost
from ..utils.resources_utils import get_path
lxcs = [] lxcs = []
@ -94,7 +96,7 @@ def generate_pct_command_for_lxc(lxc: LXC, create: bool = True):
f"--cores {lxc.get_cpu()} " \ f"--cores {lxc.get_cpu()} " \
f"--memory {lxc.get_memory()} " \ f"--memory {lxc.get_memory()} " \
f"--swap {lxc.get_swap()} " \ 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"--onboot {int(lxc.is_start_on_boot())} " \
f"--ostype {lxc.get_os_name()} " \ f"--ostype {lxc.get_os_name()} " \
f"--password {lxc.get_password()} " \ 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"--cores {lxc.get_cpu()} " \
f"--memory {lxc.get_memory()} " \ f"--memory {lxc.get_memory()} " \
f"--swap {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"--onboot {int(lxc.is_start_on_boot())} " \
f"--ostype {lxc.get_os_name()} " f"--ostype {lxc.get_os_name()} "
@ -119,22 +121,51 @@ def generate_pct_command_for_lxc(lxc: LXC, create: bool = True):
return pct_command 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 # Install bash if not installed
# Sometimes only ash or sh are installed, which doesn't work for some scripts # Sometimes only ash or sh are installed, which doesn't work for some scripts
if not lxc.has_program("bash"): if not lxc.has_program("bash"):
lxc.install_package("bash") lxc.install_package("bash")
# # Run local script # Run local script
# if "path" in step: if "path" in step:
# path = get_path(lxc, step["local_path"]) if "protected/" in step["path"]:
# _run_local_script_on_lxc(lxc, path) run_protected_script(lxc, step["path"])
# else:
# # Run remote script run_repo_script(lxc, step["path"])
# elif "url" in step:
# _run_remote_script_on_lxc(lxc, step["url"]) # Run remote script
# elif "url" in step:
# # Run script in LXC run_remote_script(lxc, step["url"])
# elif "lxc_path" in step:
# path = get_path(lxc, step["lxc_path"]) # Run script in LXC
# _run_script_on_lxc(lxc, path) 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")

View File

@ -89,7 +89,7 @@ class LinuxMachine():
pass pass
def run_script(self, script: str or Path): 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): def list_dir(self, directory: str or Path):
pass pass