OpenStack is a global collaboration of developers and cloud computing technologists producing the ubiquitous open source cloud computing platform for public and private clouds. The project aims to deliver solutions for all types of clouds by being simple to implement, massively scalable, and feature rich. The technology consists of a series of interrelated projects delivering various components for a cloud infrastructure solution.
You can either install a private OpenStack cloud for yourself or use an existing OpenStack public cloud.
If you don't have a private OpenStack cloud but still want to try it out, you can use DevStack to create your own mini-OpenStack cloud.
Because the OpenStack API is also open, the jclouds APIs that talk to private OpenStack clouds work just as well with public OpenStack clouds. OpenStack is used by several large public clouds, both the HP Cloud (HP Cloud Getting Started Guide) and Rackspace Cloud (Rackspace Getting Started Guide) are based on it. If you don't want to sign up for a paid public cloud, you can use TryStack.
javac -version
mvn -version
mkdir jclouds
cd jclouds
mvn dependency:copy-dependencies "-DoutputDirectory=./lib"
jclouds/
pom.xml
lib/
*.jar
There are some differences in terminology between jclouds and OpenStack that should be made clear.
OpenStack Compute (aka Nova) is an easy to use service that provides on-demand servers that you can use to to build dynamic websites, deliver mobile apps, or crunch big data.
jclouds/
JCloudsNova.java
pom.xml
lib/
*.jar
{% highlight java %} import com.google.common.collect.ImmutableSet; import com.google.common.io.Closeables; import com.google.inject.Module; import org.jclouds.ContextBuilder; import org.jclouds.logging.slf4j.config.SLF4JLoggingModule; import org.jclouds.openstack.nova.v2_0.NovaApi; import org.jclouds.openstack.nova.v2_0.domain.Server; import org.jclouds.openstack.nova.v2_0.features.ServerApi;
import java.io.Closeable; import java.io.IOException; import java.util.Set;
public class JCloudsNova implements Closeable { private final NovaApi novaApi; private final Set regions;
public static void main(String[] args) throws IOException { JCloudsNova jcloudsNova = new JCloudsNova(); try { jcloudsNova.listServers(); jcloudsNova.close(); } catch (Exception e) { e.printStackTrace(); } finally { jcloudsNova.close(); } } public JCloudsNova() { Iterable<Module> modules = ImmutableSet.<Module>of(new SLF4JLoggingModule()); String provider = "openstack-nova"; String identity = "demo:demo"; // tenantName:userName String credential = "devstack"; novaApi = ContextBuilder.newBuilder(provider) .endpoint("http://xxx.xxx.xxx.xxx:5000/v2.0/") .credentials(identity, credential) .modules(modules) .buildApi(NovaApi.class); regions = novaApi.getConfiguredRegions(); } private void listServers() { for (String region : regions) { ServerApi serverApi = novaApi.getServerApi(region); System.out.println("Servers in " + region); for (Server server : serverApi.listInDetail().concat()) { System.out.println(" " + server); } } } public void close() throws IOException { Closeables.close(novaApi, true); }
} {% endhighlight %}
In the constructor note that
String provider = "openstack-nova";
String identity = "demo:demo"; // tenantName:userName
String password = "devstack";
.endpoint("http://xxx.xxx.xxx.xxx:5000/v2.0/")
Keystone is serving at http://xxx.xxx.xxx.xxx:5000/v2.0/
”$ javac -classpath ".:lib/*" JCloudsNova.java $ java -classpath ".:lib/*" JCloudsNova Servers in RegionOne Server{id=...} ... # You'll see a lot of logging in the output
OpenStack Object Storage (aka Swift) provides redundant, scalable object storage using clusters of standardized servers capable of storing petabytes of data.
jclouds/
JCloudsSwift.java
pom.xml
lib/
*.jar
{% highlight java %} import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.common.io.Closeables; import com.google.inject.Module; import org.jclouds.ContextBuilder; import org.jclouds.io.Payload; import org.jclouds.logging.slf4j.config.SLF4JLoggingModule; import org.jclouds.openstack.swift.v1.SwiftApi; import org.jclouds.openstack.swift.v1.domain.Container; import org.jclouds.openstack.swift.v1.features.ContainerApi; import org.jclouds.openstack.swift.v1.features.ObjectApi; import org.jclouds.openstack.swift.v1.options.CreateContainerOptions; import org.jclouds.openstack.swift.v1.options.PutOptions;
import java.io.Closeable; import java.io.IOException; import java.util.Set;
import static com.google.common.io.ByteSource.wrap; import static org.jclouds.io.Payloads.newByteSourcePayload;
public class JCloudsSwift implements Closeable { public static final String CONTAINER_NAME = “jclouds-example”; public static final String OBJECT_NAME = “jclouds-example.txt”;
private SwiftApi swiftApi;
public static void main(String[] args) throws IOException { JCloudsSwift jcloudsSwift = new JCloudsSwift();
try { jcloudsSwift.createContainer(); jcloudsSwift.uploadObjectFromString(); jcloudsSwift.listContainers(); jcloudsSwift.close(); } catch (Exception e) { e.printStackTrace(); } finally { jcloudsSwift.close(); }
}
public JCloudsSwift() { Iterable modules = ImmutableSet.of( new SLF4JLoggingModule());
String provider = "openstack-swift"; String identity = "demo:demo"; // tenantName:userName String credential = "devstack"; swiftApi = ContextBuilder.newBuilder(provider) .endpoint("http://xxx.xxx.xxx.xxx:5000/v2.0/") .credentials(identity, credential) .modules(modules) .buildApi(SwiftApi.class);
}
private void createContainer() { System.out.println(“Create Container”);
ContainerApi containerApi = swiftApi.getContainerApi("RegionOne"); CreateContainerOptions options = CreateContainerOptions.Builder .metadata(ImmutableMap.of( "key1", "value1", "key2", "value2")); containerApi.create(CONTAINER_NAME, options); System.out.println(" " + CONTAINER_NAME);
}
private void uploadObjectFromString() { System.out.println(“Upload Object From String”);
ObjectApi objectApi = swiftApi.getObjectApi("RegionOne", CONTAINER_NAME); Payload payload = newByteSourcePayload(wrap("Hello World".getBytes())); objectApi.put(OBJECT_NAME, payload, PutOptions.Builder.metadata(ImmutableMap.of("key1", "value1"))); System.out.println(" " + OBJECT_NAME);
}
private void listContainers() { System.out.println(“List Containers”);
ContainerApi containerApi = swiftApi.getContainerApi("RegionOne"); Set<Container> containers = containerApi.list().toSet(); for (Container container : containers) { System.out.println(" " + container); }
}
public void close() throws IOException { Closeables.close(swiftApi, true); } } {% endhighlight %}
$ javac -classpath ".:lib/*" JCloudsSwift.java $ java -classpath ".:lib/*" JCloudsSwift Create Container jclouds-example Upload Object From String jclouds-example.txt List Containers Container{name=...} ... # You'll see a lot of logging in the output
{% highlight java %} import org.jclouds.Constants; import org.jclouds.openstack.keystone.v2_0.config.CredentialTypes; import org.jclouds.openstack.keystone.v2_0.config.KeystoneProperties;
// snip
public JCloudsNova() { Iterable modules = ImmutableSet.of(new SLF4JLoggingModule());
Properties overrides = new Properties(); overrides.setProperty(KeystoneProperties.CREDENTIAL_TYPE, CredentialTypes.PASSWORD_CREDENTIALS); overrides.setProperty(Constants.PROPERTY_API_VERSION, "2"); String provider = "openstack-nova"; String identity = "username"; String credential = "password"; novaApi = ContextBuilder.newBuilder(provider) .endpoint("https://identity.api.rackspacecloud.com/v2.0/") .credentials(identity, credential) .modules(modules) .overrides(overrides) .buildApi(NovaApi.class); regions = novaApi.getConfiguredRegions();
} {% endhighlight %}
"openstack-cinder"
provider to list volumes (hint: see VolumeAndSnapshotApiLiveTest.testListVolumes()).This pom.xml file specifies all of the dependencies you'll need to work with OpenStack.
{% highlight xml %} 4.0.0 <jclouds.version>{{ site.latest_version }}</jclouds.version> org.apache.jclouds.examples openstack-examples 1.0 org.apache.jclouds.driver jclouds-slf4j ${jclouds.version} org.apache.jclouds.driver jclouds-sshj ${jclouds.version} org.apache.jclouds.api openstack-keystone ${jclouds.version} org.apache.jclouds.api openstack-nova ${jclouds.version} org.apache.jclouds.api openstack-swift ${jclouds.version} org.apache.jclouds.api openstack-cinder ${jclouds.version} org.apache.jclouds.api openstack-trove ${jclouds.version} org.apache.jclouds.labs openstack-glance ${jclouds.version} org.apache.jclouds.labs openstack-marconi ${jclouds.version} org.apache.jclouds.labs openstack-neutron ${jclouds.version} ch.qos.logback logback-classic 1.0.13 mysql mysql-connector-java 5.1.25 {% endhighlight %}