2023-10-13 15:30:35 +03:00
import logging
import urllib3
from typing import Callable , List
2023-03-27 21:13:36 +03:00
from time import sleep
2023-03-24 16:31:22 +03:00
from pathlib import Path
from proxmoxer import ProxmoxAPI
2023-10-13 15:30:35 +03:00
from functions import clone_template , get_vm_ip
2023-03-24 16:31:22 +03:00
2023-10-11 14:38:56 +03:00
urllib3 . disable_warnings ( )
2023-10-13 15:30:35 +03:00
logger = logging . getLogger ( __name__ )
logger . setLevel ( logging . DEBUG )
2023-10-11 14:38:56 +03:00
2023-03-24 16:31:22 +03:00
PROXMOX_HOST = ' pve.office.basealt.ru '
PROXMOX_USER = ' stepchenkoas@BaseALT '
PROXMOX_PASSWORD = Path ( ' pve_secret ' ) . read_text ( ) . strip ( )
2023-03-27 15:51:32 +03:00
2023-10-13 15:30:35 +03:00
def wait_status ( funcs : List [ Callable ] , status : str ) :
statuses = [ False ] * len ( funcs )
while not all ( statuses ) :
logger . debug ( statuses )
sleep ( 5 )
for i in range ( len ( funcs ) ) :
statuses [ i ] = funcs [ i ] ( ) [ ' status ' ] == status
2023-03-24 16:31:22 +03:00
def main ( ) :
proxmox = ProxmoxAPI (
PROXMOX_HOST , user = PROXMOX_USER , password = PROXMOX_PASSWORD , verify_ssl = False
)
2023-03-27 21:13:36 +03:00
nodes = proxmox . nodes . get ( )
nodes = list ( filter ( lambda node : node [ ' status ' ] == ' online ' , nodes ) )
node = min ( nodes , key = lambda node : node [ ' disk ' ] ) [ ' node ' ]
2023-10-11 14:38:56 +03:00
template_id = 374
2023-03-27 21:13:36 +03:00
prefix = ' stepchenkaos-test-k8s '
2023-10-13 15:30:35 +03:00
vm_id_master , upid_master = clone_template (
2023-03-27 21:13:36 +03:00
proxmox ,
node ,
template_id ,
name = f ' { prefix } -master ' ,
)
2023-03-24 16:31:22 +03:00
2023-10-13 15:30:35 +03:00
vm_id_worker1 , upid_worker1 = clone_template (
2023-03-27 21:13:36 +03:00
proxmox ,
node ,
template_id ,
name = f ' { prefix } -node1 ' ,
)
2023-10-13 15:30:35 +03:00
vm_id_worker2 , upid_worker2 = clone_template (
2023-03-27 21:13:36 +03:00
proxmox ,
node ,
template_id ,
name = f ' { prefix } -node2 ' ,
)
2023-10-13 15:30:35 +03:00
logger . info ( ' Waiting for clone tasks to complete... ' )
wait_status (
[
proxmox . nodes ( node ) . tasks ( upid_master ) . status . get ,
proxmox . nodes ( node ) . tasks ( upid_worker1 ) . status . get ,
proxmox . nodes ( node ) . tasks ( upid_worker2 ) . status . get ,
] ,
' stopped '
)
logger . info ( ' Tasks completed! ' )
logger . info ( ' Starting VMs... ' )
proxmox . nodes ( node ) . qemu ( vm_id_master ) . status . start . post ( )
proxmox . nodes ( node ) . qemu ( vm_id_worker1 ) . status . start . post ( )
proxmox . nodes ( node ) . qemu ( vm_id_worker2 ) . status . start . post ( )
wait_status (
[
proxmox . nodes ( node ) . qemu ( vm_id_master ) . status . current . get ,
proxmox . nodes ( node ) . qemu ( vm_id_worker1 ) . status . current . get ,
proxmox . nodes ( node ) . qemu ( vm_id_worker2 ) . status . current . get ,
] ,
' running '
)
2023-10-11 14:38:56 +03:00
2023-10-13 15:30:35 +03:00
sleep ( 40 )
logger . info ( ' VMs are running! ' )
2023-03-27 21:13:36 +03:00
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 } \t master \n '
vm_ids + = f ' { vm_id_worker1 } \t worker1 \n '
vm_ids + = f ' { vm_id_worker2 } \t worker2 \n '
2023-10-13 15:30:35 +03:00
logger . info ( vm_ids )
2023-03-27 21:13:36 +03:00
ofile . write ( vm_ids )
with open ( ' hosts ' , ' w ' ) as ofile :
hosts = f ' { vm_ip_master } \t master \n '
hosts + = f ' { vm_ip_worker1 } \t worker1 \n '
hosts + = f ' { vm_ip_worker2 } \t worker2 \n '
2023-10-13 15:30:35 +03:00
logger . info ( hosts )
2023-03-27 21:13:36 +03:00
ofile . write ( hosts )
2023-03-24 16:31:22 +03:00
proxmox . logout ( )
if __name__ == ' __main__ ' :
main ( )