使用Python启动Google Compute实例 [英] Starting a Google Compute instance with Python

查看:117
本文介绍了使用Python启动Google Compute实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Google API Python客户端库启动Google Compute实例.这样一来,廉价实例(在单个内核上运行)可以定期启动和停止较昂贵的实例(具有多个内核),以降低成本.

I am trying to start a Google Compute instance with the Google API Python Client Library. This is so that a cheap instance (running on a single core) can periodically start and stop a more expensive instance (with many cores) periodically, to keep costs down.

我已经成功安装了不同的组件并运行了Google的示例脚本

I have successfully installed the different components and run Google's example script create_instance.py (which creates an instances, runs a startup script, and deletes the instance). Inspecting the PyDoc reference for the Compute Engine API, and cross-referencing how the other instances() functions work in the create_instance.py example, I would expect the start instance command to be:

python compute.instances().start(project=*, zone=*, instance=*).execute()

上面的命令给我一个错误在'('之后的表达式.在第1行char:34处出现"-这是第一个括号.

The above command gives me the error "An expression was expected after '('. at line:1 char:34" - this is the first parenthesis.

a.我做错了什么?

b.将Google API与Python结合使用是否是一种以编程方式从其他实例启动实例的好方法?

b. Is using the Google API with Python a good way to start instances from other instances, programmatically?

推荐答案

  1. 通常,我希望您需要使用import语句或运行时标志(-m somemodule?)导入api库.直接从命令行运行python行通常不是最好的方法.相反,Google提供了 gcloud命令行工具.

  1. Generally I would expect you would need to import the api library with an import statement or perhaps a runtime flag (-m somemodule?). Running a line of python directly from the command line is usually not the best way to proceed. Instead Google provides the gcloud command line tool.

通常在发送API实际命令之前调用身份验证/登录功能.在Google VM上,如果VM被特别授权调用API或充当特定帐户,则可以是ID/私有密钥或空白ID/密钥.首次创建Google VM时,可以从计算引擎Web控制面板中设置此授权.在外部VM上,它将需要一个ID/私钥才能提供给Google API.因此,Python中的一个内衬可能无法正常工作,因为它缺少此步骤.

An authentication/login function is usually called before sending the API actual commands. On a Google VM, this can be either an id/private key or a blank id/key if the VM is specifically authorized to call the API or act as a specific account. This authorization can be set up from the compute engine web control panel when creating a Google VM the first time. On an external VM, it will need an id/private key to supply to the Google API. So, a one liner in python probably won't work as it is missing this step.

compute.instances().start()函数采用必需参数来启动已停止的特定实例.这意味着:

The compute.instances().start() function takes required parameters to start a specific instance that has stopped. That means:

  • 该VM实例先前已创建
  • VM实例处于停止状态
  • 要重新启动的实例由特定的项目ID,(地理区域)区域和调用start
  • 时提供的实例名称标识
  • the VM instance has been created previously
  • the VM instance is in the stopped state
  • the instance to be restarted is identified by a specific project id, a (geo) zone, and an instance name that is supplied in the call to start

来自 Google Cloud Python文档

start(project = ,zone = ,instance = *)启动一个实例 使用instances().stop方法停止了.欲了解更多 信息,请参阅重新启动实例.

start(project=, zone=, instance=*) Starts an instance that was stopped using the instances().stop method. For more information, see Restart an instance.

Args:项目:字符串,此请求的项目ID. (必填)
zone:字符串,此请求的区域名称. (必填)
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)

...

这篇关于使用Python启动Google Compute实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆