如何通过Python API使用启动脚本启动GCP计算引擎VM?

问题描述 投票:0回答:2

GCP 有一个已发布的

create_instance()
代码片段可用 here,我在几个地方看到了 e.g.在这里。然而,正如您在第一个链接中看到的那样,它是 2015 年的(“版权所有 2015 Google Inc”),并且 Google 此后发布了另一个用于启动 2022 年 GCE 实例的代码示例。它可以在 github here 上找到,而这个较新的代码示例
create_instance
函数是 GCP 的 python API 文档中的特色功能这里

但是,我不知道如何使用现代 python 函数通过元数据传递启动脚本以在虚拟机启动时运行。我尝试添加

    instance_client.metadata.items = {'key': 'startup-script',
                                      'value': job_script}

create.py
函数(同样,可用 here 以及它调用的支持实用函数),但它抛出了一个错误,表明
instance_client
没有该属性。

使用启动脚本启动 GCE VM 的 GCP 文档页面位于 here,与大多数其他类似页面不同,它仅包含

console
gcloud
和 (REST)
API
的代码片段;不是 SDK 代码片段,例如Python 和 Ruby 可能会展示如何修改上面的 python
create_instance
函数。

从 python 进程中使用启动脚本启动 GCE VM 的最佳实践是否真的是发送 post 请求或只是包装

gcloud
命令

gcloud compute instances create VM_NAME \
  --image-project=debian-cloud \
  --image-family=debian-10 \
  --metadata-from-file=startup-script=FILE_PATH

...在

subprocess.run()
?老实说,我不介意这样做,因为代码是如此紧凑(至少是 gcloud 命令,而不是 POST 请求方式),但由于 GCP 提供了一个
create_instance
python 函数,我假设使用/修改-as -这将是 python 内部的最佳实践...

谢谢!

python google-cloud-platform virtual-machine google-compute-engine infrastructure-as-code
2个回答
3
投票

因此,使用 Python 库创建

--metadata-from-file=startup-scripts=${FILE_PATH}
等价物的最简单(!)方法可能是:

from google.cloud import compute_v1

instance = compute_v1.Instance()

metadata = compute_v1.Metadata()
metadata.items = [
    {
        "key":"startup-script",
        "value":'#!/usr/bin/env bash\necho "Hello Freddie"'
    }
]

instance.metadata = metadata

另一种方法是:

metadata = compute_v1.Metadata()

items = compute_v1.types.Items()
items.key = "startup-script"
items.value = """
#!/usr/bin/env bash

echo "Hello Freddie"
"""

metadata.items = [items]

注意 在示例中,为了方便起见,我将

FILE_PATH
的内容嵌入到脚本中,但当然,您可以使用 Python 的
open
来获得更具可比性的结果。

如果您有一个库|SDK 来调用功能,那么使用库|SDK 通常比使用subprocess 来调用二进制文件更好。正如评论中提到的,主要原因是特定于语言的调用可以让您键入(更多地使用类型化语言)、受控执行(例如

try
)和错误处理。当您调用
subprocess
时,它的基于字符串的流会一直向下。
我同意使用类的计算引擎的 Python 库感觉很麻烦,但是,当您编写脚本时,重点可能是更明确的定义的长期好处,而不是表达能力的短期痛苦。如果你只是想插入一个虚拟机,请务必使用 

gcloud compute instances create

(我在 Bash 中这样做

all
the time),但是,如果你想使用像 Python 这样更优雅的语言,那么我鼓励你完全使用Python。

CURIOSITY

gcloud 是用 Python 编写的。如果您使用 Python

subprocess
调用
gcloud
命令,则您正在使用 Python 调用运行 Python 的 shell 来进行 REST 调用;-)

    


0
投票

import googleapiclient.discovery from google.oauth2 import service_account compute = googleapiclient.discovery.build('compute', 'v1', credentials=GCPClass.credentials) metadata_response = compute.instances().get(project=GCPClass.project, zone=zone, instance=vmname).execute() current_fingerprint = metadata_response['metadata']['fingerprint'] compute_client = compute_v1.InstancesClient(credentials=GCPClass.credentials) metadata = compute_v1.Metadata() items = compute_v1.types.Items() items.key = "startup-script" items.value = script_content_crlf metadata.fingerprint = current_fingerprint metadata.items = [items] request = { "project": GCPClass.project, "zone": zone, "instance": vmname, "metadata_resource": metadata } response = compute_client.set_metadata(**request) print(response.status, response.result(), response.done())

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.