ProxmoxDeploy/src/main.py
Mathieu Broillet 350fafbfe0
Some checks failed
continuous-integration/drone/push Build is failing
fixed running from repo remotely
2023-06-15 16:55:40 +02:00

50 lines
1.8 KiB
Python

import logging
from pathlib import Path
from .lxc.lxc_utils import load_lxc, get_all_lxcs
from .utils import git_utils
from .utils.proxmox import ProxmoxHost
def run(args):
# Check if running local or SSH
if args.host is None:
pve = ProxmoxHost(path=Path(args.path))
else:
pve = ProxmoxHost(host=args.host, user=args.username, port=args.port, path=Path(args.path))
pve.connect()
if args.repo is None:
logging.warning("No repo provided, skipping updates/cloning...")
else:
# Check if the repo is already cloned
if not pve.has_directory(pve.repo_path):
logging.info(f"Cloning repository {args.repo} to {args.path}...")
git_utils.clone_repo(url=args.repo, path=args.path, linux_machine=pve)
elif pve.has_directory(args.path.joinpath(".git")):
logging.info(f"Repository already cloned at {args.path}, updating...")
git_utils.update_repo(path=args.path, linux_machine=pve)
if not pve.has_directory(pve.repo_path):
raise FileNotFoundError(f"Repo not found at {pve.repo_path}")
# Read all files in the resources directory
resources = pve.list_dir(args.path)
# resources = Path(project_path).joinpath("resources").glob("*")
# Go through each LXC file
for resource in resources:
if resource == "lxc":
# Read all files in the LXC directory
lxc_folders = pve.list_dir(Path(args.path).joinpath(resource))
for lxc_folder in lxc_folders:
lxc_file_content = pve.read_file(Path(args.path).joinpath(resource, lxc_folder, "config.json"))
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}")
lxc.create()
lxc.start()
lxc.run_creation()