【问题标题】:How to start multiples compute instances with python如何使用 python 启动多个计算实例
【发布时间】:2020-11-24 21:50:00
【问题描述】:

下面有人分享的代码(Starting a Google Compute instance with Python),我做了一些修改,它非常适合我启动一个虚拟机。我想知道是否可以调整此代码以在同一函数中启动多个实例? 这是代码:

from googleapiclient import discovery
 
service = discovery.build('compute', 'v1')
 
def autostart_vm(request):
 
        # Project ID for this request.
        project = 'secret-metrics-278618'
 
        # The name of the zone for this request.
        zone = 'us-central1-a' 
 
        # Name of the instance resource to start.
        instance = 'devops1'
        instance2 = 'devops-2'
        
 
        request = service.instances().start(project=project, zone=zone, instance=instance)
        response = request.execute()
        
 
        print('VM Instance started')

【问题讨论】:

  • 您不需要将request 作为参数传递给您的函数以使其工作。

标签: python google-cloud-functions multiple-instances


【解决方案1】:

虽然您必须进行调整,但这是可能的。来自instances().start()的描述:

启动使用 instances().stop 方法停止的实例。有关详细信息,请参阅重启实例。

意味着您必须像这样迭代方法(假设您的实例位于类似的项目和区域中):

# Name of the instance resource to start.
instanceList = ['devops1', 'devops-2']

for i in instanceList:   
  request = service.instances().start(project=project, zone=zone, instance=i)
  response = request.execute()
  print('VM instance '+ i + ' started')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-25
    • 1970-01-01
    • 2021-09-15
    • 1970-01-01
    • 2015-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多