feat: get_vm_ip, create_vm, main

Implement get_vm_ip, refactor create_vm, implement main
This commit is contained in:
Александр Степченко 2023-03-27 21:13:36 +03:00
parent c44d13f532
commit 07fa191f48

95
main.py
View File

@ -1,3 +1,4 @@
from time import sleep
from pathlib import Path
from pprint import pprint
@ -10,13 +11,14 @@ PROXMOX_PASSWORD = Path('pve_secret').read_text().strip()
def create_vm(
proxmox,
node=None,
vmid=None,
name=None,
prefix='stepchenkoas',
cores=2,
memory=2048,
disk_size=16,
node: str = None,
vmid: int = None,
name: str = None,
prefix: str = 'stepchenkoas',
cores: int = 2,
memory: int = 2048,
disk_size: int = 16,
start: int = 1,
):
if node is None:
nodes = proxmox.nodes.get()
@ -45,6 +47,7 @@ def create_vm(
scsi0=f'rbd-storage:{disk_size},discard=on',
scsihw='virtio-scsi-pci',
sockets=1,
start=start,
)
print(f'VM {vmid} was created successfully!')
@ -66,22 +69,44 @@ def clone_template(
node: str,
vmid: int,
newid: int = None,
prefix: int ='stepchenkoas',
):
name: str = None,
prefix: str ='stepchenkoas',
) -> int:
if newid is None:
newid = proxmox.cluster.get('nextid')
return proxmox.nodes(node).qemu(vmid).clone.post(
if name is None:
name = f'{prefix}-{newid}'
proxmox.nodes(node).qemu(vmid).clone.post(
newid=newid,
node=node,
vmid=vmid,
format='raw',
full=1,
name=f'{prefix}-test-clone',
name=name,
pool='Virt_LAB',
storage='rbd-storage',
target=node,
)
print(f'VM {vmid} was cloned to {newid} successfully!')
return newid
def get_vm_ip(
proxmox,
node: 'str',
vmid: int,
):
res = proxmox.nodes(node).qemu(vmid).agent('network-get-interfaces').get()
vm_ip_address = None
for net in res['result']:
if net['name'] == 'eth0':
for ip in net['ip-addresses']:
if ip['ip-address-type'] == 'ipv4':
vm_ip_address = ip['ip-address']
return vm_ip_address
def main():
@ -89,7 +114,55 @@ def main():
PROXMOX_HOST, user=PROXMOX_USER, password=PROXMOX_PASSWORD, verify_ssl=False
)
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
prefix = 'stepchenkaos-test-k8s'
vm_id_master = clone_template(
proxmox,
node,
template_id,
name=f'{prefix}-master',
)
proxmox.nodes(node).qemu(vm_id_master).status.start.post()
vm_id_worker1 = clone_template(
proxmox,
node,
template_id,
name=f'{prefix}-node1',
)
proxmox.nodes(node).qemu(vm_id_worker1).status.start.post()
vm_id_worker2 = clone_template(
proxmox,
node,
template_id,
name=f'{prefix}-node2',
)
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()
vm_ip_master = get_vm_ip(proxmox, node, vm_id_master)
vm_ip_worker1 = get_vm_ip(proxmox, node, vm_id_worker1)
vm_ip_worker2 = get_vm_ip(proxmox, node, vm_id_worker2)
with open('vm_ids' ,'w') as ofile:
vm_ids = f'{vm_id_master}\tmaster\n'
vm_ids += f'{vm_id_worker1}\tworker1\n'
vm_ids += f'{vm_id_worker2}\tworker2\n'
ofile.write(vm_ids)
with open('hosts' ,'w') as ofile:
hosts = f'{vm_ip_master}\tmaster\n'
hosts += f'{vm_ip_worker1}\tworker1\n'
hosts += f'{vm_ip_worker2}\tworker2\n'
ofile.write(hosts)
proxmox.logout()