You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@jclouds.apache.org by Everett Toews <no...@github.com> on 2014/01/13 05:51:02 UTC

[jclouds-examples] Example of creating a server with a key pair (#30)

You can merge this Pull Request by running:

  git pull https://github.com/rackerlabs/jclouds-examples rax-create-server-with-private-key

Or you can view, comment on it, or merge it online at:

  https://github.com/jclouds/jclouds-examples/pull/30

-- Commit Summary --

  * Example of creating a server with a key pair

-- File Changes --

    M rackspace/src/main/java/org/jclouds/examples/rackspace/SmokeTest.java (1)
    A rackspace/src/main/java/org/jclouds/examples/rackspace/cloudservers/CreateServerWithKeyPair.java (231)

-- Patch Links --

https://github.com/jclouds/jclouds-examples/pull/30.patch
https://github.com/jclouds/jclouds-examples/pull/30.diff

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/30

Re: [jclouds-examples] Example of creating a server with a key pair (#30)

Posted by Andrew Phillips <no...@github.com>.
> +import java.util.Properties;
> +import java.util.Set;
> +import java.util.concurrent.TimeoutException;
> +
> +import static com.google.common.base.Charsets.UTF_8;
> +import static org.jclouds.compute.config.ComputeServiceProperties.POLL_INITIAL_PERIOD;
> +import static org.jclouds.compute.config.ComputeServiceProperties.POLL_MAX_PERIOD;
> +import static org.jclouds.examples.rackspace.cloudservers.Constants.NAME;
> +import static org.jclouds.examples.rackspace.cloudservers.Constants.POLL_PERIOD_TWENTY_SECONDS;
> +import static org.jclouds.examples.rackspace.cloudservers.Constants.PROVIDER;
> +import static org.jclouds.examples.rackspace.cloudservers.Constants.ZONE;
> +import static org.jclouds.scriptbuilder.domain.Statements.exec;
> +
> +/**
> + * Create a public key in the cloud and write the private key file to the local working directory. The public key and
> + * private key together are known as a key pair (see http://en.wikipedia.org/wiki/Public-key_cryptography). This

Make `http://en.wikipedia.org/wiki/Public-key_cryptography` a link?

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/30/files#r8825456

Re: [jclouds-examples] Example of creating a server with a key pair (#30)

Posted by Everett Toews <no...@github.com>.
> +   }
> +
> +   /**
> +    * Delete the public key in the cloud and the local private key.
> +    */
> +   private void deleteKeyPair(KeyPair keyPair) {
> +      System.out.format("  Delete Key Pair%n");
> +
> +      KeyPairApi keyPairApi = nova.getApi().getKeyPairExtensionForZone(ZONE).get();
> +      keyPairApi.delete(keyPair.getName());
> +
> +      if (keyPairFile.delete()) {
> +         System.out.format("    Deleted %s%n", keyPairFile.getAbsolutePath());
> +      }
> +      else {
> +         System.out.format("    Could not delete %s%n", keyPairFile.getAbsolutePath());

Well this isn't using logging but point taken. Changed it to use System.err.

Fixed.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/30/files#r8831781

Re: [jclouds-examples] Example of creating a server with a key pair (#30)

Posted by Andrew Phillips <no...@github.com>.
> +
> +   /**
> +    * Delete the public key in the cloud and the local private key.
> +    */
> +   private void deleteKeyPair(KeyPair keyPair) {
> +      System.out.format("  Delete Key Pair%n");
> +
> +      KeyPairApi keyPairApi = nova.getApi().getKeyPairExtensionForZone(ZONE).get();
> +      keyPairApi.delete(keyPair.getName());
> +
> +      if (keyPairFile.delete()) {
> +         System.out.format("    Deleted %s%n", keyPairFile.getAbsolutePath());
> +      }
> +      else {
> +         System.out.format("    Could not delete %s%n", keyPairFile.getAbsolutePath());
> +

[minor] Blank line?

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/30/files#r8825987

Re: [jclouds-examples] Example of creating a server with a key pair (#30)

Posted by Andrew Phillips <no...@github.com>.
> +   public CreateServerWithKeyPair(String username, String apiKey) {
> +      Iterable<Module> modules = ImmutableSet.<Module> of(new SshjSshClientModule());
> +
> +      // These properties control how often jclouds polls for a status update
> +      Properties overrides = new Properties();
> +      overrides.setProperty(POLL_INITIAL_PERIOD, POLL_PERIOD_TWENTY_SECONDS);
> +      overrides.setProperty(POLL_MAX_PERIOD, POLL_PERIOD_TWENTY_SECONDS);
> +
> +      ComputeServiceContext context = ContextBuilder.newBuilder(PROVIDER)
> +            .credentials(username, apiKey)
> +            .overrides(overrides)
> +            .modules(modules)
> +            .buildView(ComputeServiceContext.class);
> +
> +      computeService = context.getComputeService();
> +      nova = computeService.getContext().unwrap();

I'm guessing we're not using `unwrapApi` because of the jclouds version?

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/30/files#r8825748

Re: [jclouds-examples] Example of creating a server with a key pair (#30)

Posted by Everett Toews <no...@github.com>.
> +import static org.jclouds.examples.rackspace.cloudservers.Constants.NAME;
> +import static org.jclouds.examples.rackspace.cloudservers.Constants.POLL_PERIOD_TWENTY_SECONDS;
> +import static org.jclouds.examples.rackspace.cloudservers.Constants.PROVIDER;
> +import static org.jclouds.examples.rackspace.cloudservers.Constants.ZONE;
> +import static org.jclouds.scriptbuilder.domain.Statements.exec;
> +
> +/**
> + * Create a public key in the cloud and write the private key file to the local working directory. The public key and
> + * private key together are known as a key pair (see http://en.wikipedia.org/wiki/Public-key_cryptography). This
> + * is a security feature that allows you to login to a server using a private key file.
> + *
> + * Create a server with the public key, use the private key to login to it, and disable password authentication.
> + */
> +public class CreateServerWithKeyPair implements Closeable {
> +   private final ComputeService computeService;
> +   private final RestContext<NovaApi, NovaAsyncApi> nova;

Fixed.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/30/files#r8831044

Re: [jclouds-examples] Example of creating a server with a key pair (#30)

Posted by Everett Toews <no...@github.com>.
> +   public CreateServerWithKeyPair(String username, String apiKey) {
> +      Iterable<Module> modules = ImmutableSet.<Module> of(new SshjSshClientModule());
> +
> +      // These properties control how often jclouds polls for a status update
> +      Properties overrides = new Properties();
> +      overrides.setProperty(POLL_INITIAL_PERIOD, POLL_PERIOD_TWENTY_SECONDS);
> +      overrides.setProperty(POLL_MAX_PERIOD, POLL_PERIOD_TWENTY_SECONDS);
> +
> +      ComputeServiceContext context = ContextBuilder.newBuilder(PROVIDER)
> +            .credentials(username, apiKey)
> +            .overrides(overrides)
> +            .modules(modules)
> +            .buildView(ComputeServiceContext.class);
> +
> +      computeService = context.getComputeService();
> +      nova = computeService.getContext().unwrap();

Correct.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/30/files#r8831629

Re: [jclouds-examples] Example of creating a server with a key pair (#30)

Posted by Everett Toews <no...@github.com>.
> +
> +   /**
> +    * Delete the public key in the cloud and the local private key.
> +    */
> +   private void deleteKeyPair(KeyPair keyPair) {
> +      System.out.format("  Delete Key Pair%n");
> +
> +      KeyPairApi keyPairApi = nova.getApi().getKeyPairExtensionForZone(ZONE).get();
> +      keyPairApi.delete(keyPair.getName());
> +
> +      if (keyPairFile.delete()) {
> +         System.out.format("    Deleted %s%n", keyPairFile.getAbsolutePath());
> +      }
> +      else {
> +         System.out.format("    Could not delete %s%n", keyPairFile.getAbsolutePath());
> +

Oops. Fixed.

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/30/files#r8831791

Re: [jclouds-examples] Example of creating a server with a key pair (#30)

Posted by Andrew Phillips <no...@github.com>.
Thanks for taking care of the nits, @everett-toews!

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/30#issuecomment-32192618

Re: [jclouds-examples] Example of creating a server with a key pair (#30)

Posted by Andrew Phillips <no...@github.com>.
> +import static org.jclouds.examples.rackspace.cloudservers.Constants.NAME;
> +import static org.jclouds.examples.rackspace.cloudservers.Constants.POLL_PERIOD_TWENTY_SECONDS;
> +import static org.jclouds.examples.rackspace.cloudservers.Constants.PROVIDER;
> +import static org.jclouds.examples.rackspace.cloudservers.Constants.ZONE;
> +import static org.jclouds.scriptbuilder.domain.Statements.exec;
> +
> +/**
> + * Create a public key in the cloud and write the private key file to the local working directory. The public key and
> + * private key together are known as a key pair (see http://en.wikipedia.org/wiki/Public-key_cryptography). This
> + * is a security feature that allows you to login to a server using a private key file.
> + *
> + * Create a server with the public key, use the private key to login to it, and disable password authentication.
> + */
> +public class CreateServerWithKeyPair implements Closeable {
> +   private final ComputeService computeService;
> +   private final RestContext<NovaApi, NovaAsyncApi> nova;

`novaApi`? `novaCtx`?

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/30/files#r8825469

Re: [jclouds-examples] Example of creating a server with a key pair (#30)

Posted by Everett Toews <no...@github.com>.
> +import java.util.Properties;
> +import java.util.Set;
> +import java.util.concurrent.TimeoutException;
> +
> +import static com.google.common.base.Charsets.UTF_8;
> +import static org.jclouds.compute.config.ComputeServiceProperties.POLL_INITIAL_PERIOD;
> +import static org.jclouds.compute.config.ComputeServiceProperties.POLL_MAX_PERIOD;
> +import static org.jclouds.examples.rackspace.cloudservers.Constants.NAME;
> +import static org.jclouds.examples.rackspace.cloudservers.Constants.POLL_PERIOD_TWENTY_SECONDS;
> +import static org.jclouds.examples.rackspace.cloudservers.Constants.PROVIDER;
> +import static org.jclouds.examples.rackspace.cloudservers.Constants.ZONE;
> +import static org.jclouds.scriptbuilder.domain.Statements.exec;
> +
> +/**
> + * Create a public key in the cloud and write the private key file to the local working directory. The public key and
> + * private key together are known as a key pair (see http://en.wikipedia.org/wiki/Public-key_cryptography). This

These are just raw source code examples meant to be viewed as such. Adding markup for a link would just be distracting. 

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/30/files#r8831036

Re: [jclouds-examples] Example of creating a server with a key pair (#30)

Posted by Andrew Phillips <no...@github.com>.
> +import java.util.Properties;
> +import java.util.Set;
> +import java.util.concurrent.TimeoutException;
> +
> +import static com.google.common.base.Charsets.UTF_8;
> +import static org.jclouds.compute.config.ComputeServiceProperties.POLL_INITIAL_PERIOD;
> +import static org.jclouds.compute.config.ComputeServiceProperties.POLL_MAX_PERIOD;
> +import static org.jclouds.examples.rackspace.cloudservers.Constants.NAME;
> +import static org.jclouds.examples.rackspace.cloudservers.Constants.POLL_PERIOD_TWENTY_SECONDS;
> +import static org.jclouds.examples.rackspace.cloudservers.Constants.PROVIDER;
> +import static org.jclouds.examples.rackspace.cloudservers.Constants.ZONE;
> +import static org.jclouds.scriptbuilder.domain.Statements.exec;
> +
> +/**
> + * Create a public key in the cloud and write the private key file to the local working directory. The public key and
> + * private key together are known as a key pair (see http://en.wikipedia.org/wiki/Public-key_cryptography). This

OK

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/30/files#r8831272

Re: [jclouds-examples] Example of creating a server with a key pair (#30)

Posted by Andrew Phillips <no...@github.com>.
> +   }
> +
> +   /**
> +    * Delete the public key in the cloud and the local private key.
> +    */
> +   private void deleteKeyPair(KeyPair keyPair) {
> +      System.out.format("  Delete Key Pair%n");
> +
> +      KeyPairApi keyPairApi = nova.getApi().getKeyPairExtensionForZone(ZONE).get();
> +      keyPairApi.delete(keyPair.getName());
> +
> +      if (keyPairFile.delete()) {
> +         System.out.format("    Deleted %s%n", keyPairFile.getAbsolutePath());
> +      }
> +      else {
> +         System.out.format("    Could not delete %s%n", keyPairFile.getAbsolutePath());

Is this a "WARN" or so..?

---
Reply to this email directly or view it on GitHub:
https://github.com/jclouds/jclouds-examples/pull/30/files#r8825979