From 350fafbfe0ca776ef9e481f57c5043772ef9f5f9 Mon Sep 17 00:00:00 2001 From: Mathieu Broillet Date: Thu, 15 Jun 2023 16:55:40 +0200 Subject: [PATCH] fixed running from repo remotely --- src/lxc/lxc_utils.py | 19 ++++++++++++------- src/main.py | 11 +++++------ src/utils/proxmox.py | 42 ++++++++++++++++++++++++------------------ 3 files changed, 41 insertions(+), 31 deletions(-) diff --git a/src/lxc/lxc_utils.py b/src/lxc/lxc_utils.py index 113b8c9..797feff 100644 --- a/src/lxc/lxc_utils.py +++ b/src/lxc/lxc_utils.py @@ -1,7 +1,7 @@ import json -from pathlib import Path from .lxc import LXC +from ..utils.proxmox import ProxmoxHost lxcs = [] @@ -10,23 +10,28 @@ def get_all_lxcs(): return lxcs -def load_lxc(lxc_id: int, filepath: Path): +def load_lxc(lxc_id: int, content: str or bytes, pve: ProxmoxHost): """Load LXC from JSON file Parameters ---------- lxc_id : int ID of the LXC to load - filepath : pathlib.Path - Path object to the JSON file of the LXC to load + content : str or bytes + Content of the JSON file + pve : ProxmoxHost + Proxmox host where the LXC is/will go on Examples -------- - >>> load_lxc(100, Path("/resources/lxc/100/config.json")) + >>> load_lxc(100, '{ }') """ + if isinstance(content, bytes): + content = content.decode("utf-8") + # Load JSON data - data = json.loads(filepath.read_text()) + data = json.loads(content) # Extract values from JSON lxc_id = lxc_id @@ -39,7 +44,7 @@ def load_lxc(lxc_id: int, filepath: Path): deploy = data["deploy"] # Create LXC object - lxc = LXC(lxc_id, lxc_hostname, os, resources, network, options, creation, deploy) + lxc = LXC(lxc_id, lxc_hostname, os, resources, network, options, creation, deploy, pve) lxcs.append(lxc) diff --git a/src/main.py b/src/main.py index 79dab93..d53cd23 100644 --- a/src/main.py +++ b/src/main.py @@ -1,7 +1,6 @@ import logging -from pathlib import PosixPath, PurePosixPath, Path +from pathlib import Path -from . import project_path from .lxc.lxc_utils import load_lxc, get_all_lxcs from .utils import git_utils from .utils.proxmox import ProxmoxHost @@ -35,13 +34,13 @@ def run(args): # Go through each LXC file for resource in resources: - if resource.name == "lxc": + if resource == "lxc": # Read all files in the LXC directory - lxc_folders = PosixPath(project_path).joinpath("resources", "lxc").glob("*") + lxc_folders = pve.list_dir(Path(args.path).joinpath(resource)) for lxc_folder in lxc_folders: - lxc_file = PosixPath(project_path).joinpath("resources", "lxc", lxc_folder, "config.json") + lxc_file_content = pve.read_file(Path(args.path).joinpath(resource, lxc_folder, "config.json")) - load_lxc(filepath=lxc_file, lxc_id=int(lxc_folder.name)) + load_lxc(content=lxc_file_content, lxc_id=int(lxc_folder), pve=pve) for lxc in get_all_lxcs(): logging.info(f"Loading LXC {lxc.lxc_id}") diff --git a/src/utils/proxmox.py b/src/utils/proxmox.py index 5f03da5..99f0d41 100644 --- a/src/utils/proxmox.py +++ b/src/utils/proxmox.py @@ -37,7 +37,7 @@ class ProxmoxHost(LinuxMachine): self.user = user self.port = port self.connection = None - self.repo_path = path + self.repo_path = path.as_posix() def __str__(self): return f"ProxmoxHost({self.host}, {self.user}, {self.port})" @@ -132,7 +132,7 @@ class ProxmoxHost(LinuxMachine): def get_all_lxcs(self): """Get all the LXCs on the Proxmox host.""" - pct_list_output = self.run_command("pct list") + pct_list_output = self.run_command("pct list", exception_on_exit=False) pct_list_output = pct_list_output.split("\n")[1:] ids = [line.split()[0] for line in pct_list_output] @@ -147,32 +147,23 @@ class ProxmoxHost(LinuxMachine): def is_lxc_running(self, lxc_id): """Check if the given LXC is running on the Proxmox host.""" - if not self.does_lxc_exist(lxc_id): - return False - pct_status_output = self.run_command(f"pct status {lxc_id}") + pct_status_output = self.run_command(f"pct status {lxc_id}", exception_on_exit=False) return "running" in pct_status_output def start_lxc(self, lxc_id): """Start the given LXC on the Proxmox host.""" - if not self.does_lxc_exist(lxc_id): - raise Exception(f"LXC {lxc_id} does not exist") - if not self.is_lxc_running(lxc_id): self.run_command(f"pct start {lxc_id}") def stop_lxc(self, lxc_id): """Stop the given LXC on the Proxmox host.""" - if not self.does_lxc_exist(lxc_id): - raise Exception(f"LXC {lxc_id} does not exist") if self.is_lxc_running(lxc_id): self.run_command(f"pct stop {lxc_id}") def reboot_lxc(self, lxc_id): """Reboot the given LXC on the Proxmox host.""" - if not self.does_lxc_exist(lxc_id): - raise Exception(f"LXC {lxc_id} does not exist") self.run_command(f"pct reboot {lxc_id}") @@ -193,6 +184,7 @@ class ProxmoxHost(LinuxMachine): def is_vm_running(self, vm_id): """Check if the given VM is running on the Proxmox host.""" + if not self.does_vm_exist(vm_id): return False @@ -201,24 +193,18 @@ class ProxmoxHost(LinuxMachine): def start_vm(self, vm_id): """Start the given VM on the Proxmox host.""" - if not self.does_vm_exist(vm_id): - raise Exception(f"VM {vm_id} does not exist") if not self.is_vm_running(vm_id): self.run_command(f"qm start {vm_id}") def stop_vm(self, vm_id): """Stop the given VM on the Proxmox host.""" - if not self.does_vm_exist(vm_id): - raise Exception(f"VM {vm_id} does not exist") if self.is_vm_running(vm_id): self.run_command(f"qm stop {vm_id}") def reboot_vm(self, vm_id): """Reboot the given VM on the Proxmox host.""" - if not self.does_vm_exist(vm_id): - raise Exception(f"VM {vm_id} does not exist") self.run_command(f"qm reboot {vm_id}") @@ -246,3 +232,23 @@ class ProxmoxHost(LinuxMachine): directory = str(directory.as_posix()) return fnmatch.filter(self.connection.sftp().listdir(path=directory), glob_filter) + + def read_file(self, file: str or Path): + """Read the given file. + + Parameters + ---------- + file: str or Path + The file to read. + + Returns + ------- + file: file + The file object. + + """ + if isinstance(file, Path): + file = str(file.as_posix()) + + with self.connection.sftp().open(file, 'r') as f: + return f.read()