Java Code Examples for org.apache.hadoop.util.Shell#ShellCommandExecutor

The following examples show how to use org.apache.hadoop.util.Shell#ShellCommandExecutor . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ShellExecEndpointCoprocessor.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Override
public void shellExec(
  final RpcController controller,
  final ShellExecRequest request,
  final RpcCallback<ShellExecResponse> done
) {
  final String command = request.getCommand();
  if (StringUtils.isBlank(command)) {
    throw new RuntimeException("Request contained an empty command.");
  }
  final boolean awaitResponse = !request.hasAwaitResponse() || request.getAwaitResponse();
  final String[] subShellCmd = new String[] { "/usr/bin/env", "bash", "-c", command };
  final Shell.ShellCommandExecutor shell = new Shell.ShellCommandExecutor(subShellCmd);

  final String msgFmt = "Executing command"
    + (!awaitResponse ? " on a background thread" : "") + ": {}";
  LOG.info(msgFmt, command);

  if (awaitResponse) {
    runForegroundTask(shell, controller, done);
  } else {
    runBackgroundTask(shell, done);
  }
}
 
Example 2
Source File: ShellExecEndpointCoprocessor.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * Execute {@code shell} and collect results into {@code builder} as side-effects.
 */
private void doExec(
  final Shell.ShellCommandExecutor shell,
  final ShellExecResponse.Builder builder
) throws IOException {
  try {
    shell.execute();
    builder
      .setExitCode(shell.getExitCode())
      .setStdout(shell.getOutput());
  } catch (Shell.ExitCodeException e) {
    LOG.warn("Launched process failed", e);
    builder
      .setExitCode(e.getExitCode())
      .setStdout(shell.getOutput())
      .setStderr(e.getMessage());
  }
}
 
Example 3
Source File: TestContainerLaunch.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * Test that script exists with non-zero exit code when command fails.
 * @throws IOException
 */
@Test (timeout = 10000)
public void testShellScriptBuilderNonZeroExitCode() throws IOException {
  ShellScriptBuilder builder = ShellScriptBuilder.create();
  builder.command(Arrays.asList(new String[] {"unknownCommand"}));
  File shellFile = Shell.appendScriptExtension(tmpDir, "testShellScriptBuilderError");
  PrintStream writer = new PrintStream(new FileOutputStream(shellFile));
  builder.write(writer);
  writer.close();
  try {
    FileUtil.setExecutable(shellFile, true);

    Shell.ShellCommandExecutor shexc = new Shell.ShellCommandExecutor(
        new String[]{shellFile.getAbsolutePath()}, tmpDir);
    try {
      shexc.execute();
      fail("builder shell command was expected to throw");
    }
    catch(IOException e) {
      // expected
      System.out.println("Received an expected exception: " + e.getMessage());
    }
  }
  finally {
    FileUtil.fullyDelete(shellFile);
  }
}
 
Example 4
Source File: TestDockerContainerExecutor.java    From hadoop with Apache License 2.0 5 votes vote down vote up
private Shell.ShellCommandExecutor shellExec(String command) {
  try {

    Shell.ShellCommandExecutor shExec = new Shell.ShellCommandExecutor(
        command.split("\\s+"),
        new File(workDir.toUri().getPath()),
        System.getenv());
    shExec.execute();
    return shExec;
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example 5
Source File: TestContainerLaunch.java    From big-c with Apache License 2.0 5 votes vote down vote up
/**
 * Test that script exists with non-zero exit code when command fails.
 * @throws IOException
 */
@Test (timeout = 10000)
public void testShellScriptBuilderNonZeroExitCode() throws IOException {
  ShellScriptBuilder builder = ShellScriptBuilder.create();
  builder.command(Arrays.asList(new String[] {"unknownCommand"}));
  File shellFile = Shell.appendScriptExtension(tmpDir, "testShellScriptBuilderError");
  PrintStream writer = new PrintStream(new FileOutputStream(shellFile));
  builder.write(writer);
  writer.close();
  try {
    FileUtil.setExecutable(shellFile, true);

    Shell.ShellCommandExecutor shexc = new Shell.ShellCommandExecutor(
        new String[]{shellFile.getAbsolutePath()}, tmpDir);
    try {
      shexc.execute();
      fail("builder shell command was expected to throw");
    }
    catch(IOException e) {
      // expected
      System.out.println("Received an expected exception: " + e.getMessage());
    }
  }
  finally {
    FileUtil.fullyDelete(shellFile);
  }
}
 
Example 6
Source File: TestDockerContainerExecutor.java    From big-c with Apache License 2.0 5 votes vote down vote up
private Shell.ShellCommandExecutor shellExec(String command) {
  try {

    Shell.ShellCommandExecutor shExec = new Shell.ShellCommandExecutor(
        command.split("\\s+"),
        new File(workDir.toUri().getPath()),
        System.getenv());
    shExec.execute();
    return shExec;
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example 7
Source File: RSGroupInfoManagerImpl.java    From hbase with Apache License 2.0 5 votes vote down vote up
RSGroupMappingScript(Configuration conf) {
  String script = conf.get(RS_GROUP_MAPPING_SCRIPT);
  if (script == null || script.isEmpty()) {
    return;
  }

  rsgroupMappingScript = new Shell.ShellCommandExecutor(
    new String[] { script, "", "" }, null, null,
    conf.getLong(RS_GROUP_MAPPING_SCRIPT_TIMEOUT, 5000) // 5 seconds
  );
}
 
Example 8
Source File: ShellExecEndpointCoprocessor.java    From hbase with Apache License 2.0 5 votes vote down vote up
private void runForegroundTask(
  final Shell.ShellCommandExecutor shell,
  final RpcController controller,
  final RpcCallback<ShellExecResponse> done
) {
  ShellExecResponse.Builder builder = ShellExecResponse.newBuilder();
  try {
    doExec(shell, builder);
  } catch (IOException e) {
    LOG.error("Failure launching process", e);
    CoprocessorRpcUtils.setControllerException(controller, e);
  }
  done.run(builder.build());
}
 
Example 9
Source File: TestContainerLaunch.java    From hadoop with Apache License 2.0 4 votes vote down vote up
@Test (timeout = 20000)
public void testInvalidEnvSyntaxDiagnostics() throws IOException  {

  File shellFile = null;
  try {
    shellFile = Shell.appendScriptExtension(tmpDir, "hello");
    Map<Path, List<String>> resources =
        new HashMap<Path, List<String>>();
    FileOutputStream fos = new FileOutputStream(shellFile);
    FileUtil.setExecutable(shellFile, true);

    Map<String, String> env = new HashMap<String, String>();
    // invalid env
    env.put(
        "APPLICATION_WORKFLOW_CONTEXT", "{\"workflowId\":\"609f91c5cd83\"," +
        "\"workflowName\":\"\n\ninsert table " +
        "\npartition (cd_education_status)\nselect cd_demo_sk, cd_gender, " );
    List<String> commands = new ArrayList<String>();
    new DefaultContainerExecutor().writeLaunchEnv(fos, env, resources, commands);
    fos.flush();
    fos.close();

    // It is supposed that LANG is set as C.
    Map<String, String> cmdEnv = new HashMap<String, String>();
    cmdEnv.put("LANG", "C");
    Shell.ShellCommandExecutor shexc
    = new Shell.ShellCommandExecutor(new String[]{shellFile.getAbsolutePath()},
      tmpDir, cmdEnv);
    String diagnostics = null;
    try {
      shexc.execute();
      Assert.fail("Should catch exception");
    } catch(ExitCodeException e){
      diagnostics = e.getMessage();
    }
    Assert.assertTrue(diagnostics.contains(Shell.WINDOWS ?
        "is not recognized as an internal or external command" :
        "command not found"));
    Assert.assertTrue(shexc.getExitCode() != 0);
  }
  finally {
    // cleanup
    if (shellFile != null
        && shellFile.exists()) {
      shellFile.delete();
    }
  }
}
 
Example 10
Source File: TestContainerLaunch.java    From hadoop with Apache License 2.0 4 votes vote down vote up
@Test (timeout = 20000)
public void testContainerLaunchStdoutAndStderrDiagnostics() throws IOException {

  File shellFile = null;
  try {
    shellFile = Shell.appendScriptExtension(tmpDir, "hello");
    // echo "hello" to stdout and "error" to stderr and exit code with 2;
    String command = Shell.WINDOWS ?
        "@echo \"hello\" & @echo \"error\" 1>&2 & exit /b 2" :
        "echo \"hello\"; echo \"error\" 1>&2; exit 2;";
    PrintWriter writer = new PrintWriter(new FileOutputStream(shellFile));
    FileUtil.setExecutable(shellFile, true);
    writer.println(command);
    writer.close();
    Map<Path, List<String>> resources =
        new HashMap<Path, List<String>>();
    FileOutputStream fos = new FileOutputStream(shellFile, true);

    Map<String, String> env = new HashMap<String, String>();
    List<String> commands = new ArrayList<String>();
    commands.add(command);
    ContainerExecutor exec = new DefaultContainerExecutor();
    exec.writeLaunchEnv(fos, env, resources, commands);
    fos.flush();
    fos.close();

    Shell.ShellCommandExecutor shexc
    = new Shell.ShellCommandExecutor(new String[]{shellFile.getAbsolutePath()}, tmpDir);
    String diagnostics = null;
    try {
      shexc.execute();
      Assert.fail("Should catch exception");
    } catch(ExitCodeException e){
      diagnostics = e.getMessage();
    }
    // test stderr
    Assert.assertTrue(diagnostics.contains("error"));
    // test stdout
    Assert.assertTrue(shexc.getOutput().contains("hello"));
    Assert.assertTrue(shexc.getExitCode() == 2);
  }
  finally {
    // cleanup
    if (shellFile != null
        && shellFile.exists()) {
      shellFile.delete();
    }
  }
}
 
Example 11
Source File: TestContainerLaunch.java    From big-c with Apache License 2.0 4 votes vote down vote up
@Test (timeout = 20000)
public void testInvalidEnvSyntaxDiagnostics() throws IOException  {

  File shellFile = null;
  try {
    shellFile = Shell.appendScriptExtension(tmpDir, "hello");
    Map<Path, List<String>> resources =
        new HashMap<Path, List<String>>();
    FileOutputStream fos = new FileOutputStream(shellFile);
    FileUtil.setExecutable(shellFile, true);

    Map<String, String> env = new HashMap<String, String>();
    // invalid env
    env.put(
        "APPLICATION_WORKFLOW_CONTEXT", "{\"workflowId\":\"609f91c5cd83\"," +
        "\"workflowName\":\"\n\ninsert table " +
        "\npartition (cd_education_status)\nselect cd_demo_sk, cd_gender, " );
    List<String> commands = new ArrayList<String>();
    new DefaultContainerExecutor().writeLaunchEnv(fos, env, resources, commands);
    fos.flush();
    fos.close();

    // It is supposed that LANG is set as C.
    Map<String, String> cmdEnv = new HashMap<String, String>();
    cmdEnv.put("LANG", "C");
    Shell.ShellCommandExecutor shexc
    = new Shell.ShellCommandExecutor(new String[]{shellFile.getAbsolutePath()},
      tmpDir, cmdEnv);
    String diagnostics = null;
    try {
      shexc.execute();
      Assert.fail("Should catch exception");
    } catch(ExitCodeException e){
      diagnostics = e.getMessage();
    }
    Assert.assertTrue(diagnostics.contains(Shell.WINDOWS ?
        "is not recognized as an internal or external command" :
        "command not found"));
    Assert.assertTrue(shexc.getExitCode() != 0);
  }
  finally {
    // cleanup
    if (shellFile != null
        && shellFile.exists()) {
      shellFile.delete();
    }
  }
}
 
Example 12
Source File: TestContainerLaunch.java    From big-c with Apache License 2.0 4 votes vote down vote up
@Test (timeout = 20000)
public void testContainerLaunchStdoutAndStderrDiagnostics() throws IOException {

  File shellFile = null;
  try {
    shellFile = Shell.appendScriptExtension(tmpDir, "hello");
    // echo "hello" to stdout and "error" to stderr and exit code with 2;
    String command = Shell.WINDOWS ?
        "@echo \"hello\" & @echo \"error\" 1>&2 & exit /b 2" :
        "echo \"hello\"; echo \"error\" 1>&2; exit 2;";
    PrintWriter writer = new PrintWriter(new FileOutputStream(shellFile));
    FileUtil.setExecutable(shellFile, true);
    writer.println(command);
    writer.close();
    Map<Path, List<String>> resources =
        new HashMap<Path, List<String>>();
    FileOutputStream fos = new FileOutputStream(shellFile, true);

    Map<String, String> env = new HashMap<String, String>();
    List<String> commands = new ArrayList<String>();
    commands.add(command);
    ContainerExecutor exec = new DefaultContainerExecutor();
    exec.writeLaunchEnv(fos, env, resources, commands);
    fos.flush();
    fos.close();

    Shell.ShellCommandExecutor shexc
    = new Shell.ShellCommandExecutor(new String[]{shellFile.getAbsolutePath()}, tmpDir);
    String diagnostics = null;
    try {
      shexc.execute();
      Assert.fail("Should catch exception");
    } catch(ExitCodeException e){
      diagnostics = e.getMessage();
    }
    // test stderr
    Assert.assertTrue(diagnostics.contains("error"));
    // test stdout
    Assert.assertTrue(shexc.getOutput().contains("hello"));
    Assert.assertTrue(shexc.getExitCode() == 2);
  }
  finally {
    // cleanup
    if (shellFile != null
        && shellFile.exists()) {
      shellFile.delete();
    }
  }
}