Permalink
Browse files

Refactored the command line-based classes to use a resource file that…

… makes changes easier and possible in "the field".
  • Loading branch information...
1 parent 0e682c1 commit 8da0ac48a9d7de8e511940e4f67a9fe4bef3ffd6 @kirktrue kirktrue committed Oct 23, 2009
View
@@ -1,8 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src/java"/>
- <classpathentry kind="src" path="contrib/ec2-testing/src/java"/>
- <classpathentry kind="src" path="contrib/ec2-testing/test"/>
+ <classpathentry kind="src" path="contrib/ec2-testing/resources"/>
+ <classpathentry kind="src" path="contrib/ec2-testing/src/java"/>
+ <classpathentry kind="src" path="contrib/ec2-testing/test"/>
<classpathentry kind="src" path="contrib/hadoop-store-builder/test"/>
<classpathentry kind="src" path="contrib/hadoop-store-builder/src/java"/>
<classpathentry kind="src" path="contrib/mongodb/example"/>
@@ -0,0 +1,12 @@
+RsyncVoldemortDeployer.ssh=ssh -o StrictHostKeyChecking=no -i ${sshPrivateKey} ${hostUserId}@${hostName} "export"
+
+RsyncVoldemortDeployer.rsync=rsync -vaz --delete --progress --exclude=.git \
+ -e "ssh -o StrictHostKeyChecking=no -i ${sshPrivateKey}" \
+ ${sourceDirectory} ${hostUserId}@${hostName}:${voldemortRootDirectory}
+
+SshVoldemortClusterStarter.ssh=ssh -i ${sshPrivateKey} ${hostUserId}@${hostName} \
+ "export VOLDEMORT_HOME=$(pwd)/${voldemortHomeDirectory} ; cd ${voldemortRootDirectory} ; nohup ./bin/voldemort-server.sh > ~/log.txt 2>&1 &"
+
+SshVoldemortClusterStopper.ssh=ssh -i ${sshPrivateKey} ${hostUserId}@${hostName} \
+ "cd ${voldemortRootDirectory} ; ./bin/voldemort-stop.sh"
+
@@ -0,0 +1,98 @@
+package voldemort.utils;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+
+import org.apache.commons.lang.StringUtils;
+
+class CommandLineAction {
+
+ protected List<UnixCommand> generateCommands(String command,
+ Collection<String> hostNames,
+ String hostUserId,
+ File sshPrivateKey,
+ String voldemortRootDirectory,
+ String voldemortHomeDirectory,
+ File sourceDirectory) throws IOException {
+ Properties properties = new Properties();
+ properties.load(getClass().getClassLoader().getResourceAsStream("commands.properties"));
+ final String rawCommand = properties.getProperty(command);
+
+ List<UnixCommand> unixCommands = new ArrayList<UnixCommand>();
+
+ for(String hostName: hostNames) {
+ String parameterizedCommand = parameterizeCommand(hostName,
+ hostUserId,
+ sshPrivateKey,
+ voldemortRootDirectory,
+ voldemortHomeDirectory,
+ sourceDirectory,
+ rawCommand);
+ UnixCommand unixCommand = generateCommand(parameterizedCommand);
+ unixCommands.add(unixCommand);
+ }
+
+ return unixCommands;
+ }
+
+ private String parameterizeCommand(String hostName,
+ String hostUserId,
+ File sshPrivateKey,
+ String voldemortRootDirectory,
+ String voldemortHomeDirectory,
+ File sourceDirectory,
+ String command) {
+ Map<String, String> variableMap = new HashMap<String, String>();
+ variableMap.put("hostName", hostName);
+ variableMap.put("hostUserId", hostUserId);
+
+ if(sshPrivateKey != null)
+ variableMap.put("sshPrivateKey", sshPrivateKey.getAbsolutePath());
+
+ variableMap.put("voldemortRootDirectory", voldemortRootDirectory);
+ variableMap.put("voldemortHomeDirectory", voldemortHomeDirectory);
+
+ if(sourceDirectory != null)
+ variableMap.put("sourceDirectory", sourceDirectory.getAbsolutePath());
+
+ for(Map.Entry<String, String> entry: variableMap.entrySet())
+ command = StringUtils.replace(command, "${" + entry.getKey() + "}", entry.getValue());
+
+ return command;
+ }
+
+ private UnixCommand generateCommand(String command) {
+ List<String> commands = new ArrayList<String>();
+ boolean isInQuotes = false;
+ int start = 0;
+
+ for(int i = 0; i < command.length(); i++) {
+ char c = command.charAt(i);
+
+ if(c == '\"') {
+ isInQuotes = !isInQuotes;
+ } else if(c == ' ' && !isInQuotes) {
+ String substring = command.substring(start, i).trim();
+ start = i + 1;
+
+ if(substring.trim().length() > 0)
+ commands.add(substring.replace("\"", ""));
+ }
+ }
+
+ String substring = command.substring(start).trim();
+
+ if(substring.length() > 0)
+ commands.add(substring.replace("\"", ""));
+
+ System.out.println(commands);
+ return new UnixCommand(commands);
+ }
+
+}
@@ -1,43 +1,47 @@
package voldemort.utils;
import java.io.File;
-import java.util.ArrayList;
+import java.io.IOException;
import java.util.Collection;
import java.util.List;
-import org.apache.commons.lang.StringUtils;
-
-public class RsyncVoldemortDeployer implements VoldemortDeployer {
+public class RsyncVoldemortDeployer extends CommandLineAction implements VoldemortDeployer {
public void deploy(Collection<String> hostNames,
String hostUserId,
File sshPrivateKey,
- File sourceDirectory,
- String clusterXml_,
- String storesXml_,
- String serverProperties_,
- String destinationDirectory) throws VoldemortDeploymentException {
- for(String hostName: hostNames) {
- List<String> command = new ArrayList<String>();
- command.add("rsync");
- command.add("-vaz");
- command.add("--delete");
- command.add("--progress");
- command.add("--exclude=.git");
- command.add("-e");
- command.add("ssh -o StrictHostKeyChecking=no -i " + sshPrivateKey);
- command.add(sourceDirectory.getAbsolutePath());
- command.add(hostUserId + "@" + hostName + ":" + destinationDirectory);
-
- System.out.println(StringUtils.join(command, " "));
-
- UnixCommand unixCommand = new UnixCommand(command);
+ String voldemortRootDirectory,
+ File sourceDirectory) throws VoldemortDeploymentException {
+ try {
+ List<UnixCommand> unixCommands = generateCommands("RsyncVoldemortDeployer.ssh",
+ hostNames,
+ hostUserId,
+ sshPrivateKey,
+ voldemortRootDirectory,
+ null,
+ sourceDirectory);
+ for(UnixCommand unixCommand: unixCommands)
+ unixCommand.execute();
+ } catch(InterruptedException e) {
+ throw new VoldemortDeploymentException(e);
+ } catch(IOException e) {
+ throw new VoldemortDeploymentException(e);
+ }
- try {
+ try {
+ List<UnixCommand> unixCommands = generateCommands("RsyncVoldemortDeployer.rsync",
+ hostNames,
+ hostUserId,
+ sshPrivateKey,
+ voldemortRootDirectory,
+ null,
+ sourceDirectory);
+ for(UnixCommand unixCommand: unixCommands)
unixCommand.execute();
- } catch(Exception e) {
- throw new VoldemortDeploymentException(e);
- }
+ } catch(InterruptedException e) {
+ throw new VoldemortDeploymentException(e);
+ } catch(IOException e) {
+ throw new VoldemortDeploymentException(e);
}
}
@@ -1,38 +1,32 @@
package voldemort.utils;
import java.io.File;
-import java.util.ArrayList;
+import java.io.IOException;
import java.util.Collection;
import java.util.List;
-import org.apache.commons.lang.StringUtils;
-
-public class SshVoldemortClusterStarter implements VoldemortClusterStarter {
+public class SshVoldemortClusterStarter extends CommandLineAction implements
+ VoldemortClusterStarter {
public void start(Collection<String> hostNames,
String hostUserId,
File sshPrivateKey,
String voldemortRootDirectory,
String voldemortHomeDirectory) throws VoldemortStartClusterException {
- for(String hostName: hostNames) {
- List<String> command = new ArrayList<String>();
- command.add("ssh");
- command.add("-i");
- command.add(sshPrivateKey.getAbsolutePath());
- command.add(hostUserId + "@" + hostName);
- command.add("export VOLDEMORT_HOME=$(pwd)/" + voldemortHomeDirectory + " ; cd "
- + voldemortRootDirectory
- + " ; nohup ./bin/voldemort-server.sh > ~/log.txt 2>&1 &");
-
- System.out.println(StringUtils.join(command, " "));
-
- UnixCommand unixCommand = new UnixCommand(command);
-
- try {
+ try {
+ List<UnixCommand> unixCommands = generateCommands("SshVoldemortClusterStarter.ssh",
+ hostNames,
+ hostUserId,
+ sshPrivateKey,
+ voldemortRootDirectory,
+ voldemortHomeDirectory,
+ null);
+ for(UnixCommand unixCommand: unixCommands)
unixCommand.execute();
- } catch(Exception e) {
- throw new VoldemortStartClusterException(e);
- }
+ } catch(InterruptedException e) {
+ throw new VoldemortStartClusterException(e);
+ } catch(IOException e) {
+ throw new VoldemortStartClusterException(e);
}
}
@@ -1,35 +1,31 @@
package voldemort.utils;
import java.io.File;
-import java.util.ArrayList;
+import java.io.IOException;
import java.util.Collection;
import java.util.List;
-import org.apache.commons.lang.StringUtils;
-
-public class SshVoldemortClusterStopper implements VoldemortClusterStopper {
+public class SshVoldemortClusterStopper extends CommandLineAction implements
+ VoldemortClusterStopper {
public void stop(Collection<String> hostNames,
String hostUserId,
File sshPrivateKey,
String voldemortRootDirectory) throws VoldemortStartClusterException {
- for(String hostName: hostNames) {
- List<String> command = new ArrayList<String>();
- command.add("ssh");
- command.add("-i");
- command.add(sshPrivateKey.getAbsolutePath());
- command.add(hostUserId + "@" + hostName);
- command.add("cd " + voldemortRootDirectory + " ; ./bin/voldemort-stop.sh");
-
- System.out.println(StringUtils.join(command, " "));
-
- UnixCommand unixCommand = new UnixCommand(command);
-
- try {
+ try {
+ List<UnixCommand> unixCommands = generateCommands("SshVoldemortClusterStopper.ssh",
+ hostNames,
+ hostUserId,
+ sshPrivateKey,
+ voldemortRootDirectory,
+ null,
+ null);
+ for(UnixCommand unixCommand: unixCommands)
unixCommand.execute();
- } catch(Exception e) {
- throw new VoldemortStartClusterException(e);
- }
+ } catch(InterruptedException e) {
+ throw new VoldemortStartClusterException(e);
+ } catch(IOException e) {
+ throw new VoldemortStartClusterException(e);
}
}
@@ -8,10 +8,7 @@
public void deploy(Collection<String> hostNames,
String hostUserId,
File sshPrivateKey,
- File sourceDirectory,
- String clusterXml,
- String storesXml,
- String serverProperties,
- String destinationDirectory) throws VoldemortDeploymentException;
+ String voldemortRootDirectory,
+ File sourceDirectory) throws VoldemortDeploymentException;
}
@@ -13,20 +13,29 @@
public class SmokeTest {
+ private String hostUserId = "root";
+
+ private File sshPrivateKey = new File("/home/kirk/Dropbox/Configuration/AWS/id_rsa-mustardgrain-keypair");
+
+ private String voldemortRootDirectory = "somesubdirectory";
+
+ private String voldemortHomeDirectory = "somesubdirectory/voldemort/config/single_node_cluster";
+
+ private File sourceDirectory = new File("/home/kirk/voldemortdev/voldemort");
+
@Test
public void test() throws Exception {
Map<String, String> dnsNames = new HashMap<String, String>();
- dnsNames.put("ec2-75-101-191-205.compute-1.amazonaws.com",
- "domU-12-31-39-00-C4-D5.compute-1.internal");
+ dnsNames.put("ec2-174-129-127-232.compute-1.amazonaws.com", "ip-10-242-203-96.ec2.internal");
// dnsNames = createInstances();
generateClusterDescriptor(dnsNames.values());
rsync(dnsNames.keySet());
startCluster(dnsNames.keySet());
-
+
Thread.sleep(15000);
-
+
stopCluster(dnsNames.keySet());
}
@@ -47,36 +56,27 @@ private void generateClusterDescriptor(Collection<String> privateDnsNames) throw
}
private void rsync(Collection<String> hostNames) throws Exception {
- File sourceDirectory = new File("/Users/kirk/voldemortdev/voldemort");
- File sshPrivateKey = new File("/Users/kirk/Dropbox/Configuration/AWS/id_rsa-mustardgrain-keypair");
-
VoldemortDeployer voldemortDeployer = new RsyncVoldemortDeployer();
voldemortDeployer.deploy(hostNames,
- "root",
+ hostUserId,
sshPrivateKey,
- sourceDirectory,
- "",
- "",
- "",
- "somesubdirectory");
+ voldemortRootDirectory,
+ sourceDirectory);
}
private void startCluster(Collection<String> hostNames) throws Exception {
- File sshPrivateKey = new File("/Users/kirk/Dropbox/Configuration/AWS/id_rsa-mustardgrain-keypair");
-
VoldemortClusterStarter voldemortClusterStarter = new SshVoldemortClusterStarter();
voldemortClusterStarter.start(hostNames,
- "root",
+ hostUserId,
sshPrivateKey,
- "somesubdirectory/voldemort",
- "somesubdirectory/voldemort/config/single_node_cluster");
+ voldemortRootDirectory + "/voldemort",
+ voldemortHomeDirectory);
}
private void stopCluster(Collection<String> hostNames) throws Exception {
- File sshPrivateKey = new File("/Users/kirk/Dropbox/Configuration/AWS/id_rsa-mustardgrain-keypair");
-
VoldemortClusterStopper voldemortClusterStopper = new SshVoldemortClusterStopper();
- voldemortClusterStopper.stop(hostNames, "root", sshPrivateKey, "somesubdirectory/voldemort");
+ voldemortClusterStopper.stop(hostNames, hostUserId, sshPrivateKey, voldemortRootDirectory
+ + "/voldemort");
}
}

0 comments on commit 8da0ac4

Please sign in to comment.