package com.zillabyte.motherbrain.universe;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Serializable;
import java.nio.channels.ClosedByInterruptException;
import java.util.Arrays;
import java.util.List;
import org.apache.log4j.Logger;
import com.zillabyte.motherbrain.utils.Utils;
public abstract class SSHFactory implements Serializable{
/**
*
*/
private static final long serialVersionUID = 8641727369432892453L;
public abstract void runCommands(String host, List<String> commands) throws InterruptedException, SSHException;
public void runCommands(String host, String... commands) throws InterruptedException, SSHException {
runCommands(host, Arrays.asList(commands));
}
public static class Vagrant extends SSHFactory {
/**
*
*/
private static final long serialVersionUID = 5350717827279099819L;
static Logger log = Logger.getLogger(SSHFactory.class);
private static final Local localFactory = new Local();
@Override
public void runCommands(String host, List<String> commands) throws InterruptedException, SSHException {
if (Utils.getHost() == host) {
localFactory.runCommands(host, commands);
return;
}
final String vagrantKey = "-----BEGIN RSA PRIVATE KEY-----\n"+
"MIIEogIBAAKCAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzI\n"+
"w+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoP\n"+
"kcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2\n"+
"hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NO\n"+
"Td0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcW\n"+
"yLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQIBIwKCAQEA4iqWPJXtzZA68mKd\n"+
"ELs4jJsdyky+ewdZeNds5tjcnHU5zUYE25K+ffJED9qUWICcLZDc81TGWjHyAqD1\n"+
"Bw7XpgUwFgeUJwUlzQurAv+/ySnxiwuaGJfhFM1CaQHzfXphgVml+fZUvnJUTvzf\n"+
"TK2Lg6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ihxJ0YDQT9Jpx4TMss4PSavPaBH3RXow5Ohe+bYoQ\n"+
"NE5OgEXk2wVfZczCZpigBKbKZHNYcelXtTt/nP3rsCuGcM4h53s=\n"+
"-----END RSA PRIVATE KEY-----";
final String vagrantFileName = "/tmp/vagrant_key";
final File vagrantFile;
try{
vagrantFile = File.createTempFile(vagrantFileName, ".pem");
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(vagrantFile));
try {
writer.append(vagrantKey);
} finally {
try {
writer.close();
} catch(ClosedByInterruptException e) {
throw (InterruptedException) new InterruptedException().initCause(e);
}
}
Utils.bash("chmod 600 " + vagrantFile.getAbsolutePath());
String fullCommand = "ssh -i " + vagrantFile.getAbsolutePath() +" -oStrictHostKeyChecking=no -oConnectTimeout=60 vagrant@" + host;
String actualCommand = "";
for(String command : commands) {
if(actualCommand == "") {
actualCommand += " \"sudo " + command;
} else {
actualCommand += " && sudo " + command;
}
}
actualCommand += "\"";
fullCommand += actualCommand;
Utils.bash(fullCommand);
} catch(ClosedByInterruptException e) {
throw (InterruptedException) new InterruptedException().initCause(e);
} finally {
vagrantFile.delete();
}
} catch(IOException e) {
throw new SSHException(e);
}
}
}
public static class Local extends SSHFactory {
/**
*
*/
private static final long serialVersionUID = 7924760568870118001L;
static Logger log = Logger.getLogger(SSHFactory.class);
@Override
public void runCommands(String host, List<String> commands) throws InterruptedException, SSHException {
String actualCommand = "";
for(String command : commands) {
if(actualCommand == "") {
actualCommand += command;
} else {
actualCommand += " && " + command;
}
}
try {
Utils.bash(actualCommand);
} catch (IOException e) {
throw new SSHException(e);
}
}
}
public static class AWS extends SSHFactory {
/**
*
*/
private static final long serialVersionUID = -8438946736747882279L;
@Override
public void runCommands(String host, List<String> commands) throws InterruptedException, SSHException {
String keyPath = "~/zb1/.credentials/ec2_2013_01.pem";
String fullCommand = "chmod 400 " + keyPath + "; ssh -i " + keyPath + " -oStrictHostKeyChecking=no -oConnectTimeout=60 " + host;
String actualCommand = "";
for(String command : commands) {
if(actualCommand == "") {
actualCommand += " \"sudo " + command;
} else {
actualCommand += " && sudo " + command;
}
}
actualCommand += "\"";
fullCommand += actualCommand;
try {
Utils.bash(fullCommand);
} catch (IOException e) {
throw new SSHException(e);
}
}
}
}