Добавлена поддержка клонирования LXC-шаблонов по CTID

This commit is contained in:
2026-08-09 16:43:35 +03:00
parent 400ad3da2a
commit c3ffed82b6
+23 -61
View File
@@ -15,14 +15,7 @@ def _client() -> ProxmoxAPI:
if "!" not in settings.pve_token_name: if "!" not in settings.pve_token_name:
raise RuntimeError("PVE_TOKEN_NAME должен иметь формат user@realm!token") raise RuntimeError("PVE_TOKEN_NAME должен иметь формат user@realm!token")
user, token_name = settings.pve_token_name.split("!", 1) user, token_name = settings.pve_token_name.split("!", 1)
return ProxmoxAPI( return ProxmoxAPI(parsed.hostname, port=parsed.port or 8006, user=user, token_name=token_name, token_value=settings.pve_token_value.get_secret_value(), verify_ssl=settings.pve_verify_ssl)
parsed.hostname,
port=parsed.port or 8006,
user=user,
token_name=token_name,
token_value=settings.pve_token_value.get_secret_value(),
verify_ssl=settings.pve_verify_ssl,
)
def _wait_task(client: ProxmoxAPI, node: str, upid: str, timeout: int = 1800) -> None: def _wait_task(client: ProxmoxAPI, node: str, upid: str, timeout: int = 1800) -> None:
@@ -43,67 +36,36 @@ def next_vmid() -> int:
return int(_client().cluster.nextid.get()) return int(_client().cluster.nextid.get())
def provision_vm( def provision_vm(source_vmid: int, vmid: int, name: str, cores: int, memory_mb: int, disk_gb: int, username: str, password: str) -> None:
source_vmid: int,
vmid: int,
name: str,
cores: int,
memory_mb: int,
disk_gb: int,
username: str,
password: str,
) -> None:
"""Клонирует cloud-init VM, настраивает ресурсы и запускает её.""" """Клонирует cloud-init VM, настраивает ресурсы и запускает её."""
client = _client() client = _client(); node = settings.pve_node
node = settings.pve_node
params = {"newid": vmid, "name": name, "full": 1} params = {"newid": vmid, "name": name, "full": 1}
if settings.pve_vm_storage: if settings.pve_vm_storage: params["storage"] = settings.pve_vm_storage
params["storage"] = settings.pve_vm_storage _wait_task(client, node, client.nodes(node).qemu(source_vmid).clone.post(**params))
upid = client.nodes(node).qemu(source_vmid).clone.post(**params) client.nodes(node).qemu(vmid).config.put(cores=cores, memory=memory_mb, ciuser=username, cipassword=password, ipconfig0="ip=dhcp")
_wait_task(client, node, upid)
client.nodes(node).qemu(vmid).config.put(
cores=cores,
memory=memory_mb,
ciuser=username,
cipassword=password,
ipconfig0="ip=dhcp",
)
# Увеличиваем диск только если шаблон меньше заданного размера.
config = client.nodes(node).qemu(vmid).config.get() config = client.nodes(node).qemu(vmid).config.get()
for key in ("scsi0", "virtio0", "sata0", "ide0"): for key in ("scsi0", "virtio0", "sata0", "ide0"):
value = config.get(key, "") match = re.search(r"size=(\d+)G", config.get(key, ""))
match = re.search(r"size=(\d+)G", value)
if match and disk_gb > int(match.group(1)): if match and disk_gb > int(match.group(1)):
client.nodes(node).qemu(vmid).resize.put( client.nodes(node).qemu(vmid).resize.put(disk=key, size=f"+{disk_gb - int(match.group(1))}G")
disk=key, size=f"+{disk_gb - int(match.group(1))}G"
)
break break
client.nodes(node).qemu(vmid).status.start.post() client.nodes(node).qemu(vmid).status.start.post()
def provision_lxc( def provision_lxc_clone(source_vmid: int, vmid: int, name: str, cores: int, memory_mb: int, disk_gb: int, password: str) -> None:
source_template: str, """Клонирует готовый LXC-шаблон по CTID и запускает контейнер."""
vmid: int, client = _client(); node = settings.pve_node
name: str, params = {"newid": vmid, "hostname": name, "full": 1}
cores: int, if settings.pve_lxc_storage: params["storage"] = settings.pve_lxc_storage
memory_mb: int, _wait_task(client, node, client.nodes(node).lxc(source_vmid).clone.post(**params))
disk_gb: int, client.nodes(node).lxc(vmid).config.put(cores=cores, memory=memory_mb, swap=memory_mb, password=password, net0=f"name=eth0,bridge={settings.pve_bridge},ip=dhcp")
password: str, client.nodes(node).lxc(vmid).resize.put(disk="rootfs", size=f"{disk_gb}G")
) -> None: client.nodes(node).lxc(vmid).status.start.post()
"""Создаёт LXC из CT-шаблона и запускает его с DHCP."""
client = _client()
node = settings.pve_node def provision_lxc(source_template: str, vmid: int, name: str, cores: int, memory_mb: int, disk_gb: int, password: str) -> None:
upid = client.nodes(node).lxc.post( """Создаёт LXC из архивного CT-шаблона и запускает его."""
vmid=vmid, client = _client(); node = settings.pve_node
hostname=name, upid = client.nodes(node).lxc.post(vmid=vmid, hostname=name, ostemplate=source_template, cores=cores, memory=memory_mb, swap=memory_mb, rootfs=f"{settings.pve_lxc_storage}:{disk_gb}", password=password, net0=f"name=eth0,bridge={settings.pve_bridge},ip=dhcp", unprivileged=1)
ostemplate=source_template,
cores=cores,
memory=memory_mb,
swap=memory_mb,
rootfs=f"{settings.pve_lxc_storage}:{disk_gb}",
password=password,
net0=f"name=eth0,bridge={settings.pve_bridge},ip=dhcp",
unprivileged=1,
)
_wait_task(client, node, upid) _wait_task(client, node, upid)
client.nodes(node).lxc(vmid).status.start.post() client.nodes(node).lxc(vmid).status.start.post()