Compare commits

...

2 Commits

Author SHA1 Message Date
e4188e8432
re-organised a bit the code
All checks were successful
continuous-integration/drone/push Build is passing
2023-06-12 11:53:42 +02:00
8939803733
add script url and path support 2023-06-12 11:10:22 +02:00
4 changed files with 96 additions and 27 deletions

View File

@ -156,7 +156,12 @@ The script step will execute a script.
"steps": [ "steps": [
{ {
"type": "script", "type": "script",
// use path for local scripts
"path": "/global/install-docker.sh" // local path (here: resources/scripts/install-docker.sh) "path": "/global/install-docker.sh" // local path (here: resources/scripts/install-docker.sh)
// or use url for remote scripts
"url": "https://xyz.abc/scripts/install-docker.sh" // remote url
// or use lxc_path for scripts inside the lxc
"lxc_path": "/root/install-docker.sh" // lxc path
} }
] ]
``` ```

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