added has_file and has_directory to lxc

This commit is contained in:
Mathieu Broillet 2023-06-14 14:29:46 +02:00
parent eb191a7a55
commit c6439c6899
No known key found for this signature in database
GPG Key ID: 7D4F25BC50A0AA32

View File

@ -1,4 +1,5 @@
import logging
from pathlib import Path
from . import creation_utils, commands_utils
from ..utils import proxmox_utils
@ -325,11 +326,13 @@ class LXC:
if not self.is_running():
self.start()
logging.info(f"Running creation checks for LXC {self.lxc_id}")
# Check if all creation conditions are met
if not creation_utils.are_all_conditions_met(self):
logging.info(f"Not all creation conditions met for LXC {self.lxc_id}, running creation steps...")
# Run creation steps
logging.info(f"Running creation steps for LXC {self.lxc_id}")
creation_utils.run_steps(self)
def deploy(self):
@ -347,6 +350,31 @@ class LXC:
elif type(program) == list:
return all((self.run_command("which " + p, return_status_code=True) == 0) for p in program)
def has_file(self, file: str or Path):
"""
Check if file exists on LXC
:param file: file or path
:return: boolean
"""
if isinstance(file, Path):
file = str(file.as_posix())
return self.run_command("test -f " + file, return_status_code=True) == 0
def has_directory(self, directory: str or Path):
"""
Check if directory exists on LXC
:param directory: directory path
:return: boolean
"""
if isinstance(directory, Path):
directory = str(directory.as_posix())
return self.run_command("test -d " + directory, return_status_code=True) == 0
def run_script(self, script_path):
"""
Run script on LXC filesystem using bash