如何从 Cloud Functions 中的实例模板创建 VM 实例?
How to create a VM instance from an instance template within Cloud Functions?
我需要根据我制作的模板启动一个 Google 计算实例,该模板有一个启动脚本,该脚本从我的服务器下载最新的游戏服务器可执行文件并 运行s 它。这一切都很好。
现在,我的自定义匹配器将确定所有当前游戏(即游戏服务器的实例)是否已满,如果是,我希望它 运行 一个 Cloud Function,它从我在上面提到的模板(它基本上为 12 名玩家扮演 lobby/game)。创建实例后,我需要云函数 return 将新创建实例的 IP 返回到调用它的任何内容(这将是我的游戏)。
我知道第一部分可以通过 HTTP POST
实现,但我在云函数中找不到任何地方 docs/compute docs/admin 允许我创建实例并获取 IP 的 SDK 文档,这可能吗?
编辑:我找到了这个 documentation 但我还没有找到从模板启动 VM 的函数,然后 return 是 VM 的对象 - 其中包括它的 IP...
您可以直接使用API。先创建VM,然后等待运行状态获取内外IP
async function main() {
const auth = new GoogleAuth({
scopes: 'https://www.googleapis.com/auth/cloud-platform'
});
const client = await auth.getClient();
const url = `https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/europe-west1-b/instances`
const template= 'projects/PROJECT_ID/global/instanceTemplates/instance-template-1';
const instanceName = 'example-instance'
const body= '{ "name": "' + instanceName + '" }'
let res = await client.request({ url: url + "?sourceInstanceTemplate=" + template,
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: body
});
res = await client.request({ url: url + "/" + instanceName,
method: 'GET'});
while (res.data['status'] != 'RUNNING') {
setTimeout(function(){},1000)
res = await client.request({ url: url + "/" + instanceName,
method: 'GET'});
}
//Internal Ip (interface0)
console.log(res.data.networkInterfaces[0].networkIP);
//External Ip
console.log(res.data.networkInterfaces[0].accessConfigs[0].natIP);
}
main().catch(console.error);
我的 NodeJs 技能很低(风格、格式、惯用语...),但这很有效。
我需要根据我制作的模板启动一个 Google 计算实例,该模板有一个启动脚本,该脚本从我的服务器下载最新的游戏服务器可执行文件并 运行s 它。这一切都很好。
现在,我的自定义匹配器将确定所有当前游戏(即游戏服务器的实例)是否已满,如果是,我希望它 运行 一个 Cloud Function,它从我在上面提到的模板(它基本上为 12 名玩家扮演 lobby/game)。创建实例后,我需要云函数 return 将新创建实例的 IP 返回到调用它的任何内容(这将是我的游戏)。
我知道第一部分可以通过 HTTP POST
实现,但我在云函数中找不到任何地方 docs/compute docs/admin 允许我创建实例并获取 IP 的 SDK 文档,这可能吗?
编辑:我找到了这个 documentation 但我还没有找到从模板启动 VM 的函数,然后 return 是 VM 的对象 - 其中包括它的 IP...
您可以直接使用API。先创建VM,然后等待运行状态获取内外IP
async function main() {
const auth = new GoogleAuth({
scopes: 'https://www.googleapis.com/auth/cloud-platform'
});
const client = await auth.getClient();
const url = `https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/europe-west1-b/instances`
const template= 'projects/PROJECT_ID/global/instanceTemplates/instance-template-1';
const instanceName = 'example-instance'
const body= '{ "name": "' + instanceName + '" }'
let res = await client.request({ url: url + "?sourceInstanceTemplate=" + template,
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: body
});
res = await client.request({ url: url + "/" + instanceName,
method: 'GET'});
while (res.data['status'] != 'RUNNING') {
setTimeout(function(){},1000)
res = await client.request({ url: url + "/" + instanceName,
method: 'GET'});
}
//Internal Ip (interface0)
console.log(res.data.networkInterfaces[0].networkIP);
//External Ip
console.log(res.data.networkInterfaces[0].accessConfigs[0].natIP);
}
main().catch(console.error);
我的 NodeJs 技能很低(风格、格式、惯用语...),但这很有效。