2023-10-13 15:30:35 +03:00
import logging
2024-04-29 03:55:10 +03:00
from proxmoxer import ProxmoxAPI
2023-10-13 15:30:35 +03:00
logger = logging . getLogger ( __name__ )
2024-05-26 08:37:52 +03:00
logger . setLevel ( logging . INFO )
2023-10-13 15:30:35 +03:00
def create_vm (
2024-04-29 03:55:10 +03:00
proxmox : ProxmoxAPI ,
node : str | None = None ,
vmid : int | None = None ,
name : str | None = None ,
2023-10-13 15:30:35 +03:00
prefix : str = ' stepchenkoas ' ,
cores : int = 2 ,
memory : int = 2048 ,
disk_size : int = 16 ,
start : int = 1 ,
2024-04-29 03:55:10 +03:00
) - > None :
2023-10-13 15:30:35 +03:00
if node is None :
nodes = proxmox . nodes . get ( )
2024-04-29 03:55:10 +03:00
nodes = list ( filter ( lambda node : node [ ' status ' ] == ' online ' , nodes ) ) # pyright: ignore
2023-10-13 15:30:35 +03:00
node = min ( nodes , key = lambda node : node [ ' disk ' ] ) [ ' node ' ]
if vmid is None :
vmid = proxmox . cluster . get ( ' nextid ' )
if name is None :
name = f ' { prefix } - { vmid } '
proxmox . nodes ( node ) . qemu . post (
node = node ,
vmid = vmid ,
description = ' This VM was created automatically through Proxmox VE API ' ,
cores = cores ,
cpu = ' host ' ,
memory = memory ,
name = name ,
net0 = ' virtio,bridge=vmbr0,tag=103,firewall=1 ' ,
ostype = ' l26 ' ,
pool = ' Virt_LAB ' ,
#alt-server-v-10.1-rc3-x86_64.iso
sata2 = ' templates:iso/alt-server-v-10.1-rc3-x86_64.iso,media=cdrom ' ,
scsi0 = f ' rbd-storage: { disk_size } ,discard=on ' ,
scsihw = ' virtio-scsi-pci ' ,
sockets = 1 ,
start = start ,
)
logger . info ( ' VM %s was created successfully! ' , vmid )
def delete_vm (
2024-04-29 03:55:10 +03:00
proxmox : ProxmoxAPI ,
node : str ,
vmid : int ,
) - > None :
2023-10-13 15:30:35 +03:00
proxmox . nodes ( node ) . qemu ( vmid ) . delete (
node = node ,
vmid = vmid ,
)
logger . info ( ' VM %s was deleted successfully! ' , vmid )
def clone_template (
2024-04-29 03:55:10 +03:00
proxmox : ProxmoxAPI ,
2023-10-13 15:30:35 +03:00
node : str ,
vmid : int ,
2024-04-29 03:55:10 +03:00
newid : int | None = None ,
name : str | None = None ,
2023-10-13 15:30:35 +03:00
prefix : str = ' stepchenkoas ' ,
2024-04-29 03:55:10 +03:00
) - > tuple [ int , str ] :
2023-10-13 15:30:35 +03:00
if newid is None :
2024-04-29 03:55:10 +03:00
newid = int ( proxmox . cluster . get ( ' nextid ' ) ) # pyright: ignore
2023-10-13 15:30:35 +03:00
if name is None :
name = f ' { prefix } - { newid } '
assert type ( newid ) is int
assert type ( name ) is str
upid : str = proxmox . nodes ( node ) . qemu ( vmid ) . clone . post (
newid = newid ,
node = node ,
vmid = vmid ,
format = ' raw ' ,
full = 1 ,
name = name ,
pool = ' Virt_LAB ' ,
storage = ' rbd-storage ' ,
target = node ,
2024-04-29 03:55:10 +03:00
) # pyright: ignore
2023-10-13 15:30:35 +03:00
# TODO: check task status
logger . debug ( " UPID ' %s ' " , upid )
logger . info ( ' VM %s was cloned to %s successfully! ' , vmid , newid )
return ( newid , upid )
def get_vm_ip (
2024-04-29 03:55:10 +03:00
proxmox : ProxmoxAPI ,
node : str ,
2023-10-13 15:30:35 +03:00
vmid : int ,
2024-04-29 03:55:10 +03:00
) - > str :
res : dict = proxmox . nodes ( node ) . qemu ( vmid ) . agent ( ' network-get-interfaces ' ) . get ( ) # pyright: ignore
2023-10-13 15:30:35 +03:00
vm_ip_address = None
for net in res [ ' result ' ] :
if net [ ' name ' ] == ' eth0 ' or net [ ' name ' ] == ' ens19 ' :
for ip in net [ ' ip-addresses ' ] :
if ip [ ' ip-address-type ' ] == ' ipv4 ' :
vm_ip_address = ip [ ' ip-address ' ]
2024-04-29 03:51:57 +03:00
if vm_ip_address is None :
raise ValueError ( f ' Could not get the ip address of VM { vmid } on node { node } ' )
2023-10-13 15:30:35 +03:00
return vm_ip_address