mirror of https://github.com/apache/jclouds.git
Issue 964:change script builder variable case format from lowerCamel to UPPER_UNDERSCORE
This commit is contained in:
parent
022ca3bfd8
commit
4dd8fdd0e2
|
@ -75,7 +75,7 @@ public class RunScriptData {
|
|||
return InitScript.builder()
|
||||
.name("jboss")
|
||||
.home(JBOSS_HOME)
|
||||
.exportVariables(ImmutableMap.of("jbossHome", JBOSS_HOME))
|
||||
.exportVariables(ImmutableMap.of("JBOSS_HOME", JBOSS_HOME))
|
||||
.init(appendFile(JBOSS_HOME + "/standalone/configuration/standalone-custom.xml", Splitter.on('\n').split(configuration)))
|
||||
.run(interpret(new StringBuilder().append("java ").append(' ')
|
||||
.append("-server -Xms128m -Xmx128m -XX:MaxPermSize=128m -Djava.net.preferIPv4Stack=true -XX:+UseFastAccessorMethods -XX:+TieredCompilation -Xverify:none -Dorg.jboss.resolver.warning=true -Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000").append(' ')
|
||||
|
|
|
@ -42,6 +42,8 @@ public class EnvBuilder {
|
|||
|
||||
/**
|
||||
* Exports a variable inside the script
|
||||
*
|
||||
* @param name name of the variable in UPPER_UNDERSCORE case format
|
||||
*/
|
||||
public EnvBuilder export(String name, String value) {
|
||||
variables.put(checkNotNull(name, "name"), checkNotNull(value, "value"));
|
||||
|
@ -66,7 +68,7 @@ public class EnvBuilder {
|
|||
builder.append(Utils.writeComment(" Example usage to set a variable", osFamily));
|
||||
builder.append(Utils.writeComment("", osFamily));
|
||||
builder.append(Utils.writeComment(" "
|
||||
+ Utils.writeVariableExporters(ImmutableMap.of("mavenOpts", "-Xms64m -Xmx128m"),
|
||||
+ Utils.writeVariableExporters(ImmutableMap.of("MAVEN_OPTS", "-Xms64m -Xmx128m"),
|
||||
osFamily), osFamily));
|
||||
builder.append(Utils.writeVariableExporters(variables, osFamily));
|
||||
builder.append(ShellToken.LF.to(osFamily));
|
||||
|
|
|
@ -67,10 +67,13 @@ public class InitBuilder extends ScriptBuilder {
|
|||
this(instanceName, instanceHome, logDir, variables, ImmutableSet.<Statement> of(), statements);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param variables keys are the variables to export in UPPER_UNDERSCORE case format
|
||||
*/
|
||||
public InitBuilder(String instanceName, String instanceHome, String logDir, Map<String, String> variables,
|
||||
Iterable<Statement> initStatements, Iterable<Statement> statements) {
|
||||
Map<String, String> defaultVariables = ImmutableMap.of("instanceName", instanceName, "instanceHome",
|
||||
instanceHome, "logDir", logDir);
|
||||
Map<String, String> defaultVariables = ImmutableMap.of("INSTANCE_NAME", instanceName, "INSTANCE_HOME",
|
||||
instanceHome, "LOG_DIR", logDir);
|
||||
this.initStatement = new StatementList(initStatements);
|
||||
this.createRunScript = createRunScript(instanceName,// TODO: convert
|
||||
// so
|
||||
|
@ -79,9 +82,9 @@ public class InitBuilder extends ScriptBuilder {
|
|||
// can take from a
|
||||
// variable
|
||||
Iterables.concat(variables.keySet(), defaultVariables.keySet()), "{varl}INSTANCE_HOME{varr}", statements);
|
||||
this.instanceName = checkNotNull(instanceName, "instanceName");
|
||||
this.instanceHome = checkNotNull(instanceHome, "instanceHome");
|
||||
this.logDir = checkNotNull(logDir, "logDir");
|
||||
this.instanceName = checkNotNull(instanceName, "INSTANCE_NAME");
|
||||
this.instanceHome = checkNotNull(instanceHome, "INSTANCE_HOME");
|
||||
this.logDir = checkNotNull(logDir, "LOG_DIR");
|
||||
|
||||
addEnvironmentVariableScope("default", defaultVariables)
|
||||
.addEnvironmentVariableScope(instanceName, variables)
|
||||
|
|
|
@ -70,7 +70,7 @@ public class InitScript extends ForwardingObject implements Statement, AcceptsSt
|
|||
* @see InitScript#getInstanceName()
|
||||
*/
|
||||
public Builder name(String instanceName) {
|
||||
this.instanceName = checkNotNull(instanceName, "instanceName");
|
||||
this.instanceName = checkNotNull(instanceName, "INSTANCE_NAME");
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -78,7 +78,7 @@ public class InitScript extends ForwardingObject implements Statement, AcceptsSt
|
|||
* @see InitScript#getInstanceHome()
|
||||
*/
|
||||
public Builder home(String instanceHome) {
|
||||
this.instanceHome = checkNotNull(instanceHome, "instanceHome");
|
||||
this.instanceHome = checkNotNull(instanceHome, "INSTANCE_HOME");
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -86,11 +86,12 @@ public class InitScript extends ForwardingObject implements Statement, AcceptsSt
|
|||
* @see InitScript#getLogDir()
|
||||
*/
|
||||
public Builder logDir(String logDir) {
|
||||
this.logDir = checkNotNull(logDir, "logDir");
|
||||
this.logDir = checkNotNull(logDir, "LOG_DIR");
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param exports keys are the variables to export in UPPER_UNDERSCORE case format
|
||||
* @see InitScript#getExportedVariables()
|
||||
*/
|
||||
public Builder exportVariables(Map<String, String> exports) {
|
||||
|
@ -176,11 +177,14 @@ public class InitScript extends ForwardingObject implements Statement, AcceptsSt
|
|||
protected final StatementList run;
|
||||
protected final ScriptBuilder delegate;
|
||||
|
||||
/**
|
||||
* @param exports keys are the variables to export in UPPER_UNDERSCORE case format
|
||||
*/
|
||||
protected InitScript(String instanceName, String instanceHome, String logDir, Map<String, String> exports,
|
||||
StatementList init, StatementList run) {
|
||||
this.instanceName = checkNotNull(instanceName, "instanceName");
|
||||
this.instanceHome = checkNotNull(instanceHome, "instanceHome");
|
||||
this.logDir = checkNotNull(logDir, "logDir");
|
||||
this.instanceName = checkNotNull(instanceName, "INSTANCE_NAME");
|
||||
this.instanceHome = checkNotNull(instanceHome, "INSTANCE_HOME");
|
||||
this.logDir = checkNotNull(logDir, "LOG_DIR");
|
||||
this.exports = ImmutableMap.<String, String> copyOf(checkNotNull(exports, "exports"));
|
||||
this.init = checkNotNull(init, "init");
|
||||
this.run = checkNotNull(run, "run");
|
||||
|
@ -188,10 +192,14 @@ public class InitScript extends ForwardingObject implements Statement, AcceptsSt
|
|||
this.delegate = makeInitScriptStatement(instanceName, instanceHome, logDir, exports, init, run);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param exports keys are the variables to export in UPPER_UNDERSCORE case format
|
||||
*/
|
||||
public static ScriptBuilder makeInitScriptStatement(String instanceName, String instanceHome, String logDir,
|
||||
Map<String, String> exports, StatementList init, StatementList run) {
|
||||
Map<String, String> defaultExports = ImmutableMap.of("instanceName", instanceName, "instanceHome", instanceHome,
|
||||
"logDir", logDir);
|
||||
Map<String, String> defaultExports = ImmutableMap.of("INSTANCE_NAME", instanceName, "INSTANCE_HOME", instanceHome,
|
||||
"LOG_DIR", logDir);
|
||||
String exitStatusFile = format("%s/rc", logDir);
|
||||
run = new StatementList(ImmutableList.<Statement> builder().add(interpret("rm -f " + exitStatusFile))
|
||||
.add(interpret(format("trap 'echo $?>%s' 0 1 2 3 15", exitStatusFile))).addAll(run.delegate()).build());
|
||||
|
@ -303,7 +311,7 @@ public class InitScript extends ForwardingObject implements Statement, AcceptsSt
|
|||
|
||||
@Override
|
||||
public String toString() {
|
||||
return toStringHelper(this).add("instanceName", instanceName).toString();
|
||||
return toStringHelper(this).add("INSTANCE_NAME", instanceName).toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -57,7 +57,7 @@ public class ScriptBuilder implements Statement, AcceptsStatementVisitor {
|
|||
Map<String, Map<String, String>> variableScopes = Maps.newLinkedHashMap();
|
||||
|
||||
@VisibleForTesting
|
||||
List<String> variablesToUnset = Lists.newArrayList("path", "javaHome", "libraryPath");
|
||||
List<String> variablesToUnset = Lists.newArrayList("PATH", "JAVA_HOME", "LIBRARY_PATH");
|
||||
|
||||
public ScriptBuilder addStatement(Statement statement) {
|
||||
statements.add(checkNotNull(statement, "statement"));
|
||||
|
@ -66,6 +66,7 @@ public class ScriptBuilder implements Statement, AcceptsStatementVisitor {
|
|||
|
||||
/**
|
||||
* Unsets a variable to ensure it is set within the script.
|
||||
* @param variable name in UPPER_UNDERSCORE case format
|
||||
*/
|
||||
public ScriptBuilder unsetEnvironmentVariable(String name) {
|
||||
variablesToUnset.add(checkNotNull(name, "name"));
|
||||
|
@ -74,6 +75,8 @@ public class ScriptBuilder implements Statement, AcceptsStatementVisitor {
|
|||
|
||||
/**
|
||||
* Exports a variable inside the script
|
||||
* @param scopeName
|
||||
* @param variables keys are the variables to export in UPPER_UNDERSCORE case format
|
||||
*/
|
||||
public ScriptBuilder addEnvironmentVariableScope(String scopeName, Map<String, String> variables) {
|
||||
variableScopes.put(checkNotNull(scopeName, "scopeName"), checkNotNull(variables, "variables"));
|
||||
|
@ -112,22 +115,12 @@ public class ScriptBuilder implements Statement, AcceptsStatementVisitor {
|
|||
functions.put("abort", Utils.writeFunctionFromResource("abort", osFamily));
|
||||
|
||||
for (Entry<String, Map<String, String>> entry : variableScopes.entrySet()) {
|
||||
functions.put(entry.getKey(),
|
||||
Utils.writeFunction(entry.getKey(), Utils.writeVariableExporters(entry.getValue())));
|
||||
functions.put(entry.getKey(), Utils.writeFunction(entry.getKey(), Utils.writeVariableExporters(entry
|
||||
.getValue(), osFamily)));
|
||||
}
|
||||
final Map<String, String> tokenValueMap = ShellToken.tokenValueMap(osFamily);
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append(ShellToken.BEGIN_SCRIPT.to(osFamily));
|
||||
builder.append(Utils.writeUnsetVariables(
|
||||
Lists.newArrayList(Iterables.transform(variablesToUnset, new Function<String, String>() {
|
||||
@Override
|
||||
public String apply(String from) {
|
||||
if (tokenValueMap.containsKey(from + "Variable"))
|
||||
return Utils.FUNCTION_UPPER_UNDERSCORE_TO_LOWER_CAMEL.apply(tokenValueMap.get(from + "Variable"));
|
||||
return from;
|
||||
}
|
||||
|
||||
})), osFamily));
|
||||
builder.append(Utils.writeUnsetVariables(variablesToUnset, osFamily));
|
||||
Map<String, String> functionsToWrite = resolveFunctionDependenciesForStatements(functions, statements, osFamily);
|
||||
writeFunctions(functionsToWrite, osFamily, builder);
|
||||
builder.append(Utils.writeZeroPath(osFamily));
|
||||
|
|
|
@ -92,7 +92,7 @@ public class AppendFile implements Statement {
|
|||
protected final boolean expandVariables;
|
||||
|
||||
protected AppendFile(String path, Iterable<String> lines, String delimeter, boolean expandVariables) {
|
||||
this.path = checkNotNull(path, "path");
|
||||
this.path = checkNotNull(path, "PATH");
|
||||
this.lines = checkNotNull(lines, "lines");
|
||||
this.delimeter = checkNotNull(delimeter, "delimeter");
|
||||
checkState(Iterables.size(lines) > 0, "you must pass something to execute");
|
||||
|
|
|
@ -36,13 +36,12 @@ import org.jclouds.scriptbuilder.ExitInsteadOfReturn;
|
|||
import org.jclouds.scriptbuilder.ScriptBuilder;
|
||||
import org.jclouds.scriptbuilder.util.Utils;
|
||||
|
||||
import com.google.common.base.CaseFormat;
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Splitter;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableList.Builder;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.collect.ImmutableList.Builder;
|
||||
|
||||
/**
|
||||
* Creates a run script
|
||||
|
@ -54,10 +53,14 @@ public class CreateRunScript extends StatementList {
|
|||
final String instanceName;
|
||||
final Iterable<String> exports;
|
||||
final String pwd;
|
||||
|
||||
|
||||
/**
|
||||
* @param exports
|
||||
* variable names to export in UPPER_UNDERSCORE case format
|
||||
*/
|
||||
public CreateRunScript(String instanceName, Iterable<String> exports, String pwd, Iterable<Statement> statements) {
|
||||
super(statements);
|
||||
this.instanceName = checkNotNull(instanceName, "instanceName");
|
||||
this.instanceName = checkNotNull(instanceName, "INSTANCE_NAME");
|
||||
this.exports = checkNotNull(exports, "exports");
|
||||
this.pwd = checkNotNull(pwd, "pwd").replaceAll("[/\\\\]", "{fs}");
|
||||
}
|
||||
|
@ -65,9 +68,13 @@ public class CreateRunScript extends StatementList {
|
|||
public static class AddExport implements Statement {
|
||||
final String export;
|
||||
final String value;
|
||||
|
||||
|
||||
/**
|
||||
* @param export
|
||||
* variable name in UPPER_UNDERSCORE case format
|
||||
*/
|
||||
public AddExport(String export, String value) {
|
||||
this.export = checkNotNull(CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, export), "export");
|
||||
this.export = checkNotNull(export, "export");
|
||||
this.value = checkNotNull(value, "value");
|
||||
}
|
||||
|
||||
|
@ -151,9 +158,8 @@ public class CreateRunScript extends StatementList {
|
|||
|
||||
@Override
|
||||
public String apply(String export) {
|
||||
String variableNameInUpper = CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, export);
|
||||
return new StringBuilder().append("export ").append(variableNameInUpper).append("='$")
|
||||
.append(variableNameInUpper).append("'").toString();
|
||||
return new StringBuilder().append("export ").append(export).append("='$")
|
||||
.append(export).append("'").toString();
|
||||
}
|
||||
})).build().render(OsFamily.UNIX));
|
||||
|
||||
|
|
|
@ -49,7 +49,7 @@ public class InitMetadata {
|
|||
this.stopDir = checkNotNull(stopDir, "stopDir");
|
||||
this.configDir = checkNotNull(configDir, "configDir");
|
||||
this.dataDir = checkNotNull(dataDir, "dataDir");
|
||||
this.logDir = checkNotNull(logDir, "logDir");
|
||||
this.logDir = checkNotNull(logDir, "LOG_DIR");
|
||||
this.goldDir = checkNotNull(goldDir, "goldDir");
|
||||
}
|
||||
|
||||
|
|
|
@ -40,7 +40,7 @@ public class JavaInitMetadata extends InitMetadata {
|
|||
String stopDir, String configDir, String dataDir, String logDir, String goldDir,
|
||||
String javaHome, String[] classpath, String mainClass, String[] opts, String[] args) {
|
||||
super(name, platformHome, endPoint, startDir, stopDir, configDir, dataDir, logDir, goldDir);
|
||||
this.javaHome = checkNotNull(javaHome, "javaHome");
|
||||
this.javaHome = checkNotNull(javaHome, "JAVA_HOME");
|
||||
this.classpath = checkNotNull(classpath, "classpath");
|
||||
this.mainClass = checkNotNull(mainClass, "mainClass");
|
||||
this.opts = checkNotNull(opts, "opts");
|
||||
|
|
|
@ -85,6 +85,10 @@ public class Statements {
|
|||
return CreateOrOverwriteFile.builder().path(path).lines(lines).delimeter(delimeter).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param exports
|
||||
* variable names to export in UPPER_UNDERSCORE case format
|
||||
*/
|
||||
public static CreateRunScript createRunScript(String instanceName, Iterable<String> exports, String pwd,
|
||||
Iterable<Statement> statements) {// TODO: convert so
|
||||
// that
|
||||
|
|
|
@ -18,7 +18,6 @@
|
|||
*/
|
||||
package org.jclouds.scriptbuilder.util;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.regex.Matcher;
|
||||
|
@ -27,6 +26,7 @@ import java.util.regex.Pattern;
|
|||
import org.jclouds.scriptbuilder.domain.OsFamily;
|
||||
import org.jclouds.scriptbuilder.domain.ShellToken;
|
||||
import org.jclouds.scriptbuilder.functionloader.CurrentFunctionLoader;
|
||||
import org.jclouds.util.Maps2;
|
||||
|
||||
import com.google.common.base.CaseFormat;
|
||||
import com.google.common.base.Function;
|
||||
|
@ -41,28 +41,35 @@ import com.google.common.collect.Iterables;
|
|||
*/
|
||||
public class Utils {
|
||||
|
||||
public static final LowerCamelToUpperUnderscore FUNCTION_LOWER_CAMEL_TO_UPPER_UNDERSCORE = new LowerCamelToUpperUnderscore();
|
||||
/**
|
||||
*
|
||||
* In {@link ShellToken}, the values whose names end in {@code _VARIABLE} designate variable
|
||||
* names we know how to translate from one platform to another. For example
|
||||
* {@link ShellToken#LIBRARY_PATH_VARIABLE} means that we can translate the variable named
|
||||
* {@code LIBRARY_PATH} to the proper platform-specific name.
|
||||
*/
|
||||
public static final class VariableNameForOsFamily implements Function<String, String> {
|
||||
private final OsFamily family;
|
||||
|
||||
public VariableNameForOsFamily(OsFamily family) {
|
||||
this.family = family;
|
||||
}
|
||||
|
||||
public static final class LowerCamelToUpperUnderscore implements Function<String, String> {
|
||||
@Override
|
||||
public String apply(String from) {
|
||||
return CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, from);
|
||||
public String apply(String input) {
|
||||
String variableNameKey = CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, input) + "Variable";
|
||||
if (ShellToken.tokenValueMap(family).containsKey(variableNameKey))
|
||||
return ShellToken.tokenValueMap(family).get(variableNameKey);
|
||||
return input;
|
||||
}
|
||||
}
|
||||
|
||||
public static final UpperUnderscoreToLowerCamel FUNCTION_UPPER_UNDERSCORE_TO_LOWER_CAMEL = new UpperUnderscoreToLowerCamel();
|
||||
|
||||
public static final class UpperUnderscoreToLowerCamel implements Function<String, String> {
|
||||
@Override
|
||||
public String apply(String from) {
|
||||
return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, from);
|
||||
}
|
||||
}
|
||||
|
||||
/** matches any expression inside curly braces (where the expression does not including an open curly brace) */
|
||||
/**
|
||||
* matches any expression inside curly braces (where the expression does not including an open
|
||||
* curly brace)
|
||||
*/
|
||||
private static final Pattern pattern = Pattern.compile("\\{([^\\{]+?)\\}");
|
||||
|
||||
|
||||
/**
|
||||
* replaces tokens that are expressed as <code>{token}</code>
|
||||
*
|
||||
|
@ -96,32 +103,31 @@ public class Utils {
|
|||
/**
|
||||
* converts a map into variable exports relevant to the specified platform.
|
||||
* <p/>
|
||||
* ex. if variablesInLowerCamelCase is "mavenOpts" -> "-Xms64m -Xmx256m" <br/>
|
||||
* ex. if {@code keys} is the map: "MAVEN_OPTS" -> "-Xms64m -Xmx256m" <br/>
|
||||
* and family is UNIX<br/>
|
||||
* then writeVariableExporters returns literally {@code export MAVEN_OPTS="-Xms64m -Xmx256m"\n}
|
||||
*
|
||||
* @param variablesInLowerCamelCase
|
||||
* lower camel keys to values
|
||||
* @param exports
|
||||
* keys are the variables to export in UPPER_UNDERSCORE case format
|
||||
* @param family
|
||||
* operating system for formatting
|
||||
* @see VariableNameForOsFamily
|
||||
*/
|
||||
public static String writeVariableExporters(Map<String, String> variablesInLowerCamelCase,
|
||||
OsFamily family) {
|
||||
return replaceTokens(writeVariableExporters(variablesInLowerCamelCase), ShellToken
|
||||
.tokenValueMap(family));
|
||||
public static String writeVariableExporters(Map<String, String> exports, final OsFamily family) {
|
||||
exports = Maps2.transformKeys(exports, new VariableNameForOsFamily(family));
|
||||
return replaceTokens(writeVariableExporters(exports), ShellToken.tokenValueMap(family));
|
||||
}
|
||||
|
||||
/**
|
||||
* converts a map into variable exporters in shell intermediate language.
|
||||
*
|
||||
* @param variablesInLowerCamelCase
|
||||
* lower camel keys to values
|
||||
* @param exports
|
||||
* keys are the variables to export in UPPER_UNDERSCORE case format
|
||||
*/
|
||||
public static String writeVariableExporters(Map<String, String> variablesInLowerCamelCase) {
|
||||
public static String writeVariableExporters(Map<String, String> exports) {
|
||||
StringBuilder initializers = new StringBuilder();
|
||||
for (Entry<String, String> entry : variablesInLowerCamelCase.entrySet()) {
|
||||
String key = CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE, entry.getKey());
|
||||
initializers.append(String.format("{export} %s={vq}%s{vq}{lf}", key, entry.getValue()));
|
||||
for (Entry<String, String> entry : exports.entrySet()) {
|
||||
initializers.append(String.format("{export} %s={vq}%s{vq}{lf}", entry.getKey(), entry.getValue()));
|
||||
}
|
||||
return initializers.toString();
|
||||
}
|
||||
|
@ -131,36 +137,34 @@ public class Utils {
|
|||
}
|
||||
|
||||
public static String writeFunctionFromResource(String function, OsFamily family) {
|
||||
String toReturn = CurrentFunctionLoader.get().loadFunction(function,family);
|
||||
String lf = ShellToken.LF.to(family);
|
||||
return toReturn.endsWith(lf) ? toReturn : new StringBuilder(toReturn).append(lf).toString();
|
||||
String toReturn = CurrentFunctionLoader.get().loadFunction(function, family);
|
||||
String lf = ShellToken.LF.to(family);
|
||||
return toReturn.endsWith(lf) ? toReturn : new StringBuilder(toReturn).append(lf).toString();
|
||||
}
|
||||
|
||||
public static String writeFunction(String function, String source) {
|
||||
public static String writeFunction(String function, String source) {
|
||||
return String.format("{fncl}%s{fncr}%s{fnce}", function, source.replaceAll("^", " "));
|
||||
}
|
||||
|
||||
public static final Map<OsFamily, String> OS_TO_POSITIONAL_VAR_PATTERN = ImmutableMap.of(
|
||||
OsFamily.UNIX, "set {key}=$1\nshift\n", OsFamily.WINDOWS, "set {key}=%1\r\nshift\r\n");
|
||||
public static final Map<OsFamily, String> OS_TO_POSITIONAL_VAR_PATTERN = ImmutableMap.of(OsFamily.UNIX,
|
||||
"set {key}=$1\nshift\n", OsFamily.WINDOWS, "set {key}=%1\r\nshift\r\n");
|
||||
|
||||
public static final Map<OsFamily, String> OS_TO_LOCAL_VAR_PATTERN = ImmutableMap.of(
|
||||
OsFamily.UNIX, "set {key}=\"{value}\"\n", OsFamily.WINDOWS, "set {key}={value}\r\n");
|
||||
public static final Map<OsFamily, String> OS_TO_LOCAL_VAR_PATTERN = ImmutableMap.of(OsFamily.UNIX,
|
||||
"set {key}=\"{value}\"\n", OsFamily.WINDOWS, "set {key}={value}\r\n");
|
||||
|
||||
/**
|
||||
* Writes an initialization statement for use inside a script or a function.
|
||||
*
|
||||
* @param positionalVariablesInLowerCamelCase
|
||||
* @param positionalVariables
|
||||
* - transfer the value of args into these statements. Note that there is no check to
|
||||
* ensure that all source args are indeed present.
|
||||
*
|
||||
*/
|
||||
public static String writePositionalVars(List<String> positionalVariablesInLowerCamelCase,
|
||||
OsFamily family) {
|
||||
public static String writePositionalVars(Iterable<String> positionalVariables, OsFamily family) {
|
||||
StringBuilder initializers = new StringBuilder();
|
||||
for (String variableInLowerCamelCase : positionalVariablesInLowerCamelCase) {
|
||||
String key = CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE,
|
||||
variableInLowerCamelCase);
|
||||
initializers.append(replaceTokens(OS_TO_POSITIONAL_VAR_PATTERN.get(family), ImmutableMap
|
||||
.of("key", key)));
|
||||
for (String positionalVariable : positionalVariables) {
|
||||
initializers.append(replaceTokens(OS_TO_POSITIONAL_VAR_PATTERN.get(family), ImmutableMap.of("key",
|
||||
positionalVariable)));
|
||||
}
|
||||
return initializers.toString();
|
||||
}
|
||||
|
@ -168,22 +172,20 @@ public class Utils {
|
|||
/**
|
||||
* Ensures that variables come from a known source instead of bleeding in from a profile
|
||||
*
|
||||
* @param variablesInLowerCamelCase
|
||||
* @param variablesToUnset
|
||||
* - System variables to unset
|
||||
* @see VariableNameForOsFamily
|
||||
*/
|
||||
public static String writeUnsetVariables(List<String> variablesInLowerCamelCase, OsFamily family) {
|
||||
public static String writeUnsetVariables(Iterable<String> variablesToUnset, OsFamily family) {
|
||||
variablesToUnset = Iterables.transform(variablesToUnset, new VariableNameForOsFamily(family));
|
||||
switch (family) {
|
||||
case UNIX:
|
||||
return String.format("unset %s\n", Joiner.on(' ').join(
|
||||
Iterables.transform(variablesInLowerCamelCase,
|
||||
FUNCTION_LOWER_CAMEL_TO_UPPER_UNDERSCORE)));
|
||||
return String.format("unset %s\n", Joiner.on(' ').join(variablesToUnset));
|
||||
case WINDOWS:
|
||||
StringBuilder initializers = new StringBuilder();
|
||||
for (String variableInLowerCamelCase : variablesInLowerCamelCase) {
|
||||
String key = CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_UNDERSCORE,
|
||||
variableInLowerCamelCase);
|
||||
initializers.append(replaceTokens(OS_TO_LOCAL_VAR_PATTERN.get(family), ImmutableMap
|
||||
.of("key", key, "value", "")));
|
||||
for (String variableToUnset : variablesToUnset) {
|
||||
initializers.append(replaceTokens(OS_TO_LOCAL_VAR_PATTERN.get(family), ImmutableMap.of("key",
|
||||
variableToUnset, "value", "")));
|
||||
}
|
||||
return initializers.toString();
|
||||
default:
|
||||
|
@ -193,8 +195,8 @@ public class Utils {
|
|||
}
|
||||
|
||||
public static final Map<OsFamily, String> OS_TO_ZERO_PATH = ImmutableMap.of(OsFamily.WINDOWS,
|
||||
"set PATH=c:\\windows\\;C:\\windows\\system32;c:\\windows\\system32\\wbem\r\n",
|
||||
OsFamily.UNIX, "export PATH=/usr/ucb/bin:/bin:/sbin:/usr/bin:/usr/sbin\n");
|
||||
"set PATH=c:\\windows\\;C:\\windows\\system32;c:\\windows\\system32\\wbem\r\n", OsFamily.UNIX,
|
||||
"export PATH=/usr/ucb/bin:/bin:/sbin:/usr/bin:/usr/sbin\n");
|
||||
|
||||
/**
|
||||
* @return line used to zero out the path of the script such that basic commands such as unix ps
|
||||
|
|
|
@ -39,7 +39,7 @@ import com.google.common.io.Resources;
|
|||
*/
|
||||
public class EnvBuilderTest {
|
||||
|
||||
EnvBuilder testScriptBuilder = new EnvBuilder().export("javaHome",
|
||||
EnvBuilder testScriptBuilder = new EnvBuilder().export("JAVA_HOME",
|
||||
"/apps/jdk1.6");
|
||||
|
||||
@Test
|
||||
|
@ -59,8 +59,8 @@ public class EnvBuilderTest {
|
|||
@Test
|
||||
public void testExport() {
|
||||
EnvBuilder builder = new EnvBuilder();
|
||||
builder.export("javaHome", "/apps/jdk1.6");
|
||||
assertEquals(builder.variables, ImmutableMap.of("javaHome", "/apps/jdk1.6"));
|
||||
builder.export("JAVA_HOME", "/apps/jdk1.6");
|
||||
assertEquals(builder.variables, ImmutableMap.of("JAVA_HOME", "/apps/jdk1.6"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -45,7 +45,7 @@ import com.google.common.io.Resources;
|
|||
@Deprecated
|
||||
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("TMP_DIR",
|
||||
"/mnt/tmp"), ImmutableList.<Statement> of(
|
||||
appendFile("{tmp}{fs}{uid}{fs}scripttest{fs}temp.txt", ImmutableList.<String> of("hello world")),
|
||||
exec("find /")));
|
||||
|
@ -69,7 +69,7 @@ public class InitBuilderTest {
|
|||
new InitBuilder("mkebsboot",// name of the script
|
||||
"/tmp",// working directory
|
||||
"/tmp/logs",// location of stdout.log and stderr.log
|
||||
ImmutableMap.of("imageDir", "/mnt/tmp", "ebsDevice", "/dev/sdh", "ebsMountPoint", "/mnt/ebs"),// variables
|
||||
ImmutableMap.of("IMAGE_DIR", "/mnt/tmp", "EBS_DEVICE", "/dev/sdh", "EBS_MOUNT_POINT", "/mnt/ebs"),// variables
|
||||
// used
|
||||
// inside
|
||||
// of
|
||||
|
|
|
@ -49,8 +49,8 @@ import com.google.common.io.Resources;
|
|||
public class ScriptBuilderTest {
|
||||
|
||||
ScriptBuilder testScriptBuilder = new ScriptBuilder()
|
||||
.unsetEnvironmentVariable("runtime")
|
||||
.addEnvironmentVariableScope("default", ImmutableMap.of("runtime", "Moo"))
|
||||
.unsetEnvironmentVariable("RUNTIME")
|
||||
.addEnvironmentVariableScope("default", ImmutableMap.of("RUNTIME", "Moo"))
|
||||
.addStatement(
|
||||
switchArg(1, ImmutableMap.of(
|
||||
"start",
|
||||
|
@ -132,8 +132,8 @@ public class ScriptBuilderTest {
|
|||
@Test
|
||||
public void testExport() {
|
||||
ScriptBuilder builder = new ScriptBuilder();
|
||||
builder.addEnvironmentVariableScope("default", ImmutableMap.of("javaHome", "/apps/jdk1.6"));
|
||||
assertEquals(builder.variableScopes, ImmutableMap.of("default", ImmutableMap.of("javaHome", "/apps/jdk1.6")));
|
||||
builder.addEnvironmentVariableScope("default", ImmutableMap.of("JAVA_HOME", "/apps/jdk1.6"));
|
||||
assertEquals(builder.variableScopes, ImmutableMap.of("default", ImmutableMap.of("JAVA_HOME", "/apps/jdk1.6")));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -39,7 +39,7 @@ import com.google.common.io.Resources;
|
|||
public class CreateRunScriptTest {
|
||||
Statement statement = createRunScript(
|
||||
"yahooprod",
|
||||
ImmutableList.<String> of("javaHome"),
|
||||
ImmutableList.<String> of("JAVA_HOME"),
|
||||
"{tmp}{fs}{uid}{fs}scripttest",
|
||||
ImmutableList
|
||||
.<Statement> of(
|
||||
|
|
|
@ -48,36 +48,57 @@ public class UtilsTest {
|
|||
}
|
||||
|
||||
public void testWriteVariableExportersUNIX() {
|
||||
assertEquals(Utils.writeVariableExporters(ImmutableMap.of("mavenOpts",
|
||||
assertEquals(Utils.writeVariableExporters(ImmutableMap.of("MAVEN_OPTS",
|
||||
"-Xms128m -Xmx256m -XX:+HeapDumpOnOutOfMemoryError"), OsFamily.UNIX),
|
||||
"export MAVEN_OPTS=\"-Xms128m -Xmx256m -XX:+HeapDumpOnOutOfMemoryError\"\n");
|
||||
}
|
||||
|
||||
public void testWriteVariableExportersWindows() {
|
||||
assertEquals(Utils.writeVariableExporters(ImmutableMap.of("mavenOpts",
|
||||
assertEquals(Utils.writeVariableExporters(ImmutableMap.of("MAVEN_OPTS",
|
||||
"-Xms128m -Xmx256m -XX:+HeapDumpOnOutOfMemoryError"), OsFamily.WINDOWS),
|
||||
"set MAVEN_OPTS=-Xms128m -Xmx256m -XX:+HeapDumpOnOutOfMemoryError\r\n");
|
||||
}
|
||||
|
||||
public void testWriteVariableExportersNameReplaceUNIX() {
|
||||
assertEquals(Utils.writeVariableExporters(ImmutableMap.of("LIBRARY_PATH", "{tmp}"), OsFamily.UNIX),
|
||||
"export LD_LIBRARY_PATH=\"/tmp\"\n");
|
||||
}
|
||||
|
||||
public void testWriteVariableExportersNameReplaceWindows() {
|
||||
assertEquals(Utils.writeVariableExporters(ImmutableMap.of("LIBRARY_PATH", "{tmp}"), OsFamily.WINDOWS),
|
||||
"set PATH=%TEMP%\r\n");
|
||||
}
|
||||
|
||||
public void testWritePositionalVarsUNIX() {
|
||||
assertEquals(Utils.writePositionalVars(ImmutableList.of("host", "port"), OsFamily.UNIX),
|
||||
assertEquals(Utils.writePositionalVars(ImmutableList.of("HOST", "PORT"), OsFamily.UNIX),
|
||||
"set HOST=$1\nshift\nset PORT=$1\nshift\n");
|
||||
}
|
||||
|
||||
public void testWritePositionalVarsWindows() {
|
||||
assertEquals(Utils.writePositionalVars(ImmutableList.of("host", "port"), OsFamily.WINDOWS),
|
||||
assertEquals(Utils.writePositionalVars(ImmutableList.of("HOST", "PORT"), OsFamily.WINDOWS),
|
||||
"set HOST=%1\r\nshift\r\nset PORT=%1\r\nshift\r\n");
|
||||
}
|
||||
|
||||
public void testWriteUnsetVariablesUNIX() {
|
||||
assertEquals(Utils.writeUnsetVariables(ImmutableList.of("host", "port"), OsFamily.UNIX),
|
||||
assertEquals(Utils.writeUnsetVariables(ImmutableList.of("HOST", "PORT"), OsFamily.UNIX),
|
||||
"unset HOST PORT\n");
|
||||
}
|
||||
|
||||
public void testWriteUnsetVariablesWindows() {
|
||||
assertEquals(Utils.writeUnsetVariables(ImmutableList.of("host", "port"), OsFamily.WINDOWS),
|
||||
assertEquals(Utils.writeUnsetVariables(ImmutableList.of("HOST", "PORT"), OsFamily.WINDOWS),
|
||||
"set HOST=\r\nset PORT=\r\n");
|
||||
}
|
||||
|
||||
|
||||
public void testWriteUnsetVariablesNameReplaceUNIX() {
|
||||
assertEquals(Utils.writeUnsetVariables(ImmutableList.of("LIBRARY_PATH"), OsFamily.UNIX),
|
||||
"unset LD_LIBRARY_PATH\n");
|
||||
}
|
||||
|
||||
public void testWriteUnsetVariablesNameReplaceWindows() {
|
||||
assertEquals(Utils.writeUnsetVariables(ImmutableList.of("LIBRARY_PATH"), OsFamily.WINDOWS),
|
||||
"set PATH=\r\n");
|
||||
}
|
||||
|
||||
public void testSingleCurlyBraceDoesntBreakLfTokenReplacement() {
|
||||
assertEquals(Utils.replaceTokens("{{lf}", ShellToken.tokenValueMap(OsFamily.UNIX)),
|
||||
|
|
Loading…
Reference in New Issue