re-organised a bit the code
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Mathieu Broillet 2023-06-12 11:53:42 +02:00
parent 8939803733
commit e4188e8432
No known key found for this signature in database
GPG Key ID: 7D4F25BC50A0AA32
3 changed files with 91 additions and 27 deletions

View File

@ -34,7 +34,7 @@ def are_all_conditions_met(lxc):
return all(result) return all(result)
def run_creations_steps(lxc): def run_steps(lxc):
""" """
Run creation steps for an LXC Run creation steps for an LXC
@ -46,6 +46,51 @@ def run_creations_steps(lxc):
for step in creation_steps: for step in creation_steps:
match step["type"]: match step["type"]:
case "file":
if type(step["path"]) is list: # Support for scripts
pass 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

View File

@ -292,8 +292,8 @@ class LXC:
def get_deploy(self): def get_deploy(self):
""" """
Get deployements Get deployments
:return: deployements :return: deployments
""" """
return self.deploy return self.deploy
@ -311,20 +311,6 @@ class LXC:
""" """
return proxmox_utils.run_command_on_pve(f"pct list | awk '/running/ && /{self.lxc_id}/'") != "" 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): def start(self):
""" """
Start LXC Start LXC
@ -336,6 +322,41 @@ class LXC:
logging.info(f"Starting LXC {self.lxc_id}") logging.info(f"Starting LXC {self.lxc_id}")
proxmox_utils.run_command_on_pve(f"pct start {self.lxc_id}", True) 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): def run_command(self, command, warn_exit_status=False, only_code=False):
""" """
Run command on LXC 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) 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): def get_pct_command(self, create=True):
""" """
Get pct command to create/edit LXC Get pct command to create/edit LXC
@ -362,6 +377,7 @@ class LXC:
""" """
if create: if create:
# Create command
pct_command = f"pct create {self.lxc_id} {self.get_os_template()} " \ pct_command = f"pct create {self.lxc_id} {self.get_os_template()} " \
f"--hostname {self.lxc_hostname} " \ f"--hostname {self.lxc_hostname} " \
f"--cores {self.cpu} " \ f"--cores {self.cpu} " \
@ -376,6 +392,7 @@ class LXC:
f"--rootfs volume={self.storage}:{self.disk},size={self.disk} " \ f"--rootfs volume={self.storage}:{self.disk},size={self.disk} " \
f"--unprivileged {not self.privileged}" f"--unprivileged {not self.privileged}"
else: else:
# Update command
pct_command = f"pct set {self.lxc_id} " \ pct_command = f"pct set {self.lxc_id} " \
f"--hostname {self.lxc_hostname} " \ f"--hostname {self.lxc_hostname} " \
f"--cores {self.cpu} " \ f"--cores {self.cpu} " \
@ -393,6 +410,8 @@ class LXC:
def get_tteck_env_variables(self): def get_tteck_env_variables(self):
""" """
Get TTECK environment variables to run scripts silently Get TTECK environment variables to run scripts silently
! Deprecated for now !
:return: environment variables :return: environment variables
""" """

View File

@ -109,4 +109,4 @@ def run_command_on_pve(command, warn_exit_status=False, only_code=False):
if only_code: if only_code:
return command.returncode return command.returncode
return command.stdout.rstrip() return command.stdout.rstrip()