ssh - 在 openstack 中使用 jclouds 传递 ssh key

标签 ssh openstack jclouds

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

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

最佳答案

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

CreateServerOptions.Builder.keyPairName(KEY_NAME)

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

关于ssh - 在 openstack 中使用 jclouds 传递 ssh key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19733905/

相关文章:

java - 提供的源组件数量 (78) 超过最大数量 (32)

cloud - jclouds 支持更新的 vCloud API

linux - 在 SSH 中使用本地和远程变量

openstack - 无法在 glusterfs : Transport endpoint is not connected 中进行对等探测

linux - 从 Expect 捕获返回码

openstack - 如何登录部署在 Openstack 基础架构上的 ubuntu-cloud 实例

hadoop - 在openstack中安装Hadoop

java - 获取 OpenStack 服务器的标签

visual-studio-code - 寻找类似于 VS Code Remote SSH Extension 的 PyCharm Remote Dev 体验

Ruby Sinatra - 为公共(public)文件夹中的文件添加自定义路由