dynamic-infra-tooling/infra_cli/proxmox.py

52 lines
1.8 KiB
Python

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 create_lxc(self, vmid, template, hostname, ip, gateway, bridge="vmbr0", storage="local-zfs", password=None):
# Professional creation command with sane defaults
cmd = f"pct create {vmid} {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