简体   繁体   中英

Pass ssh key using jclouds in openstack

I used jclouds to create a server in openstack. Although I am able to create the server, I also want to pass my public ssh key in order to connect to the server after cloud init is finished. Below is my code.

package org.chris.jcloud;

import static com.google.common.io.Closeables.closeQuietly;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.util.Set;
import java.util.concurrent.TimeoutException;

import org.jclouds.ContextBuilder;
import org.jclouds.compute.ComputeService;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.domain.TemplateBuilder;
import org.jclouds.compute.options.RunScriptOptions;
import org.jclouds.io.Payloads;
import org.jclouds.logging.slf4j.config.SLF4JLoggingModule;
import org.jclouds.openstack.nova.v2_0.NovaApi;
import org.jclouds.openstack.nova.v2_0.NovaAsyncApi;
import org.jclouds.openstack.nova.v2_0.domain.Server;
import org.jclouds.openstack.nova.v2_0.domain.ServerCreated;
import org.jclouds.openstack.nova.v2_0.features.ServerApi;
import org.jclouds.openstack.nova.v2_0.options.CreateServerOptions;
import org.jclouds.predicates.SocketOpen;
import org.jclouds.rest.RestContext;
import org.jclouds.scriptbuilder.ScriptBuilder;

import com.google.common.collect.ImmutableSet;
import com.google.inject.Module;
import com.google.common.base.Predicate;
import com.google.common.io.Closeables;
import com.google.common.net.HostAndPort;

import org.jclouds.compute.RunNodesException;
import org.jclouds.compute.domain.Template;
import org.jclouds.scriptbuilder.domain.OsFamily;
import org.jclouds.sshj.config.SshjSshClientModule;

import static java.util.concurrent.TimeUnit.SECONDS;
import static org.jclouds.compute.config.ComputeServiceProperties.POLL_INITIAL_PERIOD;
import static org.jclouds.compute.config.ComputeServiceProperties.POLL_MAX_PERIOD;
import static org.jclouds.compute.options.TemplateOptions.Builder.authorizePublicKey;

public  class JClouds implements Closeable {
   private ComputeService compute;
   private RestContext<NovaApi, NovaAsyncApi> nova;
   private Set<String> zones;

   public static void main(String[] args) throws IOException {
      JClouds jCloudsNova = new JClouds();

      try {
         jCloudsNova.init();
         jCloudsNova.listServers();
         jCloudsNova.close();
         jCloudsNova.createServers();
      }
      catch (Exception e) {
         e.printStackTrace();
      }
      finally {
         jCloudsNova.close();
      }
   }

   private void init() {
      Iterable<Module> modules = ImmutableSet.<Module> of(new SLF4JLoggingModule());

      String provider = "openstack-nova";
      String identity = "admin:admin"; // tenantName:userName
      String password = "test"; // demo account uses ADMIN_PASSWORD too

      ComputeServiceContext context = ContextBuilder.newBuilder(provider)
            .endpoint("http://192.168.1.33:5000/v2.0/")
            .credentials(identity, password)
            .modules(modules)
            .buildView(ComputeServiceContext.class);
      compute = context.getComputeService();
      nova = context.unwrap();
      zones = nova.getApi().getConfiguredZones();
   }

   private void listServers() {
      for (String zone: zones) {
         ServerApi serverApi = nova.getApi().getServerApiForZone(zone);

         System.out.println("Servers in " + zone);

         for (Server server: serverApi.listInDetail().concat()) {
            System.out.println("  " + server);
         }
      }
   }

   private void createServers() {
    for (String zone : zones) {
        ServerApi serverApi = nova.getApi().getServerApiForZone(zone);
        CreateServerOptions sv = CreateServerOptions.Builder.adminPass("test");
        ServerCreated newServer = serverApi.create("paparia", "ab8fbee6-4907-4e59-ba77-471362bc8200", "1", sv);
    //  TemplateBuilder templateBuilder = compute.templateBuilder();
    //  Template template = templateBuilder.options(authorizePublicKey(Payloads.newPayload(new File("/home/me/.ssh/id_rsa.pub")).toString())).build();


        System.out.println("Servers in " + zone);
        listServers();
    }
}

@Override
public void close() throws IOException {
    // TODO Auto-generated method stub

}



   /*public void close() {
      closeQuietly(compute.getContext());
   }*/
}

I dunno, how you can specify a new key during client runtime, but to use a predefined public key, previously uploaded to Openstack, use method

CreateServerOptions.Builder.keyPairName(KEY_NAME)

The list of public keys is available in the Openstack dashboard at "Access & Security" > "Keypairs". I'm actually not sure, if you can specify a new key for an instance, since the dashboard form for a manual instance creation doesn't support new key uploading. It's got an import of a new NAMED key instead though, which is added to the list of public keys mentioned above, after the instance is created.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM