Repository: jclouds-examples Updated Branches: refs/heads/master 5372d58e5 -> c223b7716
Update compute-basics example for GCE provider, support listimages, listnodes commands in compute-basics example Project: http://git-wip-us.apache.org/repos/asf/jclouds-examples/repo Commit: http://git-wip-us.apache.org/repos/asf/jclouds-examples/commit/c223b771 Tree: http://git-wip-us.apache.org/repos/asf/jclouds-examples/tree/c223b771 Diff: http://git-wip-us.apache.org/repos/asf/jclouds-examples/diff/c223b771 Branch: refs/heads/master Commit: c223b7716b906f1b74162d8e1b81eef17d593e28 Parents: 5372d58 Author: Suriya priya Veluchamy <[email protected]> Authored: Wed Mar 12 22:22:10 2014 -0700 Committer: Ignasi Barrera <[email protected]> Committed: Mon Mar 17 10:15:01 2014 +0100 ---------------------------------------------------------------------- compute-basics/README.md | 26 ++++++++++++- compute-basics/pom.xml | 24 ++++++++---- .../examples/compute/basics/MainApp.java | 40 +++++++++++++++++++- 3 files changed, 79 insertions(+), 11 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/jclouds-examples/blob/c223b771/compute-basics/README.md ---------------------------------------------------------------------- diff --git a/compute-basics/README.md b/compute-basics/README.md index 8da8ca5..d36abc3 100755 --- a/compute-basics/README.md +++ b/compute-basics/README.md @@ -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 \ + [email protected] \ + /path/to/private-key.pem \ + mygroup add + ## License Copyright (C) 2009-2012 jclouds, Inc. http://git-wip-us.apache.org/repos/asf/jclouds-examples/blob/c223b771/compute-basics/pom.xml ---------------------------------------------------------------------- diff --git a/compute-basics/pom.xml b/compute-basics/pom.xml index 1c2a9f9..a69700d 100644 --- a/compute-basics/pom.xml +++ b/compute-basics/pom.xml @@ -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,23 +35,28 @@ 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 --> @@ -56,7 +64,7 @@ under the License. <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> http://git-wip-us.apache.org/repos/asf/jclouds-examples/blob/c223b771/compute-basics/src/main/java/org/jclouds/examples/compute/basics/MainApp.java ---------------------------------------------------------------------- diff --git a/compute-basics/src/main/java/org/jclouds/examples/compute/basics/MainApp.java b/compute-basics/src/main/java/org/jclouds/examples/compute/basics/MainApp.java index 78d25a4..3d2744b 100644 --- a/compute-basics/src/main/java/org/jclouds/examples/compute/basics/MainApp.java +++ b/compute-basics/src/main/java/org/jclouds/examples/compute/basics/MainApp.java @@ -35,6 +35,7 @@ import static org.jclouds.compute.predicates.NodePredicates.inGroup; 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.collect.Maps; 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 @@ import com.google.inject.Module; 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 class MainApp { 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 class MainApp { // 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 class MainApp { 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 class MainApp { } } + 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) {
