Issue 29: changed userData to automatically encode data

git-svn-id: http://jclouds.googlecode.com/svn/trunk@2559 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
adrian.f.cole 2009-12-31 18:08:30 +00:00
parent 32ab37d3d7
commit 3f7058454c
3 changed files with 10 additions and 16 deletions

View File

@ -22,6 +22,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
import org.jclouds.aws.ec2.domain.InstanceType;
import org.jclouds.aws.ec2.options.internal.BaseEC2RequestOptions;
import org.jclouds.encryption.internal.Base64;
/**
* Contains options supported in the Form API for the RunInstances operation. <h2>
@ -81,10 +82,10 @@ public class RunInstancesOptions extends BaseEC2RequestOptions {
}
/**
* MIME, Base64-encoded user data.
* Unencoded data
*/
public RunInstancesOptions withUserData(String data) {
formParameters.put("UserData", checkNotNull(data, "data"));
public RunInstancesOptions withUserData(byte[] data) {
formParameters.put("UserData", Base64.encodeBytes(checkNotNull(data, "data")));
return this;
}
@ -207,9 +208,9 @@ public class RunInstancesOptions extends BaseEC2RequestOptions {
}
/**
* @see RunInstancesOptions#withUserData(String)
* @see RunInstancesOptions#withUserData(byte [])
*/
public static RunInstancesOptions withUserData(String userData) {
public static RunInstancesOptions withUserData(byte[] userData) {
RunInstancesOptions options = new RunInstancesOptions();
return options.withUserData(userData);
}

View File

@ -45,7 +45,6 @@ import org.jclouds.aws.ec2.domain.RunningInstance;
import org.jclouds.aws.ec2.domain.Image.EbsBlockDevice;
import org.jclouds.aws.ec2.domain.Volume.InstanceInitiatedShutdownBehavior;
import org.jclouds.aws.ec2.predicates.InstanceStateRunning;
import org.jclouds.encryption.internal.Base64;
import org.jclouds.http.HttpResponseException;
import org.jclouds.logging.log4j.config.Log4JLoggingModule;
import org.jclouds.predicates.RetryablePredicate;
@ -145,8 +144,6 @@ public class CloudApplicationArchitecturesEC2ClientLiveTest {
.addStatement(exec("runurl run.alestic.com/install/lamp"))//
.build(OsFamily.UNIX);
// userData must be base 64 encoded
String encodedScript = Base64.encodeBytes(script.getBytes());
RunningInstance instance = null;
while (instance == null) {
try {
@ -160,7 +157,7 @@ public class CloudApplicationArchitecturesEC2ClientLiveTest {
asType(InstanceType.M1_SMALL) // smallest instance size
.withKeyName(keyPair.getKeyName()) // key I created above
.withSecurityGroup(securityGroupName) // group I created above
.withUserData(encodedScript)); // script to run as root
.withUserData(script.getBytes())); // script to run as root
instance = Iterables.getOnlyElement(reservation.getRunningInstances());

View File

@ -38,7 +38,6 @@ import org.jclouds.aws.ec2.domain.Region;
import org.jclouds.aws.ec2.domain.Reservation;
import org.jclouds.aws.ec2.domain.RunningInstance;
import org.jclouds.aws.ec2.predicates.InstanceStateRunning;
import org.jclouds.encryption.internal.Base64;
import org.jclouds.predicates.RetryablePredicate;
import org.jclouds.predicates.SocketOpen;
import org.jclouds.rest.RestContext;
@ -160,13 +159,10 @@ public class MainApp {
static RunningInstance runInstance(EC2Client client, String securityGroupName, String keyPairName) {
String script = new ScriptBuilder() // lamp install script
.addStatement(exec("runurl run.alestic.com/apt/upgrade"))
.addStatement(exec("runurl run.alestic.com/install/lamp"))
.addStatement(exec("runurl run.alestic.com/apt/upgrade"))//
.addStatement(exec("runurl run.alestic.com/install/lamp"))//
.build(OsFamily.UNIX);
// userData must be base 64 encoded
String encodedScript = Base64.encodeBytes(script.getBytes());
System.out.printf("%d: running instance%n", System.currentTimeMillis());
Reservation reservation = client.getInstanceServices().runInstancesInRegion(Region.DEFAULT,
null, // allow ec2 to chose an availability zone
@ -176,7 +172,7 @@ public class MainApp {
asType(InstanceType.M1_SMALL) // smallest instance size
.withKeyName(keyPairName) // key I created above
.withSecurityGroup(securityGroupName) // group I created above
.withUserData(encodedScript)); // script to run as root
.withUserData(script.getBytes())); // script to run as root
return Iterables.getOnlyElement(reservation.getRunningInstances());