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

The following examples show how to use org.apache.commons.exec.CommandLine#addArgument() . 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: SimpleTestServer.java    From p4ic4idea with Apache License 2.0 7 votes vote down vote up
public int getVersion() throws Exception {
	ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
	CommandLine cmdLine = new CommandLine(p4d);
	cmdLine.addArgument("-V");
	DefaultExecutor executor = new DefaultExecutor();
	PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
	executor.setStreamHandler(streamHandler);
	executor.execute(cmdLine);

	int version = 0;
	for (String line : outputStream.toString().split("\\n")) {
		if (line.startsWith("Rev. P4D")) {
			Pattern p = Pattern.compile("\\d{4}\\.\\d{1}");
			Matcher m = p.matcher(line);
			while (m.find()) {
				String found = m.group();
				found = found.replace(".", ""); // strip "."
				version = Integer.parseInt(found);
			}
		}
	}
	logger.info("P4D Version: " + version);
	return version;
}
 
Example 2
Source File: SimpleTestServer.java    From p4ic4idea with Apache License 2.0 6 votes vote down vote up
public int getVersion() throws Exception {
	ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
	CommandLine cmdLine = new CommandLine(p4d);
	cmdLine.addArgument("-V");
	DefaultExecutor executor = new DefaultExecutor();
	PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
	executor.setStreamHandler(streamHandler);
	executor.execute(cmdLine);

	int version = 0;
	for (String line : outputStream.toString().split("\\n")) {
		if (line.startsWith("Rev. P4D")) {
			Pattern p = Pattern.compile("\\d{4}\\.\\d{1}");
			Matcher m = p.matcher(line);
			while (m.find()) {
				String found = m.group();
				found = found.replace(".", ""); // strip "."
				version = Integer.parseInt(found);
			}
		}
	}
	logger.info("P4D Version: " + version);
	return version;
}
 
Example 3
Source File: UNIXUtilsTest.java    From genie with Apache License 2.0 6 votes vote down vote up
/**
 * Test the create user method for user already exists.
 *
 * @throws IOException If there is any problem.
 */
@Test
public void testCreateUserMethodSuccessDoesNotExist1() throws IOException {
    final String user = "user";
    final String group = "group";

    final CommandLine idCheckCommandLine = new CommandLine("id");
    idCheckCommandLine.addArgument("-u");
    idCheckCommandLine.addArgument(user);

    Mockito.when(this.executor.execute(Mockito.any(CommandLine.class))).thenThrow(new IOException());

    final ArgumentCaptor<CommandLine> argumentCaptor = ArgumentCaptor.forClass(CommandLine.class);
    final List<String> command = Arrays.asList("sudo", "useradd", user, "-G", group, "-M");

    try {
        UNIXUtils.createUser(user, group, executor);
    } catch (IOException ge) {
        // ignore
    }

    Mockito.verify(this.executor, Mockito.times(3)).execute(argumentCaptor.capture());
    Assert.assertArrayEquals(command.toArray(), argumentCaptor.getAllValues().get(2).toStrings());
}
 
Example 4
Source File: SimpleTestServer.java    From p4ic4idea with Apache License 2.0 6 votes vote down vote up
public int getVersion() throws Exception {
	ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
	CommandLine cmdLine = new CommandLine(p4d);
	cmdLine.addArgument("-V");
	DefaultExecutor executor = new DefaultExecutor();
	PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
	executor.setStreamHandler(streamHandler);
	executor.execute(cmdLine);

	int version = 0;
	for (String line : outputStream.toString().split("\\n")) {
		if (line.startsWith("Rev. P4D")) {
			Pattern p = Pattern.compile("\\d{4}\\.\\d{1}");
			Matcher m = p.matcher(line);
			while (m.find()) {
				String found = m.group();
				found = found.replace(".", ""); // strip "."
				version = Integer.parseInt(found);
			}
		}
	}
	logger.info("P4D Version: " + version);
	return version;
}
 
Example 5
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 6
Source File: IngestionUtils.java    From Explorer with Apache License 2.0 5 votes vote down vote up
public static DefaultExecuteResultHandler executeBash(String command) throws IOException {
    CommandLine cmdLine = CommandLine.parse("bash");
    cmdLine.addArgument("-c", false);
    cmdLine.addArgument(command, false);

    DefaultExecutor executor = new DefaultExecutor();
    ByteArrayOutputStream outputStreamAgentStart = new ByteArrayOutputStream();
    executor.setStreamHandler(new PumpStreamHandler(outputStreamAgentStart));
    DefaultExecuteResultHandler handler = new DefaultExecuteResultHandler();
    executor.execute(cmdLine, handler);

    return handler;
}
 
Example 7
Source File: TestServer.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
private int innerExec(String... args)
        throws IOException {
    File p4d = P4ExtFileUtils.extractP4d(outDir, version);
    CommandLine cmdLine = createBaseCmdLine(p4d);
    for (String arg : args) {
        cmdLine.addArgument(arg);
    }
    DefaultExecutor executor = new DefaultExecutor();
    // default - log is pumped to stderr.
    PumpStreamHandler streamHandler = new PumpStreamHandler(status.out, status.log);
    executor.setStreamHandler(streamHandler);
    executor.setProcessDestroyer(processDestroyer);
    return executor.execute(cmdLine);
}
 
Example 8
Source File: SimpleTestServer.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
protected int exec(String[] args) throws Exception {
	CommandLine cmdLine = new CommandLine(p4d);
	cmdLine.addArgument("-C0");
	cmdLine.addArgument("-r");
	cmdLine.addArgument(formatPath(p4root.getAbsolutePath()));
	for (String arg : args) {
		cmdLine.addArgument(arg);
	}

	logger.debug("EXEC: " + cmdLine.toString());

	DefaultExecutor executor = new DefaultExecutor();
	return executor.execute(cmdLine);
}
 
Example 9
Source File: SimpleTestServer.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
protected int exec(String[] args) throws Exception {
	CommandLine cmdLine = new CommandLine(p4d);
	cmdLine.addArgument("-C0");
	cmdLine.addArgument("-r");
	cmdLine.addArgument(formatPath(p4root.getAbsolutePath()));
	for (String arg : args) {
		cmdLine.addArgument(arg);
	}

	logger.debug("EXEC: " + cmdLine.toString());

	DefaultExecutor executor = new DefaultExecutor();
	return executor.execute(cmdLine);
}
 
Example 10
Source File: KuduLocal.java    From geowave with Apache License 2.0 5 votes vote down vote up
private void startKuduLocal() throws ExecuteException, IOException, InterruptedException {
  if (!kuduDBDir.exists() && !kuduDBDir.mkdirs()) {
    LOGGER.error("unable to create directory {}", kuduDBDir.getAbsolutePath());
  } else if (!kuduDBDir.isDirectory()) {
    LOGGER.error("{} exists but is not a directory", kuduDBDir.getAbsolutePath());
  }

  final File kuduMasterBinary = new File(kuduLocalDir.getAbsolutePath(), KUDU_MASTER);
  final File kuduTabletBinary = new File(kuduLocalDir.getAbsolutePath(), KUDU_TABLET);

  final CommandLine startMaster = new CommandLine(kuduMasterBinary.getAbsolutePath());
  startMaster.addArgument("--fs_data_dirs");
  startMaster.addArgument(new File(kuduDBDir, "master_fs_data").getAbsolutePath());
  startMaster.addArgument("--fs_metadata_dir");
  startMaster.addArgument(new File(kuduDBDir, "master_fs_metadata").getAbsolutePath());
  startMaster.addArgument("--fs_wal_dir");
  startMaster.addArgument(new File(kuduDBDir, "master_fs_wal").getAbsolutePath());
  executeAsyncAndWatch(startMaster);

  for (int i = 0; i < numTablets; i++) {
    final CommandLine startTablet = new CommandLine(kuduTabletBinary.getAbsolutePath());
    startTablet.addArgument("--fs_data_dirs");
    startTablet.addArgument(new File(kuduDBDir, "t" + i + "_fs_data").getAbsolutePath());
    startTablet.addArgument("--fs_metadata_dir");
    startTablet.addArgument(new File(kuduDBDir, "t" + i + "_fs_metadata").getAbsolutePath());
    startTablet.addArgument("--fs_wal_dir");
    startTablet.addArgument(new File(kuduDBDir, "t" + i + "_fs_wal").getAbsolutePath());
    executeAsyncAndWatch(startTablet);
  }

  Thread.sleep(STARTUP_DELAY_MS);
}
 
Example 11
Source File: LocalAgentLauncherImpl.java    From genie with Apache License 2.0 5 votes vote down vote up
private CommandLine createCommandLine(
    final Map<String, String> argumentValueReplacements
) {
    final List<String> commandLineTemplate = Lists.newArrayList();

    // Run detached with setsid on Linux
    if (SystemUtils.IS_OS_LINUX) {
        commandLineTemplate.add(SETS_ID);
    }

    // Run as different user with sudo
    if (this.launcherProperties.isRunAsUserEnabled()) {
        commandLineTemplate.addAll(Lists.newArrayList("sudo", "-E", "-u", RUN_USER_PLACEHOLDER));
    }

    // Agent  command line to launch agent (i.e. JVM and its options)
    commandLineTemplate.addAll(this.launcherProperties.getLaunchCommandTemplate());

    final CommandLine commandLine = new CommandLine(commandLineTemplate.get(0));

    for (int i = 1; i < commandLineTemplate.size(); i++) {
        final String argument = commandLineTemplate.get(i);
        // If the argument placeholder is a key in the map, replace it with the corresponding value.
        // Otherwise it's not a placeholder, add it as-is to the command-line.
        commandLine.addArgument(argumentValueReplacements.getOrDefault(argument, argument));
    }

    return commandLine;
}
 
Example 12
Source File: TerraformService.java    From cloud-portal with MIT License 5 votes vote down vote up
private CommandLine buildInitCommand(String terraformPath) {

		CommandLine initCommand = new CommandLine(terraformPath);
		initCommand.addArgument(Constants.ACTION_INIT);
		initCommand.addArgument(FLAG_NO_COLOR);

		return initCommand;
	}
 
Example 13
Source File: ProcessExecutor.java    From frontend-maven-plugin with Apache License 2.0 5 votes vote down vote up
private CommandLine createCommandLine(List<String> command) {
    CommandLine commmandLine = new CommandLine(command.get(0));

    for (int i = 1;i < command.size();i++) {
        String argument = command.get(i);
        commmandLine.addArgument(argument, false);
    }

    return commmandLine;
}
 
Example 14
Source File: ScriptJobExecutor.java    From shardingsphere-elasticjob-cloud with Apache License 2.0 5 votes vote down vote up
private void executeScript(final ShardingContext shardingContext, final String scriptCommandLine) {
    CommandLine commandLine = CommandLine.parse(scriptCommandLine);
    commandLine.addArgument(GsonFactory.getGson().toJson(shardingContext), false);
    try {
        new DefaultExecutor().execute(commandLine);
    } catch (final IOException ex) {
        throw new JobConfigurationException("Execute script failure.", ex);
    }
}
 
Example 15
Source File: TestServer.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
@Nonnull
private CommandLine createBaseCmdLine(@Nonnull File p4d) {
    CommandLine cmdLine = new CommandLine(p4d);
    cmdLine
            .addArgument("-r")
            .addArgument(getPathToRoot())
            .addArgument("-p")
            .addArgument(port);
    if (caseSensitive) {
        cmdLine.addArgument(CASE_SENSITIVE_ARG);
    } else {
        cmdLine.addArgument(CASE_INSENSITIVE_ARG);
    }
    return cmdLine;
}
 
Example 16
Source File: PythonInterpreter.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
private void createGatewayServerAndStartScript() throws IOException {
  // start gateway server in JVM side
  int port = RemoteInterpreterUtils.findRandomAvailablePortOnAllLocalInterfaces();
  // use the FQDN as the server address instead of 127.0.0.1 so that python process in docker
  // container can also connect to this gateway server.
  String serverAddress = PythonUtils.getLocalIP(properties);
  String secret = PythonUtils.createSecret(256);
  this.gatewayServer = PythonUtils.createGatewayServer(this, serverAddress, port, secret,
      usePy4jAuth);
  gatewayServer.start();

  // launch python process to connect to the gateway server in JVM side
  createPythonScript();
  String pythonExec = getPythonExec();
  CommandLine cmd = CommandLine.parse(pythonExec);
  if (!pythonExec.endsWith(".py")) {
    // PythonDockerInterpreter set pythonExec with script
    cmd.addArgument(pythonWorkDir + "/zeppelin_python.py", false);
  }
  cmd.addArgument(serverAddress, false);
  cmd.addArgument(Integer.toString(port), false);

  outputStream = new InterpreterOutputStream(LOGGER);
  Map<String, String> env = setupPythonEnv();
  if (usePy4jAuth) {
    env.put("PY4J_GATEWAY_SECRET", secret);
  }
  LOGGER.info("Launching Python Process Command: " + cmd.getExecutable() +
      " " + StringUtils.join(cmd.getArguments(), " "));

  pythonProcessLauncher = new PythonProcessLauncher(cmd, env);
  pythonProcessLauncher.launch();
  pythonProcessLauncher.waitForReady(MAX_TIMEOUT_SEC * 1000);

  if (!pythonProcessLauncher.isRunning()) {
    if (pythonProcessLauncher.isLaunchTimeout()) {
      throw new IOException("Launch python process is time out.\n" +
              pythonProcessLauncher.getErrorMessage());
    } else {
      throw new IOException("Fail to launch python process.\n" +
              pythonProcessLauncher.getErrorMessage());
    }
  }
}
 
Example 17
Source File: StatusCheckerTimer.java    From oxTrust with MIT License 4 votes vote down vote up
@SuppressWarnings("deprecation")
private void setFactorAttributes(ConfigurationStatus configuration, OxtrustStat oxtrustStat) {
	if (!isLinux()) {
		return;
	}
	CommandLine commandLine = new CommandLine(OxTrustConstants.PROGRAM_FACTER);
	String facterVersion = getFacterVersion();
	boolean isOldVersion = false;
	log.debug("Facter version: " + facterVersion);
	String resultOutput;
	if (facterVersion == null) {
		return;
	}
	if (Integer.valueOf(facterVersion.substring(0, 1)) <= 1) {
		isOldVersion = true;
	}
	if (Integer.valueOf(facterVersion.substring(0, 1)) >= 3) {
		log.debug("Running facter in legacy mode");
		commandLine.addArgument("--show-legacy");
	}
	ByteArrayOutputStream bos = new ByteArrayOutputStream(4096);
	try {
		boolean result = ProcessHelper.executeProgram(commandLine, false, 0, bos);
		if (!result) {
			return;
		}
		resultOutput = new String(bos.toByteArray(), "UTF-8");
	} catch (UnsupportedEncodingException ex) {
		log.error("Failed to parse program {} output", OxTrustConstants.PROGRAM_FACTER, ex);
		return;
	} finally {
		IOUtils.closeQuietly(bos);
	}
	String[] outputLines = resultOutput.split("\\r?\\n");

	if (isOldVersion) {
		oxtrustStat.setFreeMemory(getFreeMemory(outputLines, OxTrustConstants.FACTER_FREE_MEMORY,
				OxTrustConstants.FACTER_MEMORY_SIZE));
	} else {
		oxtrustStat.setFreeMemory(getFreeMemory(outputLines, OxTrustConstants.FACTER_FREE_MEMORY_MB,
				OxTrustConstants.FACTER_MEMORY_SIZE_MB));
	}
	oxtrustStat.setFreeSwap(toIntString(getFacterPercentResult(outputLines, OxTrustConstants.FACTER_FREE_SWAP,
			OxTrustConstants.FACTER_FREE_SWAP_TOTAL)));
	String hostname = "";
	try {
		hostname = Files.readAllLines(Paths.get("/install/community-edition-setup/output/hostname")).get(0);
	} catch (IOException e) {
		log.trace("+++++++++++++++++++++++++++++++++: '{}'", "Error reading hostname from file");
	}
	if (hostname.equalsIgnoreCase("localhost")) {
		hostname = getFacterResult(outputLines, OxTrustConstants.FACTER_HOST_NAME);
	}
	configuration.setHostname(hostname);
	oxtrustStat.setIpAddress(getFacterResult(outputLines, OxTrustConstants.FACTER_IP_ADDRESS));
	oxtrustStat.setLoadAvg(getFacterResult(outputLines, OxTrustConstants.FACTER_LOAD_AVERAGE));
	getFacterBandwidth(getFacterResult(outputLines, OxTrustConstants.FACTER_BANDWIDTH_USAGE), configuration);
	oxtrustStat.setSystemUptime(getFacterResult(outputLines, OxTrustConstants.FACTER_SYSTEM_UP_TIME));
}
 
Example 18
Source File: PhantomJS.java    From java-phantomjs-wrapper with MIT License 4 votes vote down vote up
/**
 * Execute a script with environment settings, options and a list of arguments.
 *
 * @param script
 *            path of script to execute
 * @param executionEnvironment
 *            the environment to use for script execution
 * @param options
 *            options to execute
 * @param arguments
 *            list of arguments
 * @return the exit code of the script
 * @throws IOException
 *             if cmd execution fails
 */
public static PhantomJSExecutionResponse exec(final InputStream script, final Map<String, String> executionEnvironment, final PhantomJSOptions options, final CommandLineArgument... arguments) throws IOException {
    if (!PhantomJSSetup.isInitialized()) {
        throw new IllegalStateException("Unable to find and execute PhantomJS binaries");
    }

    if (script == null) {
        throw new IllegalArgumentException("Script is a required argument");
    }

    // the path where the script will be copied to
    final String renderId = getRenderId();
    final Path scriptPath = PhantomJSConstants.TEMP_SCRIPT_DIR.resolve(PhantomJSConstants.SCRIPT_PREFIX + renderId);

    // create the parent directory
    Files.createDirectories(PhantomJSConstants.TEMP_SCRIPT_DIR);

    // copy the script to the path
    Files.copy(script, scriptPath);

    // start building the phantomjs binary call
    final CommandLine cmd = new CommandLine(PhantomJSSetup.getPhantomJsBinary());
    final Map<String, Object> args = new HashMap<>();
    cmd.setSubstitutionMap(args);

    // add options to the phantomjs call
    if (options != null) {
        options.apply(cmd, args);
    }

    // then script
    args.put("_script_path", scriptPath.toFile());
    cmd.addArgument("${_script_path}");

    // then any additional arguments
    if (arguments != null) {
        for (final CommandLineArgument arg : arguments) {
            if (arg != null) {
                arg.apply(cmd, args);
            }
        }
    }

    logger.info("Running command: [{}]", cmd);

    final InfoLoggerOutputStream stdOutLogger = new InfoLoggerOutputStream();
    final ErrorLoggerOutputStream stdErrLogger = new ErrorLoggerOutputStream();

    final DefaultExecutor de = new DefaultExecutor();
    de.setStreamHandler(new PumpStreamHandler(stdOutLogger, stdErrLogger));

    int code;
    try {
        if(executionEnvironment != null && !executionEnvironment.isEmpty()) {
            code = de.execute(cmd, executionEnvironment);
        } else {
            code = de.execute(cmd);
        }
    } catch (final ExecuteException exe) {
        code = exe.getExitValue();
    }

    // remove the script after running it
    Files.deleteIfExists(scriptPath);

    logger.info("Execution Completed");

    return new PhantomJSExecutionResponse(code, stdOutLogger.getMessageContents(), stdErrLogger.getMessageContents());
}
 
Example 19
Source File: MySqlProcess.java    From trainbenchmark with Eclipse Public License 1.0 4 votes vote down vote up
public static void runScript(final String scriptFile) throws ExecuteException, IOException {
	final Executor executor = new DefaultExecutor();
	final CommandLine commandLine = new CommandLine("/bin/bash");
	commandLine.addArgument(scriptFile, false);
	executor.execute(commandLine);
}
 
Example 20
Source File: CommandBuilderHelper.java    From poor-man-transcoder with GNU General Public License v2.0 4 votes vote down vote up
public ITranscoderResource buildOutput(CommandLine cmdLine, ITranscoderResource input, ITranscodeOutput output, TokenReplacer tokenReplacer, String workingDirectory) throws Exception
{
	logger.info("Processing output destination for encode");
	
	String HLS_FILES_PATTERN = "([^\\s]+(\\.(?i)(m3u8|ts))$)";
	
	String segmentDirectory = null;
	
	ITranscoderResource template = output.getMediaOutput();
	ArrayList<IProperty> properties = output.getOutputProperties();
	ArrayList<IParameter> parameters = output.getOutputIParameters();
	ITranscoderResource transcoderOutput = IOUtils.createOutputFromInput(input, template, tokenReplacer);
	
	
	cmdLine.addArgument(Flags.OVERWRITE);
	cmdLine.addArgument(Flags.OUTPUT);
	cmdLine.addArgument(transcoderOutput.getContainer().toString());
	
	
	/************* special pre-processing for hls output ******/
	
	switch(transcoderOutput.getContainer().getType())
	{
		case SSEGMENT:
		segmentDirectory = prepareSegmentDirectory(cmdLine, transcoderOutput, tokenReplacer, workingDirectory);
		tokenReplacer.setTokenValue(TokenReplacer.TOKEN.OWN_SEGMENT_DIRECTORY, segmentDirectory);
		break;
		
		default:
		cmdLine.addArgument(transcoderOutput.describe(), true);
		break;
	}
	
	
	/***************** extra parameters for output **************/
	if(!parameters.isEmpty())
	{
		for(IParameter param: parameters){
			String key = param.getKey();
			String value = String.valueOf(param.getValue());
			
			
			switch(transcoderOutput.getContainer().getType())
			{
				case SSEGMENT:
				value = value.replaceAll(HLS_FILES_PATTERN, tokenReplacer.asPlaceholder(TokenReplacer.TOKEN.OWN_SEGMENT_DIRECTORY) + value);
				value = tokenReplacer.processReplacement(value);
				break;
				
				default:
				break;
			}
			
			cmdLine.addArgument(Flags.DASH + key);
			cmdLine.addArgument(value);
		}
	}
	
	/***************** extra properties for output **************/		
	if(!properties.isEmpty())
	{
		for(IProperty property: properties){
		String data = property.getData();
			
			
			switch(transcoderOutput.getContainer().getType())
			{
				case SSEGMENT:
				data = data.replaceAll(HLS_FILES_PATTERN, tokenReplacer.asPlaceholder(TokenReplacer.TOKEN.OWN_SEGMENT_DIRECTORY) + data);
				data = tokenReplacer.processReplacement(data);
				break;
				
				default:
				break;
			}
			
			cmdLine.addArgument(data, true);
		}
	}
	
	
	return transcoderOutput;
}