首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >gce启动实例python api使用示例指南

gce启动实例python api使用示例指南
EN

Stack Overflow用户
提问于 2016-11-23 20:05:49
回答 2查看 756关注 0票数 1

我试图复制在谷歌网站https://cloud.google.com/compute/docs/tutorials/python-guide上找到的以下指南

我不知道桶和计算是什么意思。我需要向脚本中传递什么类型的参数?如果我想开始我的私有镜像,我应该在getFromFamily( project='debian-cloud',family='debian-8').execute()中放什么?我是不是像下面这样传入变量?

代码语言:javascript
复制
create_instance(compute,projectname, us-east1-a, instance-1, bucket)

我简化了脚本,如下所示。我去掉了我不需要的参数。

代码语言:javascript
复制
def create_instance(compute, project, zone, name, bucket):

image_response = compute.images().getFromFamily(
    project='debian-cloud', family='debian-8').execute()
source_disk_image = image_response['selfLink']

# Configure the machine
machine_type = "us-east1-b/%s/machineTypes/f1-micro" % zone
startup_script = open(
    os.path.join(
        os.path.dirname(__file__), 'startup-script.sh'), 'r').read()

config = {
    'name': instance-1,
    'machineType': f1-micro,

    # Specify the boot disk and the image to use as a source.
    'disks': [
        {
            'boot': True,
            'autoDelete': True,
            'initializeParams': {
                'sourceImage': /global/imagename,
            }
        }
    ],

    # Specify a network interface with NAT to access the public
    # internet.
    'networkInterfaces': [{
        'network': 'global/networks/default',
        'accessConfigs': [
            {'type': 'ONE_TO_ONE_NAT', 'name': 'External NAT'}
        ]
    }],

    # Allow the instance to access cloud storage and logging.
    'serviceAccounts': [{
        'email': 'default',
        'scopes': [
            'https://www.googleapis.com/auth/devstorage.read_write',

        ]
    }],

    # Metadata is readable from the instance and allows you to
    # pass configuration from deployment scripts to instances.
    'metadata': {
        'items': [{
            # Startup script is automatically executed by the
            # instance upon startup.
            'key': 'startup-script',
            'value': startup_script
        },  {
            'key': 'bucket',
            'value': bucket
        }]
    }
}

return compute.instances().insert(
    project=project,
    zone=zone,
    body=config).execute()
EN

回答 2

Stack Overflow用户

发布于 2017-03-01 19:37:05

这在这里已经有一段时间了,但这里有一些信息。

仅当您希望实例具有共享存储位置(文档here)时,才需要bucket。如果不需要存储桶,则应该删除元数据bucket键值(尽管我很惊讶地看到使用假存储桶名称创建实例不会失败)。

compute是经过身份验证的客户端对象,用于与“计算引擎”服务进行交互。要获取客户端,您需要遵循这些instructions来获取密钥、密钥和刷新令牌。下面是一个代码示例:

代码语言:javascript
复制
from googleapiclient import discovery
from oauth2client.client import GoogleCredentials
from oauth2client import GOOGLE_TOKEN_URI
credentials = GoogleCredentials(access_token=None, client_id='your-client-id', 
                                client_secret='your-client-secret',
                                refresh_token='your-refresh-token',
                                token_expiry=None, token_uri=GOOGLE_TOKEN_URI,
                                user_agent='Python client library')
compute = discovery.build('compute', 'v1', credentials=credentials)

至于私有镜像,如果您想使用相同的代码,那么您需要在您的GCE console中找到并识别它们,并提供projectfamily标识符。您也可以使用该接口按名称获取镜像:

image = compute.images().get(project='your-project-id', image='your-image-name').execute(),其中需要提供与实例config中的sourceImage相同的image['selfLink']

票数 3
EN

Stack Overflow用户

发布于 2020-11-09 03:13:03

这个例子已经过时了:注意oauth2client现在已经过时了,Google Auth是当前的客户端库。这是一个最新的解决方案。关键的一步是生成正确的更新json。这可以通过在授权的服务帐户下使用等效的REST命令转换您的预期实例来实现。

代码语言:javascript
复制
import google.auth
import googleapiclient.discovery
PROJECT_ID = "<your project ID>"
ZONE = "<your zone>"

credentials, _ = google.auth.default()
compute = googleapiclient.discovery.build('compute', 'v1', 
credentials=credentials)

config = "{generate your instance's json from equivalent REST}"

compute.instances().insert(project=PROJECT_ID, zone=ZONE, 
body=config).execute()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40764114

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档