fixed running from repo remotely
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
parent
e654102176
commit
350fafbfe0
@ -1,7 +1,7 @@
|
|||||||
import json
|
import json
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
from .lxc import LXC
|
from .lxc import LXC
|
||||||
|
from ..utils.proxmox import ProxmoxHost
|
||||||
|
|
||||||
lxcs = []
|
lxcs = []
|
||||||
|
|
||||||
@ -10,23 +10,28 @@ def get_all_lxcs():
|
|||||||
return 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
|
"""Load LXC from JSON file
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
lxc_id : int
|
lxc_id : int
|
||||||
ID of the LXC to load
|
ID of the LXC to load
|
||||||
filepath : pathlib.Path
|
content : str or bytes
|
||||||
Path object to the JSON file of the LXC to load
|
Content of the JSON file
|
||||||
|
pve : ProxmoxHost
|
||||||
|
Proxmox host where the LXC is/will go on
|
||||||
|
|
||||||
Examples
|
Examples
|
||||||
--------
|
--------
|
||||||
>>> load_lxc(100, Path("<full-path>/resources/lxc/100/config.json"))
|
>>> load_lxc(100, '{ <json-data> }')
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
if isinstance(content, bytes):
|
||||||
|
content = content.decode("utf-8")
|
||||||
|
|
||||||
# Load JSON data
|
# Load JSON data
|
||||||
data = json.loads(filepath.read_text())
|
data = json.loads(content)
|
||||||
|
|
||||||
# Extract values from JSON
|
# Extract values from JSON
|
||||||
lxc_id = lxc_id
|
lxc_id = lxc_id
|
||||||
@ -39,7 +44,7 @@ def load_lxc(lxc_id: int, filepath: Path):
|
|||||||
deploy = data["deploy"]
|
deploy = data["deploy"]
|
||||||
|
|
||||||
# Create LXC object
|
# 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)
|
lxcs.append(lxc)
|
||||||
|
|
||||||
|
|
||||||
|
11
src/main.py
11
src/main.py
@ -1,7 +1,6 @@
|
|||||||
import logging
|
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 .lxc.lxc_utils import load_lxc, get_all_lxcs
|
||||||
from .utils import git_utils
|
from .utils import git_utils
|
||||||
from .utils.proxmox import ProxmoxHost
|
from .utils.proxmox import ProxmoxHost
|
||||||
@ -35,13 +34,13 @@ def run(args):
|
|||||||
|
|
||||||
# Go through each LXC file
|
# Go through each LXC file
|
||||||
for resource in resources:
|
for resource in resources:
|
||||||
if resource.name == "lxc":
|
if resource == "lxc":
|
||||||
# Read all files in the LXC directory
|
# 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:
|
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():
|
for lxc in get_all_lxcs():
|
||||||
logging.info(f"Loading LXC {lxc.lxc_id}")
|
logging.info(f"Loading LXC {lxc.lxc_id}")
|
||||||
|
@ -37,7 +37,7 @@ class ProxmoxHost(LinuxMachine):
|
|||||||
self.user = user
|
self.user = user
|
||||||
self.port = port
|
self.port = port
|
||||||
self.connection = None
|
self.connection = None
|
||||||
self.repo_path = path
|
self.repo_path = path.as_posix()
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"ProxmoxHost({self.host}, {self.user}, {self.port})"
|
return f"ProxmoxHost({self.host}, {self.user}, {self.port})"
|
||||||
@ -132,7 +132,7 @@ class ProxmoxHost(LinuxMachine):
|
|||||||
|
|
||||||
def get_all_lxcs(self):
|
def get_all_lxcs(self):
|
||||||
"""Get all the LXCs on the Proxmox host."""
|
"""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:]
|
pct_list_output = pct_list_output.split("\n")[1:]
|
||||||
ids = [line.split()[0] for line in pct_list_output]
|
ids = [line.split()[0] for line in pct_list_output]
|
||||||
|
|
||||||
@ -147,32 +147,23 @@ class ProxmoxHost(LinuxMachine):
|
|||||||
|
|
||||||
def is_lxc_running(self, lxc_id):
|
def is_lxc_running(self, lxc_id):
|
||||||
"""Check if the given LXC is running on the Proxmox host."""
|
"""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
|
return "running" in pct_status_output
|
||||||
|
|
||||||
def start_lxc(self, lxc_id):
|
def start_lxc(self, lxc_id):
|
||||||
"""Start the given LXC on the Proxmox host."""
|
"""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):
|
if not self.is_lxc_running(lxc_id):
|
||||||
self.run_command(f"pct start {lxc_id}")
|
self.run_command(f"pct start {lxc_id}")
|
||||||
|
|
||||||
def stop_lxc(self, lxc_id):
|
def stop_lxc(self, lxc_id):
|
||||||
"""Stop the given LXC on the Proxmox host."""
|
"""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):
|
if self.is_lxc_running(lxc_id):
|
||||||
self.run_command(f"pct stop {lxc_id}")
|
self.run_command(f"pct stop {lxc_id}")
|
||||||
|
|
||||||
def reboot_lxc(self, lxc_id):
|
def reboot_lxc(self, lxc_id):
|
||||||
"""Reboot the given LXC on the Proxmox host."""
|
"""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}")
|
self.run_command(f"pct reboot {lxc_id}")
|
||||||
|
|
||||||
@ -193,6 +184,7 @@ class ProxmoxHost(LinuxMachine):
|
|||||||
|
|
||||||
def is_vm_running(self, vm_id):
|
def is_vm_running(self, vm_id):
|
||||||
"""Check if the given VM is running on the Proxmox host."""
|
"""Check if the given VM is running on the Proxmox host."""
|
||||||
|
|
||||||
if not self.does_vm_exist(vm_id):
|
if not self.does_vm_exist(vm_id):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@ -201,24 +193,18 @@ class ProxmoxHost(LinuxMachine):
|
|||||||
|
|
||||||
def start_vm(self, vm_id):
|
def start_vm(self, vm_id):
|
||||||
"""Start the given VM on the Proxmox host."""
|
"""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):
|
if not self.is_vm_running(vm_id):
|
||||||
self.run_command(f"qm start {vm_id}")
|
self.run_command(f"qm start {vm_id}")
|
||||||
|
|
||||||
def stop_vm(self, vm_id):
|
def stop_vm(self, vm_id):
|
||||||
"""Stop the given VM on the Proxmox host."""
|
"""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):
|
if self.is_vm_running(vm_id):
|
||||||
self.run_command(f"qm stop {vm_id}")
|
self.run_command(f"qm stop {vm_id}")
|
||||||
|
|
||||||
def reboot_vm(self, vm_id):
|
def reboot_vm(self, vm_id):
|
||||||
"""Reboot the given VM on the Proxmox host."""
|
"""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}")
|
self.run_command(f"qm reboot {vm_id}")
|
||||||
|
|
||||||
@ -246,3 +232,23 @@ class ProxmoxHost(LinuxMachine):
|
|||||||
directory = str(directory.as_posix())
|
directory = str(directory.as_posix())
|
||||||
|
|
||||||
return fnmatch.filter(self.connection.sftp().listdir(path=directory), glob_filter)
|
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()
|
||||||
|
Loading…
Reference in New Issue
Block a user