Compare commits

..

No commits in common. "e4188e8432095cde053f335199a98b70ce492e00" and "70a7368f7a326c39c5536b6ffe381c4998c833b9" have entirely different histories.

4 changed files with 27 additions and 96 deletions

View File

@ -156,12 +156,7 @@ The script step will execute a script.
"steps": [
{
"type": "script",
// use path for local scripts
"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)
def run_steps(lxc):
def run_creations_steps(lxc):
"""
Run creation steps for an LXC
@ -46,51 +46,6 @@ def run_steps(lxc):
for step in creation_steps:
match step["type"]:
# 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
case "file":
if type(step["path"]) is list:
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):
"""
Get deployments
:return: deployments
Get deployements
:return: deployements
"""
return self.deploy
@ -311,6 +311,20 @@ class LXC:
"""
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):
"""
Start LXC
@ -322,41 +336,6 @@ class LXC:
logging.info(f"Starting LXC {self.lxc_id}")
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):
"""
Run command on LXC
@ -370,6 +349,12 @@ class LXC:
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):
"""
Get pct command to create/edit LXC
@ -377,7 +362,6 @@ class LXC:
"""
if create:
# Create command
pct_command = f"pct create {self.lxc_id} {self.get_os_template()} " \
f"--hostname {self.lxc_hostname} " \
f"--cores {self.cpu} " \
@ -392,7 +376,6 @@ class LXC:
f"--rootfs volume={self.storage}:{self.disk},size={self.disk} " \
f"--unprivileged {not self.privileged}"
else:
# Update command
pct_command = f"pct set {self.lxc_id} " \
f"--hostname {self.lxc_hostname} " \
f"--cores {self.cpu} " \
@ -410,8 +393,6 @@ class LXC:
def get_tteck_env_variables(self):
"""
Get TTECK environment variables to run scripts silently
! Deprecated for now !
:return: environment variables
"""