我使用jclouds在openstack中创建服务器。尽管我能够创建服务器,但我也想传递我的公共(public)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());
}*/
}
最佳答案
我不知道如何在客户端运行时指定新密钥,但是要使用预先上传到Openstack的预定义公共(public)密钥,请使用method
CreateServerOptions.Builder.keyPairName(KEY_NAME)
可在Openstack仪表板的“访问和安全”>“密钥对”中找到公用密钥列表。我实际上不确定,是否可以为实例指定新密钥,因为用于手动实例创建的仪表板表单不支持新密钥上传。但是,它导入了一个新的NAMED密钥,在创建该实例之后,该密钥被添加到上述公共(public)密钥列表中。
关于ssh - 在OpenStack中使用jclouds传递ssh key ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19733905/