from .ssh import SSHClient class ProxmoxManager: def __init__(self, config, node_name=None): node = config.get_node(node_name) if not node: raise ValueError(f"Node '{node_name}' not found in config") self.node_name = node_name self.host = node['host'] self.password = node.get('pass') self.user = config.get('proxmox.user', 'root') self.ssh_key = config.get('proxmox.ssh_key_path') self.client = SSHClient(self.host, self.user, self.ssh_key, self.password) def list_lxcs(self): res = self.client.run("pct list") return res.stdout def list_vms(self): res = self.client.run("qm list") return res.stdout def status_lxc(self, vmid): res = self.client.run(f"pct status {vmid}") return res.stdout def resolve_template(self, alias): """Resolves a template alias (e.g. 'debian-13') to the latest full path on the node""" if alias == "debian-13": # Search for debian-13 templates in all storages res = self.client.run("pvesm list --content vztmpl | grep 'debian-13-standard' | sort -V | tail -n 1") if res.returncode == 0 and res.stdout.strip(): # Extract first column (Volid) return res.stdout.split()[0] return alias # Fallback to original if no resolution def create_lxc(self, vmid, template, hostname, ip, gateway, bridge="vmbr0", storage="local-zfs", password=None): # Resolve template if it's an alias full_template = self.resolve_template(template) # Professional creation command with sane defaults cmd = f"pct create {vmid} {full_template} --hostname {hostname} " \ f"--net0 name=eth0,bridge={bridge},ip={ip},gw={gateway} " \ f"--storage {storage} --onboot 1" if password: cmd += f" --password {password}" res = self.client.run(cmd) if res.returncode != 0: raise RuntimeError(f"Failed to create LXC {vmid}: {res.stderr}") return res.stdout def start_lxc(self, vmid): return self.client.run(f"pct start {vmid}") def stop_lxc(self, vmid): return self.client.run(f"pct stop {vmid}") def delete_lxc(self, vmid): res = self.client.run(f"pct destroy {vmid}") if res.returncode != 0: raise RuntimeError(f"Failed to destroy LXC {vmid}: {res.stderr}") return res.stdout