mirror of https://github.com/apache/jclouds.git
Issue 126: added environment setting script builder
git-svn-id: http://jclouds.googlecode.com/svn/trunk@2345 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
parent
fbf9225f5d
commit
13d37f554d
|
@ -0,0 +1,84 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Copyright (C) 2009 Cloud Conscious, LLC. <info@cloudconscious.com>
|
||||||
|
*
|
||||||
|
* ====================================================================
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one
|
||||||
|
* or more contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. The ASF licenses this file
|
||||||
|
* to you under the Apache License, Version 2.0 (the
|
||||||
|
* "License"); you may not use this file except in compliance
|
||||||
|
* with the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
* ====================================================================
|
||||||
|
*/
|
||||||
|
package org.jclouds.initbuilder;
|
||||||
|
|
||||||
|
import static com.google.common.base.Preconditions.checkNotNull;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.jclouds.initbuilder.domain.OsFamily;
|
||||||
|
import org.jclouds.initbuilder.domain.ShellToken;
|
||||||
|
import org.jclouds.initbuilder.util.Utils;
|
||||||
|
|
||||||
|
import com.google.common.annotations.VisibleForTesting;
|
||||||
|
import com.google.common.collect.ImmutableMap;
|
||||||
|
import com.google.common.collect.Maps;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an environment file
|
||||||
|
*
|
||||||
|
* @author Adrian Cole
|
||||||
|
*/
|
||||||
|
public class EnvBuilder {
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
Map<String, String> variables = Maps.newHashMap();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Exports a variable inside the script
|
||||||
|
*/
|
||||||
|
public EnvBuilder export(String name, String value) {
|
||||||
|
variables.put(checkNotNull(name, "name"), checkNotNull(value, "value"));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* builds the environment file, by adding the following
|
||||||
|
* <ol>
|
||||||
|
* <li>example usage</li>
|
||||||
|
* <li>variable exports</li>
|
||||||
|
* <li>return statement</li>
|
||||||
|
* </ol>
|
||||||
|
*
|
||||||
|
* @param osFamily
|
||||||
|
* whether to write a cmd or bash script.
|
||||||
|
*/
|
||||||
|
public String build(final OsFamily osFamily) {
|
||||||
|
StringBuilder builder = new StringBuilder();
|
||||||
|
builder.append(Utils.writeComment(" Env file: please do not confuse people by making this executable", osFamily));
|
||||||
|
builder.append(Utils.writeComment("", osFamily));
|
||||||
|
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"),
|
||||||
|
osFamily), osFamily));
|
||||||
|
builder.append(Utils.writeVariableExporters(variables, osFamily));
|
||||||
|
builder.append(ShellToken.LF.to(osFamily));
|
||||||
|
builder.append(Utils.writeComment(
|
||||||
|
" Please retain this statement so that the script can be validated", osFamily));
|
||||||
|
builder.append(ShellToken.RETURN.to(osFamily)).append(" 0")
|
||||||
|
.append(ShellToken.LF.to(osFamily));
|
||||||
|
return builder.toString();
|
||||||
|
}
|
||||||
|
}
|
|
@ -39,7 +39,7 @@ import com.google.common.collect.Maps;
|
||||||
*/
|
*/
|
||||||
public enum ShellToken {
|
public enum ShellToken {
|
||||||
|
|
||||||
FS, PS, LF, SH, SOURCE, REM, ARGS, VARSTART, VAREND, SHEBANG, LIBRARY_PATH_VARIABLE;
|
FS, PS, LF, SH, SOURCE, REM, RETURN, ARGS, VARSTART, VAREND, SHEBANG, LIBRARY_PATH_VARIABLE;
|
||||||
|
|
||||||
private static final Map<OsFamily, Map<String, String>> familyToTokenValueMap = new MapMaker()
|
private static final Map<OsFamily, Map<String, String>> familyToTokenValueMap = new MapMaker()
|
||||||
.makeComputingMap(new Function<OsFamily, Map<String, String>>() {
|
.makeComputingMap(new Function<OsFamily, Map<String, String>>() {
|
||||||
|
@ -77,6 +77,13 @@ public enum ShellToken {
|
||||||
case UNIX:
|
case UNIX:
|
||||||
return ":";
|
return ":";
|
||||||
}
|
}
|
||||||
|
case RETURN:
|
||||||
|
switch (family) {
|
||||||
|
case WINDOWS:
|
||||||
|
return "exit /b";
|
||||||
|
case UNIX:
|
||||||
|
return "return";
|
||||||
|
}
|
||||||
case LF:
|
case LF:
|
||||||
switch (family) {
|
switch (family) {
|
||||||
case WINDOWS:
|
case WINDOWS:
|
||||||
|
|
|
@ -30,6 +30,7 @@ import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import org.jclouds.initbuilder.domain.OsFamily;
|
import org.jclouds.initbuilder.domain.OsFamily;
|
||||||
|
import org.jclouds.initbuilder.domain.ShellToken;
|
||||||
|
|
||||||
import com.google.common.base.CaseFormat;
|
import com.google.common.base.CaseFormat;
|
||||||
import com.google.common.base.Function;
|
import com.google.common.base.Function;
|
||||||
|
@ -227,4 +228,8 @@ public class Utils {
|
||||||
switchClause.append(OS_TO_END_SWITCH_PATTERN.get(family));
|
switchClause.append(OS_TO_END_SWITCH_PATTERN.get(family));
|
||||||
return switchClause.toString();
|
return switchClause.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String writeComment(String comment, OsFamily family) {
|
||||||
|
return String.format("%s%s%s", ShellToken.REM.to(family), comment, ShellToken.LF.to(family));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,59 @@
|
||||||
|
package org.jclouds.initbuilder;
|
||||||
|
|
||||||
|
import static org.testng.Assert.assertEquals;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
|
|
||||||
|
import org.jclouds.initbuilder.domain.OsFamily;
|
||||||
|
import org.jclouds.initbuilder.domain.ShellToken;
|
||||||
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
import com.google.common.base.Charsets;
|
||||||
|
import com.google.common.collect.ImmutableMap;
|
||||||
|
import com.google.common.io.CharStreams;
|
||||||
|
import com.google.common.io.Resources;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests possible uses of EnvBuilder
|
||||||
|
*
|
||||||
|
* @author Adrian Cole
|
||||||
|
*/
|
||||||
|
public class EnvBuilderTest {
|
||||||
|
|
||||||
|
EnvBuilder testScriptBuilder = new EnvBuilder().export("javaHome",
|
||||||
|
"/apps/jdk1.6");
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBuildSimpleWindows() throws MalformedURLException, IOException {
|
||||||
|
assertEquals(testScriptBuilder.build(OsFamily.WINDOWS), CharStreams.toString(Resources
|
||||||
|
.newReaderSupplier(Resources.getResource("test_env."
|
||||||
|
+ ShellToken.SH.to(OsFamily.WINDOWS)), Charsets.UTF_8)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBuildSimpleUNIX() throws MalformedURLException, IOException {
|
||||||
|
assertEquals(testScriptBuilder.build(OsFamily.UNIX), CharStreams.toString(Resources
|
||||||
|
.newReaderSupplier(Resources.getResource("test_env."
|
||||||
|
+ ShellToken.SH.to(OsFamily.UNIX)), Charsets.UTF_8)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testExport() {
|
||||||
|
EnvBuilder builder = new EnvBuilder();
|
||||||
|
builder.export("javaHome", "/apps/jdk1.6");
|
||||||
|
assertEquals(builder.variables, ImmutableMap.of("javaHome", "/apps/jdk1.6"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNoExport() {
|
||||||
|
EnvBuilder builder = new EnvBuilder();
|
||||||
|
assertEquals(builder.variables.size(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expectedExceptions = NullPointerException.class)
|
||||||
|
public void testExportNPE() {
|
||||||
|
new EnvBuilder().export(null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -40,8 +40,8 @@ public class ShellTokenTest {
|
||||||
public void testTokenValueMapUNIX() {
|
public void testTokenValueMapUNIX() {
|
||||||
Map<String, String> expected = new ImmutableMap.Builder<String, String>().put("fs", "/").put(
|
Map<String, String> expected = new ImmutableMap.Builder<String, String>().put("fs", "/").put(
|
||||||
"ps", ":").put("lf", "\n").put("sh", "bash").put("source", ".").put("rem", "#").put(
|
"ps", ":").put("lf", "\n").put("sh", "bash").put("source", ".").put("rem", "#").put(
|
||||||
"args", "$@").put("varstart", "$").put("varend", "").put("libraryPathVariable",
|
"args", "$@").put("varstart", "$").put("return", "return").put("varend", "").put(
|
||||||
"LD_LIBRARY_PATH").put("shebang", "#!/bin/bash\n").build();
|
"libraryPathVariable", "LD_LIBRARY_PATH").put("shebang", "#!/bin/bash\n").build();
|
||||||
|
|
||||||
assertEquals(ShellToken.tokenValueMap(OsFamily.UNIX), expected);
|
assertEquals(ShellToken.tokenValueMap(OsFamily.UNIX), expected);
|
||||||
}
|
}
|
||||||
|
@ -50,7 +50,8 @@ public class ShellTokenTest {
|
||||||
Map<String, String> expected = new ImmutableMap.Builder<String, String>().put("fs", "\\")
|
Map<String, String> expected = new ImmutableMap.Builder<String, String>().put("fs", "\\")
|
||||||
.put("ps", ";").put("lf", "\r\n").put("sh", "cmd").put("source", "@call").put("rem",
|
.put("ps", ";").put("lf", "\r\n").put("sh", "cmd").put("source", "@call").put("rem",
|
||||||
"@rem").put("args", "%*").put("varstart", "%").put("varend", "%").put(
|
"@rem").put("args", "%*").put("varstart", "%").put("varend", "%").put(
|
||||||
"libraryPathVariable", "PATH").put("shebang", "@echo off\r\n").build();
|
"libraryPathVariable", "PATH").put("return", "exit /b").put("shebang",
|
||||||
|
"@echo off\r\n").build();
|
||||||
|
|
||||||
assertEquals(ShellToken.tokenValueMap(OsFamily.WINDOWS), expected);
|
assertEquals(ShellToken.tokenValueMap(OsFamily.WINDOWS), expected);
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,10 @@
|
||||||
|
# Env file: please do not confuse people by making this executable
|
||||||
|
#
|
||||||
|
# Example usage to set a variable
|
||||||
|
#
|
||||||
|
# export MAVEN_OPTS="-Xms64m -Xmx128m"
|
||||||
|
|
||||||
|
export JAVA_HOME="/apps/jdk1.6"
|
||||||
|
|
||||||
|
# Please retain this statement so that the script can be validated
|
||||||
|
return 0
|
|
@ -0,0 +1,10 @@
|
||||||
|
@rem Env file: please do not confuse people by making this executable
|
||||||
|
@rem
|
||||||
|
@rem Example usage to set a variable
|
||||||
|
@rem
|
||||||
|
@rem set MAVEN_OPTS=-Xms64m -Xmx128m
|
||||||
|
|
||||||
|
set JAVA_HOME=/apps/jdk1.6
|
||||||
|
|
||||||
|
@rem Please retain this statement so that the script can be validated
|
||||||
|
exit /b 0
|
|
@ -0,0 +1,27 @@
|
||||||
|
@echo off
|
||||||
|
|
||||||
|
goto END_FUNCTIONS
|
||||||
|
:ABORT_SUB
|
||||||
|
echo Aborting: %EXCEPTION%.
|
||||||
|
exit /b 1
|
||||||
|
|
||||||
|
:SOURCE_ENV
|
||||||
|
set ENV_FILE=%1
|
||||||
|
shift
|
||||||
|
if not defined ENV_FILE (
|
||||||
|
set EXCEPTION=Internal error. Called SOURCE_ENV with no file param
|
||||||
|
exit /b 1
|
||||||
|
)
|
||||||
|
call %SETTINGS_FILE%
|
||||||
|
if errorlevel 1 (
|
||||||
|
set EXCEPTION=Please end your '%SETTINGS_FILE%' file with the command 'exit /b 0' to enable this script to detect syntax errors.
|
||||||
|
exit /b 1
|
||||||
|
)
|
||||||
|
exit /b 0
|
||||||
|
|
||||||
|
:END_FUNCTIONS
|
||||||
|
|
||||||
|
if exist "%APPENV_SETTINGS_FILE%" (
|
||||||
|
call :SOURCE_SF "%APPENV_SETTINGS_FILE%"
|
||||||
|
if errorlevel 1 goto ABORT_SUB
|
||||||
|
)
|
Loading…
Reference in New Issue