From c6439c68992e2ec3d170396ed7136457a284b766 Mon Sep 17 00:00:00 2001 From: Mathieu Broillet Date: Wed, 14 Jun 2023 14:29:46 +0200 Subject: [PATCH] added has_file and has_directory to lxc --- src/lxc/__init__.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/lxc/__init__.py b/src/lxc/__init__.py index af2b208..a45ff8f 100644 --- a/src/lxc/__init__.py +++ b/src/lxc/__init__.py @@ -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