【问题标题】:Stop VM in GCP and wait till it is stopped using gcp python sdk在 GCP 中停止 VM 并等待它使用 gcp python sdk 停止
【发布时间】:2020-11-13 04:50:26
【问题描述】:

我想在 gcp 中停止 VM 并等待该过程完成。 现在如果我打了

request = service.instances().stop(project=project, zone=zone, instance=instance_name)

它退出而不等到虚拟机实际上处于停止状态。

【问题讨论】:

  • 您必须循环并请求VM状态等待,由您自己有效停止。但是为什么要等这个呢?
  • 是的。必须建立我的逻辑来检查节点的停止状态。

标签: python python-3.x google-cloud-platform sdk google-api-client


【解决方案1】:

我必须构建自己的逻辑来检查 vm 列表的终止状态。

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多