google-compute-engine - 在谷歌计算引擎上启动和停止实例

标签 google-compute-engine google-cloud-platform jclouds

我想在谷歌计算引擎上启动/恢复和停止/暂停实例,但它给出了“java.lang.UnsupportedOperationException”。有没有其他方法 执行这些操作?

public class Example {

     public static void main(String[] args) 
     {
      String provider = "google-compute-engine";
      String identity = "****@developer.gserviceaccount.com";
      String credential = "path to private key";
      String groupName = "newgroup";
      credential = getCredentialFromJsonKeyFile(credential);
      Iterable<Module> modules = ImmutableSet.<Module> of(
              new SshjSshClientModule(),
              new SLF4JLoggingModule(),
              new EnterpriseConfigurationModule());
      ContextBuilder builder = ContextBuilder.newBuilder(provider)
              .credentials(identity, credential)
              .modules(modules);
      ComputeService compute=builder.buildView(ComputeServiceContext.class).getComputeService();

      compute.suspendNode("Instance id");
      //compute.suspendNodesMatching(Predicates.<NodeMetadata> and(inGroup(groupName)));
      System.out.println("suspended");
      compute.getContext().close();     
}

   private static String getCredentialFromJsonKeyFile(String filename) {
      try {
         String fileContents = Files.toString(new File(filename), UTF_8);
         Supplier<Credentials> credentialSupplier = new GoogleCredentialsFromJson(fileContents);
         String credential = credentialSupplier.get().credential;
         return credential;
      } catch (IOException e) {
         System.err.println("Exception reading private key from '%s': " + filename);
         e.printStackTrace();
         System.exit(1);
         return null;
      }
   }
}

输出:

suspending node(node id)

Exception in thread "main" java.lang.UnsupportedOperationException: suspend is not supported by GCE

at org.jclouds.googlecomputeengine.compute.GoogleComputeEngineServiceAdapter.suspendNode(GoogleComputeEngineServiceAdapter.java:251)

at org.jclouds.compute.strategy.impl.AdaptingComputeServiceStrategies.suspendNode(AdaptingComputeServiceStrategies.java:171)

at org.jclouds.compute.internal.BaseComputeService.suspendNode(BaseComputeService.java:503)

at org.jclouds.examples.compute.basics.Example.main(Example.java:79)

最佳答案

可移植 jclouds ComputeService 不直接支持它,但您可以从 ComputeServiceContext 获取 GoogleComputeEngineApi 和 InstanceApi,并在其中使用启动/停止方法。

仅供引用,有一个正在进行的补丁来添加对 ComputeService 中启动/停止操作的支持:https://github.com/jclouds/jclouds-labs-google/pull/141

关于google-compute-engine - 在谷歌计算引擎上启动和停止实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30435909/

相关文章:

javascript - 使用nodejs从firestore文档中删除字段

java - 无法使用最新的 OpenStack 验证 Swift ContextBuilder

google-compute-engine - 如何检索Google SDK密码

google-cloud-platform - 使用 Bitbucket 管道部署 GCloud App

google-compute-engine - 谷歌计算引擎: Internal DNS server and issues with the resolving

mysql - 如何修复 Docker Craft Cloud Run 部署中的 'CloudSQL connection failed: context deadline exceeded'

java - 使用 BlobStore 为对象生成临时 URI

jenkins - jclouds 与 GCE 无法连接 : -----END RSA PRIVATE KEY----- doesn't contain % line [-----END ]

node.js - 谷歌计算引擎外部IP

python - 在 GCE 上运行 selenium(chrome 驱动程序)时出错?