Skip to content
Permalink
Browse files
Update compute-basics example for GCE provider, support listimages, l…
…istnodes commands in compute-basics example
  • Loading branch information
suriyapriya authored and nacx committed Mar 17, 2014
1 parent 5372d58 commit c223b7716b906f1b74162d8e1b81eef17d593e28
Showing 3 changed files with 79 additions and 11 deletions.
@@ -16,8 +16,11 @@ Invoke the jar, passing the name of the cloud provider you with to access (ex. a
The 4th parameter represents the **action** you want to run. Supported actions are:

- add
- exec
- exec
- run
- destroy
- listnodes
- listimages

Noting that **destroy** will destroy all nodes in the group. If the 4th parameter is exec, you must quote a command to execute across the group of nodes.

@@ -29,10 +32,23 @@ To execute a command to all nodes of the group *mygroup*:

java -jar target/compute-basics-jar-with-dependencies.jar provider identity credential mygroup exec "echo hello"

To run a script file in all nodes of the group *mygroup*

java -jar target/compute-basics-jar-with-dependencies.jar provider identity credential mygroup run /path/to/script-file

To destroy all nodes of the group *mygroup*:

java -jar target/compute-basics-jar-with-dependencies.jar provider identity credential mygroup destroy

To list the nodes: *groupname* parameter is not used

java -jar target/compute-basics-jar-with-dependencies.jar provider identity credential mygroup listnodes

To list the images: *groupname* parameter is not used

java -jar target/compute-basics-jar-with-dependencies.jar provider identity credential mygroup listimages


### Amazon EC2

java -jar target/compute-basics-jar-with-dependencies.jar aws-ec2 \
@@ -73,6 +89,14 @@ To destroy all nodes of the group *mygroup*:
-jar target/compute-basics-jar-with-dependencies.jar \
openstack-nova-ec2 tenantId:accesskey secretkey mygroup add

### Google Cloud Engine provider

java -jar target/compute-basics-jar-with-dependencies.jar \
google-compute-engine \
your-project-service-account-email@developer.gserviceaccount.com \
/path/to/private-key.pem \
mygroup add

## License

Copyright (C) 2009-2012 jclouds, Inc.
@@ -24,6 +24,9 @@ under the License.
<modelVersion>4.0.0</modelVersion>
<groupId>org.jclouds.examples</groupId>
<artifactId>compute-basics</artifactId>
<properties>
<jclouds.version>1.7.1</jclouds.version>
</properties>
<version>1.7.2-SNAPSHOT</version>
<name>compute-basics</name>
<description>jclouds compute example that adds a node to a group, then executes "echo hello" on all nodes</description>
@@ -32,31 +35,36 @@ under the License.
<dependency>
<groupId>org.apache.jclouds</groupId>
<artifactId>jclouds-compute</artifactId>
<version>1.7.1</version>
<version>${jclouds.version}</version>
</dependency>
<dependency>
<groupId>org.apache.jclouds</groupId>
<artifactId>jclouds-allcompute</artifactId>
<version>1.7.1</version>
<version>${jclouds.version}</version>
</dependency>
<dependency>
<groupId>org.apache.jclouds.labs</groupId>
<artifactId>joyentcloud</artifactId>
<version>1.7.1</version>
<version>${jclouds.version}</version>
</dependency>
<dependency>
<groupId>org.apache.jclouds.labs</groupId>
<artifactId>virtualbox</artifactId>
<version>1.7.1</version>
<version>${jclouds.version}</version>
</dependency>
<dependency>
<groupId>org.apache.jclouds.labs</groupId>
<artifactId>google-compute-engine</artifactId>
<version>${jclouds.version}</version>
</dependency>
<!-- note that if you want a smaller distribution
remove the above dependency and place something
like below -->
<!--
<dependency>
<groupId>org.apache.jclouds.provider</groupId>
<artifactId>gogrid</artifactId>
<version>1.7.1</version>
<version>${jclouds.version}</version>
</dependency>
-->
<dependency>
@@ -68,7 +76,7 @@ under the License.
<dependency>
<groupId>org.apache.jclouds.driver</groupId>
<artifactId>jclouds-bouncycastle</artifactId>
<version>1.7.1</version>
<version>${jclouds.version}</version>
<exclusions>
<!-- bouncy castle is a provider, so
it must be signed and used as-is.
@@ -84,12 +92,12 @@ under the License.
<dependency>
<groupId>org.apache.jclouds.driver</groupId>
<artifactId>jclouds-sshj</artifactId>
<version>1.7.1</version>
<version>${jclouds.version}</version>
</dependency>
<dependency>
<groupId>org.apache.jclouds.driver</groupId>
<artifactId>jclouds-enterprise</artifactId>
<version>1.7.1</version>
<version>${jclouds.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
@@ -35,6 +35,7 @@
import static org.jclouds.scriptbuilder.domain.Statements.exec;

import java.io.File;
import java.io.FileInputStream;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
@@ -68,6 +69,11 @@
import com.google.common.io.Files;
import com.google.inject.Module;

import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.ComputeMetadata;
import org.jclouds.compute.domain.OsFamily;
import org.jclouds.util.Strings2;

/**
* Demonstrates the use of {@link ComputeService}.
* <p/>
@@ -81,7 +87,7 @@
public class MainApp {

public static enum Action {
ADD, RUN, EXEC, DESTROY;
ADD, RUN, EXEC, DESTROY, LISTIMAGES, LISTNODES;
}

public static final Map<String, ApiMetadata> allApis = Maps.uniqueIndex(Apis.viewableAs(ComputeServiceContext.class),
@@ -107,7 +113,10 @@ public static void main(String[] args) {
if (action == Action.EXEC && args.length < PARAMETERS + 1)
throw new IllegalArgumentException("please quote the command to exec as the last parameter");
String command = (action == Action.EXEC) ? args[5] : "echo hello";


if (provider.equalsIgnoreCase("google-compute-engine"))
credential = getPrivateKeyFromFile(credential); // load the pem file as string

if (action == Action.RUN && args.length < PARAMETERS + 1)
throw new IllegalArgumentException("please pass the local file to run as the last parameter");
File file = null;
@@ -135,6 +144,9 @@ public static void main(String[] args) {
// 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 = compute.templateBuilder();

if(provider.equalsIgnoreCase("google-compute-engine"))
templateBuilder.osFamily(OsFamily.CENTOS);

// If you want to up the ram and leave everything default, you can
// just tweak minRam
@@ -202,6 +214,20 @@ public static void main(String[] args) {
Predicates.<NodeMetadata> and(not(TERMINATED), inGroup(groupName)));
System.out.printf("<< destroyed nodes %s%n", destroyed);
break;
case LISTIMAGES:
Set<? extends Image> imageList = compute.listImages();
System.out.printf(">> No of images %d\n", imageList.size());
for (Image img : imageList) {
System.out.println(">>>> " + img);
}
break;
case LISTNODES:
Set<? extends ComputeMetadata> nodeList = compute.listNodes();
System.out.printf(">> No of nodes/instances %d\n", nodeList.size());
for (ComputeMetadata nodeentry : nodeList) {
System.out.println(">>>> " + nodeentry);
}
break;
}
} catch (RunNodesException e) {
System.err.println("error adding node to group " + groupName + ": " + e.getMessage());
@@ -218,6 +244,16 @@ public static void main(String[] args) {
}
}

private static String getPrivateKeyFromFile(String filename) {
try {
return Strings2.toStringAndClose(new FileInputStream(filename));
} catch (java.io.IOException e) {
System.err.println("Exception : " + e);
e.printStackTrace();
}
return null;
}

static int error = 0;

private static ComputeService initComputeService(String provider, String identity, String credential) {

0 comments on commit c223b77

Please sign in to comment.