)
throws ExecutionProblem, ExitNow {
if (clusterMembers == null || clusterMembers.length == 0) {
throw new ExecutionProblem("no clusterMembers");
}
final File topdir;
try {
topdir = CloudClientUtil.getHistoryDir(historyDir);
} catch (ParameterProblem e) {
throw new ExecutionProblem(e.getMessage(), e);
}
final int nextnum = HistoryUtil.findNextEc2ClusterNumber(topdir, print);
final String suffix = HistoryUtil.format.format(nextnum);
final String newDirName =
HistoryUtil.historyEc2ClusterDirPrefix + suffix;
final String clusterHandle = newDirName; // redundant, but clearer later
print.debugln("Next directory: " + newDirName);
final File newdir = new File(topdir, newDirName);
final String newdirPath = newdir.getAbsolutePath();
if (newdir.mkdir()) {
print.debugln("Created directory: " + newdirPath);
} else {
// could be a race condition on the name, or odd perm problem
// (note we checked parent dir was writeable)
throw new ExecutionProblem(
"Could not create directory '" + newdirPath + "'");
}
final File runLog =
HistoryUtil.newLogFile(newdir, RunTask.LOG_FILE_NAME, print);
if (runLog != null) {
try {
print.getOpts().setInfoErrFile(runLog.getAbsolutePath());
} catch (Exception e) {
print.errln("Problem setting InfoErrFile: " + e.getMessage());
// carry on
}
}
final File debugLog =
HistoryUtil.newLogFile(newdir, RunTask.DEBUG_LOG_FILE_NAME, print);
if (debugLog != null) {
try {
print.getOpts().setAllOutFile(debugLog.getAbsolutePath());
} catch (Exception e) {
print.errln("Problem setting AllOutFile: " + e.getMessage());
// carry on
}
}
// do we need to handle contextualization?
final boolean usingContextBroker =
needsContextualization(clusterMembers);
final String ctxEprPath;
final String ctxReportsDirPath;
final String[] ctxUserDataPaths = new String[clusterMembers.length];
if (!usingContextBroker) {
ctxEprPath = null;
ctxReportsDirPath = null;
} else {
final File ctxFile = new File(newdir,
HistoryUtil.CONTEXT_EPR_FILE_NAME);
ctxEprPath = ctxFile.getAbsolutePath();
final File ctxReportDir = new File(newdirPath, "reports-ctx");
ctxReportsDirPath = ctxReportDir.getAbsolutePath();
if (ctxReportDir.mkdir()) {
print.debugln("Created directory: " + ctxReportsDirPath);
} else {
throw new ExecutionProblem(
"Could not create directory '" + ctxReportsDirPath + "'");
}
final File ctxTempDir = new File(newdirPath, "ctx-tmp");
final String ctxTempDirPath = ctxTempDir.getAbsolutePath();
if (ctxTempDir.mkdir()) {
print.debugln("Created directory: " + ctxTempDirPath);
} else {
throw new ExecutionProblem(
"Could not create directory '" + ctxTempDirPath + "'");
}
createCtxAndUserdata(clusterMembers,
brokerURL,
brokerID,
doContextLock,
ctxEprPath,
ctxUserDataPaths,
ctxTempDir,
print);
}
// write the ec2 commands to both screen and a file
final StringBuffer buf = new StringBuffer();
for (int i = 0; i < clusterMembers.length; i++) {
buf.append("\nec2-run-instances")
.append(" --instance-count ")
.append(clusterMembers[i].getQuantity());
if (usingContextBroker && ctxUserDataPaths[i] != null) {
buf.append(" --user-data-file ")
.append(ctxUserDataPaths[i]);
}
buf.append(" --kernel aki-a71cf9ce --ramdisk ari-a51cf9cc");
// m1.small, m1.large, m1.xlarge, c1.medium, and c1.xlarge
buf.append(" --instance-type m1.small ");
buf.append(" --key default ");
buf.append(clusterMembers[i].getImageName());
buf.append("\n");
}
final String cmds = buf.toString();
print.infoln("\n** Sample EC2 commands:\n" + cmds);
if (ec2ScriptPath != null) {
final String toFile = "#!/bin/sh\n\n" + cmds + "\n";
try {
FileUtils.writeStringToFile(toFile, ec2ScriptPath);
} catch (Exception e) {
throw new ExecutionProblem("Problem writing sample EC2 " +
"commands to file: " + e.getMessage(), e);
}
print.infoln("\n** Wrote sample EC2 commands to '" +
ec2ScriptPath + "'\n");