[英]Google Cloud Compute Engine API: createVM directly with setMetadata
I use @google-cloud/compute to create VM instances automatically. 我使用@ google-cloud / compute自动创建VM实例。
Also I use startup scripts in those instances. 我还在那些实例中使用启动脚本。
So, firstly I call Zone.createVM and then VM.setMetadata . 因此,我首先调用Zone.createVM ,然后调用VM.setMetadata 。
But in some regions startup script is not running. 但是在某些地区,启动脚本未运行。 And it is running after VM reset, so looks like my VM.setMetadata call is just too late. 而且它在VM重置后正在运行,因此看来我的VM.setMetadata调用为时已晚。
In the web-interface we can create VM directly with metadata. 在Web界面中,我们可以直接使用元数据创建VM。 But I do not see this ability in API. 但我在API中看不到此功能。
Can it be done with API? 可以用API完成吗?
To set up a startup script during instance deployment you can provide it as part of the metadata property in the API call: 要在实例部署期间设置启动脚本,您可以在API调用中将其作为元数据属性的一部分提供:
POST https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-a/instances
{
...
"metadata": {
"items": [
{
"key": "startup-script",
"value": "#! /bin/bash\n\n# Installs apache and a custom homepage\napt-get update\napt-get install -y apache2\ncat <<EOF > /var/www/html/index.html\n<html><body><h1>Hello World</h1>\n<p>This page was created from a simple start up script!</p>\n</body></html>"
}
]
}
...
}
See the full reference for the resource "compute.instances" of the Compute Engine API here . 请在此处参阅Compute Engine API的资源“ compute.instances”的完整参考。
Basically, if you are using a Nodejs library to create the instance you are already calling this, so you will only need to add the metadata keys as documented. 基本上,如果您正在使用Nodejs库创建实例,那么您已经在调用它,因此您只需要添加记录的元数据键即可。
Also, if you are doing this frequently I guess it would be more practical if you stored the script in a bucket in GCP and simply add the URI to the metadata like this: 另外,如果您经常这样做,我想如果将脚本存储在GCP的存储桶中,然后将URI像这样简单地添加到元数据中,将更加实用:
"metadata": {
"items": [
{
"key": "startup-script-url",
"value": "gs://bucket/myfile"
}
]
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.