diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/ContainerExecutor.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/ContainerExecutor.java index 8357ebaf126..5a19d2b8e2a 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/ContainerExecutor.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/ContainerExecutor.java @@ -330,6 +330,9 @@ public abstract class ContainerExecutor implements Configurable { whitelist.add(ApplicationConstants.Environment.HADOOP_CONF_DIR.name()); whitelist.add(ApplicationConstants.Environment.JAVA_HOME.name()); + // Add "set -o pipefail -e" to validate launch_container script. + sb.setExitOnFailure(); + if (environment != null) { for (Map.Entry env : environment.entrySet()) { if (!whitelist.contains(env.getKey())) { diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/launcher/ContainerLaunch.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/launcher/ContainerLaunch.java index c2d8222b370..d29afd8b949 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/launcher/ContainerLaunch.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/main/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/launcher/ContainerLaunch.java @@ -907,6 +907,10 @@ public class ContainerLaunch implements Callable { sb.append(LINE_SEPARATOR); } + public void setExitOnFailure() { + // Dummy implementation + } + protected abstract void link(Path src, Path dst) throws IOException; protected abstract void mkdir(Path path) throws IOException; @@ -984,6 +988,11 @@ public class ContainerLaunch implements Callable { output.toString(), "\""); line("find -L . -maxdepth 5 -type l -ls 1>>\"", output.toString(), "\""); } + + @Override + public void setExitOnFailure() { + line("set -o pipefail -e"); + } } private static final class WindowsShellScriptBuilder diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/launcher/TestContainerLaunch.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/launcher/TestContainerLaunch.java index 2772e6f2b8d..ea46c29e825 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/launcher/TestContainerLaunch.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager/src/test/java/org/apache/hadoop/yarn/server/nodemanager/containermanager/launcher/TestContainerLaunch.java @@ -40,6 +40,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.StringTokenizer; @@ -1496,4 +1497,108 @@ public class TestContainerLaunch extends BaseContainerManagerTest { verify(updaterNoCall, never()).reportException((Exception)any()); } + + /* + * ${foo.version} is substituted to suffix a specific version number + */ + @Test + public void testInvalidEnvVariableSubstitutionType1() throws IOException { + Map env = new HashMap(); + // invalid env + env.put("testVar", "version${foo.version}"); + validateShellExecutorForDifferentEnvs(env); + } + + /* + * Multiple paths are substituted in a path variable + */ + @Test + public void testInvalidEnvVariableSubstitutionType2() throws IOException { + Map env = new HashMap(); + // invalid env + env.put("testPath", "/abc:/${foo.path}:/$bar"); + validateShellExecutorForDifferentEnvs(env); + } + + private void validateShellExecutorForDifferentEnvs(Map env) + throws IOException { + File shellFile = null; + try { + shellFile = Shell.appendScriptExtension(tmpDir, "hello"); + Map> resources = new HashMap>(); + FileOutputStream fos = new FileOutputStream(shellFile); + FileUtil.setExecutable(shellFile, true); + + List commands = new ArrayList(); + DefaultContainerExecutor executor = new DefaultContainerExecutor(); + executor.setConf(new Configuration()); + executor.writeLaunchEnv(fos, env, resources, commands, + new Path(localLogDir.getAbsolutePath()), user); + fos.flush(); + fos.close(); + + // It is supposed that LANG is set as C. + Map cmdEnv = new HashMap(); + cmdEnv.put("LANG", "C"); + Shell.ShellCommandExecutor shexc = new Shell.ShellCommandExecutor( + new String[] { shellFile.getAbsolutePath() }, tmpDir, cmdEnv); + try { + shexc.execute(); + Assert.fail("Should catch exception"); + } catch (ExitCodeException e) { + Assert.assertTrue(shexc.getExitCode() != 0); + } + } finally { + // cleanup + if (shellFile != null && shellFile.exists()) { + shellFile.delete(); + } + } + } + + @Test + public void testValidEnvVariableSubstitution() throws IOException { + File shellFile = null; + try { + shellFile = Shell.appendScriptExtension(tmpDir, "hello"); + Map> resources = + new HashMap>(); + FileOutputStream fos = new FileOutputStream(shellFile); + FileUtil.setExecutable(shellFile, true); + + Map env = new LinkedHashMap(); + // valid env + env.put( + "foo", "2.4.6" ); + env.put( + "testVar", "version${foo}" ); + List commands = new ArrayList(); + DefaultContainerExecutor executor = new DefaultContainerExecutor(); + executor.setConf(new Configuration()); + executor.writeLaunchEnv(fos, env, resources, commands, + new Path(localLogDir.getAbsolutePath()), user); + fos.flush(); + fos.close(); + + // It is supposed that LANG is set as C. + Map cmdEnv = new HashMap(); + cmdEnv.put("LANG", "C"); + Shell.ShellCommandExecutor shexc + = new Shell.ShellCommandExecutor(new String[]{shellFile.getAbsolutePath()}, + tmpDir, cmdEnv); + try { + shexc.execute(); + } catch(ExitCodeException e){ + Assert.fail("Should not catch exception"); + } + Assert.assertTrue(shexc.getExitCode() == 0); + } + finally { + // cleanup + if (shellFile != null + && shellFile.exists()) { + shellFile.delete(); + } + } + } }