feat: wait for vms to start

This commit is contained in:
Александр Степченко 2023-10-11 14:38:56 +03:00
parent 07fa191f48
commit 2e9bb05e94

32
main.py
View File

@ -1,19 +1,23 @@
from time import sleep
from typing import Optional
from pathlib import Path
from pprint import pprint
import urllib3
from proxmoxer import ProxmoxAPI
urllib3.disable_warnings()
PROXMOX_HOST = 'pve.office.basealt.ru'
PROXMOX_USER = 'stepchenkoas@BaseALT'
PROXMOX_PASSWORD = Path('pve_secret').read_text().strip()
def create_vm(
proxmox,
node: str = None,
vmid: int = None,
name: str = None,
node: Optional[str] = None,
vmid: Optional[int] = None,
name: Optional[str] = None,
prefix: str = 'stepchenkoas',
cores: int = 2,
memory: int = 2048,
@ -68,8 +72,8 @@ def clone_template(
proxmox,
node: str,
vmid: int,
newid: int = None,
name: str = None,
newid: Optional[int] = None,
name: Optional[str] = None,
prefix: str ='stepchenkoas',
) -> int:
if newid is None:
@ -78,7 +82,7 @@ def clone_template(
if name is None:
name = f'{prefix}-{newid}'
proxmox.nodes(node).qemu(vmid).clone.post(
res = proxmox.nodes(node).qemu(vmid).clone.post(
newid=newid,
node=node,
vmid=vmid,
@ -89,6 +93,7 @@ def clone_template(
storage='rbd-storage',
target=node,
)
print(res)
print(f'VM {vmid} was cloned to {newid} successfully!')
return newid
@ -117,7 +122,7 @@ def main():
nodes = proxmox.nodes.get()
nodes = list(filter(lambda node: node['status'] == 'online', nodes))
node = min(nodes, key=lambda node: node['disk'])['node']
template_id = 208
template_id = 374
prefix = 'stepchenkaos-test-k8s'
vm_id_master = clone_template(
@ -145,8 +150,15 @@ def main():
proxmox.nodes(node).qemu(vm_id_worker2).status.start.post()
vms_statuses = [False, False, False]
while !all(vms_statuses)
proxmox.nodes(node).qemu(vm_id_master).status.current.get()
print(vms_statuses)
while not all(vms_statuses):
sleep(5)
vms_statuses[0] = proxmox.nodes(node).qemu(vm_id_master).status.current.get()['status'] == 'running'
vms_statuses[1] = proxmox.nodes(node).qemu(vm_id_worker1).status.current.get()['status'] == 'running'
vms_statuses[2] = proxmox.nodes(node).qemu(vm_id_worker2).status.current.get()['status'] == 'running'
print(vms_statuses)
sleep(60)
vm_ip_master = get_vm_ip(proxmox, node, vm_id_master)
vm_ip_worker1 = get_vm_ip(proxmox, node, vm_id_worker1)