You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jclouds.apache.org by na...@apache.org on 2014/04/27 22:51:36 UTC
git commit: Add an example of using jclouds to create a GCE instance
(based on a Rackspace example)
Repository: jclouds-examples
Updated Branches:
refs/heads/master 46fca7ab1 -> 82f3903e0
Add an example of using jclouds to create a GCE instance (based on a Rackspace 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/82f3903e
Tree: http://git-wip-us.apache.org/repos/asf/jclouds-examples/tree/82f3903e
Diff: http://git-wip-us.apache.org/repos/asf/jclouds-examples/diff/82f3903e
Branch: refs/heads/master
Commit: 82f3903e089ca081c74e4288da4d4a7f38ba9abf
Parents: 46fca7a
Author: MikoĊaj Zalewski <mi...@google.com>
Authored: Wed Apr 16 19:42:27 2014 +0200
Committer: Ignasi Barrera <na...@apache.org>
Committed: Sun Apr 27 22:51:10 2014 +0200
----------------------------------------------------------------------
google/pom.xml | 51 +++++
.../google/computeengine/Constants.java | 34 ++++
.../google/computeengine/CreateServer.java | 185 +++++++++++++++++++
3 files changed, 270 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/jclouds-examples/blob/82f3903e/google/pom.xml
----------------------------------------------------------------------
diff --git a/google/pom.xml b/google/pom.xml
new file mode 100644
index 0000000..d7946be
--- /dev/null
+++ b/google/pom.xml
@@ -0,0 +1,51 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <properties>
+ <jclouds.version>1.7.1</jclouds.version>
+ </properties>
+ <groupId>org.apache.jclouds.examples</groupId>
+ <artifactId>google-examples</artifactId>
+ <version>1.0</version>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.jclouds.driver</groupId>
+ <artifactId>jclouds-slf4j</artifactId>
+ <version>${jclouds.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.jclouds.driver</groupId>
+ <artifactId>jclouds-sshj</artifactId>
+ <version>${jclouds.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>ch.qos.logback</groupId>
+ <artifactId>logback-classic</artifactId>
+ <version>1.0.13</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.jclouds.labs</groupId>
+ <artifactId>google-compute-engine</artifactId>
+ <version>${jclouds.version}</version>
+ </dependency>
+ </dependencies>
+</project>
http://git-wip-us.apache.org/repos/asf/jclouds-examples/blob/82f3903e/google/src/main/java/org/jclouds/examples/google/computeengine/Constants.java
----------------------------------------------------------------------
diff --git a/google/src/main/java/org/jclouds/examples/google/computeengine/Constants.java b/google/src/main/java/org/jclouds/examples/google/computeengine/Constants.java
new file mode 100644
index 0000000..29b3cd1
--- /dev/null
+++ b/google/src/main/java/org/jclouds/examples/google/computeengine/Constants.java
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.jclouds.examples.google.computeengine;
+
+import static java.util.concurrent.TimeUnit.SECONDS;
+
+/**
+ * Constants used by the Google Compute Engine Examples.
+ *
+ * @author Mikolaj Zalewski (based on code by Everett Toews)
+ */
+public interface Constants {
+ public static final String PROVIDER = System.getProperty("provider.cs", "google-compute-engine");
+ public static final String ZONE = System.getProperty("zone", "europe-west1-a");
+
+ public static final String NAME = "jclouds-example";
+ public static final String POLL_PERIOD_TWENTY_SECONDS = String.valueOf(SECONDS.toMillis(20));
+}
http://git-wip-us.apache.org/repos/asf/jclouds-examples/blob/82f3903e/google/src/main/java/org/jclouds/examples/google/computeengine/CreateServer.java
----------------------------------------------------------------------
diff --git a/google/src/main/java/org/jclouds/examples/google/computeengine/CreateServer.java b/google/src/main/java/org/jclouds/examples/google/computeengine/CreateServer.java
new file mode 100644
index 0000000..d2e8522
--- /dev/null
+++ b/google/src/main/java/org/jclouds/examples/google/computeengine/CreateServer.java
@@ -0,0 +1,185 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.jclouds.examples.google.computeengine;
+
+import com.google.common.io.Closeables;
+import com.google.common.io.Files;
+
+import org.jclouds.ContextBuilder;
+import org.jclouds.compute.ComputeService;
+import org.jclouds.compute.ComputeServiceContext;
+import org.jclouds.compute.RunNodesException;
+import org.jclouds.compute.domain.Hardware;
+import org.jclouds.compute.domain.Image;
+import org.jclouds.compute.domain.NodeMetadata;
+import org.jclouds.compute.domain.Template;
+
+import java.io.Closeable;
+import java.io.File;
+import java.io.IOException;
+import java.nio.charset.Charset;
+import java.util.Properties;
+import java.util.Set;
+import java.util.concurrent.TimeoutException;
+
+import static org.jclouds.compute.config.ComputeServiceProperties.POLL_INITIAL_PERIOD;
+import static org.jclouds.compute.config.ComputeServiceProperties.POLL_MAX_PERIOD;
+import static org.jclouds.examples.google.computeengine.Constants.*;
+
+/**
+ * This example creates a Debian Wheezy server on a f1-micro instance on the
+ * Google Compute Engine.
+ *
+ * @author Mikolaj Zalewski (based on code by Everett Toews)
+ */
+public class CreateServer implements Closeable {
+ private final ComputeService computeService;
+
+ /**
+ * To get a service account and its private key see [TODO: write some
+ * documentation on the website and put a link to it]
+ *
+ * The first argument (args[0]) must be your service account email address
+ * The second argument (args[1]) must a path to your service account
+ * private key PEM file (without a password).
+ */
+ public static void main(String[] args) throws IOException {
+ String key = Files.toString(new File(args[1]), Charset.defaultCharset());
+
+ CreateServer createServer = new CreateServer(args[0], key);
+
+ try {
+ createServer.createServer();
+ }
+ catch (Exception e) {
+ e.printStackTrace();
+ }
+ finally {
+ createServer.close();
+ }
+ }
+
+ public CreateServer(String serviceAccountEmailAddress, String serviceAccountKey) {
+ // These properties control how often jclouds polls for a status update
+ Properties overrides = new Properties();
+ overrides.setProperty(POLL_INITIAL_PERIOD, POLL_PERIOD_TWENTY_SECONDS);
+ overrides.setProperty(POLL_MAX_PERIOD, POLL_PERIOD_TWENTY_SECONDS);
+
+ ComputeServiceContext context = ContextBuilder.newBuilder(PROVIDER)
+ .credentials(serviceAccountEmailAddress, serviceAccountKey)
+ .overrides(overrides)
+ .buildView(ComputeServiceContext.class);
+ computeService = context.getComputeService();
+ }
+
+ /**
+ * Create a server based on a Template. This method uses Template.hardwareId() and Template.imageId() to
+ * also demonstrate iterating through Hardware and Images. Alternatively you do the same without iterating
+ * using the following Template.
+ *
+ * Template template = computeService.templateBuilder()
+ * .locationId(getLocationId())
+ * .osFamily(OsFamily.CENTOS)
+ * .osVersionMatches("6")
+ * .minRam(28*1024)
+ * .build();
+ */
+ private void createServer() throws RunNodesException, TimeoutException {
+ System.out.format("Create Server%n");
+
+ // TODO: make fromHardware(...) and fromImage(...) work as well. Currently,
+ // fromHardware chooses a deprecated platform and the call fails, while using
+ // hardwareId() and fromImage() causes no image to be found.
+ Template template = computeService.templateBuilder()
+ .locationId(ZONE)
+ .hardwareId(getHardware().getId())
+ .imageId(getImage().getId())
+ .build();
+
+ // This method will continue to poll for the server status and won't
+ // return until this server is ACTIVE.
+ // TODO: does GCE also log what's happening during the polling, like for
+ // Rackspace? If so, add an example for that.
+ Set<? extends NodeMetadata> nodes = computeService.createNodesInGroup(NAME, 1, template);
+
+ NodeMetadata nodeMetadata = nodes.iterator().next();
+ String publicAddress = nodeMetadata.getPublicAddresses().iterator().next();
+
+ System.out.format(" %s%n", nodeMetadata);
+ System.out.format(" Instance %s started with IP %s%n", nodeMetadata.getName(), publicAddress);
+ }
+
+ /**
+ * This method uses the generic ComputeService.listHardwareProfiles() to find the hardware profile.
+ *
+ * @return The Hardware for a f1-micro instance.
+ */
+ private Hardware getHardware() {
+ System.out.format(" Hardware Profiles%n");
+
+ Set<? extends Hardware> profiles = computeService.listHardwareProfiles();
+ Hardware result = null;
+
+ for (Hardware profile: profiles) {
+ System.out.format(" %s%n", profile);
+ if (profile.getId().equals(ZONE + "/f1-micro")) {
+ result = profile;
+ }
+ }
+
+ if (result == null) {
+ System.err.println("f1-micro flavor not found. Using first flavor found.%n");
+ result = profiles.iterator().next();
+ }
+ return result;
+ }
+
+ /**
+ * This method uses the generic ComputeService.listImages() to find the image.
+ *
+ * @return A Debian Wheezy Image
+ */
+ private Image getImage() {
+ System.out.format(" Images%n");
+
+ Set<? extends Image> images = computeService.listImages();
+ Image result = null;
+
+ for (Image image: images) {
+ System.out.format(" %s%n", image);
+ if (image.getOperatingSystem().getVersion().equals("debian.7.wheezy")) {
+ result = image;
+ }
+ }
+
+ if (result == null) {
+ System.err.println("Image with Debian Wheezy operating system not found. Using first image found.%n");
+ result = images.iterator().next();
+ }
+
+ return result;
+ }
+
+ /**
+ * Always close your service when you're done with it.
+ */
+ public void close() throws IOException {
+ Closeables.close(computeService.getContext(), true);
+ }
+}