Search code examples
google-compute-enginejclouds

Google compute project-wide SSH keys with jclouds


I'm trying to launch google compute instances from java code using jclouds. It's mostly working, however I'd like to use the project-wide SSH key I've defined instead of having jclouds generate a new user/key credential.

According to the README here - https://github.com/apache/jclouds/tree/master/providers/google-compute-engine:

For an instance to be ssable one of the following must happen: 1 - the project's metadata has an adequately built "sshKeys" entry and a corresponding private key is provided in GoogleComputeEngineTemplateOptions when createNodesInGroup is called. 2 - an instance of GoogleComputeEngineTemplateOptions with an adequate public and private key is provided.

I'm trying to do 1) above. I've correctly configured the project's metadata (I can use it to connect to manually-created instances that don't have jclouds-generated credentials), but I can't work out how to provide that key to GoogleComputeEngineTemplateOptions?

Neither GoogleComputeEngineTemplateOptions.Builder.installPrivateKey(String key) or GoogleComputeEngineTemplateOptions.Builder.overrideLoginPrivateKey(String key) seem to work.

The documentation is pretty sparse - anyone know how to do it?


Solution

  • jclouds will create a key by default if you don't provide one. You could use the following to provide your auth private key and tell jclouds not to generate a new one:

    TemplateOptions opts = computeService.templateOptions()
        .as(GoogleComputeEngineTemplateOptions.class)
        .overrideLoginPrivateKey(privateKey)
        .autoCreateKeyPair(false);