Merge remote branch 'origin'

Conflicts:
	scriptbuilder/src/test/resources/client_rb.cmd
	scriptbuilder/src/test/resources/test_script.cmd
	tools/antcontrib/src/main/java/org/jclouds/tools/ant/taskdefs/sshjava/SSHJava.java
This commit is contained in:
Andrew Phillips 2010-09-27 11:43:16 +02:00
commit cdf35beafb
68 changed files with 1957 additions and 766 deletions

View File

@ -26,13 +26,13 @@ import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
import javax.inject.Singleton; import javax.inject.Singleton;
import org.jclouds.aws.AWSResponseException;
import org.jclouds.aws.ec2.EC2Client; import org.jclouds.aws.ec2.EC2Client;
import org.jclouds.aws.ec2.compute.domain.RegionAndName; import org.jclouds.aws.ec2.compute.domain.RegionAndName;
import org.jclouds.aws.ec2.domain.KeyPair; import org.jclouds.aws.ec2.domain.KeyPair;
import org.jclouds.compute.reference.ComputeServiceConstants; import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.logging.Logger; import org.jclouds.logging.Logger;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
@ -59,7 +59,8 @@ public class CreateUniqueKeyPair implements Function<RegionAndName, KeyPair> {
return createNewKeyPairInRegion(from.getRegion(), from.getName()); return createNewKeyPairInRegion(from.getRegion(), from.getName());
} }
private KeyPair createNewKeyPairInRegion(String region, String tag) { @VisibleForTesting
KeyPair createNewKeyPairInRegion(String region, String tag) {
checkNotNull(region, "region"); checkNotNull(region, "region");
checkNotNull(tag, "tag"); checkNotNull(tag, "tag");
logger.debug(">> creating keyPair region(%s) tag(%s)", region, tag); logger.debug(">> creating keyPair region(%s) tag(%s)", region, tag);
@ -68,10 +69,8 @@ public class CreateUniqueKeyPair implements Function<RegionAndName, KeyPair> {
try { try {
keyPair = ec2Client.getKeyPairServices().createKeyPairInRegion(region, getNextName(region, tag)); keyPair = ec2Client.getKeyPairServices().createKeyPairInRegion(region, getNextName(region, tag));
logger.debug("<< created keyPair(%s)", keyPair.getKeyName()); logger.debug("<< created keyPair(%s)", keyPair.getKeyName());
} catch (AWSResponseException e) { } catch (IllegalStateException e) {
if (!e.getError().getCode().equals("InvalidKeyPair.Duplicate")) {
throw e;
}
} }
} }
return keyPair; return keyPair;

View File

@ -26,8 +26,11 @@ import static com.google.common.base.Preconditions.checkState;
import java.util.Arrays; import java.util.Arrays;
import java.util.Set; import java.util.Set;
import org.jclouds.aws.cloudwatch.CloudWatchClient;
import org.jclouds.compute.options.TemplateOptions; import org.jclouds.compute.options.TemplateOptions;
import org.jclouds.domain.Credentials;
import org.jclouds.io.Payload; import org.jclouds.io.Payload;
import org.jclouds.scriptbuilder.domain.Statement;
import org.jclouds.util.Utils; import org.jclouds.util.Utils;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
@ -261,7 +264,7 @@ public class EC2TemplateOptions extends TemplateOptions {
// methods that only facilitate returning the correct object type // methods that only facilitate returning the correct object type
/** /**
* @see TemplateOptions#blockOnPort * {@inheritDoc}
*/ */
@Override @Override
public EC2TemplateOptions blockOnPort(int port, int seconds) { public EC2TemplateOptions blockOnPort(int port, int seconds) {
@ -269,12 +272,7 @@ public class EC2TemplateOptions extends TemplateOptions {
} }
/** /**
* * {@inheritDoc}
* 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
*/ */
@Override @Override
public EC2TemplateOptions inboundPorts(int... ports) { public EC2TemplateOptions inboundPorts(int... ports) {
@ -282,41 +280,41 @@ public class EC2TemplateOptions extends TemplateOptions {
} }
/** /**
* @see TemplateOptions#authorizePublicKey(String) * {@inheritDoc}
*/ */
@Override @Override
@Deprecated
public EC2TemplateOptions authorizePublicKey(String publicKey) { public EC2TemplateOptions authorizePublicKey(String publicKey) {
return EC2TemplateOptions.class.cast(super.authorizePublicKey(publicKey)); return EC2TemplateOptions.class.cast(super.authorizePublicKey(publicKey));
} }
/** /**
* @see TemplateOptions#authorizePublicKey(Payload) * {@inheritDoc}
*/ */
@Override @Override
@Deprecated
public EC2TemplateOptions authorizePublicKey(Payload publicKey) { public EC2TemplateOptions authorizePublicKey(Payload publicKey) {
return EC2TemplateOptions.class.cast(super.authorizePublicKey(publicKey)); return EC2TemplateOptions.class.cast(super.authorizePublicKey(publicKey));
} }
/** /**
* @see TemplateOptions#installPrivateKey(String) * {@inheritDoc}
*/ */
@Override @Override
@Deprecated
public EC2TemplateOptions installPrivateKey(String privateKey) { public EC2TemplateOptions installPrivateKey(String privateKey) {
return EC2TemplateOptions.class.cast(super.installPrivateKey(privateKey)); return EC2TemplateOptions.class.cast(super.installPrivateKey(privateKey));
} }
/** /**
* @see TemplateOptions#installPrivateKey(Payload) * {@inheritDoc}
*/ */
@Override @Override
@Deprecated
public EC2TemplateOptions installPrivateKey(Payload privateKey) { public EC2TemplateOptions installPrivateKey(Payload privateKey) {
return EC2TemplateOptions.class.cast(super.installPrivateKey(privateKey)); return EC2TemplateOptions.class.cast(super.installPrivateKey(privateKey));
} }
/** /**
* @see TemplateOptions#runScript(Payload) * {@inheritDoc}
*/ */
@Override @Override
public EC2TemplateOptions runScript(Payload script) { public EC2TemplateOptions runScript(Payload script) {
@ -324,7 +322,7 @@ public class EC2TemplateOptions extends TemplateOptions {
} }
/** /**
* @see TemplateOptions#runScript(byte[]) * {@inheritDoc}
*/ */
@Override @Override
@Deprecated @Deprecated
@ -333,13 +331,61 @@ public class EC2TemplateOptions extends TemplateOptions {
} }
/** /**
* @see TemplateOptions#withMetadata * {@inheritDoc}
*/ */
@Override @Override
public EC2TemplateOptions withMetadata() { public EC2TemplateOptions withMetadata() {
return EC2TemplateOptions.class.cast(super.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 * @return groupIds the user specified to run instances with, or zero length set to create an
* implicit group * implicit group

View File

@ -94,7 +94,7 @@ public class ParseAWSErrorFromXmlContent implements HttpErrorHandler {
&& (error.getCode().endsWith(".NotFound") || error.getCode().endsWith(".Unknown"))) && (error.getCode().endsWith(".NotFound") || error.getCode().endsWith(".Unknown")))
exception = new ResourceNotFoundException(message, exception); exception = new ResourceNotFoundException(message, exception);
else if ((error != null && error.getCode() != null && (error.getCode().equals("IncorrectState") || error else if ((error != null && error.getCode() != null && (error.getCode().equals("IncorrectState") || error
.getCode().equals("InvalidGroup.Duplicate"))) .getCode().endsWith(".Duplicate")))
|| (message != null && message.indexOf("already exists") != -1)) || (message != null && message.indexOf("already exists") != -1))
exception = new IllegalStateException(message, exception); exception = new IllegalStateException(message, exception);
else if (error != null && error.getCode() != null && error.getCode().equals("AuthFailure")) else if (error != null && error.getCode() != null && error.getCode().equals("AuthFailure"))

View File

@ -172,7 +172,7 @@ public class CloudApplicationArchitecturesEC2ClientLiveTest {
String script = new ScriptBuilder() // lamp install script String script = new ScriptBuilder() // lamp install script
.addStatement(exec("runurl run.alestic.com/apt/upgrade"))// .addStatement(exec("runurl run.alestic.com/apt/upgrade"))//
.addStatement(exec("runurl run.alestic.com/install/lamp"))// .addStatement(exec("runurl run.alestic.com/install/lamp"))//
.build(OsFamily.UNIX); .render(OsFamily.UNIX);
RunningInstance instance = null; RunningInstance instance = null;
while (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}EBS_MOUNT_POINT{varr}", "echo size of source",
"du -sk {varl}IMAGE_DIR{varr}", "rm -rf {varl}IMAGE_DIR{varr}/*", "du -sk {varl}IMAGE_DIR{varr}", "rm -rf {varl}IMAGE_DIR{varr}/*",
"umount {varl}EBS_MOUNT_POINT{varr}", "echo " + SCRIPT_END))) "umount {varl}EBS_MOUNT_POINT{varr}", "echo " + SCRIPT_END)))
.build(OsFamily.UNIX); .render(OsFamily.UNIX);
} }
@Test(enabled = false, dependsOnMethods = "testCreateAndAttachVolume") @Test(enabled = false, dependsOnMethods = "testCreateAndAttachVolume")

View File

@ -74,6 +74,32 @@ public class EC2TemplateBuilderLiveTest {
return overrides; return overrides;
} }
@Test
public void testTemplateBuilderM1SMALLWithDescription() {
ComputeServiceContext newContext = null;
try {
newContext = new ComputeServiceContextFactory().createContext(provider, ImmutableSet
.<Module> of(new Log4JLoggingModule()), setupProperties());
Template template = newContext.getComputeService().templateBuilder().hardwareId(InstanceType.M1_SMALL)
.osVersionMatches("10.04").imageDescriptionMatches("ubuntu-images").osFamily(OsFamily.UBUNTU).build();
System.out.println(template.getHardware());
assert (template.getImage().getProviderId().startsWith("ami-")) : template;
assertEquals(template.getImage().getOperatingSystem().getVersion(), "10.04");
assertEquals(template.getImage().getOperatingSystem().is64Bit(), false);
assertEquals(template.getImage().getOperatingSystem().getFamily(), OsFamily.UBUNTU);
assertEquals(template.getImage().getVersion(), "20100921");
assertEquals(template.getImage().getUserMetadata().get("rootDeviceType"), "instance-store");
assertEquals(template.getLocation().getId(), "us-east-1");
assertEquals(getCores(template.getHardware()), 1.0d);
assertEquals(template.getHardware().getId(), InstanceType.M1_SMALL);
} finally {
if (newContext != null)
newContext.close();
}
}
@Test @Test
public void testTemplateBuilderCanUseImageIdAndhardwareId() { public void testTemplateBuilderCanUseImageIdAndhardwareId() {
ComputeServiceContext newContext = null; ComputeServiceContext newContext = null;

View File

@ -0,0 +1,98 @@
/**
*
* 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.aws.ec2.compute.functions;
import static org.easymock.EasyMock.expect;
import static org.easymock.classextension.EasyMock.createMock;
import static org.easymock.classextension.EasyMock.replay;
import static org.easymock.classextension.EasyMock.verify;
import static org.testng.Assert.assertEquals;
import java.net.UnknownHostException;
import org.jclouds.aws.ec2.EC2Client;
import org.jclouds.aws.ec2.domain.KeyPair;
import org.jclouds.aws.ec2.services.KeyPairClient;
import org.testng.annotations.Test;
import com.google.common.base.Supplier;
/**
* @author Adrian Cole
*/
@Test(groups = "unit", testName = "ec2.CreateUniqueKeyPairTest")
public class CreateUniqueKeyPairTest {
@SuppressWarnings( { "unchecked" })
@Test
public void testApply() throws UnknownHostException {
EC2Client client = createMock(EC2Client.class);
KeyPairClient keyClient = createMock(KeyPairClient.class);
Supplier<String> uniqueIdSupplier = createMock(Supplier.class);
KeyPair pair = createMock(KeyPair.class);
expect(client.getKeyPairServices()).andReturn(keyClient).atLeastOnce();
expect(uniqueIdSupplier.get()).andReturn("1");
expect(keyClient.createKeyPairInRegion("region", "jclouds#tag#region#1")).andReturn(pair);
replay(client);
replay(keyClient);
replay(uniqueIdSupplier);
CreateUniqueKeyPair parser = new CreateUniqueKeyPair(client, uniqueIdSupplier);
assertEquals(parser.createNewKeyPairInRegion("region", "tag"), pair);
verify(client);
verify(keyClient);
verify(uniqueIdSupplier);
}
@SuppressWarnings( { "unchecked" })
@Test
public void testApplyWithIllegalStateException() throws UnknownHostException {
EC2Client client = createMock(EC2Client.class);
KeyPairClient keyClient = createMock(KeyPairClient.class);
Supplier<String> uniqueIdSupplier = createMock(Supplier.class);
KeyPair pair = createMock(KeyPair.class);
expect(client.getKeyPairServices()).andReturn(keyClient).atLeastOnce();
expect(uniqueIdSupplier.get()).andReturn("1");
expect(keyClient.createKeyPairInRegion("region", "jclouds#tag#region#1")).andThrow(new IllegalStateException());
expect(uniqueIdSupplier.get()).andReturn("2");
expect(keyClient.createKeyPairInRegion("region", "jclouds#tag#region#2")).andReturn(pair);
replay(client);
replay(keyClient);
replay(uniqueIdSupplier);
CreateUniqueKeyPair parser = new CreateUniqueKeyPair(client, uniqueIdSupplier);
assertEquals(parser.createNewKeyPairInRegion("region", "tag"), pair);
verify(client);
verify(keyClient);
verify(uniqueIdSupplier);
}
}

View File

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

View File

@ -83,7 +83,7 @@ public class RunningInstanceToNodeMetadataTest {
DateService dateService = new SimpleDateFormatDateService(); DateService dateService = new SimpleDateFormatDateService();
@SuppressWarnings({ "unchecked", "rawtypes" }) @SuppressWarnings( { "unchecked" })
@Test @Test
public void testApplyWithEBSWhenBootIsInstanceStoreAndAvailabilityZoneNotFound() throws UnknownHostException { public void testApplyWithEBSWhenBootIsInstanceStoreAndAvailabilityZoneNotFound() throws UnknownHostException {
EC2Client client = createMock(EC2Client.class); EC2Client client = createMock(EC2Client.class);
@ -92,7 +92,7 @@ public class RunningInstanceToNodeMetadataTest {
Map<RegionAndName, KeyPair> credentialsMap = createMock(Map.class); Map<RegionAndName, KeyPair> credentialsMap = createMock(Map.class);
ConcurrentMap<RegionAndName, org.jclouds.compute.domain.Image> imageMap = createMock(ConcurrentMap.class); ConcurrentMap<RegionAndName, org.jclouds.compute.domain.Image> imageMap = createMock(ConcurrentMap.class);
Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet
.<Hardware> of(EC2Hardware.M1_SMALL)); .<Hardware> of(EC2Hardware.M1_SMALL));
PopulateDefaultLoginCredentialsForImageStrategy credentialProvider = createMock(PopulateDefaultLoginCredentialsForImageStrategy.class); PopulateDefaultLoginCredentialsForImageStrategy credentialProvider = createMock(PopulateDefaultLoginCredentialsForImageStrategy.class);
RunningInstance instance = createMock(RunningInstance.class); RunningInstance instance = createMock(RunningInstance.class);
Image image = createMock(Image.class); Image image = createMock(Image.class);
@ -104,7 +104,7 @@ public class RunningInstanceToNodeMetadataTest {
Location location = new LocationImpl(LocationScope.ZONE, "us-east-1d", "description", null); Location location = new LocationImpl(LocationScope.ZONE, "us-east-1d", "description", null);
Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.<Location> of(location)); .<Location> of(location));
org.jclouds.compute.domain.Image jcImage = createMock(org.jclouds.compute.domain.Image.class); org.jclouds.compute.domain.Image jcImage = createMock(org.jclouds.compute.domain.Image.class);
expect(instance.getIpAddress()).andReturn("174.129.1.50"); expect(instance.getIpAddress()).andReturn("174.129.1.50");
@ -118,24 +118,21 @@ public class RunningInstanceToNodeMetadataTest {
expect(imageMap.get(new RegionAndName(Region.US_EAST_1, "ami-1515f07c"))).andReturn(jcImage); expect(imageMap.get(new RegionAndName(Region.US_EAST_1, "ami-1515f07c"))).andReturn(jcImage);
expect(amiClient.describeImagesInRegion(Region.US_EAST_1, imageIds("ami-1515f07c"))).andReturn( expect(amiClient.describeImagesInRegion(Region.US_EAST_1, imageIds("ami-1515f07c"))).andReturn(
(Set) ImmutableSet.<Image> of(image)); (Set) ImmutableSet.<Image> of(image));
expect(credentialProvider.execute(image)).andReturn(new Credentials("user", "pass")); expect(credentialProvider.execute(image)).andReturn(new Credentials("user", "pass"));
expect(credentialsMap.get(new RegionAndName(Region.US_EAST_1, "jclouds#tag#us-east-1#50"))).andReturn( expect(credentialsMap.get(new RegionAndName(Region.US_EAST_1, "jclouds#tag#us-east-1#50"))).andReturn(
new KeyPair(Region.US_EAST_1, "jclouds#tag#us-east-1#50", "keyFingerprint", "pass")); new KeyPair(Region.US_EAST_1, "jclouds#tag#us-east-1#50", "keyFingerprint", "pass"));
expect(instance.getAvailabilityZone()).andReturn(AvailabilityZone.US_EAST_1A).atLeastOnce(); expect(instance.getAvailabilityZone()).andReturn(AvailabilityZone.US_EAST_1A).atLeastOnce();
expect(instance.getInstanceType()).andReturn(InstanceType.M1_SMALL).atLeastOnce(); expect(instance.getInstanceType()).andReturn(InstanceType.M1_SMALL).atLeastOnce();
expect(instance.getEbsBlockDevices()).andReturn( expect(instance.getEbsBlockDevices()).andReturn(
ImmutableMap.<String, EbsBlockDevice> of( ImmutableMap.<String, EbsBlockDevice> of("/dev/sdg", new EbsBlockDevice("vol-1f20d376",
"/dev/sdg", Attachment.Status.ATTACHED, dateService.iso8601DateParse("2009-12-11T16:32:46.000Z"), false),
new EbsBlockDevice("vol-1f20d376", Attachment.Status.ATTACHED, dateService "/dev/sdj", new EbsBlockDevice("vol-c0eb78aa", Attachment.Status.ATTACHED, dateService
.iso8601DateParse("2009-12-11T16:32:46.000Z"), false), .iso8601DateParse("2010-06-17T10:43:28.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.getRootDeviceType()).andReturn(RootDeviceType.INSTANCE_STORE);
expect(instance.getRootDeviceName()).andReturn(null).atLeastOnce(); expect(instance.getRootDeviceName()).andReturn(null).atLeastOnce();
@ -148,7 +145,7 @@ public class RunningInstanceToNodeMetadataTest {
replay(jcImage); replay(jcImage);
RunningInstanceToNodeMetadata parser = new RunningInstanceToNodeMetadata(client, credentialsMap, RunningInstanceToNodeMetadata parser = new RunningInstanceToNodeMetadata(client, credentialsMap,
credentialProvider, imageMap, locations, hardwares); credentialProvider, imageMap, locations, hardwares);
NodeMetadata metadata = parser.apply(instance); NodeMetadata metadata = parser.apply(instance);
@ -160,11 +157,11 @@ public class RunningInstanceToNodeMetadataTest {
assertEquals(metadata.getHardware().getProviderId(), "m1.small"); assertEquals(metadata.getHardware().getProviderId(), "m1.small");
assertEquals(metadata.getHardware().getProcessors(), ImmutableList.<Processor> of(new Processor(1.0, 1.0))); assertEquals(metadata.getHardware().getProcessors(), ImmutableList.<Processor> of(new Processor(1.0, 1.0)));
assertEquals(metadata.getHardware().getRam(), 1740); assertEquals(metadata.getHardware().getRam(), 1740);
assertEquals(metadata.getHardware().getVolumes(), assertEquals(metadata.getHardware().getVolumes(), ImmutableList.<Volume> of(new VolumeImpl(null,
ImmutableList.<Volume> of(new VolumeImpl(null, Volume.Type.LOCAL, 10.0f, "/dev/sda1", true, false),// Volume.Type.LOCAL, 10.0f, "/dev/sda1", true, false),//
new VolumeImpl(null, Volume.Type.LOCAL, 150.0f, "/dev/sda2", false, 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-1f20d376", Volume.Type.SAN, null, "/dev/sdg", false, true),//
new VolumeImpl("vol-c0eb78aa", Volume.Type.SAN, null, "/dev/sdj", false, true))); new VolumeImpl("vol-c0eb78aa", Volume.Type.SAN, null, "/dev/sdj", false, true)));
assertEquals(metadata.getCredentials(), new Credentials("user", "pass")); assertEquals(metadata.getCredentials(), new Credentials("user", "pass"));
@ -178,7 +175,7 @@ public class RunningInstanceToNodeMetadataTest {
} }
@SuppressWarnings({ "unchecked", "rawtypes" }) @SuppressWarnings( { "unchecked" })
@Test @Test
public void testApplyForNovaWhereNullAvailabilityZoneIpAddressNoGroups() throws UnknownHostException { public void testApplyForNovaWhereNullAvailabilityZoneIpAddressNoGroups() throws UnknownHostException {
EC2Client client = createMock(EC2Client.class); EC2Client client = createMock(EC2Client.class);
@ -187,7 +184,7 @@ public class RunningInstanceToNodeMetadataTest {
Map<RegionAndName, KeyPair> credentialsMap = createMock(Map.class); Map<RegionAndName, KeyPair> credentialsMap = createMock(Map.class);
ConcurrentMap<RegionAndName, org.jclouds.compute.domain.Image> imageMap = createMock(ConcurrentMap.class); ConcurrentMap<RegionAndName, org.jclouds.compute.domain.Image> imageMap = createMock(ConcurrentMap.class);
Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet
.<Hardware> of(EC2Hardware.M1_SMALL)); .<Hardware> of(EC2Hardware.M1_SMALL));
PopulateDefaultLoginCredentialsForImageStrategy credentialProvider = createMock(PopulateDefaultLoginCredentialsForImageStrategy.class); PopulateDefaultLoginCredentialsForImageStrategy credentialProvider = createMock(PopulateDefaultLoginCredentialsForImageStrategy.class);
RunningInstance instance = createMock(RunningInstance.class); RunningInstance instance = createMock(RunningInstance.class);
Image image = createMock(Image.class); Image image = createMock(Image.class);
@ -199,7 +196,7 @@ public class RunningInstanceToNodeMetadataTest {
Location region = new LocationImpl(LocationScope.REGION, "us-east-1", "description", null); Location region = new LocationImpl(LocationScope.REGION, "us-east-1", "description", null);
Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.<Location> of(region)); .<Location> of(region));
org.jclouds.compute.domain.Image jcImage = createMock(org.jclouds.compute.domain.Image.class); org.jclouds.compute.domain.Image jcImage = createMock(org.jclouds.compute.domain.Image.class);
expect(instance.getIpAddress()).andReturn(null); expect(instance.getIpAddress()).andReturn(null);
@ -213,7 +210,7 @@ public class RunningInstanceToNodeMetadataTest {
expect(imageMap.get(new RegionAndName(Region.US_EAST_1, "ami-1515f07c"))).andReturn(jcImage); expect(imageMap.get(new RegionAndName(Region.US_EAST_1, "ami-1515f07c"))).andReturn(jcImage);
expect(amiClient.describeImagesInRegion(Region.US_EAST_1, imageIds("ami-1515f07c"))).andReturn( expect(amiClient.describeImagesInRegion(Region.US_EAST_1, imageIds("ami-1515f07c"))).andReturn(
(Set) ImmutableSet.<Image> of(image)); (Set) ImmutableSet.<Image> of(image));
expect(credentialProvider.execute(image)).andReturn(new Credentials("user", "pass")); expect(credentialProvider.execute(image)).andReturn(new Credentials("user", "pass"));
@ -234,7 +231,7 @@ public class RunningInstanceToNodeMetadataTest {
replay(jcImage); replay(jcImage);
RunningInstanceToNodeMetadata parser = new RunningInstanceToNodeMetadata(client, credentialsMap, RunningInstanceToNodeMetadata parser = new RunningInstanceToNodeMetadata(client, credentialsMap,
credentialProvider, imageMap, locations, hardwares); credentialProvider, imageMap, locations, hardwares);
NodeMetadata metadata = parser.apply(instance); NodeMetadata metadata = parser.apply(instance);
@ -246,9 +243,9 @@ public class RunningInstanceToNodeMetadataTest {
assertEquals(metadata.getHardware().getProviderId(), "m1.small"); assertEquals(metadata.getHardware().getProviderId(), "m1.small");
assertEquals(metadata.getHardware().getProcessors(), ImmutableList.<Processor> of(new Processor(1.0, 1.0))); assertEquals(metadata.getHardware().getProcessors(), ImmutableList.<Processor> of(new Processor(1.0, 1.0)));
assertEquals(metadata.getHardware().getRam(), 1740); assertEquals(metadata.getHardware().getRam(), 1740);
assertEquals(metadata.getHardware().getVolumes(), assertEquals(metadata.getHardware().getVolumes(), ImmutableList.<Volume> of(new VolumeImpl(null,
ImmutableList.<Volume> of(new VolumeImpl(null, Volume.Type.LOCAL, 10.0f, "/dev/sda1", true, false),// Volume.Type.LOCAL, 10.0f, "/dev/sda1", true, false),//
new VolumeImpl(null, Volume.Type.LOCAL, 150.0f, "/dev/sda2", false, false))); new VolumeImpl(null, Volume.Type.LOCAL, 150.0f, "/dev/sda2", false, false)));
assertEquals(metadata.getCredentials(), new Credentials("user", null)); assertEquals(metadata.getCredentials(), new Credentials("user", null));
@ -262,7 +259,7 @@ public class RunningInstanceToNodeMetadataTest {
} }
@SuppressWarnings({ "unchecked", "rawtypes" }) @SuppressWarnings( { "unchecked" })
@Test @Test
public void testApplyWhereUnknownInstanceType() throws UnknownHostException { public void testApplyWhereUnknownInstanceType() throws UnknownHostException {
EC2Client client = createMock(EC2Client.class); EC2Client client = createMock(EC2Client.class);
@ -271,7 +268,7 @@ public class RunningInstanceToNodeMetadataTest {
Map<RegionAndName, KeyPair> credentialsMap = createMock(Map.class); Map<RegionAndName, KeyPair> credentialsMap = createMock(Map.class);
ConcurrentMap<RegionAndName, org.jclouds.compute.domain.Image> imageMap = createMock(ConcurrentMap.class); ConcurrentMap<RegionAndName, org.jclouds.compute.domain.Image> imageMap = createMock(ConcurrentMap.class);
Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet
.<Hardware> of(EC2Hardware.M1_SMALL)); .<Hardware> of(EC2Hardware.M1_SMALL));
PopulateDefaultLoginCredentialsForImageStrategy credentialProvider = createMock(PopulateDefaultLoginCredentialsForImageStrategy.class); PopulateDefaultLoginCredentialsForImageStrategy credentialProvider = createMock(PopulateDefaultLoginCredentialsForImageStrategy.class);
RunningInstance instance = createMock(RunningInstance.class); RunningInstance instance = createMock(RunningInstance.class);
Image image = createMock(Image.class); Image image = createMock(Image.class);
@ -283,7 +280,7 @@ public class RunningInstanceToNodeMetadataTest {
Location region = new LocationImpl(LocationScope.REGION, "us-east-1", "description", null); Location region = new LocationImpl(LocationScope.REGION, "us-east-1", "description", null);
Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.<Location> of(region)); .<Location> of(region));
org.jclouds.compute.domain.Image jcImage = createMock(org.jclouds.compute.domain.Image.class); org.jclouds.compute.domain.Image jcImage = createMock(org.jclouds.compute.domain.Image.class);
expect(instance.getIpAddress()).andReturn(null); expect(instance.getIpAddress()).andReturn(null);
@ -297,7 +294,7 @@ public class RunningInstanceToNodeMetadataTest {
expect(imageMap.get(new RegionAndName(Region.US_EAST_1, "ami-1515f07c"))).andReturn(jcImage); expect(imageMap.get(new RegionAndName(Region.US_EAST_1, "ami-1515f07c"))).andReturn(jcImage);
expect(amiClient.describeImagesInRegion(Region.US_EAST_1, imageIds("ami-1515f07c"))).andReturn( expect(amiClient.describeImagesInRegion(Region.US_EAST_1, imageIds("ami-1515f07c"))).andReturn(
(Set) ImmutableSet.<Image> of(image)); (Set) ImmutableSet.<Image> of(image));
expect(credentialProvider.execute(image)).andReturn(new Credentials("user", "pass")); expect(credentialProvider.execute(image)).andReturn(new Credentials("user", "pass"));
@ -316,7 +313,7 @@ public class RunningInstanceToNodeMetadataTest {
replay(jcImage); replay(jcImage);
RunningInstanceToNodeMetadata parser = new RunningInstanceToNodeMetadata(client, credentialsMap, RunningInstanceToNodeMetadata parser = new RunningInstanceToNodeMetadata(client, credentialsMap,
credentialProvider, imageMap, locations, hardwares); credentialProvider, imageMap, locations, hardwares);
NodeMetadata metadata = parser.apply(instance); NodeMetadata metadata = parser.apply(instance);
@ -337,7 +334,7 @@ public class RunningInstanceToNodeMetadataTest {
} }
@SuppressWarnings({ "unchecked", "rawtypes" }) @SuppressWarnings( { "unchecked" })
@Test @Test
public void testApplyForNovaWhereImageNotFound() throws UnknownHostException { public void testApplyForNovaWhereImageNotFound() throws UnknownHostException {
EC2Client client = createMock(EC2Client.class); EC2Client client = createMock(EC2Client.class);
@ -346,7 +343,7 @@ public class RunningInstanceToNodeMetadataTest {
Map<RegionAndName, KeyPair> credentialsMap = createMock(Map.class); Map<RegionAndName, KeyPair> credentialsMap = createMock(Map.class);
ConcurrentMap<RegionAndName, org.jclouds.compute.domain.Image> imageMap = createMock(ConcurrentMap.class); ConcurrentMap<RegionAndName, org.jclouds.compute.domain.Image> imageMap = createMock(ConcurrentMap.class);
Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet
.<Hardware> of(EC2Hardware.M1_SMALL)); .<Hardware> of(EC2Hardware.M1_SMALL));
PopulateDefaultLoginCredentialsForImageStrategy credentialProvider = createMock(PopulateDefaultLoginCredentialsForImageStrategy.class); PopulateDefaultLoginCredentialsForImageStrategy credentialProvider = createMock(PopulateDefaultLoginCredentialsForImageStrategy.class);
RunningInstance instance = createMock(RunningInstance.class); RunningInstance instance = createMock(RunningInstance.class);
@ -357,7 +354,7 @@ public class RunningInstanceToNodeMetadataTest {
Location region = new LocationImpl(LocationScope.REGION, "us-east-1", "description", null); Location region = new LocationImpl(LocationScope.REGION, "us-east-1", "description", null);
Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.<Location> of(region)); .<Location> of(region));
org.jclouds.compute.domain.Image jcImage = createMock(org.jclouds.compute.domain.Image.class); org.jclouds.compute.domain.Image jcImage = createMock(org.jclouds.compute.domain.Image.class);
expect(instance.getIpAddress()).andReturn(null); expect(instance.getIpAddress()).andReturn(null);
@ -371,7 +368,7 @@ public class RunningInstanceToNodeMetadataTest {
expect(imageMap.get(new RegionAndName(Region.US_EAST_1, "ami-1515f07c"))).andReturn(jcImage); expect(imageMap.get(new RegionAndName(Region.US_EAST_1, "ami-1515f07c"))).andReturn(jcImage);
expect(amiClient.describeImagesInRegion(Region.US_EAST_1, imageIds("ami-1515f07c"))).andReturn( expect(amiClient.describeImagesInRegion(Region.US_EAST_1, imageIds("ami-1515f07c"))).andReturn(
(Set) ImmutableSet.<Image> of()); (Set) ImmutableSet.<Image> of());
expect(credentialProvider.execute(null)).andReturn(new Credentials("root", null)); expect(credentialProvider.execute(null)).andReturn(new Credentials("root", null));
@ -392,7 +389,7 @@ public class RunningInstanceToNodeMetadataTest {
replay(jcImage); replay(jcImage);
RunningInstanceToNodeMetadata parser = new RunningInstanceToNodeMetadata(client, credentialsMap, RunningInstanceToNodeMetadata parser = new RunningInstanceToNodeMetadata(client, credentialsMap,
credentialProvider, imageMap, locations, hardwares); credentialProvider, imageMap, locations, hardwares);
NodeMetadata metadata = parser.apply(instance); NodeMetadata metadata = parser.apply(instance);
@ -404,9 +401,9 @@ public class RunningInstanceToNodeMetadataTest {
assertEquals(metadata.getHardware().getProviderId(), "m1.small"); assertEquals(metadata.getHardware().getProviderId(), "m1.small");
assertEquals(metadata.getHardware().getProcessors(), ImmutableList.<Processor> of(new Processor(1.0, 1.0))); assertEquals(metadata.getHardware().getProcessors(), ImmutableList.<Processor> of(new Processor(1.0, 1.0)));
assertEquals(metadata.getHardware().getRam(), 1740); assertEquals(metadata.getHardware().getRam(), 1740);
assertEquals(metadata.getHardware().getVolumes(), assertEquals(metadata.getHardware().getVolumes(), ImmutableList.<Volume> of(new VolumeImpl(null,
ImmutableList.<Volume> of(new VolumeImpl(null, Volume.Type.LOCAL, 10.0f, "/dev/sda1", true, false),// Volume.Type.LOCAL, 10.0f, "/dev/sda1", true, false),//
new VolumeImpl(null, Volume.Type.LOCAL, 150.0f, "/dev/sda2", false, false))); new VolumeImpl(null, Volume.Type.LOCAL, 150.0f, "/dev/sda2", false, false)));
assertEquals(metadata.getCredentials(), new Credentials("root", null)); assertEquals(metadata.getCredentials(), new Credentials("root", null));
@ -433,9 +430,9 @@ public class RunningInstanceToNodeMetadataTest {
Location location = new LocationImpl(LocationScope.ZONE, "us-east-1a", "description", null); Location location = new LocationImpl(LocationScope.ZONE, "us-east-1a", "description", null);
Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.<Location> of(location)); .<Location> of(location));
Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet
.<Hardware> of(EC2Hardware.M2_4XLARGE)); .<Hardware> of(EC2Hardware.M2_4XLARGE));
PopulateDefaultLoginCredentialsForImageStrategy credentialProvider = createMock(PopulateDefaultLoginCredentialsForImageStrategy.class); PopulateDefaultLoginCredentialsForImageStrategy credentialProvider = createMock(PopulateDefaultLoginCredentialsForImageStrategy.class);
RunningInstance instance = createMock(RunningInstance.class); RunningInstance instance = createMock(RunningInstance.class);
@ -465,7 +462,7 @@ public class RunningInstanceToNodeMetadataTest {
replay(instance); replay(instance);
RunningInstanceToNodeMetadata parser = new RunningInstanceToNodeMetadata(client, credentialsMap, RunningInstanceToNodeMetadata parser = new RunningInstanceToNodeMetadata(client, credentialsMap,
credentialProvider, imageMap, locations, hardwares); credentialProvider, imageMap, locations, hardwares);
NodeMetadata metadata = parser.apply(instance); NodeMetadata metadata = parser.apply(instance);
assertEquals(metadata.getLocation(), locations.get().iterator().next()); assertEquals(metadata.getLocation(), locations.get().iterator().next());
@ -494,9 +491,9 @@ public class RunningInstanceToNodeMetadataTest {
Location location = new LocationImpl(LocationScope.ZONE, "us-east-1a", "description", null); Location location = new LocationImpl(LocationScope.ZONE, "us-east-1a", "description", null);
Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.<Location> of(location)); .<Location> of(location));
Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet
.<Hardware> of(EC2Hardware.M2_4XLARGE)); .<Hardware> of(EC2Hardware.M2_4XLARGE));
PopulateDefaultLoginCredentialsForImageStrategy credentialProvider = createMock(PopulateDefaultLoginCredentialsForImageStrategy.class); PopulateDefaultLoginCredentialsForImageStrategy credentialProvider = createMock(PopulateDefaultLoginCredentialsForImageStrategy.class);
RunningInstance instance = createMock(RunningInstance.class); RunningInstance instance = createMock(RunningInstance.class);
@ -514,7 +511,7 @@ public class RunningInstanceToNodeMetadataTest {
expect(instance.getRegion()).andReturn("us-east-1").atLeastOnce(); expect(instance.getRegion()).andReturn("us-east-1").atLeastOnce();
expect(imageMap.get(new RegionAndName("us-east-1", "imageId"))).andThrow(new NullPointerException()) expect(imageMap.get(new RegionAndName("us-east-1", "imageId"))).andThrow(new NullPointerException())
.atLeastOnce(); .atLeastOnce();
expect(instance.getInstanceType()).andReturn(InstanceType.C1_XLARGE).atLeastOnce(); expect(instance.getInstanceType()).andReturn(InstanceType.C1_XLARGE).atLeastOnce();
@ -527,7 +524,7 @@ public class RunningInstanceToNodeMetadataTest {
replay(instance); replay(instance);
RunningInstanceToNodeMetadata parser = new RunningInstanceToNodeMetadata(client, credentialsMap, RunningInstanceToNodeMetadata parser = new RunningInstanceToNodeMetadata(client, credentialsMap,
credentialProvider, imageMap, locations, hardwares); credentialProvider, imageMap, locations, hardwares);
NodeMetadata metadata = parser.apply(instance); NodeMetadata metadata = parser.apply(instance);
assertEquals(metadata.getLocation(), locations.get().iterator().next()); assertEquals(metadata.getLocation(), locations.get().iterator().next());
@ -556,9 +553,9 @@ public class RunningInstanceToNodeMetadataTest {
Location location = new LocationImpl(LocationScope.ZONE, "us-east-1a", "description", null); Location location = new LocationImpl(LocationScope.ZONE, "us-east-1a", "description", null);
Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.<Location> of(location)); .<Location> of(location));
Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet
.<Hardware> of(EC2Hardware.M2_4XLARGE)); .<Hardware> of(EC2Hardware.M2_4XLARGE));
PopulateDefaultLoginCredentialsForImageStrategy credentialProvider = createMock(PopulateDefaultLoginCredentialsForImageStrategy.class); PopulateDefaultLoginCredentialsForImageStrategy credentialProvider = createMock(PopulateDefaultLoginCredentialsForImageStrategy.class);
RunningInstance instance = createMock(RunningInstance.class); RunningInstance instance = createMock(RunningInstance.class);
@ -593,7 +590,7 @@ public class RunningInstanceToNodeMetadataTest {
replay(instance); replay(instance);
RunningInstanceToNodeMetadata parser = new RunningInstanceToNodeMetadata(client, credentialsMap, RunningInstanceToNodeMetadata parser = new RunningInstanceToNodeMetadata(client, credentialsMap,
credentialProvider, imageMap, locations, hardwares); credentialProvider, imageMap, locations, hardwares);
NodeMetadata metadata = parser.apply(instance); NodeMetadata metadata = parser.apply(instance);
assertEquals(metadata.getLocation(), locations.get().iterator().next()); assertEquals(metadata.getLocation(), locations.get().iterator().next());
@ -623,9 +620,9 @@ public class RunningInstanceToNodeMetadataTest {
Location location = new LocationImpl(LocationScope.ZONE, "us-east-1a", "description", null); Location location = new LocationImpl(LocationScope.ZONE, "us-east-1a", "description", null);
Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.<Location> of(location)); .<Location> of(location));
Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet
.<Hardware> of(EC2Hardware.M2_4XLARGE)); .<Hardware> of(EC2Hardware.M2_4XLARGE));
PopulateDefaultLoginCredentialsForImageStrategy credentialProvider = createMock(PopulateDefaultLoginCredentialsForImageStrategy.class); PopulateDefaultLoginCredentialsForImageStrategy credentialProvider = createMock(PopulateDefaultLoginCredentialsForImageStrategy.class);
RunningInstance instance = createMock(RunningInstance.class); RunningInstance instance = createMock(RunningInstance.class);
@ -656,7 +653,7 @@ public class RunningInstanceToNodeMetadataTest {
replay(instance); replay(instance);
RunningInstanceToNodeMetadata parser = new RunningInstanceToNodeMetadata(client, credentialsMap, RunningInstanceToNodeMetadata parser = new RunningInstanceToNodeMetadata(client, credentialsMap,
credentialProvider, imageMap, locations, hardwares); credentialProvider, imageMap, locations, hardwares);
NodeMetadata metadata = parser.apply(instance); NodeMetadata metadata = parser.apply(instance);
assertEquals(metadata.getLocation(), locations.get().iterator().next()); assertEquals(metadata.getLocation(), locations.get().iterator().next());
@ -684,9 +681,9 @@ public class RunningInstanceToNodeMetadataTest {
Location location = new LocationImpl(LocationScope.ZONE, "us-east-1a", "description", null); Location location = new LocationImpl(LocationScope.ZONE, "us-east-1a", "description", null);
Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.<Location> of(location)); .<Location> of(location));
Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet
.<Hardware> of(EC2Hardware.M2_4XLARGE)); .<Hardware> of(EC2Hardware.M2_4XLARGE));
PopulateDefaultLoginCredentialsForImageStrategy credentialProvider = createMock(PopulateDefaultLoginCredentialsForImageStrategy.class); PopulateDefaultLoginCredentialsForImageStrategy credentialProvider = createMock(PopulateDefaultLoginCredentialsForImageStrategy.class);
RunningInstance instance = createMock(RunningInstance.class); RunningInstance instance = createMock(RunningInstance.class);
@ -717,7 +714,7 @@ public class RunningInstanceToNodeMetadataTest {
replay(instance); replay(instance);
RunningInstanceToNodeMetadata parser = new RunningInstanceToNodeMetadata(client, credentialsMap, RunningInstanceToNodeMetadata parser = new RunningInstanceToNodeMetadata(client, credentialsMap,
credentialProvider, imageMap, locations, hardwares); credentialProvider, imageMap, locations, hardwares);
NodeMetadata metadata = parser.apply(instance); NodeMetadata metadata = parser.apply(instance);
assertEquals(metadata.getLocation(), locations.get().iterator().next()); assertEquals(metadata.getLocation(), locations.get().iterator().next());
@ -734,7 +731,7 @@ public class RunningInstanceToNodeMetadataTest {
verify(instance); verify(instance);
} }
@SuppressWarnings({ "unchecked", "rawtypes" }) @SuppressWarnings( { "unchecked" })
@Test @Test
public void testApplyWithKeyPairCreatesTagOfParsedSecurityGroupAndCredentialsBasedOnIt() throws UnknownHostException { public void testApplyWithKeyPairCreatesTagOfParsedSecurityGroupAndCredentialsBasedOnIt() throws UnknownHostException {
EC2Client client = createMock(EC2Client.class); EC2Client client = createMock(EC2Client.class);
@ -743,7 +740,7 @@ public class RunningInstanceToNodeMetadataTest {
Map<RegionAndName, KeyPair> credentialsMap = createMock(Map.class); Map<RegionAndName, KeyPair> credentialsMap = createMock(Map.class);
ConcurrentMap<RegionAndName, org.jclouds.compute.domain.Image> imageMap = createMock(ConcurrentMap.class); ConcurrentMap<RegionAndName, org.jclouds.compute.domain.Image> imageMap = createMock(ConcurrentMap.class);
Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet
.<Hardware> of(EC2Hardware.M2_4XLARGE)); .<Hardware> of(EC2Hardware.M2_4XLARGE));
PopulateDefaultLoginCredentialsForImageStrategy credentialProvider = createMock(PopulateDefaultLoginCredentialsForImageStrategy.class); PopulateDefaultLoginCredentialsForImageStrategy credentialProvider = createMock(PopulateDefaultLoginCredentialsForImageStrategy.class);
RunningInstance instance = createMock(RunningInstance.class); RunningInstance instance = createMock(RunningInstance.class);
Image image = createMock(Image.class); Image image = createMock(Image.class);
@ -755,7 +752,7 @@ public class RunningInstanceToNodeMetadataTest {
Location location = new LocationImpl(LocationScope.ZONE, "us-east-1a", "description", null); Location location = new LocationImpl(LocationScope.ZONE, "us-east-1a", "description", null);
Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.<Location> of(location)); .<Location> of(location));
org.jclouds.compute.domain.Image jcImage = createMock(org.jclouds.compute.domain.Image.class); org.jclouds.compute.domain.Image jcImage = createMock(org.jclouds.compute.domain.Image.class);
expect(instance.getIpAddress()).andReturn("127.0.0.1"); expect(instance.getIpAddress()).andReturn("127.0.0.1");
@ -769,12 +766,12 @@ public class RunningInstanceToNodeMetadataTest {
expect(imageMap.get(new RegionAndName(Region.US_EAST_1, "imageId"))).andReturn(jcImage); expect(imageMap.get(new RegionAndName(Region.US_EAST_1, "imageId"))).andReturn(jcImage);
expect(amiClient.describeImagesInRegion(Region.US_EAST_1, imageIds("imageId"))).andReturn( expect(amiClient.describeImagesInRegion(Region.US_EAST_1, imageIds("imageId"))).andReturn(
(Set) ImmutableSet.<Image> of(image)); (Set) ImmutableSet.<Image> of(image));
expect(credentialProvider.execute(image)).andReturn(new Credentials("user", "pass")); expect(credentialProvider.execute(image)).andReturn(new Credentials("user", "pass"));
expect(credentialsMap.get(new RegionAndName(Region.US_EAST_1, "jclouds#tag#us-east-1#50"))).andReturn( expect(credentialsMap.get(new RegionAndName(Region.US_EAST_1, "jclouds#tag#us-east-1#50"))).andReturn(
new KeyPair(Region.US_EAST_1, "jclouds#tag#us-east-1#50", "keyFingerprint", "pass")); new KeyPair(Region.US_EAST_1, "jclouds#tag#us-east-1#50", "keyFingerprint", "pass"));
expect(instance.getAvailabilityZone()).andReturn(AvailabilityZone.US_EAST_1A).atLeastOnce(); expect(instance.getAvailabilityZone()).andReturn(AvailabilityZone.US_EAST_1A).atLeastOnce();
@ -789,7 +786,7 @@ public class RunningInstanceToNodeMetadataTest {
replay(jcImage); replay(jcImage);
RunningInstanceToNodeMetadata parser = new RunningInstanceToNodeMetadata(client, credentialsMap, RunningInstanceToNodeMetadata parser = new RunningInstanceToNodeMetadata(client, credentialsMap,
credentialProvider, imageMap, locations, hardwares); credentialProvider, imageMap, locations, hardwares);
NodeMetadata metadata = parser.apply(instance); NodeMetadata metadata = parser.apply(instance);
assertEquals(metadata.getTag(), "tag"); assertEquals(metadata.getTag(), "tag");
@ -808,7 +805,7 @@ public class RunningInstanceToNodeMetadataTest {
} }
@SuppressWarnings({ "unchecked", "rawtypes" }) @SuppressWarnings( { "unchecked" })
@Test @Test
public void testApplyWithTwoSecurityGroups() throws UnknownHostException { public void testApplyWithTwoSecurityGroups() throws UnknownHostException {
EC2Client client = createMock(EC2Client.class); EC2Client client = createMock(EC2Client.class);
@ -817,7 +814,7 @@ public class RunningInstanceToNodeMetadataTest {
Map<RegionAndName, KeyPair> credentialsMap = createMock(Map.class); Map<RegionAndName, KeyPair> credentialsMap = createMock(Map.class);
ConcurrentMap<RegionAndName, org.jclouds.compute.domain.Image> imageMap = createMock(ConcurrentMap.class); ConcurrentMap<RegionAndName, org.jclouds.compute.domain.Image> imageMap = createMock(ConcurrentMap.class);
Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet
.<Hardware> of(EC2Hardware.M2_4XLARGE)); .<Hardware> of(EC2Hardware.M2_4XLARGE));
PopulateDefaultLoginCredentialsForImageStrategy credentialProvider = createMock(PopulateDefaultLoginCredentialsForImageStrategy.class); PopulateDefaultLoginCredentialsForImageStrategy credentialProvider = createMock(PopulateDefaultLoginCredentialsForImageStrategy.class);
RunningInstance instance = createMock(RunningInstance.class); RunningInstance instance = createMock(RunningInstance.class);
Image image = createMock(Image.class); Image image = createMock(Image.class);
@ -829,7 +826,7 @@ public class RunningInstanceToNodeMetadataTest {
Location location = new LocationImpl(LocationScope.ZONE, "us-east-1a", "description", null); Location location = new LocationImpl(LocationScope.ZONE, "us-east-1a", "description", null);
Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.<Location> of(location)); .<Location> of(location));
org.jclouds.compute.domain.Image jcImage = createMock(org.jclouds.compute.domain.Image.class); org.jclouds.compute.domain.Image jcImage = createMock(org.jclouds.compute.domain.Image.class);
expect(instance.getIpAddress()).andReturn("127.0.0.1"); expect(instance.getIpAddress()).andReturn("127.0.0.1");
@ -843,12 +840,12 @@ public class RunningInstanceToNodeMetadataTest {
expect(imageMap.get(new RegionAndName(Region.US_EAST_1, "imageId"))).andReturn(jcImage); expect(imageMap.get(new RegionAndName(Region.US_EAST_1, "imageId"))).andReturn(jcImage);
expect(amiClient.describeImagesInRegion(Region.US_EAST_1, imageIds("imageId"))).andReturn( expect(amiClient.describeImagesInRegion(Region.US_EAST_1, imageIds("imageId"))).andReturn(
(Set) ImmutableSet.<Image> of(image)); (Set) ImmutableSet.<Image> of(image));
expect(credentialProvider.execute(image)).andReturn(new Credentials("user", "pass")); expect(credentialProvider.execute(image)).andReturn(new Credentials("user", "pass"));
expect(credentialsMap.get(new RegionAndName(Region.US_EAST_1, "jclouds#tag#us-east-1#50"))).andReturn( expect(credentialsMap.get(new RegionAndName(Region.US_EAST_1, "jclouds#tag#us-east-1#50"))).andReturn(
new KeyPair(Region.US_EAST_1, "jclouds#tag#us-east-1#50", "keyFingerprint", "pass")); new KeyPair(Region.US_EAST_1, "jclouds#tag#us-east-1#50", "keyFingerprint", "pass"));
expect(instance.getAvailabilityZone()).andReturn(AvailabilityZone.US_EAST_1A).atLeastOnce(); expect(instance.getAvailabilityZone()).andReturn(AvailabilityZone.US_EAST_1A).atLeastOnce();
@ -863,7 +860,7 @@ public class RunningInstanceToNodeMetadataTest {
replay(jcImage); replay(jcImage);
RunningInstanceToNodeMetadata parser = new RunningInstanceToNodeMetadata(client, credentialsMap, RunningInstanceToNodeMetadata parser = new RunningInstanceToNodeMetadata(client, credentialsMap,
credentialProvider, imageMap, locations, hardwares); credentialProvider, imageMap, locations, hardwares);
NodeMetadata metadata = parser.apply(instance); NodeMetadata metadata = parser.apply(instance);

View File

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

View File

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

View File

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

View File

@ -75,6 +75,12 @@ public class ParseAWSErrorFromXmlContentTest {
"<Error><Code>InvalidGroup.Duplicate</Code></Error>", IllegalStateException.class); "<Error><Code>InvalidGroup.Duplicate</Code></Error>", IllegalStateException.class);
} }
@Test
public void test400WithInvalidKeyPairGroupDuplicateIllegalStateException() {
assertCodeMakes("GET", URI.create("https://amazonaws.com/foo"), 400,"Bad Request", "application/unknown",
"<Error><Code>InvalidKeyPair.Duplicate</Code></Error>", IllegalStateException.class);
}
@Test @Test
public void test400WithTextPlainIllegalArgumentException() { public void test400WithTextPlainIllegalArgumentException() {
assertCodeMakes("GET", URI.create("https://amazonaws.com/foo"), 400, "Bad Request", "text/plain", assertCodeMakes("GET", URI.create("https://amazonaws.com/foo"), 400, "Bad Request", "text/plain",

View File

@ -160,7 +160,7 @@ public class MainApp {
.addStatement(exec("runurl run.alestic.com/apt/upgrade"))// .addStatement(exec("runurl run.alestic.com/apt/upgrade"))//
.addStatement(exec("runurl run.alestic.com/install/lamp"))// .addStatement(exec("runurl run.alestic.com/install/lamp"))//
.addStatement(exec("apt-get -y install openjdk-6-jdk"))// no license agreement! .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()); System.out.printf("%d: running instance%n", System.currentTimeMillis());
Reservation<? extends RunningInstance> reservation = client.getInstanceServices().runInstancesInRegion(null, null, // allow 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,113 @@
/**
*
* 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 java.util.Collections;
import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.util.ComputeServiceUtils;
import org.jclouds.compute.util.ComputeServiceUtils.SshCallable;
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.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 InitBuilder init;
protected final boolean runAsRoot;
protected Logger logger = Logger.NULL;
public InitAndStartScriptOnNode(NodeMetadata node, String name, Statement script, boolean runAsRoot) {
this.node = checkNotNull(node, "node");
this.init = checkNotNull(script, "script") instanceof InitBuilder ? InitBuilder.class.cast(script)
: createInitScript(checkNotNull(name, "name"), script);
this.runAsRoot = runAsRoot;
}
public static InitBuilder createInitScript(String name, Statement script) {
String path = "/tmp/" + name;
return new InitBuilder(name, path, path, Collections.<String, String> emptyMap(), Collections.singleton(script));
}
@Override
public ExecResponse call() {
ssh.put(init.getInstanceName(), init.render(OsFamily.UNIX));
ssh.exec("chmod 755 " + init.getInstanceName());
runAction("init");
return runAction("start");
}
private ExecResponse runAction(String action) {
ExecResponse returnVal;
String command = (runAsRoot) ? execScriptAsRoot(action) : execScriptAsDefaultUser(action);
returnVal = runCommand(command);
logger.debug("<< %s(%d)", action, returnVal.getExitCode());
logger.trace("<< %s[%s]", action, returnVal);
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 execScriptAsRoot(String action) {
String command;
if (node.getCredentials().identity.equals("root")) {
command = "./" + init.getInstanceName() + " " + action;
} else if (ComputeServiceUtils.isKeyAuth(node)) {
command = "sudo ./" + init.getInstanceName() + " " + action;
} else {
command = String.format("echo '%s'|sudo -S ./%s %s", node.getCredentials().credential, init.getInstanceName(),
action);
}
return command;
}
protected String execScriptAsDefaultUser(String action) {
return "./" + init.getInstanceName() + " " + action;
}
@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,130 +19,41 @@
package org.jclouds.compute.callables; 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 javax.inject.Named;
import org.jclouds.compute.domain.NodeMetadata; import org.jclouds.compute.domain.NodeMetadata;
import org.jclouds.compute.predicates.ScriptStatusReturnsZero.CommandUsingClient; 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.Statement;
import org.jclouds.scriptbuilder.domain.Statements;
import org.jclouds.ssh.ExecResponse; 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.Predicate;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
/** /**
* *
* @author Adrian Cole * @author Adrian Cole
*/ */
public class RunScriptOnNode implements SshCallable<ExecResponse> { public class RunScriptOnNode extends InitAndStartScriptOnNode {
private SshClient ssh;
protected final Predicate<CommandUsingClient> runScriptNotRunning; 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, public RunScriptOnNode(@Named("SCRIPT_COMPLETE") Predicate<CommandUsingClient> runScriptNotRunning,
NodeMetadata node, String scriptName, Payload script) { NodeMetadata node, String scriptName, Statement script, boolean runAsRoot) {
this(runScriptNotRunning, node, scriptName, script, true); super(node, scriptName, script, runAsRoot);
}
public RunScriptOnNode(@Named("SCRIPT_COMPLETE") Predicate<CommandUsingClient> runScriptNotRunning,
NodeMetadata node, String scriptName, Payload script, boolean runAsRoot) {
this.runScriptNotRunning = runScriptNotRunning; 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) {
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);
}
}
@Override @Override
public ExecResponse call() throws Exception { public ExecResponse call() {
ssh.put(scriptName, script); ExecResponse returnVal = super.call();
ExecResponse returnVal = ssh.exec("chmod 755 " + scriptName); boolean complete = runScriptNotRunning.apply(new CommandUsingClient("./" + init.getInstanceName() + " status", ssh));
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());
boolean complete = runScriptNotRunning.apply(new CommandUsingClient("./" + scriptName + " status", ssh));
logger.debug("<< complete(%s)", complete); logger.debug("<< complete(%s)", complete);
if (logger.isDebugEnabled() || returnVal.getExitCode() != 0) { if (logger.isDebugEnabled() || returnVal.getExitCode() != 0) {
logger.debug("<< stdout from %s as %s@%s\n%s", scriptName, node.getCredentials().identity, Iterables.get(node logger.debug("<< stdout from %s as %s@%s\n%s", init.getInstanceName(), node.getCredentials().identity, Iterables.get(node
.getPublicAddresses(), 0), ssh.exec("./" + scriptName + " tail").getOutput()); .getPublicAddresses(), 0), ssh.exec("./" + init.getInstanceName() + " tail").getOutput());
logger.debug("<< stderr from %s as %s@%s\n%s", scriptName, node.getCredentials().identity, Iterables.get(node logger.debug("<< stderr from %s as %s@%s\n%s", init.getInstanceName(), node.getCredentials().identity, Iterables.get(node
.getPublicAddresses(), 0), ssh.exec("./" + scriptName + " tailerr").getOutput()); .getPublicAddresses(), 0), ssh.exec("./" + init.getInstanceName() + " tailerr").getOutput());
} }
return returnVal; 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

@ -320,7 +320,7 @@ public class TemplateBuilderImpl implements TemplateBuilder {
public boolean apply(Image input) { public boolean apply(Image input) {
boolean returnVal = true; boolean returnVal = true;
if (imageDescription != null) { if (imageDescription != null) {
if (input.getName() == null) if (input.getDescription() == null)
returnVal = false; returnVal = false;
else else
returnVal = input.getDescription().equals(imageDescription) returnVal = input.getDescription().equals(imageDescription)
@ -815,6 +815,12 @@ public class TemplateBuilderImpl implements TemplateBuilder {
to.withMetadata(); to.withMetadata();
if (!from.shouldBlockUntilRunning()) if (!from.shouldBlockUntilRunning())
to.blockUntilRunning(false); to.blockUntilRunning(false);
if (!from.shouldBlockOnComplete())
to.blockOnComplete(false);
if (from.getOverrideCredentials() != null)
to.withOverridingCredentials(from.getOverrideCredentials());
if (from.getTaskName() != null)
to.nameTask(from.getTaskName());
} }
@VisibleForTesting @VisibleForTesting

View File

@ -45,7 +45,6 @@ import org.jclouds.compute.ComputeService;
import org.jclouds.compute.ComputeServiceContext; import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.RunNodesException; import org.jclouds.compute.RunNodesException;
import org.jclouds.compute.RunScriptOnNodesException; import org.jclouds.compute.RunScriptOnNodesException;
import org.jclouds.compute.callables.RunScriptOnNode;
import org.jclouds.compute.domain.ComputeMetadata; import org.jclouds.compute.domain.ComputeMetadata;
import org.jclouds.compute.domain.Hardware; import org.jclouds.compute.domain.Hardware;
import org.jclouds.compute.domain.Image; import org.jclouds.compute.domain.Image;
@ -63,12 +62,15 @@ import org.jclouds.compute.strategy.ListNodesStrategy;
import org.jclouds.compute.strategy.RebootNodeStrategy; import org.jclouds.compute.strategy.RebootNodeStrategy;
import org.jclouds.compute.strategy.RunNodesAndAddToSetStrategy; import org.jclouds.compute.strategy.RunNodesAndAddToSetStrategy;
import org.jclouds.compute.util.ComputeUtils; import org.jclouds.compute.util.ComputeUtils;
import org.jclouds.domain.Credentials;
import org.jclouds.domain.Location; import org.jclouds.domain.Location;
import org.jclouds.io.Payload; import org.jclouds.io.Payload;
import org.jclouds.logging.Logger; import org.jclouds.logging.Logger;
import org.jclouds.predicates.RetryablePredicate; import org.jclouds.predicates.RetryablePredicate;
import org.jclouds.scriptbuilder.InitBuilder;
import org.jclouds.scriptbuilder.domain.Statements;
import org.jclouds.ssh.ExecResponse; 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.Function;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
@ -151,6 +153,9 @@ public class BaseComputeService implements ComputeService {
throws RunNodesException { throws RunNodesException {
checkArgument(tag.indexOf('-') == -1, "tag cannot contain hyphens"); checkArgument(tag.indexOf('-') == -1, "tag cannot contain hyphens");
checkNotNull(template.getLocation(), "location"); checkNotNull(template.getLocation(), "location");
if (template.getOptions().getTaskName() == null && template.getOptions().getRunScript() != null
&& !(template.getOptions().getRunScript() instanceof InitBuilder))
template.getOptions().nameTask("bootstrap");
logger.debug(">> running %d node%s tag(%s) location(%s) image(%s) hardwareProfile(%s) options(%s)", count, 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 count > 1 ? "s" : "", tag, template.getLocation().getId(), template.getImage().getId(), template
.getHardware().getId(), template.getOptions()); .getHardware().getId(), template.getOptions());
@ -350,14 +355,19 @@ public class BaseComputeService implements ComputeService {
@Override @Override
public Map<NodeMetadata, ExecResponse> runScriptOnNodesMatching(Predicate<NodeMetadata> filter, public Map<NodeMetadata, ExecResponse> runScriptOnNodesMatching(Predicate<NodeMetadata> filter,
final Payload runScript, @Nullable final RunScriptOptions options) throws RunScriptOnNodesException { 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, ExecResponse> execs = Maps.newHashMap();
final Map<NodeMetadata, Future<Void>> responses = Maps.newHashMap();
final Map<NodeMetadata, Exception> badNodes = Maps.newLinkedHashMap(); final Map<NodeMetadata, Exception> badNodes = Maps.newLinkedHashMap();
nodes = filterNodesWhoCanRunScripts(nodes, badNodes, options.getOverrideCredentials());
Map<NodeMetadata, Future<Void>> responses = Maps.newHashMap();
for (final NodeMetadata node : nodes) { for (final NodeMetadata node : nodes) {
@ -365,66 +375,53 @@ public class BaseComputeService implements ComputeService {
@Override @Override
public Void call() throws Exception { public Void call() throws Exception {
try { try {
RunScriptOnNode callable; ExecResponse response = utils.runScriptOnNode(node, Statements.exec(Utils.toStringAndClose(runScript
if (options.isRunAsRoot()) .getInput())), options);
callable = utils.runScriptOnNode(node, "computeserv", runScript); if (response != null)
else execs.put(node, response);
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();
}
} catch (Exception e) { } catch (Exception e) {
badNodes.put(node, e); badNodes.put(node, e);
} }
return null; 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) { if (exceptions.size() > 0 || badNodes.size() > 0) {
throw new RunScriptOnNodesException(runScript, options, execs, exceptions, badNodes); throw new RunScriptOnNodesException(runScript, options, execs, exceptions, badNodes);
} }
return execs; return execs;
} }
private Iterable<NodeMetadata> verifyParametersAndListNodes(Predicate<NodeMetadata> filter, Payload runScript, private Iterable<? extends NodeMetadata> filterNodesWhoCanRunScripts(Iterable<? extends NodeMetadata> nodes,
final RunScriptOptions options) { final Map<NodeMetadata, Exception> badNodes, final @Nullable Credentials overridingCredentials) {
checkNotNull(filter, "Filter must be provided"); nodes = Iterables.filter(Iterables.transform(nodes, new Function<NodeMetadata, NodeMetadata>() {
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>() {
@Override @Override
public NodeMetadata apply(NodeMetadata node) { public NodeMetadata apply(NodeMetadata node) {
try {
checkArgument(node.getPublicAddresses().size() > 0, "no public ip addresses on node: " + node); checkArgument(node.getPublicAddresses().size() > 0, "no public ip addresses on node: " + node);
if (options.getOverrideCredentials() != null) { if (overridingCredentials != null) {
// override the credentials with provided to this node = installNewCredentials(node, overridingCredentials);
// method } else {
node = installNewCredentials(node, options.getOverrideCredentials()); checkNotNull(node.getCredentials(), "If the default credentials need to be used, they can't be null");
} else { checkNotNull(node.getCredentials().identity, "Account name for ssh authentication must be "
// don't override + "specified. Try passing RunScriptOptions with new credentials");
checkNotNull(node.getCredentials(), "If the default credentials need to be used, they can't be null"); checkNotNull(node.getCredentials().credential, "Key or password for ssh authentication must be "
checkNotNull(node.getCredentials().identity, "Account name for ssh authentication must be " + "specified. Try passing RunScriptOptions with new credentials");
+ "specified. Try passing RunScriptOptions with new credentials"); }
checkNotNull(node.getCredentials().credential, "Key or password for ssh authentication must be " return node;
+ "specified. Try passing RunScriptOptions with new credentials"); } catch (Exception e) {
badNodes.put(node, e);
return null;
} }
return node;
} }
}); }), Predicates.notNull());
return nodes;
} }
private Set<? extends NodeMetadata> detailsOnAllNodes() { 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 RestContext<S, A> providerSpecificContext;
private final Utils utils; private final Utils utils;
@SuppressWarnings({ "unchecked", "rawtypes" }) @SuppressWarnings({ "unchecked" })
@Inject @Inject
public ComputeServiceContextImpl(ComputeService computeService, Utils utils, public ComputeServiceContextImpl(ComputeService computeService, Utils utils,
@Nullable LoadBalancerService loadBalancerService, RestContext providerSpecificContext) { @Nullable LoadBalancerService loadBalancerService, RestContext providerSpecificContext) {

View File

@ -19,6 +19,7 @@
package org.jclouds.compute.options; package org.jclouds.compute.options;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import org.jclouds.domain.Credentials; import org.jclouds.domain.Credentials;
@ -59,8 +60,8 @@ public class RunScriptOptions {
} }
@Override @Override
public boolean isRunAsRoot() { public boolean shouldRunAsRoot() {
return delegate.isRunAsRoot(); return delegate.shouldRunAsRoot();
} }
@ -69,15 +70,54 @@ public class RunScriptOptions {
throw new IllegalArgumentException("runAsRoot is immutable"); throw new IllegalArgumentException("runAsRoot is immutable");
} }
@Override
public boolean shouldBlockOnComplete() {
return delegate.shouldBlockOnComplete();
}
@Override
public RunScriptOptions blockOnComplete(boolean blockOnComplete) {
throw new IllegalArgumentException("blockOnComplete is immutable");
}
@Override @Override
public RunScriptOptions withOverridingCredentials(Credentials overridingCredentials) { public RunScriptOptions withOverridingCredentials(Credentials overridingCredentials) {
throw new IllegalArgumentException("overridingCredentials is immutable"); throw new IllegalArgumentException("overridingCredentials is immutable");
} }
@Override
public String getTaskName() {
return delegate.getTaskName();
}
@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();
}
} }
private Credentials overridingCredentials; protected int port = -1;
private boolean runAsRoot = true; protected int seconds = -1;
protected String taskName;
protected Credentials overridingCredentials;
protected boolean runAsRoot = true;
protected boolean blockOnComplete = true;
public RunScriptOptions withOverridingCredentials(Credentials overridingCredentials) { public RunScriptOptions withOverridingCredentials(Credentials overridingCredentials) {
checkNotNull(overridingCredentials, "overridingCredentials"); checkNotNull(overridingCredentials, "overridingCredentials");
@ -87,11 +127,49 @@ public class RunScriptOptions {
return this; 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) { public RunScriptOptions runAsRoot(boolean runAsRoot) {
this.runAsRoot = runAsRoot; this.runAsRoot = runAsRoot;
return this; return this;
} }
public RunScriptOptions blockOnComplete(boolean blockOnComplete) {
this.blockOnComplete = blockOnComplete;
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 * Whether to override the credentials with ones supplied in call to
* {@link org.jclouds.compute.ComputeService#runScriptOnNodesWithTag}. By default, true. * {@link org.jclouds.compute.ComputeService#runScriptOnNodesWithTag}. By default, true.
@ -107,12 +185,26 @@ public class RunScriptOptions {
* *
* @return value * @return value
*/ */
public boolean isRunAsRoot() { public boolean shouldRunAsRoot() {
return runAsRoot; return runAsRoot;
} }
/**
* Whether to wait until the script has completed. By default, true.
*
* @return value
*/
public boolean shouldBlockOnComplete() {
return blockOnComplete;
}
public static class Builder { public static class Builder {
public static RunScriptOptions nameTask(String name) {
RunScriptOptions options = new RunScriptOptions();
return options.nameTask(name);
}
public static RunScriptOptions overrideCredentialsWith(Credentials credentials) { public static RunScriptOptions overrideCredentialsWith(Credentials credentials) {
RunScriptOptions options = new RunScriptOptions(); RunScriptOptions options = new RunScriptOptions();
return options.withOverridingCredentials(credentials); return options.withOverridingCredentials(credentials);
@ -123,12 +215,22 @@ public class RunScriptOptions {
return options.runAsRoot(value); return options.runAsRoot(value);
} }
public static RunScriptOptions blockOnComplete(boolean value) {
RunScriptOptions options = new RunScriptOptions();
return options.blockOnComplete(value);
}
public static RunScriptOptions blockOnPort(int port, int seconds) {
RunScriptOptions options = new RunScriptOptions();
return options.blockOnPort(port, seconds);
}
} }
@Override @Override
public String toString() { public String toString() {
return "RunScriptOptions [overridingCredentials=" + (overridingCredentials != null) return "[overridingCredentials=" + (overridingCredentials != null) + ", port:seconds=" + port + ":" + seconds
+ ", runAsRoot=" + runAsRoot + "]"; + ", runAsRoot=" + runAsRoot + ", blockOnComplete=" + blockOnComplete + "]";
} }
} }

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.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull; 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 java.util.Arrays;
import javax.annotation.Nullable; import org.jclouds.domain.Credentials;
import org.jclouds.compute.domain.OperatingSystem;
import org.jclouds.compute.predicates.OperatingSystemPredicates;
import org.jclouds.io.Payload; import org.jclouds.io.Payload;
import org.jclouds.scriptbuilder.domain.OsFamily;
import org.jclouds.scriptbuilder.domain.Statement; 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> * Contains options supported in the {@code ComputeService#runNodesWithTag} operation. <h2>
@ -50,7 +49,7 @@ import org.jclouds.scriptbuilder.domain.Statement;
* *
* @author Adrian Cole * @author Adrian Cole
*/ */
public class TemplateOptions { public class TemplateOptions extends RunScriptOptions {
public static final TemplateOptions NONE = new ImmutableTemplateOptions(new TemplateOptions()); public static final TemplateOptions NONE = new ImmutableTemplateOptions(new TemplateOptions());
@ -81,41 +80,26 @@ public class TemplateOptions {
throw new IllegalArgumentException("blockUntilRunning is immutable"); throw new IllegalArgumentException("blockUntilRunning is immutable");
} }
@Override
public TemplateOptions blockOnPort(int port, int seconds) {
throw new IllegalArgumentException("port, seconds are immutable");
}
@Override @Override
public int[] getInboundPorts() { public int[] getInboundPorts() {
return delegate.getInboundPorts(); return delegate.getInboundPorts();
} }
@Override @Override
public int getPort() { public String getPrivateKey() {
return delegate.getPort();
}
@Override
public Payload getPrivateKey() {
return delegate.getPrivateKey(); return delegate.getPrivateKey();
} }
@Override @Override
public Payload getPublicKey() { public String getPublicKey() {
return delegate.getPublicKey(); return delegate.getPublicKey();
} }
@Override @Override
public Payload getRunScript() { public Statement getRunScript() {
return delegate.getRunScript(); return delegate.getRunScript();
} }
@Override
public int getSeconds() {
return delegate.getSeconds();
}
@Override @Override
public boolean shouldBlockUntilRunning() { public boolean shouldBlockUntilRunning() {
return delegate.shouldBlockUntilRunning(); return delegate.shouldBlockUntilRunning();
@ -150,41 +134,29 @@ public class TemplateOptions {
protected int[] inboundPorts = new int[] { 22 }; protected int[] inboundPorts = new int[] { 22 };
protected Payload script; protected Statement script;
protected Payload privateKey; protected String privateKey;
protected Payload publicKey; protected String publicKey;
protected int port = -1;
protected int seconds = -1;
protected boolean includeMetadata; protected boolean includeMetadata;
protected boolean blockUntilRunning = true; protected boolean blockUntilRunning = true;
public int getPort() {
return port;
}
public int getSeconds() {
return seconds;
}
public int[] getInboundPorts() { public int[] getInboundPorts() {
return inboundPorts; return inboundPorts;
} }
public Payload getRunScript() { public Statement getRunScript() {
return script; return script;
} }
public Payload getPrivateKey() { public String getPrivateKey() {
return privateKey; return privateKey;
} }
public Payload getPublicKey() { public String getPublicKey() {
return publicKey; return publicKey;
} }
@ -200,28 +172,17 @@ public class TemplateOptions {
return clazz.cast(this); 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 * 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 * prologue, so no #!/bin/bash required, path set up, etc
* <p/> * <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 * @see org.jclouds.io.Payloads
*/ */
@Deprecated @Deprecated
public TemplateOptions runScript(byte[] script) { public TemplateOptions runScript(byte[] script) {
return runScript(newByteArrayPayload(checkNotNull(script, "script"))); return runScript(Statements.exec(new String(checkNotNull(script, "script"))));
} }
/** /**
@ -231,44 +192,44 @@ public class TemplateOptions {
* @see org.jclouds.io.Payloads * @see org.jclouds.io.Payloads
*/ */
public TemplateOptions runScript(Payload script) { public TemplateOptions runScript(Payload script) {
checkNotNull(script, "script"); try {
this.script = script; return runScript(Statements.exec(Utils.toStringAndClose(checkNotNull(script, "script").getInput())));
return this; } 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) { 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. * replaces the rsa ssh key used at login.
* <p/> * <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 * @see org.jclouds.io.Payloads
*/ */
@Deprecated @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) { public TemplateOptions installPrivateKey(Payload privateKey) {
this.privateKey = checkNotNull(privateKey, "privateKey"); try {
return this; return installPrivateKey(Utils.toStringAndClose(checkNotNull(privateKey, "privateKey").getInput()));
} catch (IOException e) {
Throwables.propagate(e);
return this;
}
} }
public TemplateOptions dontAuthorizePublicKey() { public TemplateOptions dontAuthorizePublicKey() {
@ -277,41 +238,29 @@ public class TemplateOptions {
} }
/** /**
* if true, return when node(s) are NODE_RUNNING, if false, return as soon as the server is * authorize an rsa ssh key.
* provisioned.
* <p/>
* default is true
*/ */
public TemplateOptions blockUntilRunning(boolean blockUntilRunning) { public TemplateOptions authorizePublicKey(String publicKey) {
this.blockUntilRunning = blockUntilRunning; checkArgument(checkNotNull(publicKey, "publicKey").startsWith("ssh-rsa"), "key should start with ssh-rsa");
if (!blockUntilRunning) this.publicKey = publicKey;
port = seconds = -1;
return this; return this;
} }
/** /**
* authorize an rsa ssh key. * authorize an rsa ssh key.
* <p/> * <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 * @see org.jclouds.io.Payloads
*/ */
@Deprecated @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) { public TemplateOptions authorizePublicKey(Payload publicKey) {
this.publicKey = checkNotNull(publicKey, "publicKey"); try {
return this; return authorizePublicKey(Utils.toStringAndClose(checkNotNull(publicKey, "publicKey").getInput()));
} catch (IOException e) {
Throwables.propagate(e);
return this;
}
} }
/** /**
@ -329,7 +278,30 @@ public class TemplateOptions {
return this; 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 * @see TemplateOptions#inboundPorts
@ -339,14 +311,6 @@ public class TemplateOptions {
return options.inboundPorts(ports); 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 * @see TemplateOptions#blockUntilRunning
*/ */
@ -432,14 +396,26 @@ public class TemplateOptions {
return options.withMetadata(); return options.withMetadata();
} }
public static TemplateOptions blockOnComplete(boolean value) {
TemplateOptions options = new TemplateOptions();
return options.blockOnComplete(value);
}
} }
@Override @Override
public String toString() { public String toString() {
return "TemplateOptions [inboundPorts=" + Arrays.toString(inboundPorts) + ", privateKey=" + (privateKey != null) return "[inboundPorts=" + Arrays.toString(inboundPorts) + ", privateKey=" + (privateKey != null) + ", publicKey="
+ ", publicKey=" + (publicKey != null) + ", runScript=" + (script != null) + ", blockUntilRunning=" + (publicKey != null) + ", runScript=" + (script != null) + ", blockUntilRunning=" + blockUntilRunning
+ blockUntilRunning + ", port:seconds=" + port + ":" + seconds + ", metadata/details: " + ", blockOnComplete=" + blockOnComplete + ", port:seconds=" + port + ":" + seconds
+ includeMetadata + "]"; + ", metadata/details: " + includeMetadata + "]";
}
public TemplateOptions blockUntilRunning(boolean blockUntilRunning) {
this.blockUntilRunning = blockUntilRunning;
if (!blockUntilRunning)
port = seconds = -1;
return this;
} }
@Override @Override
@ -493,4 +469,29 @@ public class TemplateOptions {
return false; return false;
return true; 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));
}
@Override
public TemplateOptions blockOnComplete(boolean blockOnComplete) {
return TemplateOptions.class.cast(super.blockOnComplete(blockOnComplete));
}
} }

View File

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

View File

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

View File

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

View File

@ -87,6 +87,20 @@ public class ComputeServiceUtils {
return extractTargzIntoDirectory(new HttpRequest("GET", targz), directory); 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) { public static String parseTagFromName(String from) {
Matcher matcher = DELIMETED_BY_HYPHEN_ENDING_IN_HYPHEN_HEX.matcher(from); Matcher matcher = DELIMETED_BY_HYPHEN_ENDING_IN_HYPHEN_HEX.matcher(from);
return matcher.find() ? matcher.group(1) : "NOTAG-" + from; return matcher.find() ? matcher.group(1) : "NOTAG-" + from;

View File

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

View File

@ -52,4 +52,6 @@ public interface SshClient {
@PreDestroy @PreDestroy
void disconnect(); void disconnect();
void put(String path, String contents);
} }

View File

@ -30,7 +30,8 @@ import static com.google.common.collect.Maps.newLinkedHashMap;
import static com.google.common.collect.Maps.uniqueIndex; import static com.google.common.collect.Maps.uniqueIndex;
import static com.google.common.collect.Sets.filter; import static com.google.common.collect.Sets.filter;
import static com.google.common.collect.Sets.newTreeSet; import static com.google.common.collect.Sets.newTreeSet;
import static org.jclouds.compute.options.RunScriptOptions.Builder.overrideCredentialsWith; import static org.jclouds.compute.options.TemplateOptions.Builder.blockOnComplete;
import static org.jclouds.compute.options.TemplateOptions.Builder.overrideCredentialsWith;
import static org.jclouds.compute.predicates.NodePredicates.TERMINATED; import static org.jclouds.compute.predicates.NodePredicates.TERMINATED;
import static org.jclouds.compute.predicates.NodePredicates.all; import static org.jclouds.compute.predicates.NodePredicates.all;
import static org.jclouds.compute.predicates.NodePredicates.runningWithTag; import static org.jclouds.compute.predicates.NodePredicates.runningWithTag;
@ -43,6 +44,7 @@ import static org.testng.Assert.assertNotNull;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.net.URI;
import java.util.Collection; import java.util.Collection;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
@ -96,33 +98,6 @@ import com.google.inject.Module;
*/ */
@Test(groups = { "integration", "live" }, sequential = true, testName = "compute.ComputeServiceLiveTest") @Test(groups = { "integration", "live" }, sequential = true, testName = "compute.ComputeServiceLiveTest")
public abstract class BaseComputeServiceLiveTest { public abstract class BaseComputeServiceLiveTest {
public static final String APT_RUN_SCRIPT = new StringBuilder()//
.append("echo nameserver 208.67.222.222 >> /etc/resolv.conf\n")//
.append("cp /etc/apt/sources.list /etc/apt/sources.list.old\n")//
.append(
"sed 's~us.archive.ubuntu.com~mirror.anl.gov/pub~g' /etc/apt/sources.list.old >/etc/apt/sources.list\n")//
.append("apt-get update\n")//
.append("apt-get install -f -y --force-yes openjdk-6-jdk\n")//
.toString();
public static final String YUM_RUN_SCRIPT = new StringBuilder()
.append("echo nameserver 208.67.222.222 >> /etc/resolv.conf\n")
//
.append("echo \"[jdkrepo]\" >> /etc/yum.repos.d/CentOS-Base.repo\n")
//
.append("echo \"name=jdkrepository\" >> /etc/yum.repos.d/CentOS-Base.repo\n")
//
.append(
"echo \"baseurl=http://ec2-us-east-mirror.rightscale.com/epel/5/i386/\" >> /etc/yum.repos.d/CentOS-Base.repo\n")//
.append("echo \"enabled=1\" >> /etc/yum.repos.d/CentOS-Base.repo\n")//
.append("yum --nogpgcheck -y install java-1.6.0-openjdk\n")//
.append("echo \"export PATH=\\\"/usr/lib/jvm/jre-1.6.0-openjdk/bin/:\\$PATH\\\"\" >> /root/.bashrc\n")//
.toString();
public static final String ZYPPER_RUN_SCRIPT = new StringBuilder()//
.append("echo nameserver 208.67.222.222 >> /etc/resolv.conf\n")//
.append("sudo zypper install java-1.6.0-openjdk-devl\n")//
.toString();
public void setServiceDefaults() { public void setServiceDefaults() {
@ -289,6 +264,37 @@ public abstract class BaseComputeServiceLiveTest {
} }
@Test(enabled = true, dependsOnMethods = "testCompareSizes") @Test(enabled = true, dependsOnMethods = "testCompareSizes")
public void testCreateAndRunAService() throws Exception {
String tag = this.tag + "service";
try {
client.destroyNodesMatching(withTag(tag));
} catch (Exception e) {
}
template = client.templateBuilder().options(blockOnComplete(false).blockOnPort(8080, 300).inboundPorts(22, 8080))
.build();
// note this is a dependency on the template resolution
template.getOptions().runScript(
RunScriptData.createScriptInstallAndStartJBoss(keyPair.get("public"), template.getImage()
.getOperatingSystem()));
try {
TreeSet<NodeMetadata> nodes = newTreeSet(client.runNodesWithTag(tag, 1, template));
checkHttpGet(nodes);
} finally {
client.destroyNodesMatching(withTag(tag));
}
}
protected void checkHttpGet(TreeSet<NodeMetadata> nodes) {
assert context.utils().http().get(
URI.create(String.format("http://%s:8080", get(nodes.last().getPublicAddresses(), 0)))) != null;
}
@Test(enabled = true, dependsOnMethods = "testCreateAndRunAService")
public void testCreateTwoNodesWithRunScript() throws Exception { public void testCreateTwoNodesWithRunScript() throws Exception {
try { try {
client.destroyNodesMatching(withTag(tag)); client.destroyNodesMatching(withTag(tag));
@ -320,9 +326,8 @@ public abstract class BaseComputeServiceLiveTest {
private void refreshTemplate() { private void refreshTemplate() {
template = buildTemplate(client.templateBuilder()); template = buildTemplate(client.templateBuilder());
template.getOptions().installPrivateKey(newStringPayload(keyPair.get("private"))).authorizePublicKey( template.getOptions().installPrivateKey(keyPair.get("private")).authorizePublicKey(keyPair.get("public"))
newStringPayload(keyPair.get("public"))).runScript( .runScript(newStringPayload(buildScript(template.getImage().getOperatingSystem())));
newStringPayload(buildScript(template.getImage().getOperatingSystem())));
} }
protected void checkImageIdMatchesTemplate(NodeMetadata node) { protected void checkImageIdMatchesTemplate(NodeMetadata node) {
@ -362,7 +367,7 @@ public abstract class BaseComputeServiceLiveTest {
Credentials creds) throws RunScriptOnNodesException { Credentials creds) throws RunScriptOnNodesException {
try { try {
return client.runScriptOnNodesMatching(runningWithTag(tag), newStringPayload(buildScript(os)), return client.runScriptOnNodesMatching(runningWithTag(tag), newStringPayload(buildScript(os)),
overrideCredentialsWith(creds)); overrideCredentialsWith(creds).nameTask("runScriptWithCreds"));
} catch (SshException e) { } catch (SshException e) {
throw e; throw e;
} }
@ -390,11 +395,11 @@ public abstract class BaseComputeServiceLiveTest {
public static String buildScript(OperatingSystem os) { public static String buildScript(OperatingSystem os) {
if (OperatingSystemPredicates.supportsApt().apply(os)) if (OperatingSystemPredicates.supportsApt().apply(os))
return APT_RUN_SCRIPT; return RunScriptData.APT_RUN_SCRIPT;
else if (OperatingSystemPredicates.supportsYum().apply(os)) else if (OperatingSystemPredicates.supportsYum().apply(os))
return YUM_RUN_SCRIPT; return RunScriptData.YUM_RUN_SCRIPT;
else if (OperatingSystemPredicates.supportsZypper().apply(os)) else if (OperatingSystemPredicates.supportsZypper().apply(os))
return ZYPPER_RUN_SCRIPT; return RunScriptData.ZYPPER_RUN_SCRIPT;
else else
throw new IllegalArgumentException("don't know how to handle" + os.toString()); throw new IllegalArgumentException("don't know how to handle" + os.toString());
} }
@ -606,7 +611,7 @@ public abstract class BaseComputeServiceLiveTest {
ExecResponse hello = ssh.exec("echo hello"); ExecResponse hello = ssh.exec("echo hello");
assertEquals(hello.getOutput().trim(), "hello"); assertEquals(hello.getOutput().trim(), "hello");
ExecResponse exec = ssh.exec("java -version"); 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 { } finally {
if (ssh != null) if (ssh != null)
ssh.disconnect(); ssh.disconnect();

View File

@ -0,0 +1,109 @@
/**
*
* 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;
import static org.jclouds.compute.util.ComputeServiceUtils.extractZipIntoDirectory;
import static org.jclouds.scriptbuilder.domain.Statements.exec;
import static org.jclouds.scriptbuilder.domain.Statements.interpret;
import java.net.URI;
import java.util.Map;
import org.jclouds.compute.domain.OperatingSystem;
import org.jclouds.compute.predicates.OperatingSystemPredicates;
import org.jclouds.scriptbuilder.InitBuilder;
import org.jclouds.scriptbuilder.domain.AuthorizeRSAPublicKey;
import org.jclouds.scriptbuilder.domain.Statement;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
/**
*
* @author Adrian Cole
*/
public class RunScriptData {
private static String jbossVersion = "5.0.0.CR2";
private static String jboss = String.format("jboss-%s-jdk6", jbossVersion);
private static String jbossHome = "/usr/local/jboss";
public static String createScriptInstallBase(OperatingSystem os) {
if (os == null || OperatingSystemPredicates.supportsApt().apply(os))
return APT_RUN_SCRIPT;
else if (OperatingSystemPredicates.supportsYum().apply(os))
return YUM_RUN_SCRIPT;
else if (OperatingSystemPredicates.supportsZypper().apply(os))
return ZYPPER_RUN_SCRIPT;
else
throw new IllegalArgumentException("don't know how to handle" + os.toString());
}
public static Statement createScriptInstallAndStartJBoss(String publicKey, OperatingSystem os) {
Map<String, String> envVariables = ImmutableMap.of("jbossHome", jbossHome);
Statement toReturn = new InitBuilder(
"jboss",
jbossHome,
jbossHome,
envVariables,
ImmutableList.<Statement> of(new AuthorizeRSAPublicKey(publicKey), exec(createScriptInstallBase(os)),
extractZipIntoDirectory(URI.create(String.format(
"http://superb-sea2.dl.sourceforge.net/project/jboss/JBoss/JBoss-%s/%s.zip",
jbossVersion, jboss)), "/usr/local"), exec("{md} " + jbossHome),
exec("mv /usr/local/jboss-" + jbossVersion + "/* " + jbossHome)),
ImmutableList
.<Statement> of(interpret("java -Xms128m -Xmx512m -XX:MaxPermSize=256m -Dorg.jboss.resolver.warning=true -Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000 -Djava.endorsed.dirs=lib/endorsed -classpath bin/run.jar org.jboss.Main -b 0.0.0.0")));
return toReturn;
}
public static final String APT_RUN_SCRIPT = new StringBuilder()//
.append("echo nameserver 208.67.222.222 >> /etc/resolv.conf\n")//
.append("cp /etc/apt/sources.list /etc/apt/sources.list.old\n")//
.append(
"sed 's~us.archive.ubuntu.com~mirror.anl.gov/pub~g' /etc/apt/sources.list.old >/etc/apt/sources.list\n")//
.append("apt-get update -y -qq\n")//
.append("apt-get install -f -y -qq --force-yes curl\n")//
.append("apt-get install -f -y -qq --force-yes unzip\n")//
.append("apt-get install -f -y -qq --force-yes openjdk-6-jdk\n")//
.toString();
public static final String YUM_RUN_SCRIPT = new StringBuilder()
.append("echo nameserver 208.67.222.222 >> /etc/resolv.conf\n")
//
.append("echo \"[jdkrepo]\" >> /etc/yum.repos.d/CentOS-Base.repo\n")
//
.append("echo \"name=jdkrepository\" >> /etc/yum.repos.d/CentOS-Base.repo\n")
//
.append(
"echo \"baseurl=http://ec2-us-east-mirror.rightscale.com/epel/5/i386/\" >> /etc/yum.repos.d/CentOS-Base.repo\n")//
.append("echo \"enabled=1\" >> /etc/yum.repos.d/CentOS-Base.repo\n")//
.append("yum --nogpgcheck -y install unzip\n")//
.append("yum --nogpgcheck -y install curl\n")//
.append("yum --nogpgcheck -y install java-1.6.0-openjdk\n")//
.append("echo \"export PATH=\\\"/usr/lib/jvm/jre-1.6.0-openjdk/bin/:\\$PATH\\\"\" >> /root/.bashrc\n")//
.toString();
public static final String ZYPPER_RUN_SCRIPT = new StringBuilder()//
.append("echo nameserver 208.67.222.222 >> /etc/resolv.conf\n")//
.append("sudo zypper install unzip\n")//
.append("sudo zypper install curl\n")//
.append("sudo zypper install java-1.6.0-openjdk-devl\n")//
.toString();
}

View File

@ -19,7 +19,6 @@
package org.jclouds.compute; package org.jclouds.compute;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.easymock.EasyMock.aryEq; import static org.easymock.EasyMock.aryEq;
import static org.easymock.EasyMock.eq; import static org.easymock.EasyMock.eq;
import static org.easymock.EasyMock.expect; import static org.easymock.EasyMock.expect;
@ -33,6 +32,7 @@ import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.Serializable; import java.io.Serializable;
import java.util.Set; import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -48,9 +48,6 @@ import org.jclouds.net.IPSocket;
import org.jclouds.predicates.RetryablePredicate; import org.jclouds.predicates.RetryablePredicate;
import org.jclouds.predicates.SocketOpen; import org.jclouds.predicates.SocketOpen;
import org.jclouds.rest.RestContext; 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.ExecResponse;
import org.jclouds.ssh.SshClient; import org.jclouds.ssh.SshClient;
import org.jclouds.ssh.SshException; import org.jclouds.ssh.SshException;
@ -58,11 +55,8 @@ import org.jclouds.util.Utils;
import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.common.base.Splitter;
import com.google.common.base.Throwables; import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.inject.AbstractModule; import com.google.inject.AbstractModule;
import com.google.inject.Module; import com.google.inject.Module;
@ -103,12 +97,18 @@ public class StubComputeServiceIntegrationTest extends BaseComputeServiceLiveTes
expect(socketOpen.apply(new IPSocket("144.175.1.2", 22))).andReturn(true); expect(socketOpen.apply(new IPSocket("144.175.1.2", 22))).andReturn(true);
expect(socketOpen.apply(new IPSocket("144.175.1.3", 22))).andReturn(true); expect(socketOpen.apply(new IPSocket("144.175.1.3", 22))).andReturn(true);
expect(socketOpen.apply(new IPSocket("144.175.1.4", 22))).andReturn(true); expect(socketOpen.apply(new IPSocket("144.175.1.4", 22))).andReturn(true);
expect(socketOpen.apply(new IPSocket("144.175.1.5", 22))).andReturn(true);
replay(socketOpen); replay(socketOpen);
socketTester = new RetryablePredicate<IPSocket>(socketOpen, 60, 1, TimeUnit.SECONDS); socketTester = new RetryablePredicate<IPSocket>(socketOpen, 60, 1, TimeUnit.SECONDS);
} }
@Override
protected void checkHttpGet(TreeSet<NodeMetadata> nodes) {
}
@Override @Override
protected Module getSshModule() { protected Module getSshModule() {
return new AbstractModule() { return new AbstractModule() {
@ -120,26 +120,36 @@ public class StubComputeServiceIntegrationTest extends BaseComputeServiceLiveTes
SshClient client2 = createMock(SshClient.class); SshClient client2 = createMock(SshClient.class);
SshClient client3 = createMock(SshClient.class); SshClient client3 = createMock(SshClient.class);
SshClient client4 = createMock(SshClient.class); SshClient client4 = createMock(SshClient.class);
SshClient client5 = createMock(SshClient.class);
expect(factory.create(new IPSocket("144.175.1.1", 22), "root", "romeo")).andThrow(
new SshException("Auth fail"));
expect(factory.create(new IPSocket("144.175.1.1", 22), "root", "password1")).andReturn(client1) expect(factory.create(new IPSocket("144.175.1.1", 22), "root", "password1")).andReturn(client1)
.atLeastOnce(); .atLeastOnce();
runScriptAndService(client1, 1);
client1.connect(); expect(factory.create(new IPSocket("144.175.1.2", 22), "root", "romeo")).andThrow(
runScript(client1, "computeserv", 1); new SshException("Auth fail"));
client1.disconnect();
expect(factory.create(new IPSocket("144.175.1.2", 22), "root", "password2")).andReturn(client2) expect(factory.create(new IPSocket("144.175.1.2", 22), "root", "password2")).andReturn(client2)
.atLeastOnce(); .atLeastOnce();
client2.connect();
try {
runScript(client2, "runScriptWithCreds", Utils.toStringAndClose(StubComputeServiceIntegrationTest.class
.getResourceAsStream("/runscript.sh")), 2);
} catch (IOException e) {
Throwables.propagate(e);
}
client2.disconnect();
expect(factory.create(new IPSocket("144.175.1.3", 22), "root", "password3")).andReturn(client3) expect(factory.create(new IPSocket("144.175.1.3", 22), "root", "password3")).andReturn(client3)
.atLeastOnce(); .atLeastOnce();
expect(factory.create(new IPSocket("144.175.1.4", 22), "root", "password4")).andReturn(client4) expect(factory.create(new IPSocket("144.175.1.4", 22), "root", "password4")).andReturn(client4)
.atLeastOnce(); .atLeastOnce();
expect(factory.create(new IPSocket("144.175.1.5", 22), "root", "password5")).andReturn(client5)
.atLeastOnce();
runScriptAndInstallSsh(client2, "runscript", 2); runScriptAndInstallSsh(client3, "bootstrap", 3);
runScriptAndInstallSsh(client3, "runscript", 3); runScriptAndInstallSsh(client4, "bootstrap", 4);
runScriptAndInstallSsh(client4, "runscript", 4); runScriptAndInstallSsh(client5, "bootstrap", 5);
expect( expect(
factory.create(eq(new IPSocket("144.175.1.1", 22)), eq("root"), aryEq(keyPair.get("private") factory.create(eq(new IPSocket("144.175.1.1", 22)), eq("root"), aryEq(keyPair.get("private")
@ -153,42 +163,55 @@ public class StubComputeServiceIntegrationTest extends BaseComputeServiceLiveTes
expect( expect(
factory.create(eq(new IPSocket("144.175.1.4", 22)), eq("root"), aryEq(keyPair.get("private") factory.create(eq(new IPSocket("144.175.1.4", 22)), eq("root"), aryEq(keyPair.get("private")
.getBytes()))).andReturn(client4).atLeastOnce(); .getBytes()))).andReturn(client4).atLeastOnce();
expect(
factory.create(eq(new IPSocket("155.175.1.5", 22)), eq("root"), aryEq(keyPair.get("private")
.getBytes()))).andReturn(client5).atLeastOnce();
helloAndJava(client1);
helloAndJava(client2); helloAndJava(client2);
helloAndJava(client3); helloAndJava(client3);
helloAndJava(client4); helloAndJava(client4);
helloAndJava(client5);
replay(factory); replay(factory);
replay(client1); replay(client1);
replay(client2); replay(client2);
replay(client3); replay(client3);
replay(client4); replay(client4);
replay(client5);
bind(SshClient.Factory.class).toInstance(factory); bind(SshClient.Factory.class).toInstance(factory);
} }
private void runScriptAndInstallSsh(SshClient client, String scriptName, int nodeId) { private void runScriptAndService(SshClient client, int nodeId) {
client.connect(); client.connect();
runScript(client, scriptName, nodeId); try {
runScript(client, "jboss", Utils.toStringAndClose(StubComputeServiceIntegrationTest.class
expect(client.exec("mkdir .ssh")).andReturn(EXEC_GOOD); .getResourceAsStream("/initscript_with_jboss.sh")), nodeId);
expect(client.exec("cat .ssh/id_rsa.pub >> .ssh/authorized_keys")).andReturn(EXEC_GOOD); } catch (IOException e) {
expect(client.exec("chmod 600 .ssh/authorized_keys")).andReturn(EXEC_GOOD); Throwables.propagate(e);
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);
client.disconnect(); client.disconnect();
} }
private void runScript(SshClient client, String scriptName, int nodeId) { private void runScriptAndInstallSsh(SshClient client, String scriptName, int nodeId) {
client.put(eq("" + scriptName + ""), payloadEq(initScript(scriptName, client.connect();
BaseComputeServiceLiveTest.APT_RUN_SCRIPT)));
try {
runScript(client, scriptName, Utils.toStringAndClose(StubComputeServiceIntegrationTest.class
.getResourceAsStream("/initscript_with_java.sh")), nodeId);
} catch (IOException e) {
Throwables.propagate(e);
}
client.disconnect();
}
private void runScript(SshClient client, String scriptName, String script, int nodeId) {
client.put(scriptName, script);
expect(client.exec("chmod 755 " + scriptName + "")).andReturn(EXEC_GOOD); expect(client.exec("chmod 755 " + scriptName + "")).andReturn(EXEC_GOOD);
expect(client.getUsername()).andReturn("root").atLeastOnce(); expect(client.getUsername()).andReturn("root").atLeastOnce();
expect(client.getHostAddress()).andReturn(nodeId + "").atLeastOnce(); expect(client.getHostAddress()).andReturn(nodeId + "").atLeastOnce();
@ -223,13 +246,6 @@ public class StubComputeServiceIntegrationTest extends BaseComputeServiceLiveTes
// TODO: this fails so we override it. // 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) { public static Payload payloadEq(String value) {
reportMatcher(new PayloadEquals(value)); reportMatcher(new PayloadEquals(value));
return null; return null;
@ -257,10 +273,7 @@ public class StubComputeServiceIntegrationTest extends BaseComputeServiceLiveTes
} }
try { try {
String real = Utils.toStringAndClose(((Payload) actual).getInput()); String real = Utils.toStringAndClose(((Payload) actual).getInput());
if (!expected.equals(real)) { assertEquals(real, expected);
System.err.println(real);
return false;
}
return true; return true;
} catch (IOException e) { } catch (IOException e) {
Throwables.propagate(e); Throwables.propagate(e);
@ -316,6 +329,11 @@ public class StubComputeServiceIntegrationTest extends BaseComputeServiceLiveTes
super.testAScriptExecutionAfterBootWithBasicTemplate(); super.testAScriptExecutionAfterBootWithBasicTemplate();
} }
@Test(enabled = true, dependsOnMethods = { "testCompareSizes" })
public void testCreateAndRunAService() throws Exception {
super.testCreateAndRunAService();
}
@Test(enabled = true, dependsOnMethods = "testTemplateMatch") @Test(enabled = true, dependsOnMethods = "testTemplateMatch")
public void testCreateTwoNodesWithRunScript() throws Exception { public void testCreateTwoNodesWithRunScript() throws Exception {
super.testCreateTwoNodesWithRunScript(); super.testCreateTwoNodesWithRunScript();

View File

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

View File

@ -0,0 +1,138 @@
#!/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 -y -qq
apt-get install -f -y -qq --force-yes curl
apt-get install -f -y -qq --force-yes unzip
apt-get install -f -y -qq --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,137 @@
#!/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="jboss"
export INSTANCE_HOME="/usr/local/jboss"
export LOG_DIR="/usr/local/jboss"
return 0
}
function jboss {
export JBOSS_HOME="/usr/local/jboss"
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
jboss || exit 1
mkdir -p ~/.ssh
cat >> ~/.ssh/authorized_keys <<'END_OF_FILE'
ssh-rsa
END_OF_FILE
chmod 600 ~/.ssh/authorized_keys
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 -y -qq
apt-get install -f -y -qq --force-yes curl
apt-get install -f -y -qq --force-yes unzip
apt-get install -f -y -qq --force-yes openjdk-6-jdk
(mkdir -p /usr/local &&cd /usr/local &&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 -o -qq extract.zip&& rm extract.zip)
mkdir -p /usr/local/jboss
mv /usr/local/jboss-5.0.0.CR2/* /usr/local/jboss
mkdir -p $INSTANCE_HOME
# create runscript header
cat > $INSTANCE_HOME/jboss.sh <<END_OF_SCRIPT
#!/bin/bash
set +u
shopt -s xpg_echo
shopt -s expand_aliases
PROMPT_COMMAND='echo -ne "\033]0;jboss\007"'
export PATH=/usr/ucb/bin:/bin:/sbin:/usr/bin:/usr/sbin
export INSTANCE_NAME='jboss'
export JBOSS_HOME='$JBOSS_HOME'
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/jboss.sh <<'END_OF_SCRIPT'
cd $INSTANCE_HOME
java -Xms128m -Xmx512m -XX:MaxPermSize=256m -Dorg.jboss.resolver.warning=true -Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000 -Djava.endorsed.dirs=lib/endorsed -classpath bin/run.jar org.jboss.Main -b 0.0.0.0
END_OF_SCRIPT
# add runscript footer
cat >> $INSTANCE_HOME/jboss.sh <<'END_OF_SCRIPT'
exit 0
END_OF_SCRIPT
chmod u+x $INSTANCE_HOME/jboss.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,127 @@
#!/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 -y -qq
apt-get install -f -y -qq --force-yes curl
apt-get install -f -y -qq --force-yes unzip
apt-get install -f -y -qq --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

@ -132,15 +132,7 @@ public class JschSshClient implements SshClient {
public Payload get(String path) { public Payload get(String path) {
checkNotNull(path, "path"); checkNotNull(path, "path");
checkConnected(); ChannelSftp sftp = getSftp();
logger.debug("%s@%s:%d: Opening sftp Channel.", username, host, port);
ChannelSftp sftp = null;
try {
sftp = (ChannelSftp) session.openChannel("sftp");
sftp.connect();
} catch (JSchException e) {
throw new SshException(String.format("%s@%s:%d: Error connecting to sftp.", username, host, port), e);
}
try { try {
return Payloads.newInputStreamPayload(new CloseFtpChannelOnCloseInputStream(sftp.get(path), sftp)); return Payloads.newInputStreamPayload(new CloseFtpChannelOnCloseInputStream(sftp.get(path), sftp));
} catch (SftpException e) { } catch (SftpException e) {
@ -148,10 +140,26 @@ public class JschSshClient implements SshClient {
} }
} }
@Override
public void put(String path, Payload contents) { public void put(String path, Payload contents) {
checkNotNull(path, "path"); checkNotNull(path, "path");
checkNotNull(contents, "contents"); checkNotNull(contents, "contents");
ChannelSftp sftp = getSftp();
try {
sftp.put(contents.getInput(), path);
} catch (SftpException e) {
throw new SshException(String.format("%s@%s:%d: Error putting path: %s", username, host, port, path), e);
} finally {
Closeables.closeQuietly(contents);
}
}
@Override
public void put(String path, String contents) {
put(path, Payloads.newStringPayload(checkNotNull(contents, "contents")));
}
private ChannelSftp getSftp() {
checkConnected(); checkConnected();
logger.debug("%s@%s:%d: Opening sftp Channel.", username, host, port); logger.debug("%s@%s:%d: Opening sftp Channel.", username, host, port);
ChannelSftp sftp = null; ChannelSftp sftp = null;
@ -161,13 +169,7 @@ public class JschSshClient implements SshClient {
} catch (JSchException e) { } catch (JSchException e) {
throw new SshException(String.format("%s@%s:%d: Error connecting to sftp.", username, host, port), e); throw new SshException(String.format("%s@%s:%d: Error connecting to sftp.", username, host, port), e);
} }
try { return sftp;
sftp.put(contents.getInput(), path);
} catch (SftpException e) {
throw new SshException(String.format("%s@%s:%d: Error putting path: %s", username, host, port, path), e);
} finally {
Closeables.closeQuietly(contents);
}
} }
private void checkConnected() { private void checkConnected() {

View File

@ -46,8 +46,8 @@ import com.google.inject.Injector;
*/ */
@Test(groups = "live", testName = "ssh.JschSshClientLiveTest") @Test(groups = "live", testName = "ssh.JschSshClientLiveTest")
public class JschSshClientLiveTest { public class JschSshClientLiveTest {
protected static final String sshHost = System.getProperty("test.ssh.host"); protected static final String sshHost = System.getProperty("test.ssh.host", "localhost");
protected static final String sshPort = System.getProperty("test.ssh.port"); protected static final String sshPort = System.getProperty("test.ssh.port", "22");
protected static final String sshUser = System.getProperty("test.ssh.username"); protected static final String sshUser = System.getProperty("test.ssh.username");
protected static final String sshPass = System.getProperty("test.ssh.password"); protected static final String sshPass = System.getProperty("test.ssh.password");
protected static final String sshKeyFile = System.getProperty("test.ssh.keyfile"); protected static final String sshKeyFile = System.getProperty("test.ssh.keyfile");
@ -55,7 +55,7 @@ public class JschSshClientLiveTest {
@BeforeGroups(groups = { "live" }) @BeforeGroups(groups = { "live" })
public SshClient setupClient() throws NumberFormatException, FileNotFoundException, IOException { public SshClient setupClient() throws NumberFormatException, FileNotFoundException, IOException {
int port = (sshPort != null) ? Integer.parseInt(sshPort) : 22; int port = Integer.parseInt(sshPort);
if (sshUser == null if (sshUser == null
|| ((sshPass == null || sshPass.trim().equals("")) && (sshKeyFile == null || sshKeyFile.trim().equals(""))) || ((sshPass == null || sshPass.trim().equals("")) && (sshKeyFile == null || sshKeyFile.trim().equals("")))
|| sshUser.trim().equals("")) { || sshUser.trim().equals("")) {
@ -99,6 +99,11 @@ public class JschSshClientLiveTest {
return null; return null;
} }
@Override
public void put(String path, String contents) {
}
}; };
} else { } else {
Injector i = Guice.createInjector(new JschSshClientModule()); Injector i = Guice.createInjector(new JschSshClientModule());

View File

@ -19,6 +19,7 @@
package org.jclouds.scriptbuilder; package org.jclouds.scriptbuilder;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.jclouds.scriptbuilder.domain.Statements.call; import static org.jclouds.scriptbuilder.domain.Statements.call;
import static org.jclouds.scriptbuilder.domain.Statements.createRunScript; import static org.jclouds.scriptbuilder.domain.Statements.createRunScript;
import static org.jclouds.scriptbuilder.domain.Statements.findPid; import static org.jclouds.scriptbuilder.domain.Statements.findPid;
@ -31,8 +32,10 @@ import static org.jclouds.scriptbuilder.domain.Statements.switchArg;
import java.util.Map; import java.util.Map;
import org.jclouds.scriptbuilder.domain.Statement; import org.jclouds.scriptbuilder.domain.Statement;
import org.jclouds.scriptbuilder.domain.StatementList;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
/** /**
@ -42,9 +45,21 @@ import com.google.common.collect.Iterables;
*/ */
public class InitBuilder extends ScriptBuilder { public class InitBuilder extends ScriptBuilder {
@SuppressWarnings("unchecked") private final String instanceName;
private final String instanceHome;
private final String logDir;
public InitBuilder(String instanceName, String instanceHome, String logDir, Map<String, String> variables, public InitBuilder(String instanceName, String instanceHome, String logDir, Map<String, String> variables,
Iterable<Statement> statements) { Iterable<Statement> statements) {
this(instanceName, instanceHome, logDir, variables, ImmutableSet.<Statement>of(), statements);
}
@SuppressWarnings("unchecked")
public InitBuilder(String instanceName, String instanceHome, String logDir, Map<String, String> variables,
Iterable<Statement> initStatements, Iterable<Statement> statements) {
this.instanceName = checkNotNull(instanceName, "instanceName");
this.instanceHome = checkNotNull(instanceHome, "instanceHome");
this.logDir = checkNotNull(logDir, "logDir");
Map<String, String> defaultVariables = ImmutableMap.of("instanceName", instanceName, "instanceHome", Map<String, String> defaultVariables = ImmutableMap.of("instanceName", instanceName, "instanceHome",
instanceHome, "logDir", logDir); instanceHome, "logDir", logDir);
addEnvironmentVariableScope("default", defaultVariables) addEnvironmentVariableScope("default", defaultVariables)
@ -56,7 +71,7 @@ public class InitBuilder extends ScriptBuilder {
.put( .put(
"init", "init",
newStatementList(call("default"), call(instanceName), newStatementList(call("default"), call(instanceName),
createRunScript( new StatementList(initStatements), createRunScript(
instanceName,// TODO: convert instanceName,// TODO: convert
// so // so
// that // that
@ -98,4 +113,58 @@ public class InitBuilder extends ScriptBuilder {
interpret("{varl}INSTANCE_HOME{varr}{fs}{varl}INSTANCE_NAME{varr}.{sh}{lf}"))) interpret("{varl}INSTANCE_HOME{varr}{fs}{varl}INSTANCE_NAME{varr}.{sh}{lf}")))
.build())); .build()));
} }
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((instanceHome == null) ? 0 : instanceHome.hashCode());
result = prime * result + ((instanceName == null) ? 0 : instanceName.hashCode());
result = prime * result + ((logDir == null) ? 0 : logDir.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
InitBuilder other = (InitBuilder) obj;
if (instanceHome == null) {
if (other.instanceHome != null)
return false;
} else if (!instanceHome.equals(other.instanceHome))
return false;
if (instanceName == null) {
if (other.instanceName != null)
return false;
} else if (!instanceName.equals(other.instanceName))
return false;
if (logDir == null) {
if (other.logDir != null)
return false;
} else if (!logDir.equals(other.logDir))
return false;
return true;
}
public String getInstanceName() {
return instanceName;
}
public String getInstanceHome() {
return instanceHome;
}
public String getLogDir() {
return logDir;
}
@Override
public String toString() {
return "[instanceName=" + instanceName + ", instanceHome=" + instanceHome + ", logDir=" + logDir + "]";
}
} }

View File

@ -32,6 +32,7 @@ import org.jclouds.scriptbuilder.util.Utils;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
@ -41,7 +42,7 @@ import com.google.common.collect.Maps;
* *
* @author Adrian Cole * @author Adrian Cole
*/ */
public class ScriptBuilder { public class ScriptBuilder implements Statement {
@VisibleForTesting @VisibleForTesting
List<Statement> statements = Lists.newArrayList(); List<Statement> statements = Lists.newArrayList();
@ -69,8 +70,7 @@ public class ScriptBuilder {
* Exports a variable inside the script * Exports a variable inside the script
*/ */
public ScriptBuilder addEnvironmentVariableScope(String scopeName, Map<String, String> variables) { public ScriptBuilder addEnvironmentVariableScope(String scopeName, Map<String, String> variables) {
variableScopes variableScopes.put(checkNotNull(scopeName, "scopeName"), checkNotNull(variables, "variables"));
.put(checkNotNull(scopeName, "scopeName"), checkNotNull(variables, "variables"));
return this; return this;
} }
@ -85,19 +85,21 @@ public class ScriptBuilder {
* @param osFamily * @param osFamily
* whether to write a cmd or bash script. * 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(); Map<String, String> functions = Maps.newLinkedHashMap();
functions.put("abort", Utils.writeFunctionFromResource("abort", osFamily)); functions.put("abort", Utils.writeFunctionFromResource("abort", osFamily));
for (Entry<String, Map<String, String>> entry : variableScopes.entrySet()) { for (Entry<String, Map<String, String>> entry : variableScopes.entrySet()) {
functions.put(entry.getKey(), Utils.writeFunction(entry.getKey(), Utils functions.put(entry.getKey(), Utils.writeFunction(entry.getKey(), Utils.writeVariableExporters(entry
.writeVariableExporters(entry.getValue()))); .getValue())));
} }
final Map<String, String> tokenValueMap = ShellToken.tokenValueMap(osFamily); final Map<String, String> tokenValueMap = ShellToken.tokenValueMap(osFamily);
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
builder.append(ShellToken.BEGIN_SCRIPT.to(osFamily)); builder.append(ShellToken.BEGIN_SCRIPT.to(osFamily));
builder.append(Utils.writeUnsetVariables(Lists.newArrayList(Iterables.transform( builder.append(Utils.writeUnsetVariables(Lists.newArrayList(Iterables.transform(variablesToUnset,
variablesToUnset, new Function<String, String>() { new Function<String, String>() {
@Override @Override
public String apply(String from) { public String apply(String from) {
if (tokenValueMap.containsKey(from + "Variable")) if (tokenValueMap.containsKey(from + "Variable"))
@ -141,4 +143,9 @@ public class ScriptBuilder {
functions.put(functionName, Utils.writeFunctionFromResource(functionName, osFamily)); 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 * @author Adrian Cole
*/ */
public class CreateFile implements Statement { public class AppendFile implements Statement {
public final static String MARKER = "END_OF_FILE"; public final static String MARKER = "END_OF_FILE";
final String path; final String path;
final Iterable<String> lines; 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.path = checkNotNull(path, "path");
this.lines = checkNotNull(lines, "lines"); this.lines = checkNotNull(lines, "lines");
checkState(Iterables.size(lines) > 0, "you must pass something to execute"); checkState(Iterables.size(lines) > 0, "you must pass something to execute");
@ -77,7 +77,6 @@ public class CreateFile implements Statement {
hereFile(path, builder); hereFile(path, builder);
statements.add(interpret(builder.toString())); statements.add(interpret(builder.toString()));
} else { } else {
statements.add(interpret(String.format("{rm} %s 2{closeFd}{lf}", path)));
for (String line : lines) { for (String line : lines) {
statements.add(appendToFile(line, path, family)); statements.add(appendToFile(line, path, family));
} }
@ -86,7 +85,7 @@ public class CreateFile implements Statement {
} }
private void hereFile(String path, StringBuilder builder) { 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) { for (String line : lines) {
builder.append(line).append("\n"); 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.net.URI;
import java.util.Map; import java.util.Map;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Multimap; import com.google.common.collect.Multimap;
/** /**
@ -40,12 +41,31 @@ public class Statements {
return new SwitchArg(arg, valueToActions); 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) { public static Statement call(String function, String... args) {
return new Call(function, args); return new Call(function, args);
} }
public static Statement createFile(String path, Iterable<String> lines) { public static Statement appendFile(String path, Iterable<String> lines) {
return new CreateFile(path, lines); return new AppendFile(path, lines);
} }
public static Statement createRunScript(String instanceName, Iterable<String> exports, String pwd, public static Statement createRunScript(String instanceName, Iterable<String> exports, String pwd,
@ -120,10 +140,27 @@ public class Statements {
* uri corresponding to the request * uri corresponding to the request
* @param headers * @param headers
* request headers to send * request headers to send
* @param directory
*/ */
public static Statement extractTargzIntoDirectory(String method, URI endpoint, Multimap<String, String> headers, public static Statement extractTargzIntoDirectory(String method, URI endpoint, Multimap<String, String> headers,
String directory) { String directory) {
return new PipeHttpResponseToTarxpzfIntoDirectory( method, endpoint, headers,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);
} }
/** /**

View File

@ -99,7 +99,7 @@ public class SwitchArg implements Statement {
inRunScript = inRunScript ? false : true; inRunScript = inRunScript ? false : true;
} }
if (line.indexOf(CreateFile.MARKER) != -1) { if (line.indexOf(AppendFile.MARKER) != -1) {
inCreateFile = inCreateFile ? false : true; inCreateFile = inCreateFile ? false : true;
} }
shouldIndent = !inCreateFile && !inRunScript; 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 -o -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; package org.jclouds.scriptbuilder;
import static org.jclouds.scriptbuilder.domain.Statements.call; 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 static org.testng.Assert.assertEquals;
import java.io.IOException; import java.io.IOException;
@ -47,17 +47,17 @@ public class InitBuilderTest {
InitBuilder testInitBuilder = new InitBuilder("mkebsboot", "/mnt/tmp", "/mnt/tmp", ImmutableMap.of("tmpDir", InitBuilder testInitBuilder = new InitBuilder("mkebsboot", "/mnt/tmp", "/mnt/tmp", ImmutableMap.of("tmpDir",
"/mnt/tmp"), ImmutableList.<Statement> of( "/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 @Test
public void testBuildSimpleWindows() throws MalformedURLException, IOException { 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))); .getResource("test_init." + ShellToken.SH.to(OsFamily.WINDOWS)), Charsets.UTF_8)));
} }
@Test @Test
public void testBuildSimpleUNIX() throws MalformedURLException, IOException { 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))); .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", "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}", "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----") "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))); .getResource("test_ebs." + ShellToken.SH.to(OsFamily.UNIX)), Charsets.UTF_8)));
} }
} }

View File

@ -20,7 +20,7 @@
package org.jclouds.scriptbuilder; package org.jclouds.scriptbuilder;
import static org.jclouds.scriptbuilder.domain.Statements.call; 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.findPid;
import static org.jclouds.scriptbuilder.domain.Statements.interpret; import static org.jclouds.scriptbuilder.domain.Statements.interpret;
import static org.jclouds.scriptbuilder.domain.Statements.kill; import static org.jclouds.scriptbuilder.domain.Statements.kill;
@ -65,19 +65,19 @@ public class ScriptBuilderTest {
interpret("echo stop {varl}RUNTIME{varr}{lf}")), interpret("echo stop {varl}RUNTIME{varr}{lf}")),
"status", "status",
newStatementList( newStatementList(
createFile("{tmp}{fs}{uid}{fs}scripttest{fs}temp.txt", appendFile("{tmp}{fs}{uid}{fs}scripttest{fs}temp.txt",
ImmutableList.<String> of("hello world")), ImmutableList.<String> of("hello world")),
interpret("echo {vq}the following should be []: [{varl}RUNTIME{varr}]{vq}{lf}"))))); interpret("echo {vq}the following should be []: [{varl}RUNTIME{varr}]{vq}{lf}")))));
@Test @Test
public void testBuildSimpleWindows() throws MalformedURLException, IOException { 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))); Resources.getResource("test_script." + ShellToken.SH.to(OsFamily.WINDOWS)), Charsets.UTF_8)));
} }
@Test @Test
public void testBuildSimpleUNIX() throws MalformedURLException, IOException { 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))); .getResource("test_script." + ShellToken.SH.to(OsFamily.UNIX)), Charsets.UTF_8)));
} }
@ -86,13 +86,13 @@ public class ScriptBuilderTest {
@Test @Test
public void testFindPidWindows() throws MalformedURLException, IOException { 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))); .getResource("test_find_pid." + ShellToken.SH.to(OsFamily.WINDOWS)), Charsets.UTF_8)));
} }
@Test @Test
public void testFindPidUNIX() throws MalformedURLException, IOException { 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))); .getResource("test_find_pid." + ShellToken.SH.to(OsFamily.UNIX)), Charsets.UTF_8)));
} }
@ -100,13 +100,13 @@ public class ScriptBuilderTest {
@Test @Test
public void testSeekAndDestroyWindows() throws MalformedURLException, IOException { 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))); Resources.getResource("test_seek_and_destroy." + ShellToken.SH.to(OsFamily.WINDOWS)), Charsets.UTF_8)));
} }
@Test @Test
public void testSeekAndDestroyUNIX() throws MalformedURLException, IOException { 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))); 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; 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 static org.testng.Assert.assertEquals;
import java.io.IOException; import java.io.IOException;
@ -34,9 +34,9 @@ import com.google.common.io.Resources;
/** /**
* @author Adrian Cole * @author Adrian Cole
*/ */
@Test(groups = "unit", testName = "scriptbuilder.CreateFileTest") @Test(groups = "unit", testName = "scriptbuilder.AppendFileTest")
public class CreateFileTest { public class AppendFileTest {
Statement statement = createFile("{root}etc{fs}chef{fs}client.rb", ImmutableList.of("log_level :info", 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"))); "log_location STDOUT", String.format("chef_server_url \"%s\"", "http://localhost:4000")));
public void testUNIX() throws IOException { public void testUNIX() throws IOException {
@ -50,10 +50,10 @@ public class CreateFileTest {
} }
public void testRedirectGuard() { public void testRedirectGuard() {
assertEquals(CreateFile.addSpaceToEnsureWeDontAccidentallyRedirectFd("foo>>"), "foo>>"); assertEquals(AppendFile.addSpaceToEnsureWeDontAccidentallyRedirectFd("foo>>"), "foo>>");
assertEquals(CreateFile.addSpaceToEnsureWeDontAccidentallyRedirectFd("foo0>>"), "foo0 >>"); assertEquals(AppendFile.addSpaceToEnsureWeDontAccidentallyRedirectFd("foo0>>"), "foo0 >>");
assertEquals(CreateFile.addSpaceToEnsureWeDontAccidentallyRedirectFd("foo1>>"), "foo1 >>"); assertEquals(AppendFile.addSpaceToEnsureWeDontAccidentallyRedirectFd("foo1>>"), "foo1 >>");
assertEquals(CreateFile.addSpaceToEnsureWeDontAccidentallyRedirectFd("foo2>>"), "foo2 >>"); 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 AuthorizeRSAPublicKeyTest {
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; package org.jclouds.scriptbuilder.domain;
import static org.jclouds.scriptbuilder.domain.Statements.call; 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.jclouds.scriptbuilder.domain.Statements.createRunScript;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
@ -45,7 +45,7 @@ public class CreateRunScriptTest {
ImmutableList ImmutableList
.<Statement> of( .<Statement> of(
call("echo hello"), 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")), .<String> of("hello world")),
call("echo {varl}JAVA_HOME{varr}{fs}bin{fs}java -DinstanceName={varl}INSTANCE_NAME{varr} myServer.Main"))); 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; 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.interpret;
import static org.jclouds.scriptbuilder.domain.Statements.newStatementList; import static org.jclouds.scriptbuilder.domain.Statements.newStatementList;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
@ -37,10 +37,10 @@ import com.google.common.collect.ImmutableMap;
public class SwitchArgTest { public class SwitchArgTest {
public void testSwitchArgUNIX() { 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")), "{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), 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() { 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 -o -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_level :info>>c:\etc\chef\client.rb
echo log_location STDOUT>>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 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_level :info
log_location STDOUT log_location STDOUT
chef_server_url "http://localhost:4000" chef_server_url "http://localhost:4000"

View File

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

View File

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

View File

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

View File

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

View File

@ -98,6 +98,8 @@ public class ComputeTask extends Task {
invokeActionOnService(act, context.getComputeService()); invokeActionOnService(act, context.getComputeService());
} catch (RunNodesException e) { } catch (RunNodesException e) {
throw new BuildException(e); throw new BuildException(e);
} catch (IOException e) {
throw new BuildException(e);
} }
} }
} finally { } 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) { switch (action) {
case CREATE: case CREATE:
case GET: 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(); String tag = nodeElement.getTag();
log(String.format("create tag: %s, count: %d, hardware: %s, os: %s", tag, nodeElement.getCount(), nodeElement 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 static org.jclouds.rest.RestContextFactory.getPropertiesFromResource;
import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
@ -42,11 +43,13 @@ import org.jclouds.io.Payloads;
import org.jclouds.ssh.jsch.config.JschSshClientModule; import org.jclouds.ssh.jsch.config.JschSshClientModule;
import org.jclouds.tools.ant.logging.config.AntLoggingModule; 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.Function;
import com.google.common.base.Splitter; import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import com.google.common.collect.MapMaker; import com.google.common.collect.MapMaker;
import com.google.common.io.Files;
import com.google.inject.Module; import com.google.inject.Module;
import com.google.inject.Provider; 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 * Creates a Map that associates a uri with a live connection to the compute provider. This is
* provider. This is done on-demand. * done on-demand.
* *
* @param projectProvider * @param projectProvider
* allows access to the ant project to retrieve default properties * allows access to the ant project to retrieve default properties needed for compute
* needed for compute providers. * providers.
*/ */
static Map<URI, ComputeServiceContext> buildComputeMap(final Provider<Project> projectProvider) { static Map<URI, ComputeServiceContext> buildComputeMap(final Provider<Project> projectProvider) {
return new MapMaker().makeComputingMap(new Function<URI, ComputeServiceContext>() { return new MapMaker().makeComputingMap(new Function<URI, ComputeServiceContext>() {
@ -78,8 +81,8 @@ public class ComputeTaskUtils {
String provider = from.getHost(); String provider = from.getHost();
Credentials creds = Credentials.parse(from); Credentials creds = Credentials.parse(from);
return new ComputeServiceContextFactory(props).createContext(provider, creds.identity, creds.credential, return new ComputeServiceContextFactory(props).createContext(provider, creds.identity, creds.credential,
ImmutableSet.of((Module) new AntLoggingModule(projectProvider.get(), ImmutableSet.of((Module) new AntLoggingModule(projectProvider.get(),
ComputeServiceConstants.COMPUTE_LOGGER), new JschSshClientModule()), props); ComputeServiceConstants.COMPUTE_LOGGER), new JschSshClientModule()), props);
} }
@ -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(); TemplateBuilder templateBuilder = computeService.templateBuilder();
if (nodeElement.getLocation() != null && !"".equals(nodeElement.getLocation())) if (nodeElement.getLocation() != null && !"".equals(nodeElement.getLocation()))
templateBuilder.locationId(nodeElement.getLocation()); templateBuilder.locationId(nodeElement.getLocation());
@ -116,11 +119,11 @@ public class ComputeTaskUtils {
template.biggest(); template.biggest();
} else { } else {
throw new BuildException("size: " + nodeElement.getHardware() throw new BuildException("size: " + nodeElement.getHardware()
+ " not supported. valid sizes are smallest, fastest, biggest"); + " not supported. valid sizes are smallest, fastest, biggest");
} }
} }
static TemplateOptions getNodeOptionsFromElement(NodeElement nodeElement) { static TemplateOptions getNodeOptionsFromElement(NodeElement nodeElement) throws IOException {
TemplateOptions options = new TemplateOptions().inboundPorts(getPortsToOpenFromElement(nodeElement)); TemplateOptions options = new TemplateOptions().inboundPorts(getPortsToOpenFromElement(nodeElement));
addRunScriptToOptionsIfPresentInNodeElement(nodeElement, options); addRunScriptToOptionsIfPresentInNodeElement(nodeElement, options);
addPrivateKeyToOptionsIfPresentInNodeElement(nodeElement, options); addPrivateKeyToOptionsIfPresentInNodeElement(nodeElement, options);
@ -133,14 +136,15 @@ public class ComputeTaskUtils {
options.runScript(Payloads.newFilePayload(nodeElement.getRunscript())); 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) 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) 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) { 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, InitBuilder testInitBuilder = new InitBuilder(id, basedir, basedir, envVariables,
ImmutableList.<Statement> of(Statements.interpret( commandBuilder.toString()))); ImmutableList.<Statement> of(Statements.interpret( commandBuilder.toString())));
return testInitBuilder.build(osFamily); return testInitBuilder.render(osFamily);
} }
@Override @Override

View File

@ -152,7 +152,6 @@ public class VCloudTemplateOptions extends TemplateOptions {
* @see TemplateOptions#authorizePublicKey(String) * @see TemplateOptions#authorizePublicKey(String)
*/ */
@Override @Override
@Deprecated
public VCloudTemplateOptions authorizePublicKey(String publicKey) { public VCloudTemplateOptions authorizePublicKey(String publicKey) {
return VCloudTemplateOptions.class.cast(super.authorizePublicKey(publicKey)); return VCloudTemplateOptions.class.cast(super.authorizePublicKey(publicKey));
} }
@ -161,6 +160,7 @@ public class VCloudTemplateOptions extends TemplateOptions {
* @see TemplateOptions#authorizePublicKey(Payload) * @see TemplateOptions#authorizePublicKey(Payload)
*/ */
@Override @Override
@Deprecated
public VCloudTemplateOptions authorizePublicKey(Payload publicKey) { public VCloudTemplateOptions authorizePublicKey(Payload publicKey) {
return VCloudTemplateOptions.class.cast(super.authorizePublicKey(publicKey)); return VCloudTemplateOptions.class.cast(super.authorizePublicKey(publicKey));
} }
@ -169,7 +169,6 @@ public class VCloudTemplateOptions extends TemplateOptions {
* @see TemplateOptions#installPrivateKey(String) * @see TemplateOptions#installPrivateKey(String)
*/ */
@Override @Override
@Deprecated
public VCloudTemplateOptions installPrivateKey(String privateKey) { public VCloudTemplateOptions installPrivateKey(String privateKey) {
return VCloudTemplateOptions.class.cast(super.installPrivateKey(privateKey)); return VCloudTemplateOptions.class.cast(super.installPrivateKey(privateKey));
} }
@ -178,6 +177,7 @@ public class VCloudTemplateOptions extends TemplateOptions {
* @see TemplateOptions#installPrivateKey(Payload) * @see TemplateOptions#installPrivateKey(Payload)
*/ */
@Override @Override
@Deprecated
public VCloudTemplateOptions installPrivateKey(Payload privateKey) { public VCloudTemplateOptions installPrivateKey(Payload privateKey) {
return VCloudTemplateOptions.class.cast(super.installPrivateKey(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.compute.options.TemplateOptions;
import org.jclouds.io.Payloads; import org.jclouds.io.Payloads;
import org.jclouds.util.Utils;
import org.testng.annotations.Test; import org.testng.annotations.Test;
/** /**
@ -78,8 +77,8 @@ public class VCloudTemplateOptionsTest {
@Test @Test
public void testinstallPrivateKey() throws IOException { public void testinstallPrivateKey() throws IOException {
VCloudTemplateOptions options = new VCloudTemplateOptions(); VCloudTemplateOptions options = new VCloudTemplateOptions();
options.installPrivateKey(Payloads.newPayload("-----BEGIN RSA PRIVATE KEY-----")); options.installPrivateKey("-----BEGIN RSA PRIVATE KEY-----");
assertEquals(Utils.toStringAndClose(options.getPrivateKey().getInput()), "-----BEGIN RSA PRIVATE KEY-----"); assertEquals(options.getPrivateKey(), "-----BEGIN RSA PRIVATE KEY-----");
} }
@Test @Test
@ -91,7 +90,7 @@ public class VCloudTemplateOptionsTest {
@Test @Test
public void testinstallPrivateKeyStatic() throws IOException { public void testinstallPrivateKeyStatic() throws IOException {
VCloudTemplateOptions options = installPrivateKey(Payloads.newPayload("-----BEGIN RSA PRIVATE KEY-----")); 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) @Test(expectedExceptions = NullPointerException.class)
@ -102,8 +101,8 @@ public class VCloudTemplateOptionsTest {
@Test @Test
public void testauthorizePublicKey() throws IOException { public void testauthorizePublicKey() throws IOException {
VCloudTemplateOptions options = new VCloudTemplateOptions(); VCloudTemplateOptions options = new VCloudTemplateOptions();
options.authorizePublicKey(Payloads.newPayload("ssh-rsa")); options.authorizePublicKey("ssh-rsa");
assertEquals(Utils.toStringAndClose(options.getPublicKey().getInput()), "ssh-rsa"); assertEquals(options.getPublicKey(), "ssh-rsa");
} }
@Test @Test
@ -115,7 +114,7 @@ public class VCloudTemplateOptionsTest {
@Test @Test
public void testauthorizePublicKeyStatic() throws IOException { public void testauthorizePublicKeyStatic() throws IOException {
VCloudTemplateOptions options = authorizePublicKey(Payloads.newPayload("ssh-rsa")); VCloudTemplateOptions options = authorizePublicKey(Payloads.newPayload("ssh-rsa"));
assertEquals(Utils.toStringAndClose(options.getPublicKey().getInput()), "ssh-rsa"); assertEquals(options.getPublicKey(), "ssh-rsa");
} }
@Test(expectedExceptions = NullPointerException.class) @Test(expectedExceptions = NullPointerException.class)

View File

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

View File

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