Issue 339: refactor so that scripts can be named

This commit is contained in:
Adrian Cole 2010-09-24 11:39:45 -07:00
parent cf5080b550
commit 4dec489d42
56 changed files with 1330 additions and 676 deletions

View File

@ -26,8 +26,11 @@ import static com.google.common.base.Preconditions.checkState;
import java.util.Arrays;
import java.util.Set;
import org.jclouds.aws.cloudwatch.CloudWatchClient;
import org.jclouds.compute.options.TemplateOptions;
import org.jclouds.domain.Credentials;
import org.jclouds.io.Payload;
import org.jclouds.scriptbuilder.domain.Statement;
import org.jclouds.util.Utils;
import com.google.common.collect.ImmutableSet;
@ -261,7 +264,7 @@ public class EC2TemplateOptions extends TemplateOptions {
// methods that only facilitate returning the correct object type
/**
* @see TemplateOptions#blockOnPort
* {@inheritDoc}
*/
@Override
public EC2TemplateOptions blockOnPort(int port, int seconds) {
@ -269,12 +272,7 @@ public class EC2TemplateOptions extends TemplateOptions {
}
/**
*
* special thing is that we do assume if you are passing groups that you have everything you need
* already defined. for example, our option inboundPorts normally creates ingress rules
* accordingly but if we notice you've specified securityGroups, we do not mess with rules at all
*
* @see TemplateOptions#inboundPorts
* {@inheritDoc}
*/
@Override
public EC2TemplateOptions inboundPorts(int... ports) {
@ -282,41 +280,41 @@ public class EC2TemplateOptions extends TemplateOptions {
}
/**
* @see TemplateOptions#authorizePublicKey(String)
* {@inheritDoc}
*/
@Override
@Deprecated
public EC2TemplateOptions authorizePublicKey(String publicKey) {
return EC2TemplateOptions.class.cast(super.authorizePublicKey(publicKey));
}
/**
* @see TemplateOptions#authorizePublicKey(Payload)
* {@inheritDoc}
*/
@Override
@Deprecated
public EC2TemplateOptions authorizePublicKey(Payload publicKey) {
return EC2TemplateOptions.class.cast(super.authorizePublicKey(publicKey));
}
/**
* @see TemplateOptions#installPrivateKey(String)
* {@inheritDoc}
*/
@Override
@Deprecated
public EC2TemplateOptions installPrivateKey(String privateKey) {
return EC2TemplateOptions.class.cast(super.installPrivateKey(privateKey));
}
/**
* @see TemplateOptions#installPrivateKey(Payload)
* {@inheritDoc}
*/
@Override
@Deprecated
public EC2TemplateOptions installPrivateKey(Payload privateKey) {
return EC2TemplateOptions.class.cast(super.installPrivateKey(privateKey));
}
/**
* @see TemplateOptions#runScript(Payload)
* {@inheritDoc}
*/
@Override
public EC2TemplateOptions runScript(Payload script) {
@ -324,7 +322,7 @@ public class EC2TemplateOptions extends TemplateOptions {
}
/**
* @see TemplateOptions#runScript(byte[])
* {@inheritDoc}
*/
@Override
@Deprecated
@ -333,13 +331,61 @@ public class EC2TemplateOptions extends TemplateOptions {
}
/**
* @see TemplateOptions#withMetadata
* {@inheritDoc}
*/
@Override
public EC2TemplateOptions withMetadata() {
return EC2TemplateOptions.class.cast(super.withMetadata());
}
/**
* {@inheritDoc}
*/
@Override
public EC2TemplateOptions blockUntilRunning(boolean blockUntilRunning) {
return EC2TemplateOptions.class.cast(super.blockUntilRunning(blockUntilRunning));
}
/**
* {@inheritDoc}
*/
@Override
public EC2TemplateOptions dontAuthorizePublicKey() {
return EC2TemplateOptions.class.cast(super.dontAuthorizePublicKey());
}
/**
* {@inheritDoc}
*/
@Override
public EC2TemplateOptions nameTask(String name) {
return EC2TemplateOptions.class.cast(super.nameTask(name));
}
/**
* {@inheritDoc}
*/
@Override
public EC2TemplateOptions runAsRoot(boolean runAsRoot) {
return EC2TemplateOptions.class.cast(super.runAsRoot(runAsRoot));
}
/**
* {@inheritDoc}
*/
@Override
public EC2TemplateOptions runScript(Statement script) {
return EC2TemplateOptions.class.cast(super.runScript(script));
}
/**
* {@inheritDoc}
*/
@Override
public EC2TemplateOptions withOverridingCredentials(Credentials overridingCredentials) {
return EC2TemplateOptions.class.cast(super.withOverridingCredentials(overridingCredentials));
}
/**
* @return groupIds the user specified to run instances with, or zero length set to create an
* implicit group

View File

@ -172,7 +172,7 @@ public class CloudApplicationArchitecturesEC2ClientLiveTest {
String script = new ScriptBuilder() // lamp install script
.addStatement(exec("runurl run.alestic.com/apt/upgrade"))//
.addStatement(exec("runurl run.alestic.com/install/lamp"))//
.build(OsFamily.UNIX);
.render(OsFamily.UNIX);
RunningInstance instance = null;
while (instance == null) {

View File

@ -299,7 +299,7 @@ public class EBSBootEC2ClientLiveTest {
"du -sk {varl}EBS_MOUNT_POINT{varr}", "echo size of source",
"du -sk {varl}IMAGE_DIR{varr}", "rm -rf {varl}IMAGE_DIR{varr}/*",
"umount {varl}EBS_MOUNT_POINT{varr}", "echo " + SCRIPT_END)))
.build(OsFamily.UNIX);
.render(OsFamily.UNIX);
}
@Test(enabled = false, dependsOnMethods = "testCreateAndAttachVolume")

View File

@ -44,7 +44,7 @@ import com.google.common.collect.ImmutableSet;
@Test(groups = "unit", testName = "ec2.RegionAndIdToImageTest")
public class RegionAndIdToImageTest {
@SuppressWarnings({ "unchecked", "rawtypes" })
@SuppressWarnings({ "unchecked"})
@Test
public void testApply() {
@ -76,7 +76,7 @@ public class RegionAndIdToImageTest {
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@SuppressWarnings({ "unchecked"})
@Test
public void testApplyNotFound() {

View File

@ -83,7 +83,7 @@ public class RunningInstanceToNodeMetadataTest {
DateService dateService = new SimpleDateFormatDateService();
@SuppressWarnings({ "unchecked", "rawtypes" })
@SuppressWarnings( { "unchecked" })
@Test
public void testApplyWithEBSWhenBootIsInstanceStoreAndAvailabilityZoneNotFound() throws UnknownHostException {
EC2Client client = createMock(EC2Client.class);
@ -129,12 +129,9 @@ public class RunningInstanceToNodeMetadataTest {
expect(instance.getInstanceType()).andReturn(InstanceType.M1_SMALL).atLeastOnce();
expect(instance.getEbsBlockDevices()).andReturn(
ImmutableMap.<String, EbsBlockDevice> of(
"/dev/sdg",
new EbsBlockDevice("vol-1f20d376", Attachment.Status.ATTACHED, dateService
.iso8601DateParse("2009-12-11T16:32:46.000Z"), false),
"/dev/sdj",
new EbsBlockDevice("vol-c0eb78aa", Attachment.Status.ATTACHED, dateService
ImmutableMap.<String, EbsBlockDevice> of("/dev/sdg", new EbsBlockDevice("vol-1f20d376",
Attachment.Status.ATTACHED, dateService.iso8601DateParse("2009-12-11T16:32:46.000Z"), false),
"/dev/sdj", new EbsBlockDevice("vol-c0eb78aa", Attachment.Status.ATTACHED, dateService
.iso8601DateParse("2010-06-17T10:43:28.000Z"), false)));
expect(instance.getRootDeviceType()).andReturn(RootDeviceType.INSTANCE_STORE);
expect(instance.getRootDeviceName()).andReturn(null).atLeastOnce();
@ -160,8 +157,8 @@ public class RunningInstanceToNodeMetadataTest {
assertEquals(metadata.getHardware().getProviderId(), "m1.small");
assertEquals(metadata.getHardware().getProcessors(), ImmutableList.<Processor> of(new Processor(1.0, 1.0)));
assertEquals(metadata.getHardware().getRam(), 1740);
assertEquals(metadata.getHardware().getVolumes(),
ImmutableList.<Volume> of(new VolumeImpl(null, Volume.Type.LOCAL, 10.0f, "/dev/sda1", true, false),//
assertEquals(metadata.getHardware().getVolumes(), ImmutableList.<Volume> of(new VolumeImpl(null,
Volume.Type.LOCAL, 10.0f, "/dev/sda1", true, false),//
new VolumeImpl(null, Volume.Type.LOCAL, 150.0f, "/dev/sda2", false, false),//
new VolumeImpl("vol-1f20d376", Volume.Type.SAN, null, "/dev/sdg", false, true),//
new VolumeImpl("vol-c0eb78aa", Volume.Type.SAN, null, "/dev/sdj", false, true)));
@ -178,7 +175,7 @@ public class RunningInstanceToNodeMetadataTest {
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@SuppressWarnings( { "unchecked" })
@Test
public void testApplyForNovaWhereNullAvailabilityZoneIpAddressNoGroups() throws UnknownHostException {
EC2Client client = createMock(EC2Client.class);
@ -246,8 +243,8 @@ public class RunningInstanceToNodeMetadataTest {
assertEquals(metadata.getHardware().getProviderId(), "m1.small");
assertEquals(metadata.getHardware().getProcessors(), ImmutableList.<Processor> of(new Processor(1.0, 1.0)));
assertEquals(metadata.getHardware().getRam(), 1740);
assertEquals(metadata.getHardware().getVolumes(),
ImmutableList.<Volume> of(new VolumeImpl(null, Volume.Type.LOCAL, 10.0f, "/dev/sda1", true, false),//
assertEquals(metadata.getHardware().getVolumes(), ImmutableList.<Volume> of(new VolumeImpl(null,
Volume.Type.LOCAL, 10.0f, "/dev/sda1", true, false),//
new VolumeImpl(null, Volume.Type.LOCAL, 150.0f, "/dev/sda2", false, false)));
assertEquals(metadata.getCredentials(), new Credentials("user", null));
@ -262,7 +259,7 @@ public class RunningInstanceToNodeMetadataTest {
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@SuppressWarnings( { "unchecked" })
@Test
public void testApplyWhereUnknownInstanceType() throws UnknownHostException {
EC2Client client = createMock(EC2Client.class);
@ -337,7 +334,7 @@ public class RunningInstanceToNodeMetadataTest {
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@SuppressWarnings( { "unchecked" })
@Test
public void testApplyForNovaWhereImageNotFound() throws UnknownHostException {
EC2Client client = createMock(EC2Client.class);
@ -404,8 +401,8 @@ public class RunningInstanceToNodeMetadataTest {
assertEquals(metadata.getHardware().getProviderId(), "m1.small");
assertEquals(metadata.getHardware().getProcessors(), ImmutableList.<Processor> of(new Processor(1.0, 1.0)));
assertEquals(metadata.getHardware().getRam(), 1740);
assertEquals(metadata.getHardware().getVolumes(),
ImmutableList.<Volume> of(new VolumeImpl(null, Volume.Type.LOCAL, 10.0f, "/dev/sda1", true, false),//
assertEquals(metadata.getHardware().getVolumes(), ImmutableList.<Volume> of(new VolumeImpl(null,
Volume.Type.LOCAL, 10.0f, "/dev/sda1", true, false),//
new VolumeImpl(null, Volume.Type.LOCAL, 150.0f, "/dev/sda2", false, false)));
assertEquals(metadata.getCredentials(), new Credentials("root", null));
@ -734,7 +731,7 @@ public class RunningInstanceToNodeMetadataTest {
verify(instance);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@SuppressWarnings( { "unchecked" })
@Test
public void testApplyWithKeyPairCreatesTagOfParsedSecurityGroupAndCredentialsBasedOnIt() throws UnknownHostException {
EC2Client client = createMock(EC2Client.class);
@ -808,7 +805,7 @@ public class RunningInstanceToNodeMetadataTest {
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@SuppressWarnings( { "unchecked" })
@Test
public void testApplyWithTwoSecurityGroups() throws UnknownHostException {
EC2Client client = createMock(EC2Client.class);

View File

@ -32,7 +32,6 @@ import static org.testng.Assert.assertEquals;
import java.io.IOException;
import org.jclouds.compute.options.TemplateOptions;
import org.jclouds.util.Utils;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableSet;
@ -194,19 +193,17 @@ public class EC2TemplateOptionsTest {
}
// superclass tests
@SuppressWarnings("deprecation")
@Test(expectedExceptions = IllegalArgumentException.class)
public void testinstallPrivateKeyBadFormat() {
EC2TemplateOptions options = new EC2TemplateOptions();
options.installPrivateKey("whompy");
}
@SuppressWarnings("deprecation")
@Test
public void testinstallPrivateKey() throws IOException {
EC2TemplateOptions options = new EC2TemplateOptions();
options.installPrivateKey("-----BEGIN RSA PRIVATE KEY-----");
assertEquals(Utils.toStringAndClose(options.getPrivateKey().getInput()), "-----BEGIN RSA PRIVATE KEY-----");
assertEquals(options.getPrivateKey(), "-----BEGIN RSA PRIVATE KEY-----");
}
@Test
@ -218,7 +215,7 @@ public class EC2TemplateOptionsTest {
@Test
public void testinstallPrivateKeyStatic() throws IOException {
EC2TemplateOptions options = installPrivateKey("-----BEGIN RSA PRIVATE KEY-----");
assertEquals(Utils.toStringAndClose(options.getPrivateKey().getInput()), "-----BEGIN RSA PRIVATE KEY-----");
assertEquals(options.getPrivateKey(), "-----BEGIN RSA PRIVATE KEY-----");
}
@Test(expectedExceptions = NullPointerException.class)
@ -226,19 +223,17 @@ public class EC2TemplateOptionsTest {
installPrivateKey(null);
}
@SuppressWarnings("deprecation")
@Test(expectedExceptions = IllegalArgumentException.class)
public void testauthorizePublicKeyBadFormat() {
EC2TemplateOptions options = new EC2TemplateOptions();
options.authorizePublicKey("whompy");
}
@SuppressWarnings("deprecation")
@Test
public void testauthorizePublicKey() throws IOException {
EC2TemplateOptions options = new EC2TemplateOptions();
options.authorizePublicKey("ssh-rsa");
assertEquals(Utils.toStringAndClose(options.getPublicKey().getInput()), "ssh-rsa");
assertEquals(options.getPublicKey(), "ssh-rsa");
}
@Test
@ -250,7 +245,7 @@ public class EC2TemplateOptionsTest {
@Test
public void testauthorizePublicKeyStatic() throws IOException {
EC2TemplateOptions options = authorizePublicKey("ssh-rsa");
assertEquals(Utils.toStringAndClose(options.getPublicKey().getInput()), "ssh-rsa");
assertEquals(options.getPublicKey(), "ssh-rsa");
}
@Test(expectedExceptions = NullPointerException.class)

View File

@ -90,7 +90,7 @@ public class EC2RunNodesAndAddToSetStrategyTest {
return null;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@SuppressWarnings({ "unchecked"})
private void assertRegionAndZoneForLocation(Location location, String region, String zone) {
String imageId = "ami1";
String instanceCreatedId = "instance1";

View File

@ -192,9 +192,10 @@ public class PlacementGroupClientLiveTest {
assertEquals(template.getHardware().getProviderId(), InstanceType.CC1_4XLARGE);
assertEquals(template.getImage().getId(), "us-east-1/ami-7ea24a17");
template.getOptions().installPrivateKey(newStringPayload(keyPair.get("private"))).authorizePublicKey(
newStringPayload(keyPair.get("public"))).runScript(
newStringPayload(BaseComputeServiceLiveTest.buildScript(template.getImage().getOperatingSystem())));
template.getOptions().installPrivateKey(keyPair.get("private")).authorizePublicKey(keyPair.get("public"))
.runScript(
newStringPayload(BaseComputeServiceLiveTest.buildScript(template.getImage()
.getOperatingSystem())));
String tag = PREFIX + "cccluster";
context.getComputeService().destroyNodesMatching(NodePredicates.withTag(tag));

View File

@ -160,7 +160,7 @@ public class MainApp {
.addStatement(exec("runurl run.alestic.com/apt/upgrade"))//
.addStatement(exec("runurl run.alestic.com/install/lamp"))//
.addStatement(exec("apt-get -y install openjdk-6-jdk"))// no license agreement!
.build(OsFamily.UNIX);
.render(OsFamily.UNIX);
System.out.printf("%d: running instance%n", System.currentTimeMillis());
Reservation<? extends RunningInstance> reservation = client.getInstanceServices().runInstancesInRegion(null, null, // allow

View File

@ -1,71 +0,0 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.compute.callables;
import static com.google.common.base.Preconditions.checkNotNull;
import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.util.ComputeServiceUtils.SshCallable;
import org.jclouds.io.Payload;
import org.jclouds.logging.Logger;
import org.jclouds.ssh.ExecResponse;
import org.jclouds.ssh.SshClient;
import com.google.common.collect.Iterables;
/**
*
* @author Adrian Cole
*/
public class AuthorizeRSAPublicKey implements SshCallable<ExecResponse> {
private SshClient ssh;
private final NodeMetadata node;
private final Payload publicKey;
private Logger logger = Logger.NULL;
public AuthorizeRSAPublicKey(NodeMetadata node, Payload publicKey) {
this.node = checkNotNull(node, "node");
this.publicKey = checkNotNull(publicKey, "publicKey");
}
@Override
public ExecResponse call() throws Exception {
ssh.exec("mkdir .ssh");
ssh.put(".ssh/id_rsa.pub", publicKey);
logger.debug(">> authorizing rsa public key for %s@%s", node.getCredentials().identity, Iterables.get(node
.getPublicAddresses(), 0));
ExecResponse returnVal = ssh.exec("cat .ssh/id_rsa.pub >> .ssh/authorized_keys");
returnVal = ssh.exec("chmod 600 .ssh/authorized_keys");
logger.debug("<< complete(%d)", returnVal.getExitCode());
return returnVal;
}
@Override
public void setConnection(SshClient ssh, Logger logger) {
this.logger = checkNotNull(logger, "logger");
this.ssh = checkNotNull(ssh, "ssh");
}
@Override
public NodeMetadata getNode() {
return node;
}
}

View File

@ -0,0 +1,102 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.compute.callables;
import static com.google.common.base.Preconditions.checkNotNull;
import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.util.ComputeServiceUtils;
import org.jclouds.compute.util.ComputeServiceUtils.SshCallable;
import org.jclouds.io.Payloads;
import org.jclouds.logging.Logger;
import org.jclouds.scriptbuilder.domain.OsFamily;
import org.jclouds.scriptbuilder.domain.Statement;
import org.jclouds.ssh.ExecResponse;
import org.jclouds.ssh.SshClient;
import com.google.common.collect.Iterables;
/**
*
* @author Adrian Cole
*/
public class InitAndStartScriptOnNode implements SshCallable<ExecResponse> {
protected SshClient ssh;
protected final NodeMetadata node;
protected final String scriptName;
protected final Statement script;
protected final boolean runAsRoot;
protected Logger logger = Logger.NULL;
public InitAndStartScriptOnNode(NodeMetadata node, String scriptName, Statement script, boolean runAsRoot) {
this.node = checkNotNull(node, "node");
this.scriptName = checkNotNull(scriptName, "scriptName");
this.script = checkNotNull(script, "script");
this.runAsRoot = runAsRoot;
}
@Override
public ExecResponse call() {
ssh.put(scriptName, Payloads.newPayload(script.render(OsFamily.UNIX)));
ExecResponse returnVal = ssh.exec("chmod 755 " + scriptName);
returnVal = ssh.exec("./" + scriptName + " init");
logger.debug("<< initialized(%d)", returnVal.getExitCode());
String command = (runAsRoot) ? startScriptAsRoot() : startScriptAsDefaultUser();
returnVal = runCommand(command);
logger.debug("<< start(%d)", returnVal.getExitCode());
return returnVal;
}
protected ExecResponse runCommand(String command) {
ExecResponse returnVal;
logger.debug(">> running [%s] as %s@%s", command.replace(node.getCredentials().credential, "XXXXX"), node
.getCredentials().identity, Iterables.get(node.getPublicAddresses(), 0));
returnVal = ssh.exec(command);
return returnVal;
}
@Override
public void setConnection(SshClient ssh, Logger logger) {
this.logger = checkNotNull(logger, "logger");
this.ssh = checkNotNull(ssh, "ssh");
}
protected String startScriptAsRoot() {
String command;
if (node.getCredentials().identity.equals("root")) {
command = "./" + scriptName + " start";
} else if (ComputeServiceUtils.isKeyAuth(node)) {
command = "sudo ./" + scriptName + " start";
} else {
command = String.format("echo '%s'|sudo -S ./%s", node.getCredentials().credential, scriptName + " start");
}
return command;
}
protected String startScriptAsDefaultUser() {
return "./" + scriptName + " start";
}
@Override
public NodeMetadata getNode() {
return node;
}
}

View File

@ -1,68 +0,0 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.compute.callables;
import static com.google.common.base.Preconditions.checkNotNull;
import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.util.ComputeServiceUtils.SshCallable;
import org.jclouds.io.Payload;
import org.jclouds.logging.Logger;
import org.jclouds.ssh.ExecResponse;
import org.jclouds.ssh.SshClient;
import com.google.common.collect.Iterables;
/**
*
* @author Adrian Cole
*/
public class InstallRSAPrivateKey implements SshCallable<ExecResponse> {
private SshClient ssh;
private final NodeMetadata node;
private final Payload privateKey;
private Logger logger = Logger.NULL;
public InstallRSAPrivateKey(NodeMetadata node, Payload privateKey) {
this.node = checkNotNull(node, "node");
this.privateKey = checkNotNull(privateKey, "privateKey");
}
@Override
public ExecResponse call() throws Exception {
ssh.exec("mkdir .ssh");
ssh.put(".ssh/id_rsa", privateKey);
logger.debug(">> installing rsa key for %s@%s", node.getCredentials().identity, Iterables.get(node
.getPublicAddresses(), 0));
return ssh.exec("chmod 600 .ssh/id_rsa");
}
@Override
public void setConnection(SshClient ssh, Logger logger) {
this.logger = checkNotNull(logger, "logger");
this.ssh = checkNotNull(ssh, "ssh");
}
@Override
public NodeMetadata getNode() {
return node;
}
}

View File

@ -19,86 +19,46 @@
package org.jclouds.compute.callables;
import static com.google.common.base.Preconditions.checkNotNull;
import java.io.IOException;
import java.util.Collections;
import javax.inject.Named;
import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.predicates.ScriptStatusReturnsZero.CommandUsingClient;
import org.jclouds.compute.util.ComputeServiceUtils;
import org.jclouds.compute.util.ComputeServiceUtils.SshCallable;
import org.jclouds.io.Payload;
import org.jclouds.io.Payloads;
import org.jclouds.logging.Logger;
import org.jclouds.scriptbuilder.InitBuilder;
import org.jclouds.scriptbuilder.domain.OsFamily;
import org.jclouds.scriptbuilder.domain.Statement;
import org.jclouds.scriptbuilder.domain.Statements;
import org.jclouds.ssh.ExecResponse;
import org.jclouds.ssh.SshClient;
import org.jclouds.util.Utils;
import com.google.common.base.Predicate;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
/**
*
* @author Adrian Cole
*/
public class RunScriptOnNode implements SshCallable<ExecResponse> {
private SshClient ssh;
public class RunScriptOnNode extends InitAndStartScriptOnNode {
protected final Predicate<CommandUsingClient> runScriptNotRunning;
private final NodeMetadata node;
private final String scriptName;
private final Payload script;
private final boolean runAsRoot;
private Logger logger = Logger.NULL;
public RunScriptOnNode(@Named("SCRIPT_COMPLETE") Predicate<CommandUsingClient> runScriptNotRunning,
NodeMetadata node, String scriptName, Payload script) {
NodeMetadata node, String scriptName, Statement script) {
this(runScriptNotRunning, node, scriptName, script, true);
}
public RunScriptOnNode(@Named("SCRIPT_COMPLETE") Predicate<CommandUsingClient> runScriptNotRunning,
NodeMetadata node, String scriptName, Payload script, boolean runAsRoot) {
NodeMetadata node, String scriptName, Statement script, boolean runAsRoot) {
super(node, scriptName, createInitScript(scriptName, script), runAsRoot);
this.runScriptNotRunning = runScriptNotRunning;
this.node = checkNotNull(node, "node");
this.scriptName = checkNotNull(scriptName, "scriptName");
this.script = createRunScript(scriptName, script);
this.runAsRoot = runAsRoot;
}
public static Payload createRunScript(String scriptName, Payload script) {
public static Statement createInitScript(String scriptName, Statement script) {
String path = "/tmp/" + scriptName;
InitBuilder initBuilder = new InitBuilder(scriptName, path, path, Collections.<String, String> emptyMap(),
ImmutableList.<Statement> of(Statements.interpret(splitOnNewlines(script))));
return Payloads.newByteArrayPayload(initBuilder.build(OsFamily.UNIX).getBytes());
}
static String[] splitOnNewlines(Payload script) {
try {
String asString = Utils.toStringAndClose(checkNotNull(script, "script").getInput());
return Iterables.toArray(Splitter.on("\n").split(asString), String.class);
} catch (IOException e) {
throw new RuntimeException(e);
}
return new InitBuilder(scriptName, path, path, Collections.<String, String> emptyMap(), Collections
.singleton(script));
}
@Override
public ExecResponse call() throws Exception {
ssh.put(scriptName, script);
ExecResponse returnVal = ssh.exec("chmod 755 " + scriptName);
returnVal = ssh.exec("./" + scriptName + " init");
logger.debug("<< initialized(%d)", returnVal.getExitCode());
String command = (runAsRoot) ? runScriptAsRoot() : runScriptAsDefaultUser();
returnVal = runCommand(command);
logger.debug("<< start(%d)", returnVal.getExitCode());
public ExecResponse call() {
ExecResponse returnVal = super.call();
boolean complete = runScriptNotRunning.apply(new CommandUsingClient("./" + scriptName + " status", ssh));
logger.debug("<< complete(%s)", complete);
@ -110,39 +70,4 @@ public class RunScriptOnNode implements SshCallable<ExecResponse> {
}
return returnVal;
}
private ExecResponse runCommand(String command) {
ExecResponse returnVal;
logger.debug(">> running [%s] as %s@%s", command.replace(node.getCredentials().credential, "XXXXX"), node
.getCredentials().identity, Iterables.get(node.getPublicAddresses(), 0));
returnVal = ssh.exec(command);
return returnVal;
}
@Override
public void setConnection(SshClient ssh, Logger logger) {
this.logger = checkNotNull(logger, "logger");
this.ssh = checkNotNull(ssh, "ssh");
}
private String runScriptAsRoot() {
String command;
if (node.getCredentials().identity.equals("root")) {
command = "./" + scriptName + " start";
} else if (ComputeServiceUtils.isKeyAuth(node)) {
command = "sudo ./" + scriptName + " start";
} else {
command = String.format("echo '%s'|sudo -S ./%s", node.getCredentials().credential, scriptName + " start");
}
return command;
}
private String runScriptAsDefaultUser() {
return "./" + scriptName + " start";
}
@Override
public NodeMetadata getNode() {
return node;
}
}

View File

@ -45,7 +45,6 @@ import org.jclouds.compute.ComputeService;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.RunNodesException;
import org.jclouds.compute.RunScriptOnNodesException;
import org.jclouds.compute.callables.RunScriptOnNode;
import org.jclouds.compute.domain.ComputeMetadata;
import org.jclouds.compute.domain.Hardware;
import org.jclouds.compute.domain.Image;
@ -63,12 +62,14 @@ import org.jclouds.compute.strategy.ListNodesStrategy;
import org.jclouds.compute.strategy.RebootNodeStrategy;
import org.jclouds.compute.strategy.RunNodesAndAddToSetStrategy;
import org.jclouds.compute.util.ComputeUtils;
import org.jclouds.domain.Credentials;
import org.jclouds.domain.Location;
import org.jclouds.io.Payload;
import org.jclouds.logging.Logger;
import org.jclouds.predicates.RetryablePredicate;
import org.jclouds.scriptbuilder.domain.Statements;
import org.jclouds.ssh.ExecResponse;
import org.jclouds.ssh.SshClient;
import org.jclouds.util.Utils;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
@ -151,6 +152,8 @@ public class BaseComputeService implements ComputeService {
throws RunNodesException {
checkArgument(tag.indexOf('-') == -1, "tag cannot contain hyphens");
checkNotNull(template.getLocation(), "location");
if (template.getOptions().getTaskName() == null && template.getOptions().getRunScript() != null)
template.getOptions().nameTask("bootstrap");
logger.debug(">> running %d node%s tag(%s) location(%s) image(%s) hardwareProfile(%s) options(%s)", count,
count > 1 ? "s" : "", tag, template.getLocation().getId(), template.getImage().getId(), template
.getHardware().getId(), template.getOptions());
@ -350,14 +353,19 @@ public class BaseComputeService implements ComputeService {
@Override
public Map<NodeMetadata, ExecResponse> runScriptOnNodesMatching(Predicate<NodeMetadata> filter,
final Payload runScript, @Nullable final RunScriptOptions options) throws RunScriptOnNodesException {
Iterable<NodeMetadata> nodes = verifyParametersAndListNodes(filter, runScript, (options != null) ? options
: RunScriptOptions.NONE);
checkNotNull(filter, "Filter must be provided");
checkNotNull(runScript, "runScript");
checkNotNull(options, "options");
if (options.getTaskName() == null)
options.nameTask("jclouds-script-" + System.currentTimeMillis());
Iterable<? extends NodeMetadata> nodes = Iterables.filter(detailsOnAllNodes(), filter);
final Map<NodeMetadata, ExecResponse> execs = Maps.newHashMap();
final Map<NodeMetadata, Future<Void>> responses = Maps.newHashMap();
final Map<NodeMetadata, Exception> badNodes = Maps.newLinkedHashMap();
Map<NodeMetadata, Future<Void>> responses = Maps.newHashMap();
nodes = filterNodesWhoCanRunScripts(nodes, badNodes, options.getOverrideCredentials());
for (final NodeMetadata node : nodes) {
@ -365,57 +373,39 @@ public class BaseComputeService implements ComputeService {
@Override
public Void call() throws Exception {
try {
RunScriptOnNode callable;
if (options.isRunAsRoot())
callable = utils.runScriptOnNode(node, "computeserv", runScript);
else
callable = utils.runScriptOnNodeAsDefaultUser(node, "computeserv", runScript);
SshClient ssh = utils.createSshClientOncePortIsListeningOnNode(node);
try {
ssh.connect();
callable.setConnection(ssh, logger);
execs.put(node, callable.call());
} finally {
if (ssh != null)
ssh.disconnect();
}
ExecResponse response = utils.runScriptOnNode(node, Statements.exec(Utils.toStringAndClose(runScript
.getInput())), options);
if (response != null)
execs.put(node, response);
} catch (Exception e) {
badNodes.put(node, e);
}
return null;
}
}));
}
Map<?, Exception> exceptions = awaitCompletion(responses, executor, null, logger, "starting nodes");
Map<?, Exception> exceptions = awaitCompletion(responses, executor, null, logger, "running script on nodes");
if (exceptions.size() > 0 || badNodes.size() > 0) {
throw new RunScriptOnNodesException(runScript, options, execs, exceptions, badNodes);
}
return execs;
}
private Iterable<NodeMetadata> verifyParametersAndListNodes(Predicate<NodeMetadata> filter, Payload runScript,
final RunScriptOptions options) {
checkNotNull(filter, "Filter must be provided");
checkNotNull(runScript, "The script (represented by bytes array - use \"script\".getBytes() must be provided");
checkNotNull(options, "options");
Iterable<? extends NodeMetadata> nodes = Iterables.filter(detailsOnAllNodes(), filter);
// TODO parallel
return Iterables.transform(nodes, new Function<NodeMetadata, NodeMetadata>() {
private Iterable<? extends NodeMetadata> filterNodesWhoCanRunScripts(Iterable<? extends NodeMetadata> nodes,
final Map<NodeMetadata, Exception> badNodes, final @Nullable Credentials overridingCredentials) {
nodes = Iterables.filter(Iterables.transform(nodes, new Function<NodeMetadata, NodeMetadata>() {
@Override
public NodeMetadata apply(NodeMetadata node) {
try {
checkArgument(node.getPublicAddresses().size() > 0, "no public ip addresses on node: " + node);
if (options.getOverrideCredentials() != null) {
// override the credentials with provided to this
// method
node = installNewCredentials(node, options.getOverrideCredentials());
if (overridingCredentials != null) {
node = installNewCredentials(node, overridingCredentials);
} else {
// don't override
checkNotNull(node.getCredentials(), "If the default credentials need to be used, they can't be null");
checkNotNull(node.getCredentials().identity, "Account name for ssh authentication must be "
+ "specified. Try passing RunScriptOptions with new credentials");
@ -423,8 +413,13 @@ public class BaseComputeService implements ComputeService {
+ "specified. Try passing RunScriptOptions with new credentials");
}
return node;
} catch (Exception e) {
badNodes.put(node, e);
return null;
}
});
}
}), Predicates.notNull());
return nodes;
}
private Set<? extends NodeMetadata> detailsOnAllNodes() {

View File

@ -41,7 +41,7 @@ public class ComputeServiceContextImpl<S, A> implements ComputeServiceContext {
private final RestContext<S, A> providerSpecificContext;
private final Utils utils;
@SuppressWarnings({ "unchecked", "rawtypes" })
@SuppressWarnings({ "unchecked" })
@Inject
public ComputeServiceContextImpl(ComputeService computeService, Utils utils,
@Nullable LoadBalancerService loadBalancerService, RestContext providerSpecificContext) {

View File

@ -19,6 +19,7 @@
package org.jclouds.compute.options;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import org.jclouds.domain.Credentials;
@ -74,10 +75,37 @@ public class RunScriptOptions {
throw new IllegalArgumentException("overridingCredentials is immutable");
}
@Override
public String getTaskName() {
return delegate.getTaskName();
}
private Credentials overridingCredentials;
private boolean runAsRoot = true;
@Override
public RunScriptOptions nameTask(String name) {
throw new IllegalArgumentException("taskName is immutable");
}
@Override
public RunScriptOptions blockOnPort(int port, int seconds) {
throw new IllegalArgumentException("port, seconds are immutable");
}
@Override
public int getPort() {
return delegate.getPort();
}
@Override
public int getSeconds() {
return delegate.getSeconds();
}
}
protected int port = -1;
protected int seconds = -1;
protected String taskName;
protected Credentials overridingCredentials;
protected boolean runAsRoot = true;
public RunScriptOptions withOverridingCredentials(Credentials overridingCredentials) {
checkNotNull(overridingCredentials, "overridingCredentials");
@ -86,11 +114,43 @@ public class RunScriptOptions {
this.overridingCredentials = overridingCredentials;
return this;
}
/**
* @return What to call the task relating to this script; default {@code
* jclouds-script-timestamp} where timestamp is millis since epoch
*
*/
public RunScriptOptions nameTask(String name) {
this.taskName = name;
return this;
}
public RunScriptOptions runAsRoot(boolean runAsRoot) {
this.runAsRoot = runAsRoot;
return this;
}
/**
* When the node is started, wait until the following port is active
*/
public RunScriptOptions blockOnPort(int port, int seconds) {
checkArgument(port > 0 && port < 65536, "port must be a positive integer < 65535");
checkArgument(seconds > 0, "seconds must be a positive integer");
this.port = port;
this.seconds = seconds;
return this;
}
public String getTaskName() {
return taskName;
}
public int getPort() {
return port;
}
public int getSeconds() {
return seconds;
}
/**
* Whether to override the credentials with ones supplied in call to
@ -113,6 +173,11 @@ public class RunScriptOptions {
public static class Builder {
public static RunScriptOptions nameTask(String name) {
RunScriptOptions options = new RunScriptOptions();
return options.nameTask(name);
}
public static RunScriptOptions overrideCredentialsWith(Credentials credentials) {
RunScriptOptions options = new RunScriptOptions();
return options.withOverridingCredentials(credentials);
@ -123,12 +188,19 @@ public class RunScriptOptions {
return options.runAsRoot(value);
}
/**
* @see RunScriptOptions#blockOnPort
*/
public static RunScriptOptions blockOnPort(int port, int seconds) {
RunScriptOptions options = new RunScriptOptions();
return options.blockOnPort(port, seconds);
}
}
@Override
public String toString() {
return "RunScriptOptions [overridingCredentials=" + (overridingCredentials != null)
+ ", runAsRoot=" + runAsRoot + "]";
return "[overridingCredentials=" + (overridingCredentials != null) + ", port:seconds=" + port + ":" + seconds + ", runAsRoot=" + runAsRoot + "]";
}
}

View File

@ -21,18 +21,17 @@ package org.jclouds.compute.options;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.io.Payloads.newByteArrayPayload;
import static org.jclouds.io.Payloads.newStringPayload;
import java.io.IOException;
import java.util.Arrays;
import javax.annotation.Nullable;
import org.jclouds.compute.domain.OperatingSystem;
import org.jclouds.compute.predicates.OperatingSystemPredicates;
import org.jclouds.domain.Credentials;
import org.jclouds.io.Payload;
import org.jclouds.scriptbuilder.domain.OsFamily;
import org.jclouds.scriptbuilder.domain.Statement;
import org.jclouds.scriptbuilder.domain.Statements;
import org.jclouds.util.Utils;
import com.google.common.base.Throwables;
/**
* Contains options supported in the {@code ComputeService#runNodesWithTag} operation. <h2>
@ -50,7 +49,7 @@ import org.jclouds.scriptbuilder.domain.Statement;
*
* @author Adrian Cole
*/
public class TemplateOptions {
public class TemplateOptions extends RunScriptOptions {
public static final TemplateOptions NONE = new ImmutableTemplateOptions(new TemplateOptions());
@ -81,41 +80,26 @@ public class TemplateOptions {
throw new IllegalArgumentException("blockUntilRunning is immutable");
}
@Override
public TemplateOptions blockOnPort(int port, int seconds) {
throw new IllegalArgumentException("port, seconds are immutable");
}
@Override
public int[] getInboundPorts() {
return delegate.getInboundPorts();
}
@Override
public int getPort() {
return delegate.getPort();
}
@Override
public Payload getPrivateKey() {
public String getPrivateKey() {
return delegate.getPrivateKey();
}
@Override
public Payload getPublicKey() {
public String getPublicKey() {
return delegate.getPublicKey();
}
@Override
public Payload getRunScript() {
public Statement getRunScript() {
return delegate.getRunScript();
}
@Override
public int getSeconds() {
return delegate.getSeconds();
}
@Override
public boolean shouldBlockUntilRunning() {
return delegate.shouldBlockUntilRunning();
@ -150,41 +134,29 @@ public class TemplateOptions {
protected int[] inboundPorts = new int[] { 22 };
protected Payload script;
protected Statement script;
protected Payload privateKey;
protected String privateKey;
protected Payload publicKey;
protected int port = -1;
protected int seconds = -1;
protected String publicKey;
protected boolean includeMetadata;
protected boolean blockUntilRunning = true;
public int getPort() {
return port;
}
public int getSeconds() {
return seconds;
}
public int[] getInboundPorts() {
return inboundPorts;
}
public Payload getRunScript() {
public Statement getRunScript() {
return script;
}
public Payload getPrivateKey() {
public String getPrivateKey() {
return privateKey;
}
public Payload getPublicKey() {
public String getPublicKey() {
return publicKey;
}
@ -200,28 +172,17 @@ public class TemplateOptions {
return clazz.cast(this);
}
/**
* When the node is started, wait until the following port is active
*/
public TemplateOptions blockOnPort(int port, int seconds) {
checkArgument(port > 0 && port < 65536, "port must be a positive integer < 65535");
checkArgument(seconds > 0, "seconds must be a positive integer");
this.port = port;
this.seconds = seconds;
return this;
}
/**
* This script will be executed as the root user upon system startup. This script gets a
* prologue, so no #!/bin/bash required, path set up, etc
* <p/>
* please use alternative that uses the {@link org.jclouds.io.Payload} object
* please use alternative that uses the {@link org.jclouds.scriptbuilder.domain.Statement} object
*
* @see org.jclouds.io.Payloads
*/
@Deprecated
public TemplateOptions runScript(byte[] script) {
return runScript(newByteArrayPayload(checkNotNull(script, "script")));
return runScript(Statements.exec(new String(checkNotNull(script, "script"))));
}
/**
@ -231,45 +192,45 @@ public class TemplateOptions {
* @see org.jclouds.io.Payloads
*/
public TemplateOptions runScript(Payload script) {
checkNotNull(script, "script");
this.script = script;
try {
return runScript(Statements.exec(Utils.toStringAndClose(checkNotNull(script, "script").getInput())));
} catch (IOException e) {
Throwables.propagate(e);
return this;
}
public TemplateOptions runScript(Statement script, @Nullable OperatingSystem os) {
return runScript(newStringPayload(checkNotNull(script, "script").render(
os == null || OperatingSystemPredicates.isUnix().apply(os) ? OsFamily.UNIX : OsFamily.WINDOWS)));
}
public TemplateOptions runScript(Statement script) {
return runScript(script, null);
this.script = checkNotNull(script, "script");
return this;
}
/**
* replaces the rsa ssh key used at login.
*/
public TemplateOptions installPrivateKey(String privateKey) {
checkArgument(checkNotNull(privateKey, "privateKey").startsWith("-----BEGIN RSA PRIVATE KEY-----"),
"key should start with -----BEGIN RSA PRIVATE KEY-----");
this.privateKey = privateKey;
return this;
}
/**
* replaces the rsa ssh key used at login.
* <p/>
* please use alternative that uses the {@link org.jclouds.io.Payload} object
* please use alternative that uses {@link java.lang.String}
*
* @see org.jclouds.io.Payloads
*/
@Deprecated
public TemplateOptions installPrivateKey(String privateKey) {
checkArgument(checkNotNull(privateKey, "privateKey").startsWith("-----BEGIN RSA PRIVATE KEY-----"),
"key should start with -----BEGIN RSA PRIVATE KEY-----");
Payload payload = newStringPayload(privateKey);
payload.getContentMetadata().setContentType("text/plain");
return installPrivateKey(payload);
}
/**
* replaces the rsa ssh key used at login.
*
* @see org.jclouds.io.Payloads
*/
public TemplateOptions installPrivateKey(Payload privateKey) {
this.privateKey = checkNotNull(privateKey, "privateKey");
try {
return installPrivateKey(Utils.toStringAndClose(checkNotNull(privateKey, "privateKey").getInput()));
} catch (IOException e) {
Throwables.propagate(e);
return this;
}
}
public TemplateOptions dontAuthorizePublicKey() {
this.publicKey = null;
@ -277,42 +238,30 @@ public class TemplateOptions {
}
/**
* if true, return when node(s) are NODE_RUNNING, if false, return as soon as the server is
* provisioned.
* <p/>
* default is true
* authorize an rsa ssh key.
*/
public TemplateOptions blockUntilRunning(boolean blockUntilRunning) {
this.blockUntilRunning = blockUntilRunning;
if (!blockUntilRunning)
port = seconds = -1;
public TemplateOptions authorizePublicKey(String publicKey) {
checkArgument(checkNotNull(publicKey, "publicKey").startsWith("ssh-rsa"), "key should start with ssh-rsa");
this.publicKey = publicKey;
return this;
}
/**
* authorize an rsa ssh key.
* <p/>
* please use alternative that uses the {@link org.jclouds.io.Payload} object
* please use alternative that uses {@link java.lang.String}
*
* @see org.jclouds.io.Payloads
*/
@Deprecated
public TemplateOptions authorizePublicKey(String publicKey) {
checkArgument(checkNotNull(publicKey, "publicKey").startsWith("ssh-rsa"), "key should start with ssh-rsa");
Payload payload = newStringPayload(publicKey);
payload.getContentMetadata().setContentType("text/plain");
return authorizePublicKey(payload);
}
/**
* authorize an rsa ssh key.
*
* @see org.jclouds.io.Payloads
*/
public TemplateOptions authorizePublicKey(Payload publicKey) {
this.publicKey = checkNotNull(publicKey, "publicKey");
try {
return authorizePublicKey(Utils.toStringAndClose(checkNotNull(publicKey, "publicKey").getInput()));
} catch (IOException e) {
Throwables.propagate(e);
return this;
}
}
/**
* Opens the set of ports to public access.
@ -329,7 +278,30 @@ public class TemplateOptions {
return this;
}
public static class Builder {
public static class Builder extends org.jclouds.compute.options.RunScriptOptions.Builder {
public static TemplateOptions nameTask(String name) {
TemplateOptions options = new TemplateOptions();
return options.nameTask(name);
}
public static TemplateOptions overrideCredentialsWith(Credentials credentials) {
TemplateOptions options = new TemplateOptions();
return options.withOverridingCredentials(credentials);
}
public static TemplateOptions runAsRoot(boolean value) {
TemplateOptions options = new TemplateOptions();
return options.runAsRoot(value);
}
/**
* @see TemplateOptions#blockOnPort
*/
public static TemplateOptions blockOnPort(int port, int seconds) {
TemplateOptions options = new TemplateOptions();
return options.blockOnPort(port, seconds);
}
/**
* @see TemplateOptions#inboundPorts
@ -339,14 +311,6 @@ public class TemplateOptions {
return options.inboundPorts(ports);
}
/**
* @see TemplateOptions#port
*/
public static TemplateOptions blockOnPort(int port, int seconds) {
TemplateOptions options = new TemplateOptions();
return options.blockOnPort(port, seconds);
}
/**
* @see TemplateOptions#blockUntilRunning
*/
@ -436,10 +400,16 @@ public class TemplateOptions {
@Override
public String toString() {
return "TemplateOptions [inboundPorts=" + Arrays.toString(inboundPorts) + ", privateKey=" + (privateKey != null)
+ ", publicKey=" + (publicKey != null) + ", runScript=" + (script != null) + ", blockUntilRunning="
+ blockUntilRunning + ", port:seconds=" + port + ":" + seconds + ", metadata/details: "
+ includeMetadata + "]";
return "[inboundPorts=" + Arrays.toString(inboundPorts) + ", privateKey=" + (privateKey != null) + ", publicKey="
+ (publicKey != null) + ", runScript=" + (script != null) + ", blockUntilRunning=" + blockUntilRunning
+ ", port:seconds=" + port + ":" + seconds + ", metadata/details: " + includeMetadata + "]";
}
public TemplateOptions blockUntilRunning(boolean blockUntilRunning) {
this.blockUntilRunning = blockUntilRunning;
if (!blockUntilRunning)
port = seconds = -1;
return this;
}
@Override
@ -493,4 +463,24 @@ public class TemplateOptions {
return false;
return true;
}
@Override
public TemplateOptions blockOnPort(int port, int seconds) {
return TemplateOptions.class.cast(super.blockOnPort(port, seconds));
}
@Override
public TemplateOptions nameTask(String name) {
return TemplateOptions.class.cast(super.nameTask(name));
}
@Override
public TemplateOptions runAsRoot(boolean runAsRoot) {
return TemplateOptions.class.cast(super.runAsRoot(runAsRoot));
}
@Override
public TemplateOptions withOverridingCredentials(Credentials overridingCredentials) {
return TemplateOptions.class.cast(super.withOverridingCredentials(overridingCredentials));
}
}

View File

@ -33,7 +33,7 @@ import com.google.inject.Module;
*
* @author Adrian Cole
*/
@SuppressWarnings("rawtypes")
@SuppressWarnings("unchecked")
public class StubComputeServiceContextBuilder extends ComputeServiceContextBuilder<ConcurrentMap, ConcurrentMap> {
public StubComputeServiceContextBuilder(Properties props) {

View File

@ -25,7 +25,7 @@ import org.jclouds.http.RequiresHttp;
import org.jclouds.rest.ConfiguresRestClient;
import org.jclouds.rest.config.RestClientModule;
@SuppressWarnings("rawtypes")
@SuppressWarnings("unchecked")
@ConfiguresRestClient
@RequiresHttp
public class StubComputeServiceClientModule extends RestClientModule<ConcurrentMap, ConcurrentMap> {

View File

@ -158,7 +158,7 @@ public class StubComputeServiceContextModule extends BaseComputeServiceContextMo
}
@SuppressWarnings( { "rawtypes" })
@SuppressWarnings("unchecked")
@Override
protected void configure() {
bind(new TypeLiteral<ComputeServiceContext>() {

View File

@ -87,6 +87,20 @@ public class ComputeServiceUtils {
return extractTargzIntoDirectory(new HttpRequest("GET", targz), directory);
}
/**
* build a shell script that invokes the contents of the http request in bash.
*
* @return a shell script that will invoke the http request
*/
public static Statement extractZipIntoDirectory(HttpRequest zip, String directory) {
return Statements
.extractZipIntoDirectory(zip.getMethod(), zip.getEndpoint(), zip.getHeaders(), directory);
}
public static Statement extractZipIntoDirectory(URI zip, String directory) {
return extractZipIntoDirectory(new HttpRequest("GET", zip), directory);
}
public static String parseTagFromName(String from) {
Matcher matcher = DELIMETED_BY_HYPHEN_ENDING_IN_HYPHEN_HEX.matcher(from);
return matcher.find() ? matcher.group(1) : "NOTAG-" + from;

View File

@ -40,20 +40,23 @@ import javax.inject.Named;
import javax.inject.Singleton;
import org.jclouds.Constants;
import org.jclouds.compute.callables.AuthorizeRSAPublicKey;
import org.jclouds.compute.callables.InstallRSAPrivateKey;
import org.jclouds.compute.callables.RunScriptOnNode;
import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.options.RunScriptOptions;
import org.jclouds.compute.options.TemplateOptions;
import org.jclouds.compute.predicates.ScriptStatusReturnsZero.CommandUsingClient;
import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.compute.reference.ComputeServiceConstants.Timeouts;
import org.jclouds.compute.strategy.GetNodeMetadataStrategy;
import org.jclouds.compute.util.ComputeServiceUtils.SshCallable;
import org.jclouds.io.Payload;
import org.jclouds.logging.Logger;
import org.jclouds.net.IPSocket;
import org.jclouds.predicates.RetryablePredicate;
import org.jclouds.scriptbuilder.domain.AuthorizeRSAPublicKey;
import org.jclouds.scriptbuilder.domain.InstallRSAPrivateKey;
import org.jclouds.scriptbuilder.domain.Statement;
import org.jclouds.scriptbuilder.domain.StatementList;
import org.jclouds.ssh.ExecResponse;
import org.jclouds.ssh.SshClient;
import com.google.common.base.Predicate;
@ -136,35 +139,49 @@ public class ComputeUtils {
throw new IllegalStateException(String.format(
"node didn't achieve the state running on node %s within %d seconds, final state: %s", node.getId(),
timeouts.nodeRunning / 1000, node.getState()));
List<SshCallable<?>> callables = Lists.newArrayList();
if (options.getRunScript() != null) {
callables.add(runScriptOnNode(node, "runscript", options.getRunScript()));
}
if (options.getPublicKey() != null) {
callables.add(authorizeKeyOnNode(node, options.getPublicKey()));
List<Statement> bootstrap = Lists.newArrayList();
if (options.getRunScript() != null)
bootstrap.add(options.getRunScript());
if (options.getPublicKey() != null)
bootstrap.add(new AuthorizeRSAPublicKey(options.getPublicKey()));
if (options.getPrivateKey() != null)
bootstrap.add(new InstallRSAPrivateKey(options.getPrivateKey()));
if (bootstrap.size() >0)
runScriptOnNode(node, new StatementList(bootstrap), options);
return node;
}
// changing the key "MUST" come last or else the other commands may
// fail.
if (callables.size() > 0 || options.getPrivateKey() != null) {
runCallablesOnNode(node, callables, options.getPrivateKey() != null ? installKeyOnNode(node, options
.getPrivateKey()) : null);
public void checkNodeHasPublicIps(NodeMetadata node) {
checkState(node.getPublicAddresses().size() > 0, "node does not have IP addresses configured: " + node);
}
public ExecResponse runScriptOnNode(NodeMetadata node, Statement runScript, RunScriptOptions options)
{
RunScriptOnNode callable;
String taskName = options.getTaskName();
ExecResponse response;
if (options.isRunAsRoot()) {
callable = runScriptOnNode(node, taskName, runScript);
} else
callable = runScriptOnNodeAsDefaultUser(node, taskName, runScript);
SshClient ssh = createSshClientOncePortIsListeningOnNode(node);
try {
ssh.connect();
callable.setConnection(ssh, logger);
response = callable.call();
} finally {
if (ssh != null)
ssh.disconnect();
}
if (options.getPort() > 0) {
checkNodeHasPublicIps(node);
blockUntilPortIsListeningOnPublicIp(options.getPort(), options.getSeconds(), Iterables.get(node
.getPublicAddresses(), 0));
}
return node;
return response;
}
private void checkNodeHasPublicIps(NodeMetadata node) {
checkState(node.getPublicAddresses().size() > 0, "node does not have IP addresses configured: " + node);
}
private void blockUntilPortIsListeningOnPublicIp(int port, int seconds, String inetAddress) {
public void blockUntilPortIsListeningOnPublicIp(int port, int seconds, String inetAddress) {
logger.debug(">> blocking on port %s:%d for %d seconds", inetAddress, port, seconds);
RetryablePredicate<IPSocket> tester = new RetryablePredicate<IPSocket>(socketTester, seconds, 1, TimeUnit.SECONDS);
IPSocket socket = new IPSocket(inetAddress, port);
@ -175,19 +192,11 @@ public class ComputeUtils {
logger.warn("<< port %s:%d didn't open after %d seconds", inetAddress, port, seconds);
}
public InstallRSAPrivateKey installKeyOnNode(NodeMetadata node, Payload privateKey) {
return new InstallRSAPrivateKey(node, privateKey);
}
public AuthorizeRSAPublicKey authorizeKeyOnNode(NodeMetadata node, Payload publicKey) {
return new AuthorizeRSAPublicKey(node, publicKey);
}
public RunScriptOnNode runScriptOnNode(NodeMetadata node, String scriptName, Payload script) {
public RunScriptOnNode runScriptOnNode(NodeMetadata node, String scriptName, Statement script) {
return new RunScriptOnNode(runScriptNotRunning, node, scriptName, script);
}
public RunScriptOnNode runScriptOnNodeAsDefaultUser(NodeMetadata node, String scriptName, Payload script) {
public RunScriptOnNode runScriptOnNodeAsDefaultUser(NodeMetadata node, String scriptName, Statement script) {
return new RunScriptOnNode(runScriptNotRunning, node, scriptName, script, false);
}

View File

@ -320,9 +320,8 @@ public abstract class BaseComputeServiceLiveTest {
private void refreshTemplate() {
template = buildTemplate(client.templateBuilder());
template.getOptions().installPrivateKey(newStringPayload(keyPair.get("private"))).authorizePublicKey(
newStringPayload(keyPair.get("public"))).runScript(
newStringPayload(buildScript(template.getImage().getOperatingSystem())));
template.getOptions().installPrivateKey(keyPair.get("private")).authorizePublicKey(keyPair.get("public"))
.runScript(newStringPayload(buildScript(template.getImage().getOperatingSystem())));
}
protected void checkImageIdMatchesTemplate(NodeMetadata node) {
@ -362,7 +361,7 @@ public abstract class BaseComputeServiceLiveTest {
Credentials creds) throws RunScriptOnNodesException {
try {
return client.runScriptOnNodesMatching(runningWithTag(tag), newStringPayload(buildScript(os)),
overrideCredentialsWith(creds));
overrideCredentialsWith(creds).nameTask("runScriptWithCreds"));
} catch (SshException e) {
throw e;
}
@ -606,7 +605,7 @@ public abstract class BaseComputeServiceLiveTest {
ExecResponse hello = ssh.exec("echo hello");
assertEquals(hello.getOutput().trim(), "hello");
ExecResponse exec = ssh.exec("java -version");
assert exec.getError().indexOf("OpenJDK") != -1 : exec;
assert exec.getError().indexOf("OpenJDK") != -1 || exec.getOutput().indexOf("OpenJDK") != -1 : exec;
} finally {
if (ssh != null)
ssh.disconnect();

View File

@ -19,7 +19,6 @@
package org.jclouds.compute;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.easymock.EasyMock.aryEq;
import static org.easymock.EasyMock.eq;
import static org.easymock.EasyMock.expect;
@ -48,9 +47,6 @@ import org.jclouds.net.IPSocket;
import org.jclouds.predicates.RetryablePredicate;
import org.jclouds.predicates.SocketOpen;
import org.jclouds.rest.RestContext;
import org.jclouds.scriptbuilder.InitBuilder;
import org.jclouds.scriptbuilder.domain.Statement;
import org.jclouds.scriptbuilder.domain.Statements;
import org.jclouds.ssh.ExecResponse;
import org.jclouds.ssh.SshClient;
import org.jclouds.ssh.SshException;
@ -58,11 +54,8 @@ import org.jclouds.util.Utils;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.google.common.base.Splitter;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.inject.AbstractModule;
import com.google.inject.Module;
@ -127,7 +120,12 @@ public class StubComputeServiceIntegrationTest extends BaseComputeServiceLiveTes
.atLeastOnce();
client1.connect();
runScript(client1, "computeserv", 1);
try {
runScript(client1, "runScriptWithCreds", Utils.toStringAndClose(StubComputeServiceIntegrationTest.class
.getResourceAsStream("/runscript.sh")), 1);
} catch (IOException e) {
Throwables.propagate(e);
}
client1.disconnect();
expect(factory.create(new IPSocket("144.175.1.2", 22), "root", "password2")).andReturn(client2)
@ -137,9 +135,9 @@ public class StubComputeServiceIntegrationTest extends BaseComputeServiceLiveTes
expect(factory.create(new IPSocket("144.175.1.4", 22), "root", "password4")).andReturn(client4)
.atLeastOnce();
runScriptAndInstallSsh(client2, "runscript", 2);
runScriptAndInstallSsh(client3, "runscript", 3);
runScriptAndInstallSsh(client4, "runscript", 4);
runScriptAndInstallSsh(client2, "bootstrap", 2);
runScriptAndInstallSsh(client3, "bootstrap", 3);
runScriptAndInstallSsh(client4, "bootstrap", 4);
expect(
factory.create(eq(new IPSocket("144.175.1.1", 22)), eq("root"), aryEq(keyPair.get("private")
@ -171,24 +169,19 @@ public class StubComputeServiceIntegrationTest extends BaseComputeServiceLiveTes
private void runScriptAndInstallSsh(SshClient client, String scriptName, int nodeId) {
client.connect();
runScript(client, scriptName, nodeId);
expect(client.exec("mkdir .ssh")).andReturn(EXEC_GOOD);
expect(client.exec("cat .ssh/id_rsa.pub >> .ssh/authorized_keys")).andReturn(EXEC_GOOD);
expect(client.exec("chmod 600 .ssh/authorized_keys")).andReturn(EXEC_GOOD);
client.put(eq(".ssh/id_rsa.pub"), payloadEq(keyPair.get("public")));
expect(client.exec("mkdir .ssh")).andReturn(EXEC_GOOD);
client.put(eq(".ssh/id_rsa"), payloadEq(keyPair.get("private")));
expect(client.exec("chmod 600 .ssh/id_rsa")).andReturn(EXEC_GOOD);
try {
runScript(client, scriptName, Utils.toStringAndClose(StubComputeServiceIntegrationTest.class
.getResourceAsStream("/initscript_with_keys.sh")), nodeId);
} catch (IOException e) {
Throwables.propagate(e);
}
client.disconnect();
}
private void runScript(SshClient client, String scriptName, int nodeId) {
client.put(eq("" + scriptName + ""), payloadEq(initScript(scriptName,
BaseComputeServiceLiveTest.APT_RUN_SCRIPT)));
private void runScript(SshClient client, String scriptName, String script, int nodeId) {
client.put(eq("" + scriptName + ""), payloadEq(script));
expect(client.exec("chmod 755 " + scriptName + "")).andReturn(EXEC_GOOD);
expect(client.getUsername()).andReturn("root").atLeastOnce();
expect(client.getHostAddress()).andReturn(nodeId + "").atLeastOnce();
@ -223,13 +216,6 @@ public class StubComputeServiceIntegrationTest extends BaseComputeServiceLiveTes
// TODO: this fails so we override it.
}
public static String initScript(String scriptName, String script) {
return new InitBuilder(scriptName, "/tmp/" + scriptName, "/tmp/" + scriptName,
ImmutableMap.<String, String> of(), ImmutableList.<Statement> of(Statements.interpret(Iterables.toArray(
Splitter.on("\n").split(new String(checkNotNull(script, "script"))), String.class))))
.build(org.jclouds.scriptbuilder.domain.OsFamily.UNIX);
}
public static Payload payloadEq(String value) {
reportMatcher(new PayloadEquals(value));
return null;
@ -257,10 +243,7 @@ public class StubComputeServiceIntegrationTest extends BaseComputeServiceLiveTes
}
try {
String real = Utils.toStringAndClose(((Payload) actual).getInput());
if (!expected.equals(real)) {
System.err.println(real);
return false;
}
assertEquals(real, expected);
return true;
} catch (IOException e) {
Throwables.propagate(e);

View File

@ -28,7 +28,6 @@ import static org.testng.Assert.assertEquals;
import java.io.IOException;
import org.jclouds.util.Utils;
import org.testng.annotations.Test;
/**
@ -37,19 +36,17 @@ import org.testng.annotations.Test;
* @author Adrian Cole
*/
public class TemplateOptionsTest {
@SuppressWarnings("deprecation")
@Test(expectedExceptions = IllegalArgumentException.class)
public void testinstallPrivateKeyBadFormat() {
TemplateOptions options = new TemplateOptions();
options.installPrivateKey("whompy");
}
@SuppressWarnings("deprecation")
@Test
public void testinstallPrivateKey() throws IOException {
TemplateOptions options = new TemplateOptions();
options.installPrivateKey("-----BEGIN RSA PRIVATE KEY-----");
assertEquals(Utils.toStringAndClose(options.getPrivateKey().getInput()), "-----BEGIN RSA PRIVATE KEY-----");
assertEquals(options.getPrivateKey(), "-----BEGIN RSA PRIVATE KEY-----");
}
@Test
@ -62,7 +59,7 @@ public class TemplateOptionsTest {
@Test
public void testinstallPrivateKeyStatic() throws IOException {
TemplateOptions options = installPrivateKey("-----BEGIN RSA PRIVATE KEY-----");
assertEquals(Utils.toStringAndClose(options.getPrivateKey().getInput()), "-----BEGIN RSA PRIVATE KEY-----");
assertEquals(options.getPrivateKey(), "-----BEGIN RSA PRIVATE KEY-----");
}
@SuppressWarnings("deprecation")
@ -71,7 +68,6 @@ public class TemplateOptionsTest {
installPrivateKey((String) null);
}
@SuppressWarnings("deprecation")
@Test(expectedExceptions = IllegalArgumentException.class)
public void testauthorizePublicKeyBadFormat() {
TemplateOptions options = new TemplateOptions();
@ -79,11 +75,10 @@ public class TemplateOptionsTest {
}
@Test
@SuppressWarnings("deprecation")
public void testauthorizePublicKey() throws IOException {
TemplateOptions options = new TemplateOptions();
options.authorizePublicKey("ssh-rsa");
assertEquals(Utils.toStringAndClose(options.getPublicKey().getInput()), "ssh-rsa");
assertEquals(options.getPublicKey(), "ssh-rsa");
}
@Test
@ -96,7 +91,7 @@ public class TemplateOptionsTest {
@Test
public void testauthorizePublicKeyStatic() throws IOException {
TemplateOptions options = authorizePublicKey("ssh-rsa");
assertEquals(Utils.toStringAndClose(options.getPublicKey().getInput()), "ssh-rsa");
assertEquals(options.getPublicKey(), "ssh-rsa");
}
@SuppressWarnings("deprecation")

View File

@ -0,0 +1,136 @@
#!/bin/bash
set +u
shopt -s xpg_echo
shopt -s expand_aliases
unset PATH JAVA_HOME LD_LIBRARY_PATH
function abort {
echo "aborting: $@" 1>&2
exit 1
}
function default {
export INSTANCE_NAME="bootstrap"
export INSTANCE_HOME="/tmp/bootstrap"
export LOG_DIR="/tmp/bootstrap"
return 0
}
function bootstrap {
return 0
}
function findPid {
unset FOUND_PID;
[ $# -eq 1 ] || {
abort "findPid requires a parameter of pattern to match"
return 1
}
local PATTERN="$1"; shift
local _FOUND=`ps auxwww|grep "$PATTERN"|grep -v " $0"|grep -v grep|awk '{print $2}'`
[ -n "$_FOUND" ] && {
export FOUND_PID=$_FOUND
return 0
} || {
return 1
}
}
function forget {
unset FOUND_PID;
[ $# -eq 3 ] || {
abort "forget requires parameters INSTANCE_NAME SCRIPT LOG_DIR"
return 1
}
local INSTANCE_NAME="$1"; shift
local SCRIPT="$1"; shift
local LOG_DIR="$1"; shift
mkdir -p $LOG_DIR
findPid $INSTANCE_NAME
[ -n "$FOUND_PID" ] && {
echo $INSTANCE_NAME already running pid [$FOUND_PID]
} || {
nohup $SCRIPT >$LOG_DIR/stdout.log 2>$LOG_DIR/stderr.log &
sleep 1
findPid $INSTANCE_NAME
[ -n "$FOUND_PID" ] || abort "$INSTANCE_NAME did not start"
}
return 0
}
export PATH=/usr/ucb/bin:/bin:/sbin:/usr/bin:/usr/sbin
case $1 in
init)
default || exit 1
bootstrap || exit 1
mkdir -p $INSTANCE_HOME
# create runscript header
cat > $INSTANCE_HOME/bootstrap.sh <<END_OF_SCRIPT
#!/bin/bash
set +u
shopt -s xpg_echo
shopt -s expand_aliases
PROMPT_COMMAND='echo -ne "\033]0;bootstrap\007"'
export PATH=/usr/ucb/bin:/bin:/sbin:/usr/bin:/usr/sbin
export INSTANCE_NAME='bootstrap'
export INSTANCE_NAME='$INSTANCE_NAME'
export INSTANCE_HOME='$INSTANCE_HOME'
export LOG_DIR='$LOG_DIR'
END_OF_SCRIPT
# add desired commands from the user
cat >> $INSTANCE_HOME/bootstrap.sh <<'END_OF_SCRIPT'
cd $INSTANCE_HOME
echo nameserver 208.67.222.222 >> /etc/resolv.conf
cp /etc/apt/sources.list /etc/apt/sources.list.old
sed 's~us.archive.ubuntu.com~mirror.anl.gov/pub~g' /etc/apt/sources.list.old >/etc/apt/sources.list
apt-get update
apt-get install -f -y --force-yes openjdk-6-jdk
mkdir -p .ssh
cat >> .ssh/authorized_keys <<'END_OF_FILE'
ssh-rsa
END_OF_FILE
chmod 600 .ssh/authorized_keys
mkdir -p .ssh
rm .ssh/id_rsa
cat >> .ssh/id_rsa <<'END_OF_FILE'
-----BEGIN RSA PRIVATE KEY-----
END_OF_FILE
chmod 600 .ssh/id_rsa
END_OF_SCRIPT
# add runscript footer
cat >> $INSTANCE_HOME/bootstrap.sh <<'END_OF_SCRIPT'
exit 0
END_OF_SCRIPT
chmod u+x $INSTANCE_HOME/bootstrap.sh
;;
status)
default || exit 1
findPid $INSTANCE_NAME || exit 1
echo [$FOUND_PID]
;;
stop)
default || exit 1
findPid $INSTANCE_NAME || exit 1
[ -n "$FOUND_PID" ] && {
echo stopping $FOUND_PID
kill -9 $FOUND_PID
}
;;
start)
default || exit 1
forget $INSTANCE_NAME $INSTANCE_HOME/$INSTANCE_NAME.sh $LOG_DIR || exit 1
;;
tail)
default || exit 1
tail $LOG_DIR/stdout.log
;;
tailerr)
default || exit 1
tail $LOG_DIR/stderr.log
;;
run)
default || exit 1
$INSTANCE_HOME/$INSTANCE_NAME.sh
;;
esac
exit 0

View File

@ -0,0 +1,125 @@
#!/bin/bash
set +u
shopt -s xpg_echo
shopt -s expand_aliases
unset PATH JAVA_HOME LD_LIBRARY_PATH
function abort {
echo "aborting: $@" 1>&2
exit 1
}
function default {
export INSTANCE_NAME="runScriptWithCreds"
export INSTANCE_HOME="/tmp/runScriptWithCreds"
export LOG_DIR="/tmp/runScriptWithCreds"
return 0
}
function runScriptWithCreds {
return 0
}
function findPid {
unset FOUND_PID;
[ $# -eq 1 ] || {
abort "findPid requires a parameter of pattern to match"
return 1
}
local PATTERN="$1"; shift
local _FOUND=`ps auxwww|grep "$PATTERN"|grep -v " $0"|grep -v grep|awk '{print $2}'`
[ -n "$_FOUND" ] && {
export FOUND_PID=$_FOUND
return 0
} || {
return 1
}
}
function forget {
unset FOUND_PID;
[ $# -eq 3 ] || {
abort "forget requires parameters INSTANCE_NAME SCRIPT LOG_DIR"
return 1
}
local INSTANCE_NAME="$1"; shift
local SCRIPT="$1"; shift
local LOG_DIR="$1"; shift
mkdir -p $LOG_DIR
findPid $INSTANCE_NAME
[ -n "$FOUND_PID" ] && {
echo $INSTANCE_NAME already running pid [$FOUND_PID]
} || {
nohup $SCRIPT >$LOG_DIR/stdout.log 2>$LOG_DIR/stderr.log &
sleep 1
findPid $INSTANCE_NAME
[ -n "$FOUND_PID" ] || abort "$INSTANCE_NAME did not start"
}
return 0
}
export PATH=/usr/ucb/bin:/bin:/sbin:/usr/bin:/usr/sbin
case $1 in
init)
default || exit 1
runScriptWithCreds || exit 1
mkdir -p $INSTANCE_HOME
# create runscript header
cat > $INSTANCE_HOME/runScriptWithCreds.sh <<END_OF_SCRIPT
#!/bin/bash
set +u
shopt -s xpg_echo
shopt -s expand_aliases
PROMPT_COMMAND='echo -ne "\033]0;runScriptWithCreds\007"'
export PATH=/usr/ucb/bin:/bin:/sbin:/usr/bin:/usr/sbin
export INSTANCE_NAME='runScriptWithCreds'
export INSTANCE_NAME='$INSTANCE_NAME'
export INSTANCE_HOME='$INSTANCE_HOME'
export LOG_DIR='$LOG_DIR'
END_OF_SCRIPT
# add desired commands from the user
cat >> $INSTANCE_HOME/runScriptWithCreds.sh <<'END_OF_SCRIPT'
cd $INSTANCE_HOME
echo nameserver 208.67.222.222 >> /etc/resolv.conf
cp /etc/apt/sources.list /etc/apt/sources.list.old
sed 's~us.archive.ubuntu.com~mirror.anl.gov/pub~g' /etc/apt/sources.list.old >/etc/apt/sources.list
apt-get update
apt-get install -f -y --force-yes openjdk-6-jdk
END_OF_SCRIPT
# add runscript footer
cat >> $INSTANCE_HOME/runScriptWithCreds.sh <<'END_OF_SCRIPT'
exit 0
END_OF_SCRIPT
chmod u+x $INSTANCE_HOME/runScriptWithCreds.sh
;;
status)
default || exit 1
findPid $INSTANCE_NAME || exit 1
echo [$FOUND_PID]
;;
stop)
default || exit 1
findPid $INSTANCE_NAME || exit 1
[ -n "$FOUND_PID" ] && {
echo stopping $FOUND_PID
kill -9 $FOUND_PID
}
;;
start)
default || exit 1
forget $INSTANCE_NAME $INSTANCE_HOME/$INSTANCE_NAME.sh $LOG_DIR || exit 1
;;
tail)
default || exit 1
tail $LOG_DIR/stdout.log
;;
tailerr)
default || exit 1
tail $LOG_DIR/stderr.log
;;
run)
default || exit 1
$INSTANCE_HOME/$INSTANCE_NAME.sh
;;
esac
exit 0

View File

@ -32,6 +32,7 @@ import org.jclouds.scriptbuilder.util.Utils;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
@ -41,7 +42,7 @@ import com.google.common.collect.Maps;
*
* @author Adrian Cole
*/
public class ScriptBuilder {
public class ScriptBuilder implements Statement {
@VisibleForTesting
List<Statement> statements = Lists.newArrayList();
@ -69,8 +70,7 @@ public class ScriptBuilder {
* Exports a variable inside the script
*/
public ScriptBuilder addEnvironmentVariableScope(String scopeName, Map<String, String> variables) {
variableScopes
.put(checkNotNull(scopeName, "scopeName"), checkNotNull(variables, "variables"));
variableScopes.put(checkNotNull(scopeName, "scopeName"), checkNotNull(variables, "variables"));
return this;
}
@ -85,19 +85,21 @@ public class ScriptBuilder {
* @param osFamily
* whether to write a cmd or bash script.
*/
public String build(final OsFamily osFamily) {
@Override
public String render(OsFamily osFamily) {
Map<String, String> functions = Maps.newLinkedHashMap();
functions.put("abort", Utils.writeFunctionFromResource("abort", osFamily));
for (Entry<String, Map<String, String>> entry : variableScopes.entrySet()) {
functions.put(entry.getKey(), Utils.writeFunction(entry.getKey(), Utils
.writeVariableExporters(entry.getValue())));
functions.put(entry.getKey(), Utils.writeFunction(entry.getKey(), Utils.writeVariableExporters(entry
.getValue())));
}
final Map<String, String> tokenValueMap = ShellToken.tokenValueMap(osFamily);
StringBuilder builder = new StringBuilder();
builder.append(ShellToken.BEGIN_SCRIPT.to(osFamily));
builder.append(Utils.writeUnsetVariables(Lists.newArrayList(Iterables.transform(
variablesToUnset, new Function<String, String>() {
builder.append(Utils.writeUnsetVariables(Lists.newArrayList(Iterables.transform(variablesToUnset,
new Function<String, String>() {
@Override
public String apply(String from) {
if (tokenValueMap.containsKey(from + "Variable"))
@ -141,4 +143,9 @@ public class ScriptBuilder {
functions.put(functionName, Utils.writeFunctionFromResource(functionName, osFamily));
}
}
@Override
public Iterable<String> functionDependecies(OsFamily family) {
return ImmutableSet.<String> of();
}
}

View File

@ -39,12 +39,12 @@ import com.google.common.collect.Maps;
*
* @author Adrian Cole
*/
public class CreateFile implements Statement {
public class AppendFile implements Statement {
public final static String MARKER = "END_OF_FILE";
final String path;
final Iterable<String> lines;
public CreateFile(String path, Iterable<String> lines) {// TODO: convert so
public AppendFile(String path, Iterable<String> lines) {// TODO: convert so
this.path = checkNotNull(path, "path");
this.lines = checkNotNull(lines, "lines");
checkState(Iterables.size(lines) > 0, "you must pass something to execute");
@ -77,7 +77,6 @@ public class CreateFile implements Statement {
hereFile(path, builder);
statements.add(interpret(builder.toString()));
} else {
statements.add(interpret(String.format("{rm} %s 2{closeFd}{lf}", path)));
for (String line : lines) {
statements.add(appendToFile(line, path, family));
}
@ -86,7 +85,7 @@ public class CreateFile implements Statement {
}
private void hereFile(String path, StringBuilder builder) {
builder.append("cat > ").append(path).append(" <<'").append(MARKER).append("'\n");
builder.append("cat >> ").append(path).append(" <<'").append(MARKER).append("'\n");
for (String line : lines) {
builder.append(line).append("\n");
}

View File

@ -0,0 +1,56 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.scriptbuilder.domain;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.scriptbuilder.domain.Statements.appendFile;
import static org.jclouds.scriptbuilder.domain.Statements.exec;
import java.util.Collections;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
/**
*
* @author Adrian Cole
*/
public class AuthorizeRSAPublicKey implements Statement {
private final String publicKey;
public AuthorizeRSAPublicKey(String publicKey) {
this.publicKey = checkNotNull(publicKey, "publicKey");
}
@Override
public Iterable<String> functionDependecies(OsFamily family) {
return Collections.emptyList();
}
@Override
public String render(OsFamily family) {
checkNotNull(family, "family");
if (family == OsFamily.WINDOWS)
throw new UnsupportedOperationException("windows not yet implemented");
return new StatementList(ImmutableList.of(exec("{md} .ssh"), appendFile(".ssh/authorized_keys", Splitter.on('\n')
.split(publicKey)), exec("chmod 600 .ssh/authorized_keys"))).render(family);
}
}

View File

@ -0,0 +1,57 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.scriptbuilder.domain;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.scriptbuilder.domain.Statements.appendFile;
import static org.jclouds.scriptbuilder.domain.Statements.exec;
import static org.jclouds.scriptbuilder.domain.Statements.rm;
import java.util.Collections;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
/**
*
* @author Adrian Cole
*/
public class InstallRSAPrivateKey implements Statement {
private final String privateKey;
public InstallRSAPrivateKey(String privateKey) {
this.privateKey = checkNotNull(privateKey, "privateKey");
}
@Override
public Iterable<String> functionDependecies(OsFamily family) {
return Collections.emptyList();
}
@Override
public String render(OsFamily family) {
checkNotNull(family, "family");
if (family == OsFamily.WINDOWS)
throw new UnsupportedOperationException("windows not yet implemented");
return new StatementList(ImmutableList.of(exec("{md} .ssh"), rm(".ssh/id_rsa"), appendFile(".ssh/id_rsa", Splitter.on('\n')
.split(privateKey)), exec("chmod 600 .ssh/id_rsa"))).render(family);
}
}

View File

@ -0,0 +1,59 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.scriptbuilder.domain;
import java.net.URI;
import java.util.Map.Entry;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.Iterables;
import com.google.common.collect.Multimap;
/**
* saves the content of the http response to a file
*
* @author Adrian Cole
*/
public class SaveHttpResponseTo extends InterpretableStatement {
/**
*
* @param dir
* location to save file
* @param method
* http method: ex GET
* @param endpoint
* uri corresponding to the request
* @param headers
* request headers to send
*/
public SaveHttpResponseTo(String dir, String file, String method, URI endpoint, Multimap<String, String> headers) {
super(String.format("({md} %s &&{cd} %s &&curl -X %s -s --retry 20 %s %s >%s\n", dir, dir, method, Joiner.on(' ')
.join(Iterables.transform(headers.entries(), new Function<Entry<String, String>, String>() {
@Override
public String apply(Entry<String, String> from) {
return String.format("-H \"%s: %s\"", from.getKey(), from.getValue());
}
})), endpoint.toASCIIString(), file));
}
}

View File

@ -22,6 +22,7 @@ package org.jclouds.scriptbuilder.domain;
import java.net.URI;
import java.util.Map;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Multimap;
/**
@ -40,12 +41,31 @@ public class Statements {
return new SwitchArg(arg, valueToActions);
}
public static Statement rm(final String path) {
return new Statement() {
@Override
public Iterable<String> functionDependecies(OsFamily family) {
return ImmutableList.of();
}
@Override
public String render(OsFamily family) {
if (family == OsFamily.WINDOWS)
return exec(String.format("{rm} %s 2{closeFd}", path)).render(family);
else
return exec(String.format("{rm} %s", path)).render(family);
}
};
}
public static Statement call(String function, String... args) {
return new Call(function, args);
}
public static Statement createFile(String path, Iterable<String> lines) {
return new CreateFile(path, lines);
public static Statement appendFile(String path, Iterable<String> lines) {
return new AppendFile(path, lines);
}
public static Statement createRunScript(String instanceName, Iterable<String> exports, String pwd,
@ -120,12 +140,29 @@ public class Statements {
* uri corresponding to the request
* @param headers
* request headers to send
* @param directory
*/
public static Statement extractTargzIntoDirectory(String method, URI endpoint, Multimap<String, String> headers,
String directory) {
return new PipeHttpResponseToTarxpzfIntoDirectory(method, endpoint, headers, directory);
}
/**
* unzip the data received from the request parameters.
*
* @param method
* http method: ex GET
* @param endpoint
* uri corresponding to the request
* @param headers
* request headers to send
* @param directory
*/
public static Statement extractZipIntoDirectory(String method, URI endpoint, Multimap<String, String> headers,
String directory) {
return new UnzipHttpResponseIntoDirectory(method, endpoint, headers, directory);
}
/**
* exec the data received from the request parameters.
*

View File

@ -99,7 +99,7 @@ public class SwitchArg implements Statement {
inRunScript = inRunScript ? false : true;
}
if (line.indexOf(CreateFile.MARKER) != -1) {
if (line.indexOf(AppendFile.MARKER) != -1) {
inCreateFile = inCreateFile ? false : true;
}
shouldIndent = !inCreateFile && !inRunScript;

View File

@ -0,0 +1,62 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.scriptbuilder.domain;
import static com.google.common.collect.Iterables.transform;
import static java.lang.String.format;
import java.net.URI;
import java.util.Map.Entry;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.Multimap;
/**
* unzips the content into a directory
*
* @author Adrian Cole
*/
public class UnzipHttpResponseIntoDirectory extends InterpretableStatement {
/**
*
*
* @param method
* http method: ex GET
* @param endpoint
* uri corresponding to the request
* @param headers
* request headers to send
*/
public UnzipHttpResponseIntoDirectory(String method, URI endpoint, Multimap<String, String> headers, String dir) {
super(
format(
"({md} %s &&{cd} %s &&curl -X %s -s --retry 20 %s %s >extract.zip && unzip -qq extract.zip&& rm extract.zip)\n",
dir, dir, method, Joiner.on(' ').join(
transform(headers.entries(), new Function<Entry<String, String>, String>() {
@Override
public String apply(Entry<String, String> from) {
return String.format("-H \"%s: %s\"", from.getKey(), from.getValue());
}
})), endpoint.toASCIIString()));
}
}

View File

@ -20,7 +20,7 @@
package org.jclouds.scriptbuilder;
import static org.jclouds.scriptbuilder.domain.Statements.call;
import static org.jclouds.scriptbuilder.domain.Statements.createFile;
import static org.jclouds.scriptbuilder.domain.Statements.appendFile;
import static org.testng.Assert.assertEquals;
import java.io.IOException;
@ -47,17 +47,17 @@ public class InitBuilderTest {
InitBuilder testInitBuilder = new InitBuilder("mkebsboot", "/mnt/tmp", "/mnt/tmp", ImmutableMap.of("tmpDir",
"/mnt/tmp"), ImmutableList.<Statement> of(
createFile("{tmp}{fs}{uid}{fs}scripttest{fs}temp.txt", ImmutableList.<String> of("hello world")), call("find /")));
appendFile("{tmp}{fs}{uid}{fs}scripttest{fs}temp.txt", ImmutableList.<String> of("hello world")), call("find /")));
@Test
public void testBuildSimpleWindows() throws MalformedURLException, IOException {
assertEquals(testInitBuilder.build(OsFamily.WINDOWS), CharStreams.toString(Resources.newReaderSupplier(Resources
assertEquals(testInitBuilder.render(OsFamily.WINDOWS), CharStreams.toString(Resources.newReaderSupplier(Resources
.getResource("test_init." + ShellToken.SH.to(OsFamily.WINDOWS)), Charsets.UTF_8)));
}
@Test
public void testBuildSimpleUNIX() throws MalformedURLException, IOException {
assertEquals(testInitBuilder.build(OsFamily.UNIX), CharStreams.toString(Resources.newReaderSupplier(Resources
assertEquals(testInitBuilder.render(OsFamily.UNIX), CharStreams.toString(Resources.newReaderSupplier(Resources
.getResource("test_init." + ShellToken.SH.to(OsFamily.UNIX)), Charsets.UTF_8)));
}
@ -87,7 +87,7 @@ public class InitBuilderTest {
"tar -cSf - * | tar xf - -C {varl}EBS_MOUNT_POINT{varr}", "echo size of ebs",
"du -sk {varl}EBS_MOUNT_POINT{varr}", "echo size of source", "du -sk {varl}IMAGE_DIR{varr}",
"rm -rf {varl}IMAGE_DIR{varr}/*", "umount {varl}EBS_MOUNT_POINT{varr}", "echo ----COMPLETE----")
)).build(OsFamily.UNIX), CharStreams.toString(Resources.newReaderSupplier(Resources
)).render(OsFamily.UNIX), CharStreams.toString(Resources.newReaderSupplier(Resources
.getResource("test_ebs." + ShellToken.SH.to(OsFamily.UNIX)), Charsets.UTF_8)));
}
}

View File

@ -20,7 +20,7 @@
package org.jclouds.scriptbuilder;
import static org.jclouds.scriptbuilder.domain.Statements.call;
import static org.jclouds.scriptbuilder.domain.Statements.createFile;
import static org.jclouds.scriptbuilder.domain.Statements.appendFile;
import static org.jclouds.scriptbuilder.domain.Statements.findPid;
import static org.jclouds.scriptbuilder.domain.Statements.interpret;
import static org.jclouds.scriptbuilder.domain.Statements.kill;
@ -65,19 +65,19 @@ public class ScriptBuilderTest {
interpret("echo stop {varl}RUNTIME{varr}{lf}")),
"status",
newStatementList(
createFile("{tmp}{fs}{uid}{fs}scripttest{fs}temp.txt",
appendFile("{tmp}{fs}{uid}{fs}scripttest{fs}temp.txt",
ImmutableList.<String> of("hello world")),
interpret("echo {vq}the following should be []: [{varl}RUNTIME{varr}]{vq}{lf}")))));
@Test
public void testBuildSimpleWindows() throws MalformedURLException, IOException {
assertEquals(testScriptBuilder.build(OsFamily.WINDOWS), CharStreams.toString(Resources.newReaderSupplier(
assertEquals(testScriptBuilder.render(OsFamily.WINDOWS), CharStreams.toString(Resources.newReaderSupplier(
Resources.getResource("test_script." + ShellToken.SH.to(OsFamily.WINDOWS)), Charsets.UTF_8)));
}
@Test
public void testBuildSimpleUNIX() throws MalformedURLException, IOException {
assertEquals(testScriptBuilder.build(OsFamily.UNIX), CharStreams.toString(Resources.newReaderSupplier(Resources
assertEquals(testScriptBuilder.render(OsFamily.UNIX), CharStreams.toString(Resources.newReaderSupplier(Resources
.getResource("test_script." + ShellToken.SH.to(OsFamily.UNIX)), Charsets.UTF_8)));
}
@ -86,13 +86,13 @@ public class ScriptBuilderTest {
@Test
public void testFindPidWindows() throws MalformedURLException, IOException {
assertEquals(findPidBuilder.build(OsFamily.WINDOWS), CharStreams.toString(Resources.newReaderSupplier(Resources
assertEquals(findPidBuilder.render(OsFamily.WINDOWS), CharStreams.toString(Resources.newReaderSupplier(Resources
.getResource("test_find_pid." + ShellToken.SH.to(OsFamily.WINDOWS)), Charsets.UTF_8)));
}
@Test
public void testFindPidUNIX() throws MalformedURLException, IOException {
assertEquals(findPidBuilder.build(OsFamily.UNIX), CharStreams.toString(Resources.newReaderSupplier(Resources
assertEquals(findPidBuilder.render(OsFamily.UNIX), CharStreams.toString(Resources.newReaderSupplier(Resources
.getResource("test_find_pid." + ShellToken.SH.to(OsFamily.UNIX)), Charsets.UTF_8)));
}
@ -100,13 +100,13 @@ public class ScriptBuilderTest {
@Test
public void testSeekAndDestroyWindows() throws MalformedURLException, IOException {
assertEquals(seekAndDestroyBuilder.build(OsFamily.WINDOWS), CharStreams.toString(Resources.newReaderSupplier(
assertEquals(seekAndDestroyBuilder.render(OsFamily.WINDOWS), CharStreams.toString(Resources.newReaderSupplier(
Resources.getResource("test_seek_and_destroy." + ShellToken.SH.to(OsFamily.WINDOWS)), Charsets.UTF_8)));
}
@Test
public void testSeekAndDestroyUNIX() throws MalformedURLException, IOException {
assertEquals(seekAndDestroyBuilder.build(OsFamily.UNIX), CharStreams.toString(Resources.newReaderSupplier(
assertEquals(seekAndDestroyBuilder.render(OsFamily.UNIX), CharStreams.toString(Resources.newReaderSupplier(
Resources.getResource("test_seek_and_destroy." + ShellToken.SH.to(OsFamily.UNIX)), Charsets.UTF_8)));
}

View File

@ -19,7 +19,7 @@
package org.jclouds.scriptbuilder.domain;
import static org.jclouds.scriptbuilder.domain.Statements.createFile;
import static org.jclouds.scriptbuilder.domain.Statements.appendFile;
import static org.testng.Assert.assertEquals;
import java.io.IOException;
@ -34,9 +34,9 @@ import com.google.common.io.Resources;
/**
* @author Adrian Cole
*/
@Test(groups = "unit", testName = "scriptbuilder.CreateFileTest")
public class CreateFileTest {
Statement statement = createFile("{root}etc{fs}chef{fs}client.rb", ImmutableList.of("log_level :info",
@Test(groups = "unit", testName = "scriptbuilder.AppendFileTest")
public class AppendFileTest {
Statement statement = appendFile("{root}etc{fs}chef{fs}client.rb", ImmutableList.of("log_level :info",
"log_location STDOUT", String.format("chef_server_url \"%s\"", "http://localhost:4000")));
public void testUNIX() throws IOException {
@ -50,10 +50,10 @@ public class CreateFileTest {
}
public void testRedirectGuard() {
assertEquals(CreateFile.addSpaceToEnsureWeDontAccidentallyRedirectFd("foo>>"), "foo>>");
assertEquals(CreateFile.addSpaceToEnsureWeDontAccidentallyRedirectFd("foo0>>"), "foo0 >>");
assertEquals(CreateFile.addSpaceToEnsureWeDontAccidentallyRedirectFd("foo1>>"), "foo1 >>");
assertEquals(CreateFile.addSpaceToEnsureWeDontAccidentallyRedirectFd("foo2>>"), "foo2 >>");
assertEquals(AppendFile.addSpaceToEnsureWeDontAccidentallyRedirectFd("foo>>"), "foo>>");
assertEquals(AppendFile.addSpaceToEnsureWeDontAccidentallyRedirectFd("foo0>>"), "foo0 >>");
assertEquals(AppendFile.addSpaceToEnsureWeDontAccidentallyRedirectFd("foo1>>"), "foo1 >>");
assertEquals(AppendFile.addSpaceToEnsureWeDontAccidentallyRedirectFd("foo2>>"), "foo2 >>");
}
}

View File

@ -0,0 +1,43 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.scriptbuilder.domain;
import static org.testng.Assert.assertEquals;
import org.testng.annotations.Test;
/**
* @author Adrian Cole
*/
@Test(groups = "unit", testName = "scriptbuilder.AuthorizeRSAPublicKeyTest")
public class CopyOfAuthorizeRSAPublicKeyTest {
AuthorizeRSAPublicKey auth = new AuthorizeRSAPublicKey("ssh-dss AAAAB");
public void testAuthorizeRSAPublicKeyUNIX() {
assertEquals(
auth.render(OsFamily.UNIX),
"mkdir -p .ssh\ncat >> .ssh/authorized_keys <<'END_OF_FILE'\nssh-dss AAAAB\nEND_OF_FILE\nchmod 600 .ssh/authorized_keys\n"); }
@Test(expectedExceptions = UnsupportedOperationException.class)
public void testAuthorizeRSAPublicKeyWINDOWS() {
auth.render(OsFamily.WINDOWS);
}
}

View File

@ -20,7 +20,7 @@
package org.jclouds.scriptbuilder.domain;
import static org.jclouds.scriptbuilder.domain.Statements.call;
import static org.jclouds.scriptbuilder.domain.Statements.createFile;
import static org.jclouds.scriptbuilder.domain.Statements.appendFile;
import static org.jclouds.scriptbuilder.domain.Statements.createRunScript;
import static org.testng.Assert.assertEquals;
@ -45,7 +45,7 @@ public class CreateRunScriptTest {
ImmutableList
.<Statement> of(
call("echo hello"),
createFile("{tmp}{fs}{uid}{fs}scripttest{fs}temp.txt", ImmutableList
appendFile("{tmp}{fs}{uid}{fs}scripttest{fs}temp.txt", ImmutableList
.<String> of("hello world")),
call("echo {varl}JAVA_HOME{varr}{fs}bin{fs}java -DinstanceName={varl}INSTANCE_NAME{varr} myServer.Main")));

View File

@ -0,0 +1,44 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.scriptbuilder.domain;
import static org.testng.Assert.assertEquals;
import org.testng.annotations.Test;
/**
* @author Adrian Cole
*/
@Test(groups = "unit", testName = "scriptbuilder.InstallRSAPrivateKeyTest")
public class InstallRSAPrivateKeyTest {
InstallRSAPrivateKey key = new InstallRSAPrivateKey("-----BEGIN RSA PRIVATE KEY-----\n-----END RSA PRIVATE KEY-----\n");
public void testInstallRSAPrivateKeyUNIX() {
assertEquals(
key.render(OsFamily.UNIX),
"mkdir -p .ssh\nrm .ssh/id_rsa\ncat >> .ssh/id_rsa <<'END_OF_FILE'\n-----BEGIN RSA PRIVATE KEY-----\n-----END RSA PRIVATE KEY-----\n\nEND_OF_FILE\nchmod 600 .ssh/id_rsa\n");
}
@Test(expectedExceptions = UnsupportedOperationException.class)
public void testInstallRSAPrivateKeyWINDOWS() {
key.render(OsFamily.WINDOWS);
}
}

View File

@ -19,7 +19,7 @@
package org.jclouds.scriptbuilder.domain;
import static org.jclouds.scriptbuilder.domain.Statements.createFile;
import static org.jclouds.scriptbuilder.domain.Statements.appendFile;
import static org.jclouds.scriptbuilder.domain.Statements.interpret;
import static org.jclouds.scriptbuilder.domain.Statements.newStatementList;
import static org.testng.Assert.assertEquals;
@ -37,10 +37,10 @@ import com.google.common.collect.ImmutableMap;
public class SwitchArgTest {
public void testSwitchArgUNIX() {
assertEquals(new SwitchArg(1, ImmutableMap.of("0", newStatementList(createFile(
assertEquals(new SwitchArg(1, ImmutableMap.of("0", newStatementList(appendFile(
"{tmp}{fs}{uid}{fs}scripttest{fs}temp.txt", Collections.singleton("hello world")),
interpret("echo hello zero{lf}")), "1", interpret("echo hello one{lf}"))).render(OsFamily.UNIX),
"case $1 in\n0)\n cat > /tmp/$USER/scripttest/temp.txt <<'END_OF_FILE'\nhello world\nEND_OF_FILE\n echo hello zero\n ;;\n1)\n echo hello one\n ;;\nesac\n");
"case $1 in\n0)\n cat >> /tmp/$USER/scripttest/temp.txt <<'END_OF_FILE'\nhello world\nEND_OF_FILE\n echo hello zero\n ;;\n1)\n echo hello one\n ;;\nesac\n");
}
public void testSwitchArgWindows() {

View File

@ -0,0 +1,50 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.scriptbuilder.domain;
import static org.testng.Assert.assertEquals;
import java.net.URI;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMultimap;
/**
* @author Adrian Cole
*/
@Test(groups = "unit", testName = "scriptbuilder.UnzipHttpResponseIntoDirectoryToTest")
public class UnzipHttpResponseIntoDirectoryToTest {
UnzipHttpResponseIntoDirectory jboss = new UnzipHttpResponseIntoDirectory(
"GET",
URI
.create("http://superb-sea2.dl.sourceforge.net/project/jboss/JBoss/JBoss-5.0.0.CR2/jboss-5.0.0.CR2-jdk6.zip"),
ImmutableMultimap.<String, String> of(), "/tmp");
public void testUnzipHttpResponseIntoDirectoryUNIX() {
assertEquals(
jboss.render(OsFamily.UNIX),
"(mkdir -p /tmp &&cd /tmp &&curl -X GET -s --retry 20 http://superb-sea2.dl.sourceforge.net/project/jboss/JBoss/JBoss-5.0.0.CR2/jboss-5.0.0.CR2-jdk6.zip >extract.zip && unzip -qq extract.zip&& rm extract.zip)\n");
}
public void testUnzipHttpResponseIntoDirectoryWINDOWS() {
jboss.render(OsFamily.WINDOWS); }
}

View File

@ -1,4 +1,3 @@
del c:\etc\chef\client.rb 2>NUL
echo log_level :info>>c:\etc\chef\client.rb
echo log_location STDOUT>>c:\etc\chef\client.rb
echo chef_server_url "http://localhost:4000">>c:\etc\chef\client.rb

View File

@ -1,4 +1,4 @@
cat > /etc/chef/client.rb <<'END_OF_FILE'
cat >> /etc/chef/client.rb <<'END_OF_FILE'
log_level :info
log_location STDOUT
chef_server_url "http://localhost:4000"

View File

@ -78,7 +78,7 @@ END_OF_SCRIPT
# add desired commands from the user
cat >> $INSTANCE_HOME/mkebsboot.sh <<'END_OF_SCRIPT'
cd $INSTANCE_HOME
cat > /tmp/$USER/scripttest/temp.txt <<'END_OF_FILE'
cat >> /tmp/$USER/scripttest/temp.txt <<'END_OF_FILE'
hello world
END_OF_FILE

View File

@ -17,7 +17,7 @@ cat >> /tmp/$USER/scripttest/yahooprod.sh <<'END_OF_SCRIPT'
cd /tmp/$USER/scripttest
echo hello || return 1
cat > /tmp/$USER/scripttest/temp.txt <<'END_OF_FILE'
cat >> /tmp/$USER/scripttest/temp.txt <<'END_OF_FILE'
hello world
END_OF_FILE

View File

@ -28,7 +28,6 @@ goto CASE_%1
echo stop %RUNTIME%
GOTO END_SWITCH
:CASE_status
del %TEMP%\%USERNAME%\scripttest\temp.txt 2>NUL
echo hello world>>%TEMP%\%USERNAME%\scripttest\temp.txt
echo the following should be []: [%RUNTIME%]
GOTO END_SWITCH

View File

@ -22,7 +22,7 @@ stop)
echo stop $RUNTIME
;;
status)
cat > /tmp/$USER/scripttest/temp.txt <<'END_OF_FILE'
cat >> /tmp/$USER/scripttest/temp.txt <<'END_OF_FILE'
hello world
END_OF_FILE
echo "the following should be []: [$RUNTIME]"

View File

@ -98,6 +98,8 @@ public class ComputeTask extends Task {
invokeActionOnService(act, context.getComputeService());
} catch (RunNodesException e) {
throw new BuildException(e);
} catch (IOException e) {
throw new BuildException(e);
}
}
} finally {
@ -105,7 +107,7 @@ public class ComputeTask extends Task {
}
}
private void invokeActionOnService(Action action, ComputeService computeService) throws RunNodesException {
private void invokeActionOnService(Action action, ComputeService computeService) throws RunNodesException, IOException {
switch (action) {
case CREATE:
case GET:
@ -191,7 +193,7 @@ public class ComputeTask extends Task {
}
}
private void create(ComputeService computeService) throws RunNodesException {
private void create(ComputeService computeService) throws RunNodesException, IOException {
String tag = nodeElement.getTag();
log(String.format("create tag: %s, count: %d, hardware: %s, os: %s", tag, nodeElement.getCount(), nodeElement

View File

@ -21,6 +21,7 @@ package org.jclouds.tools.ant.taskdefs.compute;
import static org.jclouds.rest.RestContextFactory.getPropertiesFromResource;
import java.io.IOException;
import java.net.URI;
import java.util.Map;
import java.util.NoSuchElementException;
@ -42,11 +43,13 @@ import org.jclouds.io.Payloads;
import org.jclouds.ssh.jsch.config.JschSshClientModule;
import org.jclouds.tools.ant.logging.config.AntLoggingModule;
import com.google.common.base.Charsets;
import com.google.common.base.Function;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.MapMaker;
import com.google.common.io.Files;
import com.google.inject.Module;
import com.google.inject.Provider;
@ -58,12 +61,12 @@ public class ComputeTaskUtils {
/**
*
* Creates a Map that associates a uri with a live connection to the compute
* provider. This is done on-demand.
* Creates a Map that associates a uri with a live connection to the compute provider. This is
* done on-demand.
*
* @param projectProvider
* allows access to the ant project to retrieve default properties
* needed for compute providers.
* allows access to the ant project to retrieve default properties needed for compute
* providers.
*/
static Map<URI, ComputeServiceContext> buildComputeMap(final Provider<Project> projectProvider) {
return new MapMaker().makeComputingMap(new Function<URI, ComputeServiceContext>() {
@ -87,7 +90,7 @@ public class ComputeTaskUtils {
}
static Template createTemplateFromElement(NodeElement nodeElement, ComputeService computeService) {
static Template createTemplateFromElement(NodeElement nodeElement, ComputeService computeService) throws IOException {
TemplateBuilder templateBuilder = computeService.templateBuilder();
if (nodeElement.getLocation() != null && !"".equals(nodeElement.getLocation()))
templateBuilder.locationId(nodeElement.getLocation());
@ -120,7 +123,7 @@ public class ComputeTaskUtils {
}
}
static TemplateOptions getNodeOptionsFromElement(NodeElement nodeElement) {
static TemplateOptions getNodeOptionsFromElement(NodeElement nodeElement) throws IOException {
TemplateOptions options = new TemplateOptions().inboundPorts(getPortsToOpenFromElement(nodeElement));
addRunScriptToOptionsIfPresentInNodeElement(nodeElement, options);
addPrivateKeyToOptionsIfPresentInNodeElement(nodeElement, options);
@ -133,14 +136,15 @@ public class ComputeTaskUtils {
options.runScript(Payloads.newFilePayload(nodeElement.getRunscript()));
}
static void addPrivateKeyToOptionsIfPresentInNodeElement(NodeElement nodeElement, TemplateOptions options) {
static void addPrivateKeyToOptionsIfPresentInNodeElement(NodeElement nodeElement, TemplateOptions options)
throws IOException {
if (nodeElement.getPrivatekeyfile() != null)
options.installPrivateKey(Payloads.newFilePayload(nodeElement.getPrivatekeyfile()));
options.installPrivateKey(Files.toString(nodeElement.getPrivatekeyfile(), Charsets.UTF_8));
}
static void addPublicKeyToOptionsIfPresentInNodeElement(NodeElement nodeElement, TemplateOptions options) {
static void addPublicKeyToOptionsIfPresentInNodeElement(NodeElement nodeElement, TemplateOptions options) throws IOException {
if (nodeElement.getPrivatekeyfile() != null)
options.authorizePublicKey(Payloads.newFilePayload(nodeElement.getPublickeyfile()));
options.authorizePublicKey(Files.toString(nodeElement.getPublickeyfile(), Charsets.UTF_8));
}
static String ipOrEmptyString(Set<String> set) {

View File

@ -376,7 +376,7 @@ public class SSHJava extends Java {
InitBuilder testInitBuilder = new InitBuilder(id, basedir, basedir, envVariables,
ImmutableList.<Statement> of(Statements.interpret( commandBuilder.toString())));
return testInitBuilder.build(osFamily);
return testInitBuilder.render(osFamily);
}
@Override

View File

@ -152,7 +152,6 @@ public class VCloudTemplateOptions extends TemplateOptions {
* @see TemplateOptions#authorizePublicKey(String)
*/
@Override
@Deprecated
public VCloudTemplateOptions authorizePublicKey(String publicKey) {
return VCloudTemplateOptions.class.cast(super.authorizePublicKey(publicKey));
}
@ -161,6 +160,7 @@ public class VCloudTemplateOptions extends TemplateOptions {
* @see TemplateOptions#authorizePublicKey(Payload)
*/
@Override
@Deprecated
public VCloudTemplateOptions authorizePublicKey(Payload publicKey) {
return VCloudTemplateOptions.class.cast(super.authorizePublicKey(publicKey));
}
@ -169,7 +169,6 @@ public class VCloudTemplateOptions extends TemplateOptions {
* @see TemplateOptions#installPrivateKey(String)
*/
@Override
@Deprecated
public VCloudTemplateOptions installPrivateKey(String privateKey) {
return VCloudTemplateOptions.class.cast(super.installPrivateKey(privateKey));
}
@ -178,6 +177,7 @@ public class VCloudTemplateOptions extends TemplateOptions {
* @see TemplateOptions#installPrivateKey(Payload)
*/
@Override
@Deprecated
public VCloudTemplateOptions installPrivateKey(Payload privateKey) {
return VCloudTemplateOptions.class.cast(super.installPrivateKey(privateKey));
}

View File

@ -30,7 +30,6 @@ import java.io.IOException;
import org.jclouds.compute.options.TemplateOptions;
import org.jclouds.io.Payloads;
import org.jclouds.util.Utils;
import org.testng.annotations.Test;
/**
@ -78,8 +77,8 @@ public class VCloudTemplateOptionsTest {
@Test
public void testinstallPrivateKey() throws IOException {
VCloudTemplateOptions options = new VCloudTemplateOptions();
options.installPrivateKey(Payloads.newPayload("-----BEGIN RSA PRIVATE KEY-----"));
assertEquals(Utils.toStringAndClose(options.getPrivateKey().getInput()), "-----BEGIN RSA PRIVATE KEY-----");
options.installPrivateKey("-----BEGIN RSA PRIVATE KEY-----");
assertEquals(options.getPrivateKey(), "-----BEGIN RSA PRIVATE KEY-----");
}
@Test
@ -91,7 +90,7 @@ public class VCloudTemplateOptionsTest {
@Test
public void testinstallPrivateKeyStatic() throws IOException {
VCloudTemplateOptions options = installPrivateKey(Payloads.newPayload("-----BEGIN RSA PRIVATE KEY-----"));
assertEquals(Utils.toStringAndClose(options.getPrivateKey().getInput()), "-----BEGIN RSA PRIVATE KEY-----");
assertEquals(options.getPrivateKey(), "-----BEGIN RSA PRIVATE KEY-----");
}
@Test(expectedExceptions = NullPointerException.class)
@ -102,8 +101,8 @@ public class VCloudTemplateOptionsTest {
@Test
public void testauthorizePublicKey() throws IOException {
VCloudTemplateOptions options = new VCloudTemplateOptions();
options.authorizePublicKey(Payloads.newPayload("ssh-rsa"));
assertEquals(Utils.toStringAndClose(options.getPublicKey().getInput()), "ssh-rsa");
options.authorizePublicKey("ssh-rsa");
assertEquals(options.getPublicKey(), "ssh-rsa");
}
@Test
@ -115,7 +114,7 @@ public class VCloudTemplateOptionsTest {
@Test
public void testauthorizePublicKeyStatic() throws IOException {
VCloudTemplateOptions options = authorizePublicKey(Payloads.newPayload("ssh-rsa"));
assertEquals(Utils.toStringAndClose(options.getPublicKey().getInput()), "ssh-rsa");
assertEquals(options.getPublicKey(), "ssh-rsa");
}
@Test(expectedExceptions = NullPointerException.class)

View File

@ -29,11 +29,10 @@ import org.jclouds.io.Payload;
import org.jclouds.util.Utils;
/**
* Contains options supported in the {@code ComputeService#runNode} operation on
* the "trmk-vcloudexpress" provider. <h2>
* Usage</h2> The recommended way to instantiate a
* TerremarkVCloudTemplateOptions object is to statically import
* TerremarkVCloudTemplateOptions.* and invoke a static creation method followed
* Contains options supported in the {@code ComputeService#runNode} operation on the
* "trmk-vcloudexpress" provider. <h2>
* Usage</h2> The recommended way to instantiate a TerremarkVCloudTemplateOptions object is to
* statically import TerremarkVCloudTemplateOptions.* and invoke a static creation method followed
* by an instance mutator (if needed):
* <p/>
* <code>
@ -162,10 +161,9 @@ public class TerremarkVCloudTemplateOptions extends TemplateOptions {
/**
*
* special thing is that we do assume if you are passing groups that you have
* everything you need already defined. for example, our option inboundPorts
* normally creates ingress rules accordingly but if we notice you've
* specified securityGroups, we do not mess with rules at all
* special thing is that we do assume if you are passing groups that you have everything you need
* already defined. for example, our option inboundPorts normally creates ingress rules
* accordingly but if we notice you've specified securityGroups, we do not mess with rules at all
*
* @see TemplateOptions#inboundPorts
*/
@ -178,7 +176,6 @@ public class TerremarkVCloudTemplateOptions extends TemplateOptions {
* @see TemplateOptions#authorizePublicKey(String)
*/
@Override
@Deprecated
public TerremarkVCloudTemplateOptions authorizePublicKey(String publicKey) {
return TerremarkVCloudTemplateOptions.class.cast(super.authorizePublicKey(publicKey));
}
@ -187,6 +184,7 @@ public class TerremarkVCloudTemplateOptions extends TemplateOptions {
* @see TemplateOptions#authorizePublicKey(Payload)
*/
@Override
@Deprecated
public TerremarkVCloudTemplateOptions authorizePublicKey(Payload publicKey) {
return TerremarkVCloudTemplateOptions.class.cast(super.authorizePublicKey(publicKey));
}
@ -195,7 +193,6 @@ public class TerremarkVCloudTemplateOptions extends TemplateOptions {
* @see TemplateOptions#installPrivateKey(String)
*/
@Override
@Deprecated
public TerremarkVCloudTemplateOptions installPrivateKey(String privateKey) {
return TerremarkVCloudTemplateOptions.class.cast(super.installPrivateKey(privateKey));
}
@ -204,6 +201,7 @@ public class TerremarkVCloudTemplateOptions extends TemplateOptions {
* @see TemplateOptions#installPrivateKey(Payload)
*/
@Override
@Deprecated
public TerremarkVCloudTemplateOptions installPrivateKey(Payload privateKey) {
return TerremarkVCloudTemplateOptions.class.cast(super.installPrivateKey(privateKey));
}
@ -234,8 +232,7 @@ public class TerremarkVCloudTemplateOptions extends TemplateOptions {
}
/**
* @return keyPair to use when running the instance or null, to generate a
* keypair.
* @return keyPair to use when running the instance or null, to generate a keypair.
*/
public String getSshKeyFingerprint() {
return keyPair;

View File

@ -30,7 +30,6 @@ import static org.testng.Assert.assertEquals;
import java.io.IOException;
import org.jclouds.compute.options.TemplateOptions;
import org.jclouds.util.Utils;
import org.jclouds.vcloud.terremark.compute.options.TerremarkVCloudTemplateOptions;
import org.testng.annotations.Test;
@ -114,19 +113,17 @@ public class TerremarkVCloudTemplateOptionsTest {
}
// superclass tests
@SuppressWarnings("deprecation")
@Test(expectedExceptions = IllegalArgumentException.class)
public void testinstallPrivateKeyBadFormat() {
TerremarkVCloudTemplateOptions options = new TerremarkVCloudTemplateOptions();
options.installPrivateKey("whompy");
}
@SuppressWarnings("deprecation")
@Test
public void testinstallPrivateKey() throws IOException {
TerremarkVCloudTemplateOptions options = new TerremarkVCloudTemplateOptions();
options.installPrivateKey("-----BEGIN RSA PRIVATE KEY-----");
assertEquals(Utils.toStringAndClose(options.getPrivateKey().getInput()), "-----BEGIN RSA PRIVATE KEY-----");
assertEquals(options.getPrivateKey(), "-----BEGIN RSA PRIVATE KEY-----");
}
@Test
@ -138,7 +135,7 @@ public class TerremarkVCloudTemplateOptionsTest {
@Test
public void testinstallPrivateKeyStatic() throws IOException {
TerremarkVCloudTemplateOptions options = installPrivateKey("-----BEGIN RSA PRIVATE KEY-----");
assertEquals(Utils.toStringAndClose(options.getPrivateKey().getInput()), "-----BEGIN RSA PRIVATE KEY-----");
assertEquals(options.getPrivateKey(), "-----BEGIN RSA PRIVATE KEY-----");
}
@Test(expectedExceptions = NullPointerException.class)
@ -146,19 +143,17 @@ public class TerremarkVCloudTemplateOptionsTest {
installPrivateKey(null);
}
@SuppressWarnings("deprecation")
@Test(expectedExceptions = IllegalArgumentException.class)
public void testauthorizePublicKeyBadFormat() {
TerremarkVCloudTemplateOptions options = new TerremarkVCloudTemplateOptions();
options.authorizePublicKey("whompy");
}
@SuppressWarnings("deprecation")
@Test
public void testauthorizePublicKey() throws IOException {
TerremarkVCloudTemplateOptions options = new TerremarkVCloudTemplateOptions();
options.authorizePublicKey("ssh-rsa");
assertEquals(Utils.toStringAndClose(options.getPublicKey().getInput()), "ssh-rsa");
assertEquals(options.getPublicKey(), "ssh-rsa");
}
@Test
@ -170,7 +165,7 @@ public class TerremarkVCloudTemplateOptionsTest {
@Test
public void testauthorizePublicKeyStatic() throws IOException {
TerremarkVCloudTemplateOptions options = authorizePublicKey("ssh-rsa");
assertEquals(Utils.toStringAndClose(options.getPublicKey().getInput()), "ssh-rsa");
assertEquals(options.getPublicKey(), "ssh-rsa");
}
@Test(expectedExceptions = NullPointerException.class)