List of usage examples for org.apache.commons.exec DefaultExecutor setWorkingDirectory
public void setWorkingDirectory(final File dir)
From source file:org.apache.camel.component.exec.impl.DefaultExecCommandExecutor.java
protected DefaultExecutor prepareDefaultExecutor(ExecCommand execCommand) { DefaultExecutor executor = new DefaultExecutor(); executor.setExitValues(null);/*w ww . j a v a 2 s . com*/ if (execCommand.getWorkingDir() != null) { executor.setWorkingDirectory(new File(execCommand.getWorkingDir()).getAbsoluteFile()); } if (execCommand.getTimeout() != ExecEndpoint.NO_TIMEOUT) { executor.setWatchdog(new ExecuteWatchdog(execCommand.getTimeout())); } executor.setProcessDestroyer(new ShutdownHookProcessDestroyer()); return executor; }
From source file:org.apache.cloudstack.wix.HeatMojo.java
@Override public void execute() throws MojoExecutionException, MojoFailureException { try {/* w w w .j a v a 2s . co m*/ CommandLine commandLine = new CommandLine("heat"); if (dir != null && !dir.trim().isEmpty()) { commandLine.addArgument("dir"); commandLine.addArgument(dir); } commandLine.addArgument("-gg"); commandLine.addArgument("-cg"); commandLine.addArgument(componentGroup); commandLine.addArgument("-ke"); commandLine.addArgument("-sfrag"); if (template == null || template.trim().isEmpty()) { commandLine.addArgument("-template"); commandLine.addArgument("fragment"); } else { commandLine.addArgument("-template"); commandLine.addArgument(template); } if (outputFile != null) { commandLine.addArgument("-out"); commandLine.addArgument(outputFile.getAbsolutePath()); } if (directoryName != null) { commandLine.addArgument("-dr"); commandLine.addArgument(directoryName); } if (vars != null) { commandLine.addArguments(vars, false); } DefaultExecutor executor = new DefaultExecutor(); getLog().debug("working directory " + commandLine.toString()); executor.setWorkingDirectory(getWorkingDirectory(workingDirectory)); int exitValue = executor.execute(commandLine); if (exitValue != 0) { throw new MojoExecutionException("Problem executing heat, return code " + exitValue); } } catch (ExecuteException e) { throw new MojoExecutionException("Problem executing heat", e); } catch (IOException e) { throw new MojoExecutionException("Problem executing heat", e); } }
From source file:org.apache.stratos.cartridge.agent.test.JavaCartridgeAgentTest.java
/** * Execute shell command/*w ww . j a v a2 s. c o m*/ * * @param commandText */ private ByteArrayOutputStreamLocal executeCommand(final String commandText, File workingDir) { final ByteArrayOutputStreamLocal outputStream = new ByteArrayOutputStreamLocal(); try { CommandLine commandline = CommandLine.parse(commandText); DefaultExecutor exec = new DefaultExecutor(); if (workingDir != null) { exec.setWorkingDirectory(workingDir); } PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); exec.setStreamHandler(streamHandler); ExecuteWatchdog watchdog = new ExecuteWatchdog(TIMEOUT); exec.setWatchdog(watchdog); log.info("Executing command: " + commandText + (workingDir == null ? "" : " at " + workingDir.getCanonicalPath())); exec.execute(commandline, new ExecuteResultHandler() { @Override public void onProcessComplete(int i) { log.info(commandText + " process completed"); } @Override public void onProcessFailed(ExecuteException e) { log.error(commandText + " process failed", e); } }); executorList.put(commandText, exec); return outputStream; } catch (Exception e) { log.error(outputStream.toString(), e); throw new RuntimeException(e); } }
From source file:org.apache.stratos.python.cartridge.agent.integration.tests.PythonAgentIntegrationTest.java
/** * Execute shell command// w ww . java 2 s. co m * * @param commandText Command string to be executed */ protected ByteArrayOutputStreamLocal executeCommand(final String commandText, int timeout) { final ByteArrayOutputStreamLocal outputStream = new ByteArrayOutputStreamLocal(); try { CommandLine commandline = CommandLine.parse(commandText); DefaultExecutor exec = new DefaultExecutor(); PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); exec.setWorkingDirectory(new File(PythonAgentIntegrationTest.class.getResource(PATH_SEP).getPath() + PATH_SEP + ".." + PATH_SEP + PYTHON_AGENT_DIR_NAME)); exec.setStreamHandler(streamHandler); ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout); exec.setWatchdog(watchdog); exec.execute(commandline, new ExecuteResultHandler() { @Override public void onProcessComplete(int i) { log.info(commandText + " process completed"); } @Override public void onProcessFailed(ExecuteException e) { log.error(commandText + " process failed", e); } }); executorList.put(commandText, exec); return outputStream; } catch (Exception e) { log.error(outputStream.toString(), e); throw new RuntimeException(e); } }
From source file:org.apache.stratos.python.cartridge.agent.test.PythonAgentTestManager.java
/** * Execute shell command/*from w w w. jav a2 s. c om*/ * * @param commandText */ protected ByteArrayOutputStreamLocal executeCommand(final String commandText) { final ByteArrayOutputStreamLocal outputStream = new ByteArrayOutputStreamLocal(); try { CommandLine commandline = CommandLine.parse(commandText); DefaultExecutor exec = new DefaultExecutor(); PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream); exec.setWorkingDirectory(new File(PythonAgentTestManager.class.getResource(PATH_SEP).getPath() + PATH_SEP + ".." + PATH_SEP + PYTHON_AGENT_DIR_NAME)); exec.setStreamHandler(streamHandler); ExecuteWatchdog watchdog = new ExecuteWatchdog(TIMEOUT); exec.setWatchdog(watchdog); exec.execute(commandline, new ExecuteResultHandler() { @Override public void onProcessComplete(int i) { log.info(commandText + " process completed"); } @Override public void onProcessFailed(ExecuteException e) { log.error(commandText + " process failed", e); } }); executorList.put(commandText, exec); return outputStream; } catch (Exception e) { log.error(outputStream.toString(), e); throw new RuntimeException(e); } }
From source file:org.apache.zeppelin.submarine.job.thread.JobRunThread.java
public void run() { boolean tryLock = lockRunning.tryLock(); if (false == tryLock) { LOGGER.warn("Can not get JobRunThread lockRunning!"); return;//from w ww . j ava2 s .c o m } SubmarineUI submarineUI = submarineJob.getSubmarineUI(); try { InterpreterContext intpContext = submarineJob.getIntpContext(); String noteId = intpContext.getNoteId(); String userName = intpContext.getAuthenticationInfo().getUser(); String jobName = SubmarineUtils.getJobName(userName, noteId); if (true == running.get()) { String message = String.format("Job %s already running.", jobName); submarineUI.outputLog("WARN", message); LOGGER.warn(message); return; } running.set(true); Properties properties = submarineJob.getProperties(); HdfsClient hdfsClient = submarineJob.getHdfsClient(); File pythonWorkDir = submarineJob.getPythonWorkDir(); submarineJob.setCurrentJobState(EXECUTE_SUBMARINE); String algorithmPath = properties.getProperty(SubmarineConstants.SUBMARINE_ALGORITHM_HDFS_PATH, ""); if (!algorithmPath.startsWith("hdfs://")) { String message = "Algorithm file upload HDFS path, " + "Must be `hdfs://` prefix. now setting " + algorithmPath; submarineUI.outputLog("Configuration error", message); return; } List<ParagraphInfo> paragraphInfos = intpContext.getIntpEventClient().getParagraphList(userName, noteId); String outputMsg = hdfsClient.saveParagraphToFiles(noteId, paragraphInfos, pythonWorkDir == null ? "" : pythonWorkDir.getAbsolutePath(), properties); if (!StringUtils.isEmpty(outputMsg)) { submarineUI.outputLog("Save algorithm file", outputMsg); } HashMap jinjaParams = SubmarineUtils.propertiesToJinjaParams(properties, submarineJob, true); URL urlTemplate = Resources.getResource(SubmarineJob.SUBMARINE_JOBRUN_TF_JINJA); String template = Resources.toString(urlTemplate, Charsets.UTF_8); Jinjava jinjava = new Jinjava(); String submarineCmd = jinjava.render(template, jinjaParams); // If the first line is a newline, delete the newline int firstLineIsNewline = submarineCmd.indexOf("\n"); if (firstLineIsNewline == 0) { submarineCmd = submarineCmd.replaceFirst("\n", ""); } StringBuffer sbLogs = new StringBuffer(submarineCmd); submarineUI.outputLog("Submarine submit command", sbLogs.toString()); long timeout = Long .valueOf(properties.getProperty(SubmarineJob.TIMEOUT_PROPERTY, SubmarineJob.defaultTimeout)); CommandLine cmdLine = CommandLine.parse(SubmarineJob.shell); cmdLine.addArgument(submarineCmd, false); DefaultExecutor executor = new DefaultExecutor(); ExecuteWatchdog watchDog = new ExecuteWatchdog(timeout); executor.setWatchdog(watchDog); StringBuffer sbLogOutput = new StringBuffer(); executor.setStreamHandler(new PumpStreamHandler(new LogOutputStream() { @Override protected void processLine(String line, int level) { line = line.trim(); if (!StringUtils.isEmpty(line)) { sbLogOutput.append(line + "\n"); } } })); if (Boolean.valueOf(properties.getProperty(SubmarineJob.DIRECTORY_USER_HOME))) { executor.setWorkingDirectory(new File(System.getProperty("user.home"))); } Map<String, String> env = new HashMap<>(); String launchMode = (String) jinjaParams.get(SubmarineConstants.INTERPRETER_LAUNCH_MODE); if (StringUtils.equals(launchMode, "yarn")) { // Set environment variables in the submarine interpreter container run on yarn String javaHome, hadoopHome, hadoopConf; javaHome = (String) jinjaParams.get(SubmarineConstants.DOCKER_JAVA_HOME); hadoopHome = (String) jinjaParams.get(SubmarineConstants.DOCKER_HADOOP_HDFS_HOME); hadoopConf = (String) jinjaParams.get(SubmarineConstants.SUBMARINE_HADOOP_CONF_DIR); env.put("JAVA_HOME", javaHome); env.put("HADOOP_HOME", hadoopHome); env.put("HADOOP_HDFS_HOME", hadoopHome); env.put("HADOOP_CONF_DIR", hadoopConf); env.put("YARN_CONF_DIR", hadoopConf); env.put("CLASSPATH", "`$HADOOP_HDFS_HOME/bin/hadoop classpath --glob`"); env.put("ZEPPELIN_FORCE_STOP", "true"); } LOGGER.info("Execute EVN: {}, Command: {} ", env.toString(), submarineCmd); AtomicBoolean cmdLineRunning = new AtomicBoolean(true); executor.execute(cmdLine, env, new DefaultExecuteResultHandler() { @Override public void onProcessComplete(int exitValue) { String message = String.format("jobName %s ProcessComplete exit value is : %d", jobName, exitValue); LOGGER.info(message); submarineUI.outputLog("JOR RUN COMPLETE", message); cmdLineRunning.set(false); submarineJob.setCurrentJobState(EXECUTE_SUBMARINE_FINISHED); } @Override public void onProcessFailed(ExecuteException e) { String message = String.format("jobName %s ProcessFailed exit value is : %d, exception is : %s", jobName, e.getExitValue(), e.getMessage()); LOGGER.error(message); submarineUI.outputLog("JOR RUN FAILED", message); cmdLineRunning.set(false); submarineJob.setCurrentJobState(EXECUTE_SUBMARINE_ERROR); } }); int loopCount = 100; while ((loopCount-- > 0) && cmdLineRunning.get() && running.get()) { Thread.sleep(1000); } if (watchDog.isWatching()) { watchDog.destroyProcess(); Thread.sleep(1000); } if (watchDog.isWatching()) { watchDog.killedProcess(); } // Check if it has been submitted to YARN Map<String, Object> jobState = submarineJob.getJobStateByYarn(jobName); loopCount = 50; while ((loopCount-- > 0) && !jobState.containsKey("state") && running.get()) { Thread.sleep(3000); jobState = submarineJob.getJobStateByYarn(jobName); } if (!jobState.containsKey("state")) { String message = String.format("JOB %s was not submitted to YARN!", jobName); LOGGER.error(message); submarineUI.outputLog("JOR RUN FAILED", message); submarineJob.setCurrentJobState(EXECUTE_SUBMARINE_ERROR); } } catch (Exception e) { LOGGER.error(e.getMessage(), e); submarineJob.setCurrentJobState(EXECUTE_SUBMARINE_ERROR); submarineUI.outputLog("Exception", e.getMessage()); } finally { running.set(false); lockRunning.unlock(); } }
From source file:org.apache.zeppelin.submarine.job.thread.TensorboardRunThread.java
public void run() { SubmarineUI submarineUI = submarineJob.getSubmarineUI(); boolean tryLock = lockRunning.tryLock(); try {//from w w w . j av a2s. c o m Properties properties = submarineJob.getProperties(); String tensorboardName = SubmarineUtils.getTensorboardName(submarineJob.getUserName()); if (true == running.get()) { String message = String.format("tensorboard %s already running.", tensorboardName); submarineUI.outputLog("WARN", message); LOGGER.warn(message); return; } running.set(true); HashMap jinjaParams = SubmarineUtils.propertiesToJinjaParams(properties, submarineJob, false); // update jobName -> tensorboardName jinjaParams.put(SubmarineConstants.JOB_NAME, tensorboardName); URL urlTemplate = Resources.getResource(SubmarineJob.SUBMARINE_TENSORBOARD_JINJA); String template = Resources.toString(urlTemplate, Charsets.UTF_8); Jinjava jinjava = new Jinjava(); String submarineCmd = jinjava.render(template, jinjaParams); // If the first line is a newline, delete the newline int firstLineIsNewline = submarineCmd.indexOf("\n"); if (firstLineIsNewline == 0) { submarineCmd = submarineCmd.replaceFirst("\n", ""); } StringBuffer sbLogs = new StringBuffer(submarineCmd); submarineUI.outputLog("Submarine submit command", sbLogs.toString()); long timeout = Long .valueOf(properties.getProperty(SubmarineJob.TIMEOUT_PROPERTY, SubmarineJob.defaultTimeout)); CommandLine cmdLine = CommandLine.parse(SubmarineJob.shell); cmdLine.addArgument(submarineCmd, false); DefaultExecutor executor = new DefaultExecutor(); ExecuteWatchdog watchDog = new ExecuteWatchdog(timeout); executor.setWatchdog(watchDog); StringBuffer sbLogOutput = new StringBuffer(); executor.setStreamHandler(new PumpStreamHandler(new LogOutputStream() { @Override protected void processLine(String line, int level) { line = line.trim(); if (!StringUtils.isEmpty(line)) { sbLogOutput.append(line + "\n"); } } })); if (Boolean.valueOf(properties.getProperty(SubmarineJob.DIRECTORY_USER_HOME))) { executor.setWorkingDirectory(new File(System.getProperty("user.home"))); } Map<String, String> env = new HashMap<>(); String launchMode = (String) jinjaParams.get(SubmarineConstants.INTERPRETER_LAUNCH_MODE); if (StringUtils.equals(launchMode, "yarn")) { // Set environment variables in the container String javaHome, hadoopHome, hadoopConf; javaHome = (String) jinjaParams.get(SubmarineConstants.DOCKER_JAVA_HOME); hadoopHome = (String) jinjaParams.get(SubmarineConstants.DOCKER_HADOOP_HDFS_HOME); hadoopConf = (String) jinjaParams.get(SubmarineConstants.SUBMARINE_HADOOP_CONF_DIR); env.put("JAVA_HOME", javaHome); env.put("HADOOP_HOME", hadoopHome); env.put("HADOOP_HDFS_HOME", hadoopHome); env.put("HADOOP_CONF_DIR", hadoopConf); env.put("YARN_CONF_DIR", hadoopConf); env.put("CLASSPATH", "`$HADOOP_HDFS_HOME/bin/hadoop classpath --glob`"); } LOGGER.info("Execute EVN: {}, Command: {} ", env.toString(), submarineCmd); AtomicBoolean cmdLineRunning = new AtomicBoolean(true); executor.execute(cmdLine, env, new DefaultExecuteResultHandler() { @Override public void onProcessComplete(int exitValue) { String message = String.format("jobName %s ProcessComplete exit value is : %d", tensorboardName, exitValue); LOGGER.info(message); submarineUI.outputLog("TENSORBOARD RUN COMPLETE", message); cmdLineRunning.set(false); } @Override public void onProcessFailed(ExecuteException e) { String message = String.format("jobName %s ProcessFailed exit value is : %d, exception is : %s", tensorboardName, e.getExitValue(), e.getMessage()); LOGGER.error(message); submarineUI.outputLog("TENSORBOARD RUN FAILED", message); cmdLineRunning.set(false); } }); int loopCount = 100; while ((loopCount-- > 0) && cmdLineRunning.get() && running.get()) { Thread.sleep(1000); } if (watchDog.isWatching()) { watchDog.destroyProcess(); Thread.sleep(1000); } if (watchDog.isWatching()) { watchDog.killedProcess(); } // Check if it has been submitted to YARN Map<String, Object> jobState = submarineJob.getJobStateByYarn(tensorboardName); loopCount = 50; while ((loopCount-- > 0) && !jobState.containsKey("state") && running.get()) { Thread.sleep(3000); jobState = submarineJob.getJobStateByYarn(tensorboardName); } if (!jobState.containsKey("state")) { String message = String.format("tensorboard %s was not submitted to YARN!", tensorboardName); LOGGER.error(message); submarineUI.outputLog("JOR RUN FAILED", message); } } catch (Exception e) { LOGGER.error(e.getMessage(), e); submarineUI.outputLog("Exception", e.getMessage()); } finally { running.set(false); lockRunning.unlock(); } }
From source file:org.bonitasoft.platform.setup.PlatformSetupTestUtils.java
public static DefaultExecutor createExecutor(File distFolder) { DefaultExecutor oDefaultExecutor = new DefaultExecutor(); oDefaultExecutor.setWorkingDirectory(distFolder); return oDefaultExecutor; }
From source file:org.cloudifysource.shell.commands.StartLocalCloud.java
private void startLocalAgent(Admin admin) throws ExecuteException, IOException { File binDir = new File(Environment.getHomeDirectory(), "bin"); DefaultExecutor executor = new DefaultExecutor(); executor.setWorkingDirectory(binDir); CommandLine cmdLine = createCommandLine(); executor.setExitValue(0);//from ww w . j a v a 2 s . c o m executor.execute(cmdLine, new ExecuteResultHandler() { @Override public void onProcessFailed(ExecuteException e) { logger.log(Level.SEVERE, "Local Cloud Agent terminated unexpectedly", e); } @Override public void onProcessComplete(int arg0) { logger.fine("Local Cloud Agent terminated"); } }); boolean foundLus = admin.getLookupServices().waitFor(1, 1, TimeUnit.MINUTES); if (!foundLus) { throw new java.lang.IllegalStateException("Local Cloud Lookup Service did not start!"); } }
From source file:org.codehaus.mojo.antlr.AbstractAntlrMojo.java
protected void performGeneration(GenerationPlan plan, Artifact antlrArtifact) throws MojoExecutionException { if (!plan.getGenerationDirectory().getParentFile().exists()) { plan.getGenerationDirectory().getParentFile().mkdirs(); }/*from w ww . j av a 2s . c o m*/ // ---------------------------------------------------------------------- // Wrap arguments // Note: grammar file should be last // ---------------------------------------------------------------------- List arguments = new LinkedList(); addArgIf(arguments, debug, "-debug"); addArgIf(arguments, diagnostic, "-diagnostic"); addArgIf(arguments, trace, "-trace"); addArgIf(arguments, traceParser, "-traceParser"); addArgIf(arguments, traceLexer, "-traceLexer"); addArgIf(arguments, traceTreeParser, "-traceTreeParser"); addArgs(arguments); arguments.add("-o"); arguments.add(plan.getGenerationDirectory().getPath()); if (plan.getCollectedSuperGrammarIds().size() > 0) { arguments.add("-glib"); StringBuffer buffer = new StringBuffer(); Iterator ids = plan.getCollectedSuperGrammarIds().iterator(); while (ids.hasNext()) { buffer.append(new File(sourceDirectory, (String) ids.next())); if (ids.hasNext()) { buffer.append(';'); } } arguments.add(buffer.toString()); } arguments.add(plan.getSource().getPath()); String[] args = (String[]) arguments.toArray(new String[arguments.size()]); if (plan.getImportVocabTokenTypesDirectory() != null && !plan.getImportVocabTokenTypesDirectory().equals(plan.getGenerationDirectory())) { // we need to spawn a new process to properly set up PWD CommandLine commandLine = new CommandLine("java"); commandLine.addArgument("-classpath", false); commandLine.addArgument(generateClasspathForProcessSpawning(antlrArtifact), true); commandLine.addArgument("antlr.Tool", false); commandLine.addArguments(args, true); DefaultExecutor executor = new DefaultExecutor(); executor.setWorkingDirectory(plan.getImportVocabTokenTypesDirectory()); try { executor.execute(commandLine); } catch (IOException e) { getLog().warn("Error spawning process to execute antlr tool : " + e.getMessage()); } return; } // ---------------------------------------------------------------------- // Call Antlr // ---------------------------------------------------------------------- if (getLog().isDebugEnabled()) { getLog().debug("antlr args=\n" + StringUtils.join(args, "\n")); } boolean failedSetManager = false; SecurityManager oldSm = null; try { oldSm = System.getSecurityManager(); System.setSecurityManager(NoExitSecurityManager.INSTANCE); } catch (SecurityException ex) { // ANTLR-12 oldSm = null; failedSetManager = true; // ignore, in embedded environment the security manager can already be set. // in such a case assume the exit call is handled properly.. getLog().warn("Cannot set custom SecurityManager. " + "Antlr's call to System.exit() can cause application shutdown " + "if not handled by the current SecurityManager."); } String originalUserDir = null; if (plan.getImportVocabTokenTypesDirectory() != null) { originalUserDir = System.getProperty("user.dir"); System.setProperty("user.dir", plan.getImportVocabTokenTypesDirectory().getPath()); } PrintStream oldErr = System.err; OutputStream errOS = new StringOutputStream(); PrintStream err = new PrintStream(errOS); System.setErr(err); try { executeAntlrInIsolatedClassLoader((String[]) arguments.toArray(new String[0]), antlrArtifact); } catch (SecurityException e) { if (e.getMessage().equals("exitVM-0") || e.getClass().getName().equals("org.netbeans.core.execution.ExitSecurityException")) // netbeans // IDE Sec // Manager. { // ANTLR-12 // now basically every secutiry manager could set different message, how to handle in generic way? // probably only by external execution // / in case of NetBeans SecurityManager, it's not possible to distinguish exit codes, rather swallow // than fail. getLog().debug(e); } else { throw new MojoExecutionException( "Antlr execution failed: " + e.getMessage() + "\n Error output:\n" + errOS, e); } } finally { if (originalUserDir != null) { System.setProperty("user.dir", originalUserDir); } if (!failedSetManager) { System.setSecurityManager(oldSm); } System.setErr(oldErr); System.err.println(errOS.toString()); } }