Java Code Examples for org.apache.commons.exec.CommandLine#parse()

The following examples show how to use org.apache.commons.exec.CommandLine#parse() . 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: ProcessExecClient.java    From Quicksql with MIT License 6 votes vote down vote up
/**
 * Start a new process to execute.
 *
 * @return log or result output stream
 */
public OutputStream exec() {
    CommandLine commandLine = CommandLine.parse(submit());
    commandLine.addArguments(arguments());
    LOGGER.debug("CommandLine is " + commandLine.toString());

    DefaultExecutor executor = new DefaultExecutor();
    executor.setStreamHandler(new PumpStreamHandler(System.out));
    try {
        executor.execute(commandLine);
    } catch (Exception ex) {
        ex.printStackTrace();
        LOGGER.error("Process executing failed!! Caused by: " + ex.getMessage());
        throw new RuntimeException(ex);
    }

    return System.out;
}
 
Example 2
Source File: ShellInterpreter.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
public void createSecureConfiguration() throws InterpreterException {
  Properties properties = getProperties();
  CommandLine cmdLine = CommandLine.parse(shell);
  cmdLine.addArgument("-c", false);
  String kinitCommand = String.format("kinit -k -t %s %s",
      properties.getProperty("zeppelin.shell.keytab.location"),
      properties.getProperty("zeppelin.shell.principal"));
  cmdLine.addArgument(kinitCommand, false);
  DefaultExecutor executor = new DefaultExecutor();
  try {
    executor.execute(cmdLine);
  } catch (Exception e) {
    LOGGER.error("Unable to run kinit for zeppelin user " + kinitCommand, e);
    throw new InterpreterException(e);
  }
}
 
Example 3
Source File: DockerCompose.java    From football-events with MIT License 6 votes vote down vote up
private String execProcess(String command) {
    logger.debug(command);

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    CommandLine commandline = CommandLine.parse(command);
    DefaultExecutor exec = new DefaultExecutor();
    exec.setStreamHandler(new PumpStreamHandler(outputStream));
    int exitCode;

    try {
        exitCode = exec.execute(commandline);
    } catch (IOException e) {
        throw new RuntimeException("Unable to execute " + command + ": " + outputStream, e);
    }
    if (exitCode != 0) {
        throw new RuntimeException(command + " exited with code " + exitCode + ", " + outputStream);
    }
    String output = outputStream.toString();
    logger.debug(System.lineSeparator() + output);
    return output;
}
 
Example 4
Source File: NetworkUtils.java    From logstash with Apache License 2.0 6 votes vote down vote up
public String getDockerHostIpAddress(Map<String, String> environment) {
    String ipAddress = LOCALHOST; // Default of localhost
    String dockerMachineName = getDockerMachineName(environment);

    if (!dockerMachineName.isEmpty()) {
        LOG.debug("Docker machine name = " + dockerMachineName);
        CommandLine commandline = CommandLine.parse(DOCKER_MACHINE_IP);
        commandline.addArgument(dockerMachineName);
        LOG.debug("Running exec: " + commandline.toString());
        try {
            ipAddress = StringUtils.strip(runCommand(commandline));
        } catch (IOException e) {
            LOG.error("Unable to run exec command to find ip address.", e);
        }
    } else {
        ipAddress = getDocker0AdapterIPAddress();
    }
    LOG.debug("Returned IP address: " + ipAddress);
    return ipAddress;
}
 
Example 5
Source File: SubmarineShellInterpreter.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
public void createSecureConfiguration() throws InterpreterException {
  Properties properties = getProperties();
  CommandLine cmdLine = CommandLine.parse(shell);
  cmdLine.addArgument("-c", false);
  String kinitCommand = String.format("kinit -k -t %s %s",
      properties.getProperty(SUBMARINE_HADOOP_KEYTAB),
      properties.getProperty(SUBMARINE_HADOOP_PRINCIPAL));
  cmdLine.addArgument(kinitCommand, false);
  DefaultExecutor executor = new DefaultExecutor();
  try {
    executor.execute(cmdLine);
  } catch (Exception e) {
    LOGGER.error("Unable to run kinit for zeppelin user " + kinitCommand, e);
    throw new InterpreterException(e);
  }
}
 
Example 6
Source File: CommandRunnerTest.java    From graphviz-java with Apache License 2.0 6 votes vote down vote up
@Test
void testRunEchoHelloWorld() throws IOException, InterruptedException {
    final CommandLine expected = System.getProperty("os.name").contains("Windows")
            ? CommandLine.parse("cmd /C echo hello world")
            : CommandLine.parse("/bin/sh -c").addArgument("echo hello world", false);

    final CommandLineExecutor cmdExecMock = Mockito.mock(CommandLineExecutor.class);
    final CommandRunner cmdRunner = new CommandBuilder()
            .withShellWrapper(true)
            .withCommandExecutor(cmdExecMock)
            .build();

    cmdRunner.exec(CommandLine.parse("echo hello world"), null, 5000);

    verify(cmdExecMock).execute(runEchoCaptor.capture(), (File) isNull(), eq(5000));
    assertEquals(expected.toString(), runEchoCaptor.getValue().toString());
}
 
Example 7
Source File: NetworkUtils.java    From elasticsearch with Apache License 2.0 6 votes vote down vote up
public static String getDockerHostIpAddress(Map<String, String> environment) {
    String ipAddress = LOCALHOST; // Default of localhost
    String dockerMachineName = getDockerMachineName(environment);

    if (!dockerMachineName.isEmpty()) {
        LOG.debug("Docker machine name = " + dockerMachineName);
        CommandLine commandline = CommandLine.parse(DOCKER_MACHINE_IP);
        commandline.addArgument(dockerMachineName);
        LOG.debug("Running exec: " + commandline.toString());
        try {
            ipAddress = StringUtils.strip(runCommand(commandline));
        } catch (IOException e) {
            LOG.error("Unable to run exec command to find ip address.", e);
        }
    } else {
        ipAddress = getDocker0AdapterIPAddress();
    }
    LOG.debug("Returned IP address: " + ipAddress);
    return ipAddress;
}
 
Example 8
Source File: AbstractTestRestApi.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
public static void ps() {
  DefaultExecutor executor = new DefaultExecutor();
  executor.setStreamHandler(new PumpStreamHandler(System.out, System.err));

  CommandLine cmd = CommandLine.parse("ps");
  cmd.addArgument("aux", false);

  try {
    executor.execute(cmd);
  } catch (IOException e) {
    LOG.error(e.getMessage(), e);
  }
}
 
Example 9
Source File: ProcPragma.java    From CQL with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void execute() {
	try {
		for (String cmd : cmds) {
			CommandLine cmdLine = CommandLine.parse(cmd);
			DefaultExecutor executor = new DefaultExecutor();
			executor.setStreamHandler(new ProcHandler(cmd));
			executor.execute(cmdLine);
		}
	} catch (IOException e) {
		throw new RuntimeException(e);
	}
}
 
Example 10
Source File: JupyterKernelInterpreter.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
private void launchJupyterKernel(int kernelPort)
        throws IOException {
  LOGGER.info("Launching Jupyter Kernel at port: " + kernelPort);
  // copy the python scripts to a temp directory, then launch jupyter kernel in that folder
  this.kernelWorkDir = Files.createTempDirectory(
          "zeppelin_jupyter_kernel_" + getKernelName()).toFile();
  String[] kernelScripts = {"kernel_server.py", "kernel_pb2.py", "kernel_pb2_grpc.py"};
  for (String kernelScript : kernelScripts) {
    URL url = getClass().getClassLoader().getResource("grpc/jupyter"
            + "/" + kernelScript);
    FileUtils.copyURLToFile(url, new File(kernelWorkDir, kernelScript));
  }

  CommandLine cmd = CommandLine.parse(pythonExecutable);
  cmd.addArgument(kernelWorkDir.getAbsolutePath() + "/kernel_server.py");
  cmd.addArgument(getKernelName());
  cmd.addArgument(kernelPort + "");

  Map<String, String> envs = setupKernelEnv();
  jupyterKernelProcessLauncher = new JupyterKernelProcessLauncher(cmd, envs);
  jupyterKernelProcessLauncher.launch();
  jupyterKernelProcessLauncher.waitForReady(kernelLaunchTimeout);

  if (jupyterKernelProcessLauncher.isLaunchTimeout()) {
    throw new IOException("Fail to launch Jupyter Kernel in " + kernelLaunchTimeout / 1000
            + " seconds.\n" + jupyterKernelProcessLauncher.getErrorMessage());
  }
  if (!jupyterKernelProcessLauncher.isRunning()) {
    throw new IOException("Fail to launch Jupyter Kernel as the python process is failed.\n"
            + jupyterKernelProcessLauncher.getErrorMessage());
  }
}
 
Example 11
Source File: Worker.java    From s3-bucket-loader with Apache License 2.0 5 votes vote down vote up
private void runPreValidateModeCommands(Properties props) throws Exception {
	
	// pre-validate command and environment vars
	String preValCmd 		= 	props.getProperty("worker.pre.validate.cmd");
	String preValCmdEnv 	= 	props.getProperty("worker.pre.validate.cmd.env");
	
	// note that either of these can reference %worker.initialize.cmd% and/or %worker.initialize.cmd.env%
	// so we will replace them if present
	preValCmd = StringUtils.replace(preValCmd, "%worker.initialize.cmd%", props.getProperty("worker.initialize.cmd"));
	preValCmdEnv = StringUtils.replace(preValCmdEnv, "%worker.initialize.cmd.env%", props.getProperty("worker.initialize.cmd.env"));
	
	if (preValCmd != null) {
		
		Map<String,String> env = null;
		if (preValCmdEnv != null) {
			env = Splitter.on(",").withKeyValueSeparator("=").split(preValCmdEnv);
		}
		
		// preValCmd can have multiple delimited by ;
		List<String> cmdsToRun = new ArrayList<String>();
		if (preValCmd.indexOf(";") != -1) {
			cmdsToRun.addAll(Arrays.asList(preValCmd.split(";")));
		}
		
		for (String cmd : cmdsToRun) {
			// execute it!
			logger.debug("Running pre.validate command: " + cmd);
			CommandLine cmdLine = CommandLine.parse(cmd);
			DefaultExecutor executor = new DefaultExecutor();
			executor.execute(cmdLine, env);
		}
		
	}
}
 
Example 12
Source File: CommonsExecOsCommandOperations.java    From spring-data-dev-tools with Apache License 2.0 5 votes vote down vote up
private Future<CommandResult> executeCommand(String command, File executionDirectory, boolean silent)
		throws IOException {

	StringWriter writer = new StringWriter();
	DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();

	try (WriterOutputStream outputStream = new WriterOutputStream(writer)) {

		String outerCommand = "/bin/bash -lc";

		CommandLine outer = CommandLine.parse(outerCommand);
		outer.addArgument(command, false);

		DefaultExecutor executor = new DefaultExecutor();
		executor.setWorkingDirectory(executionDirectory);
		executor.setStreamHandler(new PumpStreamHandler(silent ? outputStream : System.out, null));
		executor.execute(outer, ENVIRONMENT, resultHandler);

		resultHandler.waitFor();

	} catch (InterruptedException e) {
		throw new IllegalStateException(e);
	}

	return new AsyncResult<CommandResult>(
			new CommandResult(resultHandler.getExitValue(), writer.toString(), resultHandler.getException()));
}
 
Example 13
Source File: YeomanMojo.java    From yeoman-maven-plugin with Apache License 2.0 5 votes vote down vote up
void executeCommand(String command) throws MojoExecutionException {
    try {
        if (isWindows()) {
            command = "cmd /c " + command;
        }
        CommandLine cmdLine = CommandLine.parse(command);
        DefaultExecutor executor = new DefaultExecutor();
        executor.setWorkingDirectory(yeomanProjectDirectory);
        executor.execute(cmdLine);
    } catch (IOException e) {
        throw new MojoExecutionException("Error during : " + command, e);
    }
}
 
Example 14
Source File: GitHubExampleIT.java    From feign with Apache License 2.0 5 votes vote down vote up
@Test
public void runMain() throws Exception {
  final String jar = Arrays.stream(new File("target").listFiles())
      .filter(file -> file.getName().startsWith("feign-example-github")
          && file.getName().endsWith(".jar"))
      .findFirst()
      .map(File::getAbsolutePath)
      .get();

  final String line = "java -jar " + jar;
  final CommandLine cmdLine = CommandLine.parse(line);
  final int exitValue = new DefaultExecutor().execute(cmdLine);

  assertThat(exitValue, CoreMatchers.equalTo(0));
}
 
Example 15
Source File: TaskLaunchScheduledService.java    From shardingsphere-elasticjob-cloud with Apache License 2.0 5 votes vote down vote up
private Protos.CommandInfo buildCommand(final Protos.CommandInfo.URI uri, final String script, final ShardingContexts shardingContexts, final boolean isCommandExecutor) {
    Protos.CommandInfo.Builder result = Protos.CommandInfo.newBuilder().addUris(uri).setShell(true);
    if (isCommandExecutor) {
        CommandLine commandLine = CommandLine.parse(script);
        commandLine.addArgument(GsonFactory.getGson().toJson(shardingContexts), false);
        result.setValue(Joiner.on(" ").join(commandLine.getExecutable(), Joiner.on(" ").join(commandLine.getArguments())));
    } else {
        result.setValue(script);
    }
    return result.build();
}
 
Example 16
Source File: WikipediaExampleIT.java    From feign with Apache License 2.0 5 votes vote down vote up
@Test
public void runMain() throws Exception {
  final String jar = Arrays.stream(new File("target").listFiles())
      .filter(file -> file.getName().startsWith("feign-example-wikipedia")
          && file.getName().endsWith(".jar"))
      .findFirst()
      .map(File::getAbsolutePath)
      .get();

  final String line = "java -jar " + jar;
  final CommandLine cmdLine = CommandLine.parse(line);
  final int exitValue = new DefaultExecutor().execute(cmdLine);

  assertThat(exitValue, CoreMatchers.equalTo(0));
}
 
Example 17
Source File: LineUtils.java    From super-cloudops with Apache License 2.0 5 votes vote down vote up
/**
 * Execution shell commands
 * 
 * @param line
 * @param timeout
 * @param charset
 * @return
 */
public static String execAsString(String line, long timeout, String charset) {
	// Standard output
	ByteArrayOutputStream out = new ByteArrayOutputStream();
	// Error output
	ByteArrayOutputStream err = new ByteArrayOutputStream();
	CommandLine commandline = CommandLine.parse(line);
	DefaultExecutor exec = new DefaultExecutor();
	exec.setExitValues(null);
	// Timeout
	ExecuteWatchdog watch = new ExecuteWatchdog(timeout);
	exec.setWatchdog(watch);
	PumpStreamHandler handler = new PumpStreamHandler(out, err);
	exec.setStreamHandler(handler);
	try {
		exec.execute(commandline);
		// Different operating systems should pay attention to coding,
		// otherwise the results will be scrambled.
		String error = err.toString(charset);
		if (isNotBlank(error)) {
			throw new IllegalStateException(error.toString());
		}
		return out.toString(charset);
	} catch (IOException e) {
		throw new IllegalStateException(e);
	}
}
 
Example 18
Source File: LinuxUtil.java    From SikuliX1 with MIT License 5 votes vote down vote up
@Override
public App get(App app) {
  int pid;
  if (app == null) {
    return app;
  }
  pid = app.getPID();
  if (!app.isClosing() && pid < 0) {
    if (app.getNameGiven() != null && !app.getNameGiven().isEmpty()) {
      pid = findWindowPID(app.getNameGiven(), 0);
      app.setPID(pid);
    }
    return app;
  }
  if (app.isClosing() && pid > -1) {
    DefaultExecutor executor = new DefaultExecutor();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    executor.setStreamHandler(new PumpStreamHandler(outputStream));
    CommandLine command = CommandLine.parse("ps -p " + pid);
    try {
      executor.execute(command);
      app.setPID(pid);
    } catch (Exception e) {
      if (outputStream.toString().split("\\n").length == 1) {
        app.setPID(-1);
        app.setWindow("");
      } else {
        Debug.log(3, "[error] LinuxUtil::executeCommand: %s (%s)", command, e.getMessage());
        logCommandSysout(command.toString(), outputStream);
      }
    }
  }
  return app;
}
 
Example 19
Source File: BenchmarkRunner.java    From trainbenchmark with Eclipse Public License 1.0 4 votes vote down vote up
public static int runPerformanceBenchmark(final BenchmarkConfig bc, final ExecutionConfig ec)
		throws IOException, InterruptedException {
	final Joiner joiner = Joiner.on(", ");
	System.out.println("Running benchmark.");
	System.out.println("Workload: " + bc.getConfigBase().getWorkload());
	System.out.println("Tool: " + bc.getToolName());
	System.out.println("Model: " + bc.getConfigBase().getModelPath());
	System.out.println("Description: " + bc.getDescription());
	System.out.println("Operations: [" + joiner.join(bc.getConfigBase().getOperations()) + "]");
	System.out.println("Execution configuration: " + ec);
	System.out.println("Runs: " + bc.getConfigBase().getRuns());

	final File configFile = File.createTempFile("trainbenchmark-benchmark-", ".conf");
	final String configPath = configFile.getAbsolutePath();
	bc.saveToFile(configPath);

	final String projectName = String.format("trainbenchmark-tool-%s", bc.getProjectName());
	final String jarPath = String.format("../%s/build/libs/%s-1.0.0-SNAPSHOT-fat.jar %s", projectName, projectName,
			configPath);

	final String javaCommand = String.format("java -Xms%s -Xmx%s -server -jar %s %s", ec.getXms(), ec.getXmx(),
			jarPath, configPath);
	final CommandLine cmdLine = CommandLine.parse(javaCommand);

	final long timeoutInSeconds = bc.getConfigBase().getTimeout();
	final long timeoutInMilliseconds = timeoutInSeconds * 1000;
	final ExecuteWatchdog watchdog = new ExecuteWatchdog(timeoutInMilliseconds);
	final Executor executor = new DefaultExecutor();
	executor.setWatchdog(watchdog);
	executor.setStreamHandler(new PumpStreamHandler());
	try {
		final int exitValue = executor.execute(cmdLine);
		System.out.println();
		return exitValue;
	} catch (final ExecuteException e) {
		if (watchdog.killedProcess()) {
			System.out.println("Process timed out.");
		} else {
			e.printStackTrace(System.out);
		}
		return e.getExitValue();
	}
}
 
Example 20
Source File: RemoteInterpreterManagedProcess.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
@Override
public void start(String userName) throws IOException {
  // start server process
  CommandLine cmdLine = CommandLine.parse(interpreterRunner);
  cmdLine.addArgument("-d", false);
  cmdLine.addArgument(interpreterDir, false);
  cmdLine.addArgument("-c", false);
  cmdLine.addArgument(intpEventServerHost, false);
  cmdLine.addArgument("-p", false);
  cmdLine.addArgument(String.valueOf(intpEventServerPort), false);
  cmdLine.addArgument("-r", false);
  cmdLine.addArgument(interpreterPortRange, false);
  cmdLine.addArgument("-i", false);
  cmdLine.addArgument(interpreterGroupId, false);
  if (isUserImpersonated && !userName.equals("anonymous")) {
    cmdLine.addArgument("-u", false);
    cmdLine.addArgument(userName, false);
  }
  cmdLine.addArgument("-l", false);
  cmdLine.addArgument(localRepoDir, false);
  cmdLine.addArgument("-g", false);
  cmdLine.addArgument(interpreterSettingName, false);

  interpreterProcessLauncher = new InterpreterProcessLauncher(cmdLine, env);
  interpreterProcessLauncher.launch();
  interpreterProcessLauncher.waitForReady(getConnectTimeout());
  if (interpreterProcessLauncher.isLaunchTimeout()) {
    throw new IOException(String.format("Interpreter Process creation is time out in %d seconds",
            getConnectTimeout()/1000) + "\n" + "You can increase timeout threshold via " +
            "setting zeppelin.interpreter.connect.timeout of this interpreter.\n" +
            interpreterProcessLauncher.getErrorMessage());
  }

  if (!interpreterProcessLauncher.isRunning()) {
    throw new IOException("Fail to launch interpreter process:\n" +
            interpreterProcessLauncher.getErrorMessage());
  } else {
    String launchOutput = interpreterProcessLauncher.getProcessLaunchOutput();
    Matcher m = YARN_APP_PATTER.matcher(launchOutput);
    if (m.find()) {
      String appId = m.group(1);
      LOGGER.info("Detected yarn app: " + appId + ", add it to YarnAppMonitor");
      YarnAppMonitor.get().addYarnApp(ConverterUtils.toApplicationId(appId), this);
    }
  }
}