Jclouds with eucalyptus

I was working on eucalyptus when i had to test an integration of eucalyptus with Jclouds, here’s a brief step that would users get started for integrating eucalyptus with Jcloud.

 

Installing Jclouds

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

taken from: http://blog.phymata.com/2012/08/15/getting-started-with-jclouds/

$mkdir jclouds;cd jclouds

$curl -o lein.sh https://raw.github.com/technomancy/leiningen/stable/bin/lein

$ 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
 
Sample code that lists all running instances in eucalyptus cloud + creates a security group + adds an ingress rule.
==========================================
 

import java.util.Set;
import java.lang.Thread.UncaughtExceptionHandler;
import java.util.Properties;

import org.jclouds.ContextBuilder;
import org.jclouds.compute.ComputeService;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.domain.ComputeMetadata;
import org.jclouds.logging.slf4j.config.SLF4JLoggingModule;
import org.jclouds.sshj.config.SshjSshClientModule;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.inject.Module;
import org.jclouds.compute.ComputeServiceContextFactory;
import org.jclouds.ec2.services.ElasticBlockStoreClient;
import org.jclouds.ec2.services.SecurityGroupClient;
import org.jclouds.ec2.EC2Client;
import org.jclouds.rest.RestContext;
import org.jclouds.ec2.EC2AsyncClient;
import org.jclouds.ec2.EC2Client;
import org.jclouds.ec2.domain.IpProtocol;

public class JCloudsTest {

public static void main(String[] args) {
String provider = “eucalyptus”;
String identity = “HEIV7IISYV0ZR3ZIQDCOO”;
String credential = “OtKODhCwjQ90dRQyuHFNHbRjToRmoRuMGC6gi44J”;
JCloudsTest jCloudsTest = new JCloudsTest();
JCloudsTest.init();

ComputeService compute = initComputeService(provider, identity, credential);
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);
}
JCloudsTest.initComputeSecurity(provider, identity, credential);

System.exit(0);
}

private static void init() {
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
public void uncaughtException(Thread t, Throwable e) {
e.printStackTrace();
System.exit(1);
}
});
}
private static ComputeService initComputeService(String provider, String identity, String credential) {
Properties properties = new Properties();
properties.setProperty(“eucalyptus.endpoint”, “http://10.101.1.118:8773/services/Eucalyptus&#8221;);
Iterable<Module> modules = ImmutableSet.<Module> of(
new SshjSshClientModule(),
new SLF4JLoggingModule());

ContextBuilder builder = ContextBuilder.newBuilder(provider)
.credentials(identity, credential)
.modules(modules)
.overrides(properties);
System.out.printf(“>> initializing %s%n”, builder.getApiMetadata());
return builder.buildView(ComputeServiceContext.class).getComputeService();
}
private static void initComputeSecurity(String provider, String identity, String credential) {
Properties properties = new Properties();
properties.setProperty(“eucalyptus.endpoint”, “http://10.101.1.118:8773/services/Eucalyptus&#8221;);
Iterable<Module> modules = ImmutableSet.<Module> of(
new SshjSshClientModule(),
new SLF4JLoggingModule());

ContextBuilder builder = ContextBuilder.newBuilder(provider)
.credentials(identity, credential)
.modules(modules)
.overrides(properties);
builder.getApiMetadata();
ComputeServiceContext context = builder.buildView(ComputeServiceContext.class);
RestContext<EC2Client, EC2AsyncClient> context1 = context.getProviderSpecificContext();
EC2Client client = context1.getApi();
SecurityGroupClient secClient = client.getSecurityGroupServices();
try{

secClient.createSecurityGroupInRegion(“eucalyptus”,”bentest”,”benno testing”);
secClient.authorizeSecurityGroupIngressInRegion(“eucalyptus”, “bentest”,IpProtocol.TCP, 5,65, “0.0.0.0/0”);
} catch (Exception e){
e.printStackTrace();
}
}

}

Complile

==========

javac -cp “.:lib/*” JCloudsTest.java

 

Run the code

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

 

java -cp “.:lib/*” JCloudsTest

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s