Return-Path: X-Original-To: apmail-incubator-ace-commits-archive@minotaur.apache.org Delivered-To: apmail-incubator-ace-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 0F8C674C6 for ; Sat, 3 Dec 2011 09:11:00 +0000 (UTC) Received: (qmail 3551 invoked by uid 500); 3 Dec 2011 09:10:58 -0000 Delivered-To: apmail-incubator-ace-commits-archive@incubator.apache.org Received: (qmail 3526 invoked by uid 500); 3 Dec 2011 09:10:58 -0000 Mailing-List: contact ace-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: ace-dev@incubator.apache.org Delivered-To: mailing list ace-commits@incubator.apache.org Received: (qmail 3516 invoked by uid 99); 3 Dec 2011 09:10:56 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 03 Dec 2011 09:10:56 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 03 Dec 2011 09:10:54 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 7C5192388A66; Sat, 3 Dec 2011 09:10:33 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1209861 - in /incubator/ace/trunk/ace-nodelauncher-amazon: pom.xml src/main/java/org/apache/ace/nodelauncher/amazon/AmazonNodeLauncher.java Date: Sat, 03 Dec 2011 09:10:33 -0000 To: ace-commits@incubator.apache.org From: marrs@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20111203091033.7C5192388A66@eris.apache.org> Author: marrs Date: Sat Dec 3 09:10:33 2011 New Revision: 1209861 URL: http://svn.apache.org/viewvc?rev=1209861&view=rev Log: ACE-208 applied the patch, did some additional cleanup, formatting and documentation. Modified: incubator/ace/trunk/ace-nodelauncher-amazon/pom.xml incubator/ace/trunk/ace-nodelauncher-amazon/src/main/java/org/apache/ace/nodelauncher/amazon/AmazonNodeLauncher.java Modified: incubator/ace/trunk/ace-nodelauncher-amazon/pom.xml URL: http://svn.apache.org/viewvc/incubator/ace/trunk/ace-nodelauncher-amazon/pom.xml?rev=1209861&r1=1209860&r2=1209861&view=diff ============================================================================== --- incubator/ace/trunk/ace-nodelauncher-amazon/pom.xml (original) +++ incubator/ace/trunk/ace-nodelauncher-amazon/pom.xml Sat Dec 3 09:10:33 2011 @@ -88,7 +88,7 @@ org.jclouds.driver - jclouds-jsch + jclouds-sshj 1.2.1 compile Modified: incubator/ace/trunk/ace-nodelauncher-amazon/src/main/java/org/apache/ace/nodelauncher/amazon/AmazonNodeLauncher.java URL: http://svn.apache.org/viewvc/incubator/ace/trunk/ace-nodelauncher-amazon/src/main/java/org/apache/ace/nodelauncher/amazon/AmazonNodeLauncher.java?rev=1209861&r1=1209860&r2=1209861&view=diff ============================================================================== --- incubator/ace/trunk/ace-nodelauncher-amazon/src/main/java/org/apache/ace/nodelauncher/amazon/AmazonNodeLauncher.java (original) +++ incubator/ace/trunk/ace-nodelauncher-amazon/src/main/java/org/apache/ace/nodelauncher/amazon/AmazonNodeLauncher.java Sat Dec 3 09:10:33 2011 @@ -18,6 +18,9 @@ */ package org.apache.ace.nodelauncher.amazon; +import com.google.common.collect.ImmutableSet; +import com.google.common.io.Files; +import com.google.inject.Module; import org.apache.ace.nodelauncher.NodeLauncher; import org.jclouds.compute.ComputeService; import org.jclouds.compute.ComputeServiceContext; @@ -27,15 +30,18 @@ import org.jclouds.compute.domain.NodeMe import org.jclouds.compute.domain.NodeState; import org.jclouds.compute.domain.Template; import org.jclouds.compute.options.RunScriptOptions; +import org.jclouds.domain.Credentials; import org.jclouds.ec2.compute.options.EC2TemplateOptions; import org.jclouds.ec2.domain.InstanceType; import org.jclouds.scriptbuilder.domain.Statements; -import org.jclouds.ssh.jsch.config.JschSshClientModule; +import org.jclouds.sshj.config.SshjSshClientModule; import org.osgi.service.cm.ConfigurationException; import org.osgi.service.cm.ManagedService; +import java.io.File; import java.net.MalformedURLException; import java.net.URL; +import java.nio.charset.Charset; import java.util.*; import static org.jclouds.compute.predicates.NodePredicates.runningInGroup; @@ -43,10 +49,10 @@ import static org.jclouds.compute.predic /** * Simple NodeLauncher implementation that launches nodes based on a given AMI in Amazon EC2. * We expect the AMI we launch to have a java on its path, at least after bootstrap.

- * + *

* This service is configured using Config Admin; see the constants in the class for more information * about this.

- * + *

* After the node has been started up, this service will install a management agent on it. For this * to work, there should be an ace-launcher in the OBR of the server the node should connect to. */ @@ -81,6 +87,13 @@ public class AmazonNodeLauncher implemen public static final String SECRET_ACCESS_KEY = "secretAccessKey"; /** + * Configuration key: The (optional) name of an existing keypair to use when creating a new node. If you + * do not specify it, a new keypair will be created. Specifying an existing keypair makes it easier to + * for example log into each node with SSH using your existing keypair. + */ + public static final String KEYPAIR = "keypair"; + + /** * Configuration key: A prefix to use for all nodes launched by this service. You can use this (a) allow * multiple nodes with the same ID, but launcher from different NodeLauncher services, or (b) to more * easily identify your nodes in the AWS management console. @@ -102,17 +115,20 @@ public class AmazonNodeLauncher implemen * the possible options. */ public static final String LAUNCHER_ARGUMENTS = "launcherArguments"; - + /** * Configuration key: Extra ports to open on the nodes, besides the default ones (see DEFAULT_PORTS). */ public static final String EXTRA_PORTS = "extraPorts"; - + /** * Configuration key: Should we run the process as root? */ public static final String RUN_AS_ROOT = "runAsRoot"; + /** + * Configuration key: The hardware ID to use for the node. + */ public static final String HARDWARE_ID = "hardwareId"; /** @@ -120,18 +136,39 @@ public class AmazonNodeLauncher implemen */ public static final int[] DEFAULT_PORTS = new int[] {22, 80, 8080}; + /** + * Configuration key: The (optional) name of the JAR to launch to + * bootstrap the OSGi framework (or whatever you want to run on the + * node). + */ public static final String ACE_LAUNCHER = "aceLauncher"; + /** + * Configuration key: An additional list of artifacts that must be + * downloaded from the OBR when bootstrapping. + */ public static final String ADDITIONAL_OBR_DOWNLOADS = "additionalObrDownloads"; - public static final String EXERNAL_DOWNLOAD_URLS = "externalDownloadUrls"; + /** + * Configuration key: An additional list of URLs that must be downloaded when + * bootstrapping. + */ + public static final String EXTERNAL_DOWNLOAD_URLS = "externalDownloadUrls"; + + /** + * Configuration key: The (optional) private key file, which you must install on + * the ACE server locally if you want it to be used when creating new nodes. + */ + private static final String PRIVATE_KEY_FILE = "privateKeyFile"; private URL m_server; - private String m_amiId; + private String m_amiId; private String m_location; private String m_hardwareId; private String m_accessKeyId; private String m_secretAccessKey; + private String m_keypair; + private String m_privateKeyFile; private String m_tagPrefix; private String m_vmOptions; private String m_nodeBootstrap; @@ -143,37 +180,45 @@ public class AmazonNodeLauncher implemen private String m_externalDownloadUrls; - private ComputeServiceContext m_computeServiceContext; - + public void start() { Properties props = new Properties(); - m_computeServiceContext = new ComputeServiceContextFactory().createContext("aws-ec2", - m_accessKeyId, m_secretAccessKey, (List) Arrays.asList(new JschSshClientModule()), props); + m_computeServiceContext = new ComputeServiceContextFactory().createContext("aws-ec2", m_accessKeyId, m_secretAccessKey, ImmutableSet. of(new SshjSshClientModule()), props); } public void start(String id) throws Exception { ComputeService computeService = m_computeServiceContext.getComputeService(); Template template = computeService.templateBuilder() - .imageId(m_location + "/" + m_amiId) - .hardwareId(m_hardwareId) - .locationId(m_location) - .build(); - + .imageId(m_location + "/" + m_amiId) + .hardwareId(m_hardwareId) + .locationId(m_location) + .build(); + int[] extraPorts = parseExtraPorts(m_extraPorts); int[] inboundPorts = mergePorts(DEFAULT_PORTS, extraPorts); template.getOptions().as(EC2TemplateOptions.class).inboundPorts(inboundPorts); template.getOptions().blockOnComplete(false); template.getOptions().runAsRoot(m_runAsRoot); + if (useConfiguredKeyPair()) { + template.getOptions().as(EC2TemplateOptions.class).keyPair(m_keypair); + template.getOptions().overrideCredentialsWith(new Credentials("ec2-user", Files.toString(new File(m_privateKeyFile), Charset.defaultCharset()))); + } + Set tag = computeService.createNodesInGroup(m_tagPrefix + id, 1, template); - System.out.println("In case you need it, this is the key to ssh to " + id + ":\n" - + tag.iterator().next().getCredentials().credential); + if (!useConfiguredKeyPair()) { + System.out.println("In case you need it, this is the key to ssh to " + id + ":\n" + tag.iterator().next().getCredentials().credential); + } computeService.runScriptOnNodesMatching(runningInGroup(m_tagPrefix + id), - Statements.exec(buildStartupScript(id)), - RunScriptOptions.Builder.blockOnComplete(false)); + Statements.exec(buildStartupScript(id)), + RunScriptOptions.Builder.blockOnComplete(false)); } - + + private boolean useConfiguredKeyPair() { + return m_keypair != null && m_keypair.length() > 0; + } + int[] mergePorts(int[] first, int[] last) { int[] result = new int[first.length + last.length]; for (int i = 0; i < result.length; i++) { @@ -181,11 +226,11 @@ public class AmazonNodeLauncher implemen } return result; } - + int[] parseExtraPorts(String extraPorts) { extraPorts = extraPorts.trim(); if (extraPorts.isEmpty()) { - return new int[] {}; + return new int[]{}; } String[] ports = extraPorts.split(","); int[] result = new int[ports.length]; @@ -202,15 +247,12 @@ public class AmazonNodeLauncher implemen } script.append("wget ").append(new URL(m_server, "/obr/" + m_aceLauncher)).append(" ;"); - - for(String additonalDownload : m_additionalObrDownloads.split(",")) { + for (String additonalDownload : m_additionalObrDownloads.split(",")) { script.append("wget ").append(new URL(m_server, "/obr/" + additonalDownload.trim())).append(" ;"); } - - for(String additonalDownload : m_externalDownloadUrls.split(",")) { + for (String additonalDownload : m_externalDownloadUrls.split(",")) { script.append("wget ").append(additonalDownload.trim()).append(" ;"); } - script.append("nohup java -jar ").append(m_aceLauncher).append(" "); script.append("discovery=").append(m_server.toExternalForm()).append(" "); script.append("identification=").append(id).append(" "); @@ -222,10 +264,10 @@ public class AmazonNodeLauncher implemen public void stop(String id) { m_computeServiceContext.getComputeService().destroyNodesMatching(runningInGroup(m_tagPrefix + id)); } - + public Properties getProperties(String id) throws Exception { Properties result = new Properties(); - + NodeMetadata nodeMetadata = getNodeMetadataForRunningNodeWithTag(m_tagPrefix + id); if (nodeMetadata == null) { return null; @@ -233,7 +275,7 @@ public class AmazonNodeLauncher implemen result.put("id", id); result.put("node-id", nodeMetadata.getId()); result.put("ip", nodeMetadata.getPublicAddresses().iterator().next()); - + return result; } @@ -261,7 +303,8 @@ public class AmazonNodeLauncher implemen String hardwareId = getConfigProperty(properties, HARDWARE_ID, InstanceType.C1_MEDIUM); String accessKeyId = getConfigProperty(properties, ACCESS_KEY_ID); String secretAccessKey = getConfigProperty(properties, SECRET_ACCESS_KEY); - + String keyPair = getConfigProperty(properties, KEYPAIR, ""); + String privateKeyFile = getConfigProperty(properties, PRIVATE_KEY_FILE, ""); String vmOptions = getConfigProperty(properties, VM_OPTIONS, ""); String nodeBootstrap = getConfigProperty(properties, NODE_BOOTSTRAP, ""); String tagPrefix = getConfigProperty(properties, TAG_PREFIX, ""); @@ -270,7 +313,7 @@ public class AmazonNodeLauncher implemen String runAsRoot = getConfigProperty(properties, RUN_AS_ROOT, "false"); String aceLauncher = getConfigProperty(properties, ACE_LAUNCHER, "ace-launcher.jar"); String additionalObrDownloads = getConfigProperty(properties, ADDITIONAL_OBR_DOWNLOADS); - String externalDownloadUrls = getConfigProperty(properties, EXERNAL_DOWNLOAD_URLS); + String externalDownloadUrls = getConfigProperty(properties, EXTERNAL_DOWNLOAD_URLS); m_server = server; m_amiId = amiId; @@ -278,6 +321,8 @@ public class AmazonNodeLauncher implemen m_hardwareId = hardwareId; m_accessKeyId = accessKeyId; m_secretAccessKey = secretAccessKey; + m_keypair = keyPair; + m_privateKeyFile = privateKeyFile; m_tagPrefix = tagPrefix; m_vmOptions = vmOptions; m_nodeBootstrap = nodeBootstrap;