使用 Python 启动 Google 计算实例
Starting a Google Compute instance with Python
我正在尝试使用 Google API Python 客户端库启动一个 Google 计算实例。这样一来,便宜的实例(运行ning 在单核上)可以定期启动和停止更昂贵的实例(有很多核),以降低成本。
我已经成功安装了不同的组件和 运行 Google 的示例脚本 create_instance.py(它创建了一个实例,运行 是一个启动脚本,并删除了实例)。检查 Compute Engine API 的 PyDoc 参考,并交叉引用其他 instances() 函数在 create_instance.py 示例中的工作方式,我希望启动实例命令为:
python compute.instances().start(project=*, zone=*, instance=*).execute()
上面的命令给我错误 "An expression was expected after '('. at line:1 char:34" - 这是第一个括号。
一个。我做错了什么?
b。将 Google API 与 Python 一起使用是否是一种以编程方式从其他实例启动实例的好方法?
通常我希望您需要使用 import
语句或运行时标志(-m somemodule
?)导入 api 库。 运行 直接从命令行输入一行 python 通常不是最好的处理方式。相反 Google 提供 gcloud
command line tool.
通常在发送 API 实际命令之前调用 authentication/login 函数。在 Google VM 上,这可以是 id/private 键或空白 id/key 如果 VM 被特别授权调用 API 或充当特定帐户。首次创建 Google VM 时,可以从计算引擎 Web 控制面板设置此授权。在外部 VM 上,它将需要一个 id/private 密钥来提供给 Google API。因此,python 中的单行可能无法工作,因为它缺少此步骤。
compute.instances().start()
函数采用必需的参数来启动已停止的特定实例。这意味着:
- 之前已创建 VM 实例
- VM 实例处于停止状态
- 要重启的实例由特定项目 ID、(地理)区域和调用
start
时提供的实例名称标识
来自Google Cloud Python Documentation
start(project=, zone=, instance=*) Starts an instance that was
stopped using the instances().stop method. For more
information, see Restart an instance.
Args: project: string, Project ID for this request. (required)
zone: string, The name of the zone for this request. (required)
instance: string, Name of the instance resource to start. (required)
...
下面是启动计算引擎实例所需的代码
from googleapiclient import discovery
service = discovery.build('compute', 'v1')
print('VM Instance starting')
# Project ID for this request.
project = 'project_name'
# The name of the zone for this request.
zone = 'zone_value'
# Name of the instance resource to start.
instance = 'instance_name'
request = service.instances().start(project=project, zone=zone, instance=instance)
response = request.execute()
print('VM Instance started')
这是我用于从云函数启动 VM 实例的代码。
这里需要注意的一点是,如果实例处于停止状态,这只能启动一个实例,这非常适合我的要求。
我使用了@user570778 分享的代码,对我来说效果很好。
`从 googleapiclient 导入发现
服务 = discovery.build('compute', 'v1')
打印('VM Instance starting')
此请求的项目 ID。
项目='project_name'
此请求的区域名称。
区域 = 'zone_value'
要启动的实例资源的名称。
实例='instance_name'
request = service.instances().start(project=project, zone=zone, instance=instance)
响应 = request.execute()
打印('VM Instance started')
`
我想知道,是否可以在同一个函数中启动多个实例?
我正在尝试使用 Google API Python 客户端库启动一个 Google 计算实例。这样一来,便宜的实例(运行ning 在单核上)可以定期启动和停止更昂贵的实例(有很多核),以降低成本。
我已经成功安装了不同的组件和 运行 Google 的示例脚本 create_instance.py(它创建了一个实例,运行 是一个启动脚本,并删除了实例)。检查 Compute Engine API 的 PyDoc 参考,并交叉引用其他 instances() 函数在 create_instance.py 示例中的工作方式,我希望启动实例命令为:
python compute.instances().start(project=*, zone=*, instance=*).execute()
上面的命令给我错误 "An expression was expected after '('. at line:1 char:34" - 这是第一个括号。
一个。我做错了什么?
b。将 Google API 与 Python 一起使用是否是一种以编程方式从其他实例启动实例的好方法?
通常我希望您需要使用
import
语句或运行时标志(-m somemodule
?)导入 api 库。 运行 直接从命令行输入一行 python 通常不是最好的处理方式。相反 Google 提供gcloud
command line tool.通常在发送 API 实际命令之前调用 authentication/login 函数。在 Google VM 上,这可以是 id/private 键或空白 id/key 如果 VM 被特别授权调用 API 或充当特定帐户。首次创建 Google VM 时,可以从计算引擎 Web 控制面板设置此授权。在外部 VM 上,它将需要一个 id/private 密钥来提供给 Google API。因此,python 中的单行可能无法工作,因为它缺少此步骤。
compute.instances().start()
函数采用必需的参数来启动已停止的特定实例。这意味着:- 之前已创建 VM 实例
- VM 实例处于停止状态
- 要重启的实例由特定项目 ID、(地理)区域和调用
start
时提供的实例名称标识
来自Google Cloud Python Documentation
start(project=, zone=, instance=*) Starts an instance that was stopped using the instances().stop method. For more information, see Restart an instance.
Args: project: string, Project ID for this request. (required)
zone: string, The name of the zone for this request. (required)
instance: string, Name of the instance resource to start. (required)...
下面是启动计算引擎实例所需的代码
from googleapiclient import discovery
service = discovery.build('compute', 'v1')
print('VM Instance starting')
# Project ID for this request.
project = 'project_name'
# The name of the zone for this request.
zone = 'zone_value'
# Name of the instance resource to start.
instance = 'instance_name'
request = service.instances().start(project=project, zone=zone, instance=instance)
response = request.execute()
print('VM Instance started')
这是我用于从云函数启动 VM 实例的代码。
这里需要注意的一点是,如果实例处于停止状态,这只能启动一个实例,这非常适合我的要求。
我使用了@user570778 分享的代码,对我来说效果很好。
`从 googleapiclient 导入发现
服务 = discovery.build('compute', 'v1') 打印('VM Instance starting')
此请求的项目 ID。
项目='project_name'
此请求的区域名称。
区域 = 'zone_value'
要启动的实例资源的名称。
实例='instance_name'
request = service.instances().start(project=project, zone=zone, instance=instance) 响应 = request.execute()
打印('VM Instance started') ` 我想知道,是否可以在同一个函数中启动多个实例?