Problems using jclouds on OpenStack nova API

671 views
Skip to first unread message

Marina Deslaugiers

unread,
Apr 13, 2012, 12:38:00 PM4/13/12
to jcl...@googlegroups.com
Hi All,

I can't succeed using jclouds with Nova API on OpenStack Essex tag RC1 (the one just before the official release). I have tried various configurations but I always get exceptions or errors.  I have also tried the compute-basics example without success. Since it seems other people succeded, can you, please, provide me with a working code example.

Thanks for your help.
-M

Below is my client code followed by console log


package org.ow2.sirocco.cloudconnector.jclouds.nova;

import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.collect.Iterables.concat;
import static com.google.common.collect.Iterables.getOnlyElement;
import static org.jclouds.Constants.PROPERTY_API_VERSION;
// p-novamaster2 - does not work with jclouds 1.5.0 -> to be tested with 1.5.0.aplha.2
import static org.jclouds.openstack.nova.options.ListOptions.Builder.*;
import org.jclouds.Constants;
import org.jclouds.compute.ComputeService;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.ComputeServiceContextFactory;
import org.jclouds.compute.domain.ComputeMetadata;
import org.jclouds.compute.domain.Hardware;
import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.domain.TemplateBuilder;
import org.jclouds.compute.options.TemplateOptions;
import org.jclouds.domain.Location;
import org.jclouds.enterprise.config.EnterpriseConfigurationModule;
import org.jclouds.logging.slf4j.config.SLF4JLoggingModule;
import org.jclouds.openstack.nova.NovaClient;
import org.jclouds.openstack.nova.domain.Flavor;
import org.jclouds.openstack.nova.domain.Image;
import org.jclouds.openstack.nova.domain.RebootType;
import org.jclouds.openstack.nova.domain.Server;
import org.jclouds.scriptbuilder.domain.Statement;
import org.jclouds.scriptbuilder.statements.login.AdminAccess;
import org.jclouds.sshj.config.SshjSshClientModule;

import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Module;

import java.util.Properties;
import java.util.Set;

/**
 * jclouds to OpenStack nova API
 *
 */
public class TestNovaClient {
    private String endPoint;
    private ComputeServiceContext context;
    private ComputeService client;
    private NovaClient novaClient;
   
    Server myServer;

    TestNovaClient(String endPoint) {
        this.endPoint = endPoint;
    }
   
    void connect(String user, String apiKey) {
        Properties overrides = new Properties();
        overrides.setProperty(Constants.PROPERTY_ENDPOINT, endPoint);
        overrides.setProperty(PROPERTY_API_VERSION, "1.1");
        overrides.setProperty(Constants.PROPERTY_TRUST_ALL_CERTS, "true");
        overrides.setProperty(Constants.PROPERTY_RELAX_HOSTNAME, "true");
       
         // example of injecting a ssh implementation
          Iterable<Module> modules = ImmutableSet.<Module> of(
                new SshjSshClientModule(),
                new SLF4JLoggingModule(),
                new EnterpriseConfigurationModule());
         
        // get a context with nova that offers the portable ComputeService api
        context = new ComputeServiceContextFactory().createContext("nova", user, apiKey, modules, overrides);
            //    ImmutableSet.<Module> of(new JschSshClientModule()), overrides);
        client = context.getComputeService();
        // when you need access to nova-specific features, use the provider-specific context
        novaClient = NovaClient.class.cast(context.getProviderSpecificContext().getApi());
    }

    void disconnect() {
        context.close();
    }

    void listMachines() {
        Set<Server> servers = novaClient.listServers(withDetails());
        for (Server server : servers) {
            System.out.println("SERVER: " + server);
        }
    }

    void listLocations() {
        Set<? extends Location> locations = client.listAssignableLocations();

        for (Location location : locations) {
            System.out.println("LOCATION: " + location);
        }
    }

    void listMachineConfigurations() {
        for (Hardware hardware : client.listHardwareProfiles()) {
            System.out.println("HARDWARE: " + hardware);
        }
        for (Flavor flavor : novaClient.listFlavors(withDetails())) {
            System.out.println("FLAVOR: " + flavor);
        }
    }

    void listImages() {
        System.out.println("METHOD listImages");
        Set<? extends Image> images = (Set<? extends Image>) client.listImages();
       
        //Set<Image> images = novaClient.listImages(startAt(0).maxResults(100));
        //for (Image image : images) {
        for (Image image : images) {
            System.out.println("IMAGE: " + image);
        }
    }

    String createMachine(String imageId, String hardwareId) throws Exception {
 
        //MDL GIVES AN EXCEPTION
        //System.out.printf("IMAGE_ID= ", imageId);
       
        // MDL
         //System.out.println("INFO login identity="+login.identity);

        // Default template chooses the smallest size on an operating system
        // that tested to work with java, which tends to be Ubuntu or CentOS
        TemplateBuilder templateBuilder = client.templateBuilder();
        //System.out.printf("AVANT TEMPLATE BUILDER imageId");
        templateBuilder.imageId(imageId);

               
        templateBuilder.hardwareId(hardwareId);
       
        // note this will create a user with the same name as you on the
        // node. ex. you can connect via ssh publicip
        Statement bootInstructions = AdminAccess.standard();
          
        // to run commands as root, we use the runScript option in the template.
        TemplateOptions options= new TemplateOptions();
        options.blockUntilRunning(false);
        options.runScript(bootInstructions);
       
        // p-novamaster2
        NodeMetadata newNode= getOnlyElement(client.createNodesInGroup("SG_SSH", 1, templateBuilder.build()));
        System.out.printf("<< node %s: %s%n", newNode.getId(),
              concat(newNode.getPrivateAddresses(), newNode.getPublicAddresses()));

        return newNode.getId();
    }
   
    void listMachinePrivateAddr(String nodeId) { // NOT OK -> returns EMPTY SET
        Set<String> privAddresses = novaClient.listPrivateAddresses(Integer.parseInt(nodeId));       
        for (String privAddress : privAddresses) {
            System.out.println("PRIV ADDRESS: " + privAddress);
        }
    }
   
    void listMachinePublicAddr(String nodeId) { // NOT OK -> returns EMPTY SET
        Set<String> pubAddresses = novaClient.listPublicAddresses(Integer.parseInt(nodeId));
       
        for (String pubAddress : pubAddresses) {
            System.out.println("PRIV ADDRESS: " + pubAddress);
        }
    }
   
    // adding pre-allocated floating public IP to node
    void addFloatingIP(String nodeId, String ip) {
        System.out.println("nodeId="+nodeId);
        novaClient.addFloatingIP(Integer.parseInt(nodeId), ip);
    }
   
    void pauseMachine(String nodeId) throws Exception { // IDEM FOR unpause
        // -> jclouds: NOT OPERATION FOUND
    }
   
    void suspendMachine(String nodeId) throws Exception {
        client.suspendNode(nodeId);  // -> jclouds: NOT SUPPORTED EXCEPTION
    }

    void resumeMachine(String nodeId) throws Exception {
        client.resumeNode(nodeId); // -> jclouds: a priori NOT SUPPORTED EXCEPTION since suspended state should not be reached
    }
   
    void destroyMachine(String nodeId) throws Exception {
        //client.destroyNode(nodeId);
        novaClient.deleteServer(nodeId);
    }
   
    void rebootMachine(String nodeId) {
        System.out.println("NOVA CLIENT Rebooting machine..."+nodeId);
        //client.rebootNode(nodeId);
        novaClient.rebootServer(Integer.parseInt(nodeId), RebootType.SOFT);
    }

    String getMachineStatus(final String nodeId) throws Exception {
        Set<? extends NodeMetadata> nodes = client.listNodesDetailsMatching(new Predicate<ComputeMetadata>() {

            public boolean apply(ComputeMetadata data) {
                return data.getId().equals(nodeId);
            }
        });
        if (!nodes.isEmpty())
            return nodes.iterator().next().getState().toString();
        else throw new Exception("Invalid nodeId");
    }
   
    // JCLOUDS note on issues: Request to create image from a running instance returns incorrect JSON output. Possibly a bug in OpenStack.
    Image createImageFromMachine(String newImageName, String nodeId) throws Exception {
        System.out.println("METHOD createImageFromMachine");
        System.out.println("RAPPEL Node Id pour creation  image= " + nodeId);
        Image newImage= novaClient.createImageFromServer(newImageName, Integer.parseInt(nodeId));
        return(newImage);
    }
   
    String getImageStatus(Image newImage) throws Exception {
        System.out.println("METHOD getImageStatus");
        if (newImage!=null)
            return newImage.getStatus().toString();
        else throw new Exception("Invalid imageId");
    }

    public static void main(String[] args) throws Exception {
       
        String nodeId= null;
        //Image newImage;
       
        // p-novamaster2 - 10.193.112.102 - NOVA_URL= DOES not WORK?
        TestNovaClient provider = new TestNovaClient("http://10.193.112.102:5000");
        provider.connect("tenant_admin:openstack_admin", "orange");

        System.out.println("jclouds to OpenStack nova API main - listing locations");
        provider.listLocations();
       
        System.out.println("jclouds to OpenStack nova API main - listing images");
        provider.listImages();
       
        System.out.println("jclouds to OpenStack nova API main - listing nodes");
        provider.listMachines();

        System.out.println("-- listing Machine Config ");
        provider.listMachineConfigurations();

        System.out.println("Creating machine...");
        // p-novamaster2 - DOES NOT WORK
        nodeId = provider.createMachine("416af940-2d3c-4a7c-977c-a9030685ad5e", "1");
       
        System.out.println("New Node: " + nodeId);
       
        String state;
       
        while(true) {
            state= provider.getMachineStatus(nodeId);
            System.out.println("Node state="+state);
            if (!state.equals("PENDING")) break;
            Thread.sleep(2000);
        }
       
        Thread.sleep(10000);

        System.out.println("-- listing Machine Private Addresses");
        provider.listMachinePrivateAddr(nodeId);
       
        System.out.println("-- listing Machine Public Addresses");
        provider.listMachinePublicAddr(nodeId);

        // allocating public IP
/*        System.out.println("-- Allocating floating IP to this node");
        provider.addFloatingIP(nodeId, "10.193.112.115");*/
       
/*        Thread.sleep(5000);
       
        System.out.println("-- listing Machine Public Addresses after allocation");
        provider.listMachinePublicAddr(nodeId);*/
       
/*  // DOES NOT WORK
 *     System.out.println("-- creating Image from running machine");
        newImage= provider.createImageFromMachine("imgFromMachine", nodeId);

        while(true) {
            state= provider.getImageStatus(newImage);
            System.out.println("Image state="+state);
            if (!state.equals("ACTIVE")) break;
            Thread.sleep(2000);
        }*/

/*        System.out.println("Rebooting machine...");
        provider.rebootMachine(nodeId);
       
        while(true) {
            state= provider.getMachineStatus(nodeId);
            System.out.println("Node state="+state);
            if (!state.equals("PENDING")) break;
            Thread.sleep(2000);
        }*/
       
/*        System.out.println("Destroying machine...");
        provider.destroyMachine(nodeId);*/

        provider.disconnect();
    }
}

========================

jclouds to OpenStack nova API main - listing locations
17:05:18.222 [main] DEBUG o.j.l.s.f.RegionIdsFromConfiguration - no jclouds.regions configured for provider nova
17:05:18.226 [main] DEBUG o.j.l.s.f.ZoneIdsFromConfiguration - no jclouds.zones configured for provider nova
LOCATION: [id=nova, scope=PROVIDER, description=http://10.193.112.102:5000, parent=null, iso3166Codes=[], metadata={}]
jclouds to OpenStack nova API main - listing images
METHOD listImages
17:05:18.271 [main] DEBUG o.j.r.internal.AsyncRestClientProxy - Invoking OpenStackAuthAsyncClient.authenticate
17:05:18.274 [i/o thread 0] DEBUG o.j.h.i.JavaUrlHttpCommandExecutorService - Sending request 1019520889: GET http://10.193.112.102:5000/v1.1 HTTP/1.1
17:05:18.275 [i/o thread 0] DEBUG jclouds.headers - >> GET http://10.193.112.102:5000/v1.1 HTTP/1.1
17:05:18.276 [i/o thread 0] DEBUG jclouds.headers - >> X-Auth-User: tenant_admin:openstack_admin
17:05:18.276 [i/o thread 0] DEBUG jclouds.headers - >> X-Auth-Key: orange
17:05:18.276 [i/o thread 0] DEBUG jclouds.headers - >> Accept: */*
17:05:18.667 [i/o thread 0] DEBUG o.j.h.i.JavaUrlHttpCommandExecutorService - Receiving response 1019520889: HTTP/1.1 404 Not Found
17:05:18.667 [i/o thread 0] DEBUG jclouds.headers - << HTTP/1.1 404 Not Found
17:05:18.667 [i/o thread 0] DEBUG jclouds.headers - << Date: Fri, 13 Apr 2012 15:02:48 GMT
17:05:18.668 [i/o thread 0] DEBUG jclouds.headers - << Connection: Keep-Alive
17:05:18.668 [i/o thread 0] DEBUG jclouds.headers - << Proxy-Connection: Keep-Alive
17:05:18.668 [i/o thread 0] DEBUG jclouds.headers - << Content-Type: text/html; charset=UTF-8
17:05:18.668 [i/o thread 0] DEBUG jclouds.headers - << Content-Length: 154
17:05:18.669 [i/o thread 0] DEBUG jclouds.wire - << "<html>[\n]"
17:05:18.669 [i/o thread 0] DEBUG jclouds.wire - << " <head>[\n]"
17:05:18.669 [i/o thread 0] DEBUG jclouds.wire - << "  <title>404 Not Found</title>[\n]"
17:05:18.669 [i/o thread 0] DEBUG jclouds.wire - << " </head>[\n]"
17:05:18.669 [i/o thread 0] DEBUG jclouds.wire - << " <body>[\n]"
17:05:18.669 [i/o thread 0] DEBUG jclouds.wire - << "  <h1>404 Not Found</h1>[\n]"
17:05:18.669 [i/o thread 0] DEBUG jclouds.wire - << "  The resource could not be found.<br /><br />[\n]"
17:05:18.669 [i/o thread 0] DEBUG jclouds.wire - << "[\n]"
17:05:18.669 [i/o thread 0] DEBUG jclouds.wire - << "[\n]"
17:05:18.669 [i/o thread 0] DEBUG jclouds.wire - << "[\n]"
17:05:18.669 [i/o thread 0] DEBUG jclouds.wire - << " </body>[\n]"
17:05:18.669 [i/o thread 0] DEBUG jclouds.wire - << "</html>"
jclouds to OpenStack nova API main - listing nodes
17:05:18.677 [main] DEBUG o.j.r.internal.AsyncRestClientProxy - Invoking OpenStackAuthAsyncClient.authenticate
17:05:18.677 [i/o thread 0] DEBUG o.j.h.i.JavaUrlHttpCommandExecutorService - Sending request 1019520889: GET http://10.193.112.102:5000/v1.1 HTTP/1.1
17:05:18.678 [i/o thread 0] DEBUG jclouds.headers - >> GET http://10.193.112.102:5000/v1.1 HTTP/1.1
17:05:18.678 [i/o thread 0] DEBUG jclouds.headers - >> X-Auth-User: tenant_admin:openstack_admin
17:05:18.678 [i/o thread 0] DEBUG jclouds.headers - >> X-Auth-Key: orange
17:05:18.678 [i/o thread 0] DEBUG jclouds.headers - >> Accept: */*
17:05:18.684 [i/o thread 0] DEBUG o.j.h.i.JavaUrlHttpCommandExecutorService - Receiving response 1019520889: HTTP/1.1 404 Not Found
17:05:18.684 [i/o thread 0] DEBUG jclouds.headers - << HTTP/1.1 404 Not Found
17:05:18.684 [i/o thread 0] DEBUG jclouds.headers - << Date: Fri, 13 Apr 2012 15:02:48 GMT
17:05:18.684 [i/o thread 0] DEBUG jclouds.headers - << Connection: Keep-Alive
17:05:18.684 [i/o thread 0] DEBUG jclouds.headers - << Proxy-Connection: Keep-Alive
17:05:18.684 [i/o thread 0] DEBUG jclouds.headers - << Content-Type: text/html; charset=UTF-8
17:05:18.684 [i/o thread 0] DEBUG jclouds.headers - << Content-Length: 154
17:05:18.684 [i/o thread 0] DEBUG jclouds.wire - << "<html>[\n]"
17:05:18.684 [i/o thread 0] DEBUG jclouds.wire - << " <head>[\n]"
17:05:18.684 [i/o thread 0] DEBUG jclouds.wire - << "  <title>404 Not Found</title>[\n]"
17:05:18.684 [i/o thread 0] DEBUG jclouds.wire - << " </head>[\n]"
17:05:18.684 [i/o thread 0] DEBUG jclouds.wire - << " <body>[\n]"
17:05:18.685 [i/o thread 0] DEBUG jclouds.wire - << "  <h1>404 Not Found</h1>[\n]"
17:05:18.685 [i/o thread 0] DEBUG jclouds.wire - << "  The resource could not be found.<br /><br />[\n]"
17:05:18.685 [i/o thread 0] DEBUG jclouds.wire - << "[\n]"
17:05:18.685 [i/o thread 0] DEBUG jclouds.wire - << "[\n]"
17:05:18.685 [i/o thread 0] DEBUG jclouds.wire - << "[\n]"
17:05:18.685 [i/o thread 0] DEBUG jclouds.wire - << " </body>[\n]"
17:05:18.685 [i/o thread 0] DEBUG jclouds.wire - << "</html>"
-- listing Machine Config
17:05:18.688 [main] DEBUG o.j.r.internal.AsyncRestClientProxy - Invoking OpenStackAuthAsyncClient.authenticate
17:05:18.689 [i/o thread 0] DEBUG o.j.h.i.JavaUrlHttpCommandExecutorService - Sending request 1019520889: GET http://10.193.112.102:5000/v1.1 HTTP/1.1
17:05:18.689 [i/o thread 0] DEBUG jclouds.headers - >> GET http://10.193.112.102:5000/v1.1 HTTP/1.1
17:05:18.689 [i/o thread 0] DEBUG jclouds.headers - >> X-Auth-User: tenant_admin:openstack_admin
17:05:18.689 [i/o thread 0] DEBUG jclouds.headers - >> X-Auth-Key: orange
17:05:18.689 [i/o thread 0] DEBUG jclouds.headers - >> Accept: */*
17:05:18.694 [i/o thread 0] DEBUG o.j.h.i.JavaUrlHttpCommandExecutorService - Receiving response 1019520889: HTTP/1.1 404 Not Found
17:05:18.695 [i/o thread 0] DEBUG jclouds.headers - << HTTP/1.1 404 Not Found
17:05:18.695 [i/o thread 0] DEBUG jclouds.headers - << Date: Fri, 13 Apr 2012 15:02:48 GMT
17:05:18.695 [i/o thread 0] DEBUG jclouds.headers - << Connection: Keep-Alive
17:05:18.695 [i/o thread 0] DEBUG jclouds.headers - << Proxy-Connection: Keep-Alive
17:05:18.695 [i/o thread 0] DEBUG jclouds.headers - << Content-Type: text/html; charset=UTF-8
17:05:18.695 [i/o thread 0] DEBUG jclouds.headers - << Content-Length: 154
17:05:18.696 [i/o thread 0] DEBUG jclouds.wire - << "<html>[\n]"
17:05:18.696 [i/o thread 0] DEBUG jclouds.wire - << " <head>[\n]"
17:05:18.696 [i/o thread 0] DEBUG jclouds.wire - << "  <title>404 Not Found</title>[\n]"
17:05:18.696 [i/o thread 0] DEBUG jclouds.wire - << " </head>[\n]"
17:05:18.696 [i/o thread 0] DEBUG jclouds.wire - << " <body>[\n]"
17:05:18.696 [i/o thread 0] DEBUG jclouds.wire - << "  <h1>404 Not Found</h1>[\n]"
17:05:18.696 [i/o thread 0] DEBUG jclouds.wire - << "  The resource could not be found.<br /><br />[\n]"
17:05:18.696 [i/o thread 0] DEBUG jclouds.wire - << "[\n]"
17:05:18.696 [i/o thread 0] DEBUG jclouds.wire - << "[\n]"
17:05:18.696 [i/o thread 0] DEBUG jclouds.wire - << "[\n]"
17:05:18.696 [i/o thread 0] DEBUG jclouds.wire - << " </body>[\n]"
17:05:18.696 [i/o thread 0] DEBUG jclouds.wire - << "</html>"
17:05:18.700 [main] DEBUG o.j.r.internal.AsyncRestClientProxy - Invoking OpenStackAuthAsyncClient.authenticate
17:05:18.700 [i/o thread 0] DEBUG o.j.h.i.JavaUrlHttpCommandExecutorService - Sending request 1019520889: GET http://10.193.112.102:5000/v1.1 HTTP/1.1
17:05:18.700 [i/o thread 0] DEBUG jclouds.headers - >> GET http://10.193.112.102:5000/v1.1 HTTP/1.1
17:05:18.701 [i/o thread 0] DEBUG jclouds.headers - >> X-Auth-User: tenant_admin:openstack_admin
17:05:18.701 [i/o thread 0] DEBUG jclouds.headers - >> X-Auth-Key: orange
17:05:18.701 [i/o thread 0] DEBUG jclouds.headers - >> Accept: */*
17:05:18.709 [i/o thread 0] DEBUG o.j.h.i.JavaUrlHttpCommandExecutorService - Receiving response 1019520889: HTTP/1.1 404 Not Found
17:05:18.709 [i/o thread 0] DEBUG jclouds.headers - << HTTP/1.1 404 Not Found
17:05:18.709 [i/o thread 0] DEBUG jclouds.headers - << Date: Fri, 13 Apr 2012 15:02:48 GMT
17:05:18.710 [i/o thread 0] DEBUG jclouds.headers - << Connection: Keep-Alive
17:05:18.710 [i/o thread 0] DEBUG jclouds.headers - << Proxy-Connection: Keep-Alive
17:05:18.710 [i/o thread 0] DEBUG jclouds.headers - << Content-Type: text/html; charset=UTF-8
17:05:18.710 [i/o thread 0] DEBUG jclouds.headers - << Content-Length: 154
17:05:18.710 [i/o thread 0] DEBUG jclouds.wire - << "<html>[\n]"
17:05:18.710 [i/o thread 0] DEBUG jclouds.wire - << " <head>[\n]"
17:05:18.710 [i/o thread 0] DEBUG jclouds.wire - << "  <title>404 Not Found</title>[\n]"
17:05:18.710 [i/o thread 0] DEBUG jclouds.wire - << " </head>[\n]"
17:05:18.710 [i/o thread 0] DEBUG jclouds.wire - << " <body>[\n]"
17:05:18.710 [i/o thread 0] DEBUG jclouds.wire - << "  <h1>404 Not Found</h1>[\n]"
17:05:18.710 [i/o thread 0] DEBUG jclouds.wire - << "  The resource could not be found.<br /><br />[\n]"
17:05:18.710 [i/o thread 0] DEBUG jclouds.wire - << "[\n]"
17:05:18.710 [i/o thread 0] DEBUG jclouds.wire - << "[\n]"
17:05:18.710 [i/o thread 0] DEBUG jclouds.wire - << "[\n]"
17:05:18.710 [i/o thread 0] DEBUG jclouds.wire - << " </body>[\n]"
17:05:18.710 [i/o thread 0] DEBUG jclouds.wire - << "</html>"
Creating machine...
17:05:18.720 [main] DEBUG jclouds.compute - >> searching params([biggest=false, fastest=false, imageName=null, imageDescription=null, imageId=416af940-2d3c-4a7c-977c-a9030685ad5e, imagePredicate=null, imageVersion=null, location=null, minCores=0.0, minRam=0, osFamily=null, osName=null, osDescription=null, osVersion=null, osArch=null, os64Bit=null, hardwareId=1, hypervisor=null])
Exception in thread "main" java.util.NoSuchElementException: imageId(416af940-2d3c-4a7c-977c-a9030685ad5e) not found
    at org.jclouds.compute.domain.internal.TemplateBuilderImpl.build(TemplateBuilderImpl.java:620)
    at org.ow2.sirocco.cloudconnector.jclouds.nova.TestNovaClient.createMachine(TestNovaClient.java:140)
    at org.ow2.sirocco.cloudconnector.jclouds.nova.TestNovaClient.main(TestNovaClient.java:241)
Reply all
Reply to author
Forward
0 new messages