首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >挂起并恢复GCP实例的GCP云功能

挂起并恢复GCP实例的GCP云功能
EN

Stack Overflow用户
提问于 2020-09-03 11:40:03
回答 2查看 1.1K关注 0票数 0

我们可以使用GCP云函数来启动和停止GCP实例,但是我需要使用云函数和调度器来执行GCP实例的计划挂起和恢复。

从GCP文档中,我了解到我们可以开始和停止使用https://github.com/GoogleCloudPlatform/nodejs-docs-samples/tree/master/functions/scheduleinstance下面可用的云功能。

是否有相同的节点JS或其他语言Pcgks可用于挂起和恢复GCP实例?

如果不能,我们可以创建自己的挂起/恢复。

当我尝试一个错误时,我得到了"TypeError: compute.zone(.).vm(.).resume不是一个函数“

编辑,谢谢克里斯和纪尧姆,在浏览了你的链接后,我编辑了我的代码,下面是我的index.js文件。由于某种原因,当我执行gcloud函数部署resumeInstancePubSub触发器主题简历实例运行时nodejs10允许未经身份验证。

我总是在提供的模块中没有定义函数'resumeInstancePubSub1‘。resumeInstancePubSub1 2020-09-04 10:57:00.333您指定了要执行的正确目标函数吗?

我以前没有使用过Node或JS,我期待类似的东西来启动/停止文档,我可以很容易地使用下面的git https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git来工作。

我的index.js文件

代码语言:javascript
复制
// BEFORE RUNNING:
// ---------------
// 1. If not already done, enable the Compute Engine API
//    and check the quota for your project at
//    https://console.developers.google.com/apis/api/compute
// 2. This sample uses Application Default Credentials for authentication.
//    If not already done, install the gcloud CLI from
//    https://cloud.google.com/sdk and run
//    `gcloud beta auth application-default login`.
//    For more information, see
//    https://developers.google.com/identity/protocols/application-default-credentials
// 3. Install the Node.js client library by running
//    `npm install googleapis --save`

const {google} = require('googleapis');
var compute = google.compute('beta');

authorize(function(authClient) {
  var request = {
    // Project ID for this request.
    project: 'my-project',  // TODO: Update placeholder value.

    // The name of the zone for this request.
    zone: 'my-zone',  // TODO: Update placeholder value.

    // Name of the instance resource to resume.
    instance: 'my-instance',  // TODO: Update placeholder value.

    resource: {
      // TODO: Add desired properties to the request body.
    },

    auth: authClient,
  };
exports.resumeInstancePubSub = async (event, context, callback) => {
try {
    const payload = _validatePayload(
      JSON.parse(Buffer.from(event.data, 'base64').toString())
    );
    const options = {filter: `labels.${payload.label}`};
    const [vms] = await compute.getVMs(options);
    await Promise.all(
      vms.map(async (instance) => {
        if (payload.zone === instance.zone.id) {
          const [operation] = await compute
            .zone(payload.zone)
            .vm(instance.name)
            .resume();

          // Operation pending
          return operation.promise();
        }
      })
    );
          // Operation complete. Instance successfully started.
    const message = `Successfully started instance(s)`;
    console.log(message);
    callback(null, message);
  } catch (err) {
    console.log(err);
    callback(err);
  }
};
 compute.instances.resume(request, function(err, response) {
    if (err) {
      console.error(err);
      return;
    }

    // TODO: Change code below to process the `response` object:
    console.log(JSON.stringify(response, null, 2));
  });
});

function authorize(callback) {
  google.auth.getClient({
    scopes: ['https://www.googleapis.com/auth/cloud-platform']
  }).then(client => {
    callback(client);
  }).catch(err => {
    console.error('authentication failed: ', err);
  });
}
EN

回答 2

Stack Overflow用户

发布于 2020-09-03 11:46:54

这里这里是api的新beta版本的文档。您可以看到可以挂起如下所示的实例:

代码语言:javascript
复制
compute.instances.suspend(request, function(err, response) {
    if (err) {
      console.error(err);
      return;
    }

您可以以类似的方式恢复一个实例:

代码语言:javascript
复制
 compute.instances.resume(request, function(err, response) {
    if (err) {
      console.error(err);
      return;
    }
票数 0
EN

Stack Overflow用户

发布于 2021-12-14 11:18:43

GCP最近添加了"create“功能,以根据配置的计划启动和停止VM实例。更多详情可在

时间表

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63722888

复制
相关文章

相似问题

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