【问题标题】:gce launch instance python api using example guidegce 启动实例 python api 使用示例指南
【发布时间】:2017-04-07 10:36:29
【问题描述】:

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

我无法弄清楚存储桶和计算的含义。我需要将什么样的参数传递给脚本? 我应该在 getFromFamily( project='debian-cloud', family='debian-8').execute() 如果我想启动我的私有镜像? 我是否像下面这样传递变量?

create_instance(compute,projectname, us-east1-a, instance-1, bucket)

我已将脚本简化如下。我取出了我认为不需要的参数。

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()

【问题讨论】:

    标签: python api google-cloud-platform createinstance google-cloud-python


    【解决方案1】:

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

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

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

    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 标识符。您还可以使用 API 按名称获取图像:

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

    【讨论】:

      【解决方案2】:

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

      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()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-26
        • 2011-10-27
        • 1970-01-01
        • 1970-01-01
        • 2015-09-29
        • 2014-05-19
        相关资源
        最近更新 更多