1

我使用 jclouds 在 openstack 中创建了一个服务器。虽然我可以创建服务器,但我还想传递我的公共 ssh 密钥,以便在云初始化完成后连接到服务器。下面是我的代码。

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());
   }*/
}
4

1 回答 1

1

我不知道,如何在客户端运行时指定新密钥,但要使用预先上传到 Openstack 的预定义公钥,使用方法

CreateServerOptions.Builder.keyPairName(KEY_NAME)

公钥列表可在 Openstack 仪表板的“访问和安全”>“密钥对”中找到。实际上,我不确定是否可以为实例指定新密钥,因为手动创建实例的仪表板表单不支持新密钥上传。但是,它导入了一个新的 NAMED 密钥,在创建实例后,它被添加到上面提到的公钥列表中。

于 2014-07-15T15:41:43.553 回答