I want to stop the VM in gcp and wait till the process is complete. Right now if I hit
request = service.instances().stop(project=project, zone=zone, instance=instance_name)
it exits without waiting till the VM is actually in stopped state.
I had to build my own logic to check the terminate state of the list of vms.
vm_status = {}
operation_status = {}
for instance_name, instance_details in instances.items():
print("Switching {} VM {}".format(state, instance_name))
if state == 'OFF':
request = service.instances().stop(project=project, zone=zone, instance=instance_name)
elif state == 'ON':
request = service.instances().start(project=project, zone=zone, instance=instance_name)
response = request.execute()
operation_status[response['name']] = response['status']
print(response)
vm_status[instance_name] = response['status']
print("Waiting till all the VMs are in {}".format(state))
print(vm_status)
wait_for_operation(service, )
timeout = time.time() + 60 * 15
if state == 'OFF':
all_stop = False
while (not all_stop or time.time() > timeout):
for instance_name, instance_details in instances.items():
request = service.instances().get(project=project, zone=zone, instance=instance_name)
response = request.execute()
vm_status[instance_name] = response['status']
stop = True
for instance_name, status in vm_status.items():
if status != 'TERMINATED':
stop = False
break
print(vm_status)
if stop:
all_stop = True
time.sleep(1)