2013-11-01 61 views
1

我使用jclouds在openstack中創建服務器。雖然我能夠創建服務器,但我還想傳遞公共ssh密鑰以便在完成雲初始化後連接到服務器。以下是我的代碼。在openstack中使用jclouds傳遞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()); 
    }*/ 
} 
+0

從我可以看到,它看起來像模板被註釋掉,將添加公鑰。其次,模板需要在創建調用期間使用。 ['createNodesInGroup'將採用您製作的模板](https://github.com/jclouds/jclouds-examples/blob/f5f038cc4c92c85fb8bc61c7ca8a591b7b2dacad/rackspace/src/main/java/org/jclouds/examples/rackspace/cloudservers/CreateServer。 Java的#L97-L106)。 –

回答

1

我不知道,你怎麼可以指定客戶端運行時新的密鑰,而是使用預定義的公共密鑰,之前上傳到OpenStack的,使用方法

CreateServerOptions.Builder.keyPairName(KEY_NAME) 

公鑰的列表可在Openstack儀表板的「Access &安全」>「Keypairs」中。實際上,我不確定是否可以爲實例指定新的密鑰,因爲手動實例創建的儀表板表單不支持新的密鑰上傳。它有一個新的NAMED鍵的導入,但是,在創建實例後,它會添加到上面提到的公鑰列表中。