简体   繁体   English

使用Python API创建GCE实例

[英]Create GCE instance using Python API

I want to create a Google Compute Engine instance using the Python api. 我想使用Python API创建一个Google Compute Engine实例。 In particular, I want the source image to be Ubuntu 14.04. 特别是,我希望源映像为Ubuntu 14.04。

In the doc, it is written to specify the json body of the request as follows: 在文档中,编写该代码以指定请求的json主体,如下所示:

{
  "name": "example-instance",
  "machineType": "zones/us-central1-f/machineTypes/f1-micro"
  "networkInterfaces": [{
    "accessConfigs": [{
      "type": "ONE_TO_ONE_NAT",
      "name": "External NAT"
     }],
    "network": "global/networks/default"
  }],
  "disks": [{
     "autoDelete": "true",
     "boot": "true",
     "type": "PERSISTENT",
     "initializeParams": {
        "sourceImage": "projects/debian-cloud/global/images/v20150818"
     }
   }]
}

How can I modify the sourceImage parameter value so that it points to the ubuntu 14.04 image? 如何修改sourceImage参数值,使其指向ubuntu 14.04图像? Indeed, the syntax "projects/debian-cloud/global/images/v20150818" is too specific to understand how to use it for other public images. 实际上,语法“ projects / debian-cloud / global / images / v20150818”太具体了,无法理解如何将其用于其他公共图像。

I am guessing you're referring to these docs ( source code ) which use the following URL format: 我猜您是在参考使用以下URL格式的这些文档源代码 ):

"projects/debian-cloud/global/images/debian-7-wheezy-v20150320"

The pattern here is: 这里的模式是:

"projects/${PROJECT}/global/images/${IMAGE}"

You can find the available images via: 您可以通过以下方式找到可用的图像:

$ gcloud compute images list

which will show you output similar to the following: 这将向您显示类似于以下内容的输出:

NAME                                PROJECT            ALIAS              DEPRECATED STATUS
centos-6-v20151104                  centos-cloud       centos-6                      READY
centos-7-v20151104                  centos-cloud       centos-7                      READY
coreos-alpha-891-0-0-v20151211      coreos-cloud                                     READY
coreos-beta-877-1-0-v20151202       coreos-cloud                                     READY
coreos-stable-835-9-0-v20151208     coreos-cloud       coreos                        READY
backports-debian-7-wheezy-v20151104 debian-cloud       debian-7-backports            READY
debian-7-wheezy-v20151104           debian-cloud       debian-7                      READY
debian-8-jessie-v20151104           debian-cloud       debian-8                      READY
container-vm-v20151103              google-containers  container-vm                  READY
opensuse-13-1-v20150822             opensuse-cloud     opensuse-13                   READY
opensuse-13-2-v20150511             opensuse-cloud     opensuse-13                   READY
opensuse-leap-42-1-v20151124        opensuse-cloud                                   READY
rhel-6-v20151104                    rhel-cloud         rhel-6                        READY
rhel-7-v20151104                    rhel-cloud         rhel-7                        READY
sles-11-sp4-v20150714               suse-cloud         sles-11                       READY
sles-12-sp1-v20151215               suse-cloud         sles-12                       READY
ubuntu-1204-precise-v20151119       ubuntu-os-cloud    ubuntu-12-04                  READY
ubuntu-1404-trusty-v20151113        ubuntu-os-cloud    ubuntu-14-04                  READY
ubuntu-1504-vivid-v20151120         ubuntu-os-cloud    ubuntu-15-04                  READY
ubuntu-1510-wily-v20151114          ubuntu-os-cloud                                  READY
windows-server-2008-r2-dc-v20151006 windows-cloud      windows-2008-r2               READY
windows-server-2012-r2-dc-v20151006 windows-cloud      windows-2012-r2               READY

So in the case of Ubuntu 14.04, the image path would be: 因此,对于Ubuntu 14.04,映像路径为:

"projects/ubuntu-os-cloud/global/images/ubuntu-1404-trusty-v20151113"

You can also find out more metadata about an image via: 您还可以通过以下方式查找有关图像的更多元数据:

$ gcloud compute images describe --project ubuntu-os-cloud ubuntu-1404-trusty-20151113

and the selfLink attribute in the output will provide a full URL to the image. 并且输出中的selfLink属性将提供图像的完整URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM