Everett Toews

Helping you shave narwhals

Getting started with jclouds

15 Aug 2012

Update: Using jclouds 1.5.1

I’m just starting to get familiar with the open source jclouds cross-cloud toolkit. Whenever I get started with something new like this I like to start out with the most simple application possible that still does something sort of useful. And when starting out, I prefer to work from the command line so I can get a feel for how something is really put together. After that, when I move to using a build tool like Maven or Ant or putting everything into Eclipse or another IDE, I have a better understanding of its dependecies and config.

With that in mind I headed over to the jclouds installation page to download the JARs for it. Okay…what is lein???

Looks lein is part of leiningen. One of the tools that jclouds can use to manage dependencies. Downloading the lein script is no problem but I also need the project.clj file. No problem…but what version to put in the dependencies list?

After sifting through the jclouds group and jclouds-dev groups I find a post that points to beta.10 being the latest and greatest. I find a blog that points to 1.5.1 being the latest and greatest. I’ll try putting that in the project.clj file and give it a shot.

$ mkdir jclouds-test
$ cd jcloud-test
$ curl -o lein.sh https://raw.github.com/technomancy/leiningen/stable/bin/lein
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 9905 100 9905 0 0 13005 0 --:--:-- --:--:-- --:--:-- 31444
$ chmod u+x lein.sh
$ echo '(defproject deps "1" :dependencies [[org.jclouds/jclouds-all "1.5.1"] [org.jclouds.driver/jclouds-sshj "1.5.1"]])' > project.clj
$ ./lein.sh deps
<output omitted>
$ ls lib/
aopalliance-1.0.jar elastichosts-sat-p-1.5.1.jar ninefold-storage-1.5.1.jar
atmos-1.5.1.jar elasticstack-1.5.1.jar nova-1.5.1.jar
aws-cloudwatch-1.5.1.jar eucalyptus-1.5.1.jar oauth-20100527.jar
aws-common-1.5.1.jar eucalyptus-partnercloud-ec2-1.5.1.jar openhosting-east1-1.5.1.jar
aws-ec2-1.5.1.jar eucalyptus-partnercloud-s3-1.5.1.jar openstack-common-1.5.1.jar
aws-s3-1.5.1.jar filesystem-1.5.1.jar openstack-keystone-1.5.1.jar
aws-sqs-1.5.1.jar go2cloud-jhb1-1.5.1.jar openstack-nova-1.5.1.jar
azure-common-1.5.1.jar gogrid-1.5.1.jar openstack-nova-ec2-1.5.1.jar
azureblob-1.5.1.jar greenhousedata-element-vcloud-1.5.1.jar rackspace-cloudidentity-1.5.1.jar
bcprov-jdk16-1.46.jar gson-2.2.jar rackspace-cloudservers-us-1.5.1.jar
bluelock-vcloud-zone01-1.5.1.jar guava-13.0.jar rimuhosting-1.5.1.jar
byon-1.5.1.jar guice-3.0.jar rocoto-6.1.jar
cloudfiles-1.5.1.jar guice-assistedinject-3.0.jar s3-1.5.1.jar
cloudfiles-uk-1.5.1.jar hpcloud-compute-1.5.1.jar serverlove-z1-man-1.5.1.jar
cloudfiles-us-1.5.1.jar hpcloud-objectstorage-1.5.1.jar skalicloud-sdg-my-1.5.1.jar
cloudloadbalancers-1.5.1.jar java-xmlbuilder-0.4.jar slf4j-api-1.6.4.jar
cloudloadbalancers-uk-1.5.1.jar javax.inject-1.jar slicehost-1.5.1.jar
cloudloadbalancers-us-1.5.1.jar jclouds-all-1.5.1.jar snakeyaml-1.10.jar
cloudonestorage-1.5.1.jar jclouds-allblobstore-1.5.1.jar softlayer-1.5.1.jar
cloudservers-1.5.1.jar jclouds-allcompute-1.5.1.jar sqs-1.5.1.jar
cloudservers-uk-1.5.1.jar jclouds-allloadbalancer-1.5.1.jar sshj-0.8.1.jar
cloudservers-us-1.5.1.jar jclouds-blobstore-1.5.1.jar stratogen-vcloud-mycloud-1.5.1.jar
cloudsigma-1.5.1.jar jclouds-bouncycastle-1.5.1.jar swift-1.5.1.jar
cloudsigma-lvs-1.5.1.jar jclouds-compute-1.5.1.jar synaptic-storage-1.5.1.jar
cloudsigma-zrh-1.5.1.jar jclouds-core-1.5.1.jar trmk-common-1.5.1.jar
cloudstack-1.5.1.jar jclouds-loadbalancer-1.5.1.jar trmk-ecloud-1.5.1.jar
cloudwatch-1.5.1.jar jclouds-scriptbuilder-1.5.1.jar trmk-vcloudexpress-1.5.1.jar
commons-io-2.4.jar jclouds-slf4j-1.5.1.jar trystack-nova-1.5.1.jar
deltacloud-1.5.1.jar jclouds-sshj-1.5.1.jar vcloud-1.5.1.jar
ec2-1.5.1.jar jersey-core-1.12.jar walrus-1.5.1.jar
elastichosts-lon-b-1.5.1.jar jsr250-api-1.0.jar
elastichosts-lon-p-1.5.1.jar ninefold-compute-1.5.1.jar
view raw install.sh hosted with ❤ by GitHub

That’s a lot of JARs.

Now all I want to do is list all of my servers on the Rackspace open cloud. After some trial and error I came up with the following app.

import java.util.Set;
import java.lang.Thread.UncaughtExceptionHandler;
import org.jclouds.ContextBuilder;
import org.jclouds.compute.ComputeService;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.domain.ComputeMetadata;
public class JCloudsTest {
private ComputeService compute;
public static void main(String[] args) {
JCloudsTest jCloudsTest = new JCloudsTest();
jCloudsTest.init();
jCloudsTest.listServers();
System.exit(0);
}
private void listServers() {
System.out.println("Calling listNodes...");
Set<? extends ComputeMetadata> nodes = compute.listNodes();
System.out.println("Total Number of Nodes = " + nodes.size());
for (ComputeMetadata node: nodes) {
System.out.println("\t" + node);
}
}
private void init() {
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
public void uncaughtException(Thread t, Throwable e) {
e.printStackTrace();
System.exit(1);
}
});
String provider = "rackspace-cloudservers-us";
String identity = "my-username";
String apiKey = "my-api-key";
ComputeServiceContext context = ContextBuilder.newBuilder(provider)
.credentials(identity, apiKey)
.buildView(ComputeServiceContext.class);
compute = context.getComputeService();
}
}

Follow the instructions below to compile and run the app. Thankfully Java 1.6 allows for wildcard expansion on the command line, otherwise it would be a serious pain including all of those JARs.

$ pwd
/Users/phymata/test/jclouds-test
$ ls
JCloudsTest.java lein.sh lib project.clj
$ javac -cp ".:lib/*" JCloudsTest.java
$ ls
JCloudsTest$1.class JCloudsTest.class JCloudsTest.java lein.sh lib project.clj
$ java -cp ".:lib/*" JCloudsTest
Calling listNodes...
Total Number of Nodes = 1
{id=DFW/45b23d7b-ec6a-49dc-a642-dadbba42af17, providerId=45b23d7b-ec6a-49dc-a642-dadbba42af17, name=test-1b, location={scope=HOST, id=44ae081dce76afa64907ffd3822debbe4c51644611e4399478bb8034, description=44ae081dce76afa64907ffd3822debbe4c51644611e4399478bb8034, parent=DFW}, group=test, imageId=DFW/5cebb13a-f783-4f8c-8058-c4182c724ccd, os={family=ubuntu, name=Ubuntu 12.04 LTS (Precise Pangolin), version=12.04, description=Ubuntu 12.04 LTS (Precise Pangolin), is64Bit=true}, status=RUNNING, loginPort=22, hostname=test-1b, privateAddresses=[123.123.123.123], publicAddresses=[123.123.123.123], hardware={id=DFW/3, providerId=3, name=1GB Standard Instance, location={scope=ZONE, id=DFW, description=DFW, parent=rackspace-cloudservers-us, iso3166Codes=[US-TX]}, processors=[{cores=1.0, speed=1.0}], ram=1024, volumes=[{type=LOCAL, size=40.0, bootDevice=true, durable=true}], supportsImage=ALWAYS_TRUE}}
view raw compile.sh hosted with ❤ by GitHub

There you have it. If anyone is more familiar with lein and/or leiningen and can show me how to limit it to downloading just the JARs I need, let me know in the comments.

This post was written on Mac OS X 10.8, Java 1.6.0, jclouds 1.5.1 1.5.0-beta.10. If you’re on a different OS and run into problems, please feel free to comment.