diff --git a/src/get_path_file.py b/src/__init__.py similarity index 100% rename from src/get_path_file.py rename to src/__init__.py diff --git a/src/utils/lxc_utils.py b/src/lxc/__init__.py similarity index 90% rename from src/utils/lxc_utils.py rename to src/lxc/__init__.py index 39858e6..eb3511b 100644 --- a/src/utils/lxc_utils.py +++ b/src/lxc/__init__.py @@ -1,75 +1,7 @@ -import json import logging -from src.utils import proxmox_utils, creation_utils, lxc_commands_utils - -lxcs = [] - - -def get_all_lxcs(): - """Get all LXC objects - - Returns - ------- - list - List of all loaded LXC objects - """ - - return lxcs - - -def get_lxc(lxc_id): - """Get LXC by ID - - Parameters - ---------- - lxc_id : str - ID of the LXC to get - - Returns - ------- - LXC - LXC object - """ - - for lxc in lxcs: - if lxc.get_id() == lxc_id: - return lxc - - return None - - -def load_lxc(file, lxc_id): - """Load LXC from JSON file - - Parameters - ---------- - file : str - Path to the JSON file of the LXC to load - lxc_id : str - ID of the LXC to load - - Examples - -------- - >>> load_lxc("./resources/lxc/100/config.json", "100") - """ - - # Load JSON data - data = json.loads(file) - - # Extract values from JSON - lxc_id = lxc_id - lxc_hostname = data["lxc_hostname"] - os = data["os"] - resources = data["resources"] - network = data["network"] - options = data["options"] - creation = data["creation"] - deploy = data["deploy"] - - # Create LXC object - lxc = LXC(lxc_id, lxc_hostname, os, resources, network, options, creation, deploy) - lxcs.append(lxc) +from . import creation_utils, commands_utils +from ..utils import proxmox_utils class LXC: @@ -413,7 +345,7 @@ class LXC: :param script_path: :return: """ - return lxc_commands_utils.run_script(self, {"lxc_path": script_path}) + return commands_utils.run_script(self, {"lxc_path": script_path}) def run_command(self, command, ssh=False, warn_exit_status=False, only_code=False, working_directory=None): """ diff --git a/src/utils/lxc_commands_utils.py b/src/lxc/commands_utils.py similarity index 98% rename from src/utils/lxc_commands_utils.py rename to src/lxc/commands_utils.py index 47c6adc..7c5e3bb 100644 --- a/src/utils/lxc_commands_utils.py +++ b/src/lxc/commands_utils.py @@ -1,7 +1,7 @@ from pathlib import Path -from src.utils import proxmox_utils -from src.utils.resources_utils import get_path +from ..utils import proxmox_utils +from ..utils.resources_utils import get_path def run_script(lxc, step): diff --git a/src/utils/creation_utils.py b/src/lxc/creation_utils.py similarity index 71% rename from src/utils/creation_utils.py rename to src/lxc/creation_utils.py index 0f375c8..56ab5a9 100644 --- a/src/utils/creation_utils.py +++ b/src/lxc/creation_utils.py @@ -1,7 +1,7 @@ import logging from pathlib import Path -from src.utils import lxc_commands_utils +from . import commands_utils def are_all_conditions_met(lxc): @@ -81,32 +81,32 @@ def run_steps(lxc): # Support for scripts case "script": - lxc_commands_utils.run_script(lxc, step) + commands_utils.run_script(lxc, step) case "file_create": - lxc_commands_utils.create_file_in_lxc(lxc, step["path"], optional(step["permission"], 644)) + commands_utils.create_file_in_lxc(lxc, step["path"], optional(step["permission"], 644)) case "file_copy": - lxc_commands_utils.copy_local_file_to_lxc(lxc, step["path"], step["destination"]) + commands_utils.copy_local_file_to_lxc(lxc, step["path"], step["destination"]) case "folder": - lxc_commands_utils.create_folder_in_lxc(lxc, step["path"], optional(step["permission"], 755)) + commands_utils.create_folder_in_lxc(lxc, step["path"], optional(step["permission"], 755)) case "folder_copy": - lxc_commands_utils.copy_local_folder_to_lxc(lxc, step["path"], step["destination"]) + commands_utils.copy_local_folder_to_lxc(lxc, step["path"], step["destination"]) case "command": lxc.run_command(command=step["command"], working_directory=optional(step["working_directory"], None)) case "docker": - lxc_commands_utils.run_docker_command(lxc, step["container"], step["command"]) + commands_utils.run_docker_command(lxc, step["container"], step["command"]) case "docker_compose": - lxc_commands_utils.run_docker_compose_command(lxc, step["command"], + commands_utils.run_docker_compose_command(lxc, step["command"], optional(step["working_directory"], None)) case "git": lxc.run_command(command=f"git clone {step['url']} {step['destination']}") case "download": - lxc_commands_utils.download_file(lxc, step["url"], step["destination"]) + commands_utils.download_file(lxc, step["url"], step["destination"]) case "unzip": - lxc_commands_utils.unzip_file(lxc, step["path"], optional(step["destination"], None)) + commands_utils.unzip_file(lxc, step["path"], optional(step["destination"], None)) case "install-package": - lxc_commands_utils.install_package(lxc, step["package"]) + commands_utils.install_package(lxc, step["package"]) case "remove-package": - lxc_commands_utils.remove_package(lxc, step["package"]) + commands_utils.remove_package(lxc, step["package"]) case "start": lxc.start() case "stop": @@ -114,4 +114,4 @@ def run_steps(lxc): case "reboot": lxc.reboot() case "replace-in-files": - lxc_commands_utils.replace_in_files(lxc, step["files"], step["search"], step["replace"]) + commands_utils.replace_in_files(lxc, step["files"], step["search"], step["replace"]) diff --git a/src/lxc/utils.py b/src/lxc/utils.py new file mode 100644 index 0000000..ba82f83 --- /dev/null +++ b/src/lxc/utils.py @@ -0,0 +1,71 @@ +import json + +from . import LXC + +lxcs = [] + + +def get_all_lxcs(): + """Get all LXC objects + + Returns + ------- + list + List of all loaded LXC objects + """ + + return lxcs + + +def get_lxc(lxc_id): + """Get LXC by ID + + Parameters + ---------- + lxc_id : str + ID of the LXC to get + + Returns + ------- + LXC + LXC object + """ + + for lxc in lxcs: + if lxc.get_id() == lxc_id: + return lxc + + return None + + +def load_lxc(file, lxc_id): + """Load LXC from JSON file + + Parameters + ---------- + file : str + Path to the JSON file of the LXC to load + lxc_id : str + ID of the LXC to load + + Examples + -------- + >>> load_lxc("./resources/lxc/100/config.json", "100") + """ + + # Load JSON data + data = json.loads(file) + + # Extract values from JSON + lxc_id = lxc_id + lxc_hostname = data["lxc_hostname"] + os = data["os"] + resources = data["resources"] + network = data["network"] + options = data["options"] + creation = data["creation"] + deploy = data["deploy"] + + # Create LXC object + lxc = LXC(lxc_id, lxc_hostname, os, resources, network, options, creation, deploy) + lxcs.append(lxc) diff --git a/src/main.py b/src/main.py index 881a637..3dd8fb3 100644 --- a/src/main.py +++ b/src/main.py @@ -1,8 +1,8 @@ import logging import os -from src.get_path_file import project_path -from src.utils.lxc_utils import load_lxc, get_all_lxcs +from . import project_path +from .lxc.utils import load_lxc, get_all_lxcs def run(): diff --git a/src/utils/proxmox_utils.py b/src/utils/proxmox_utils.py index 113880f..da6d869 100644 --- a/src/utils/proxmox_utils.py +++ b/src/utils/proxmox_utils.py @@ -3,7 +3,7 @@ import logging import os import subprocess -from src.get_path_file import project_path +from .. import project_path def get_pve_version(): diff --git a/src/utils/resources_utils.py b/src/utils/resources_utils.py index e4085c4..c276bf6 100644 --- a/src/utils/resources_utils.py +++ b/src/utils/resources_utils.py @@ -1,6 +1,6 @@ import os -from src.get_path_file import project_path +from .. import project_path def get_path(lxc, path):