reorder imports and global structure for better understanding
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Mathieu Broillet 2023-06-12 22:17:06 +02:00
parent 9dcf370a07
commit eba11df2af
Signed by: mathieu
GPG Key ID: A08E484FE95074C1
8 changed files with 93 additions and 90 deletions

View File

@ -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):
"""

View File

@ -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):

View File

@ -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"])

71
src/lxc/utils.py Normal file
View File

@ -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)

View File

@ -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():

View File

@ -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():

View File

@ -1,6 +1,6 @@
import os
from src.get_path_file import project_path
from .. import project_path
def get_path(lxc, path):