2 * Copyright (C) 2007-2012 Argeo GmbH
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.slc
.core
.execution
.tasks
;
19 import java
.io
.FileOutputStream
;
20 import java
.io
.FileWriter
;
21 import java
.io
.IOException
;
22 import java
.io
.InputStream
;
23 import java
.io
.OutputStream
;
24 import java
.io
.PipedInputStream
;
25 import java
.io
.PipedOutputStream
;
26 import java
.io
.Writer
;
27 import java
.util
.ArrayList
;
28 import java
.util
.Collections
;
29 import java
.util
.HashMap
;
30 import java
.util
.List
;
32 import java
.util
.UUID
;
34 import javax
.security
.auth
.callback
.CallbackHandler
;
36 import org
.apache
.commons
.exec
.CommandLine
;
37 import org
.apache
.commons
.exec
.DefaultExecutor
;
38 import org
.apache
.commons
.exec
.ExecuteException
;
39 import org
.apache
.commons
.exec
.ExecuteResultHandler
;
40 import org
.apache
.commons
.exec
.ExecuteStreamHandler
;
41 import org
.apache
.commons
.exec
.ExecuteWatchdog
;
42 import org
.apache
.commons
.exec
.Executor
;
43 import org
.apache
.commons
.exec
.LogOutputStream
;
44 import org
.apache
.commons
.exec
.PumpStreamHandler
;
45 import org
.apache
.commons
.exec
.ShutdownHookProcessDestroyer
;
46 import org
.apache
.commons
.io
.FileUtils
;
47 import org
.apache
.commons
.io
.IOUtils
;
48 import org
.apache
.commons
.logging
.Log
;
49 import org
.apache
.commons
.logging
.LogFactory
;
50 import org
.argeo
.slc
.SlcException
;
51 import org
.argeo
.slc
.UnsupportedException
;
52 import org
.argeo
.slc
.core
.execution
.ExecutionResources
;
53 import org
.argeo
.slc
.core
.test
.SimpleResultPart
;
54 import org
.argeo
.slc
.test
.TestResult
;
55 import org
.argeo
.slc
.test
.TestStatus
;
56 import org
.springframework
.core
.io
.Resource
;
57 import org
.springframework
.security
.Authentication
;
58 import org
.springframework
.security
.context
.SecurityContextHolder
;
60 /** Execute an OS specific system call. */
61 public class SystemCall
implements Runnable
{
62 public final static String LOG_STDOUT
= "System.out";
64 private final Log log
= LogFactory
.getLog(getClass());
66 private String execDir
;
68 private String cmd
= null;
69 private List
<Object
> command
= null;
71 private Executor executor
= new DefaultExecutor();
72 private Boolean synchronous
= true;
74 private String stdErrLogLevel
= "ERROR";
75 private String stdOutLogLevel
= "INFO";
77 private Resource stdOutFile
= null;
78 private Resource stdErrFile
= null;
80 private Resource stdInFile
= null;
82 * If no {@link #stdInFile} provided, writing to this stream will write to
83 * the stdin of the process.
85 private OutputStream stdInSink
= null;
87 private Boolean redirectStdOut
= false;
89 private List
<SystemCallOutputListener
> outputListeners
= Collections
90 .synchronizedList(new ArrayList
<SystemCallOutputListener
>());
92 private Map
<String
, List
<Object
>> osCommands
= new HashMap
<String
, List
<Object
>>();
93 private Map
<String
, String
> osCmds
= new HashMap
<String
, String
>();
94 private Map
<String
, String
> environmentVariables
= new HashMap
<String
, String
>();
96 private Boolean logCommand
= false;
97 private Boolean redirectStreams
= true;
98 private Boolean exceptionOnFailed
= true;
99 private Boolean mergeEnvironmentVariables
= true;
101 private Authentication authentication
;
103 private String osConsole
= null;
104 private String generateScript
= null;
107 private Long watchdogTimeout
= 24 * 60 * 60 * 1000l;
109 private TestResult testResult
;
111 private ExecutionResources executionResources
;
113 /** Sudo the command, as root if empty or as user if not. */
114 private String sudo
= null;
115 // TODO make it more secure and robust, test only once
116 private final String sudoPrompt
= UUID
.randomUUID().toString();
117 private String askPassProgram
= "/usr/libexec/openssh/ssh-askpass";
118 private boolean firstLine
= true;
119 private CallbackHandler callbackHandler
;
120 /** Chroot to the this path (must not be empty) */
121 private String chroot
= null;
124 /** Current watchdog, null if process is completed */
125 ExecuteWatchdog currentWatchdog
= null;
127 /** Empty constructor */
128 public SystemCall() {
133 * Constructor based on the provided command list.
138 public SystemCall(List
<Object
> command
) {
139 this.command
= command
;
143 * Constructor based on the provided command.
146 * the command. If the provided string contains no space a
147 * command list is initialized with the argument as first
148 * component (useful for chained construction)
150 public SystemCall(String cmd
) {
151 if (cmd
.indexOf(' ') < 0) {
152 command
= new ArrayList
<Object
>();
159 /** Executes the system call. */
161 authentication
= SecurityContextHolder
.getContext().getAuthentication();
164 Writer stdOutWriter
= null;
165 OutputStream stdOutputStream
= null;
166 Writer stdErrWriter
= null;
167 InputStream stdInStream
= null;
168 if (stdOutFile
!= null)
170 stdOutputStream
= createOutputStream(stdOutFile
);
172 stdOutWriter
= createWriter(stdOutFile
, true);
174 if (stdErrFile
!= null) {
175 stdErrWriter
= createWriter(stdErrFile
, true);
177 if (stdOutFile
!= null && !redirectStdOut
)
178 stdErrWriter
= createWriter(stdOutFile
, true);
182 if (stdInFile
!= null)
183 stdInStream
= stdInFile
.getInputStream();
185 stdInStream
= new PipedInputStream();
186 stdInSink
= new PipedOutputStream(
187 (PipedInputStream
) stdInStream
);
189 } catch (IOException e2
) {
190 throw new SlcException("Cannot open a stream for " + stdInFile
, e2
);
193 if (log
.isTraceEnabled()) {
194 log
.debug("os.name=" + System
.getProperty("os.name"));
195 log
.debug("os.arch=" + System
.getProperty("os.arch"));
196 log
.debug("os.version=" + System
.getProperty("os.version"));
199 // Execution directory
200 File dir
= new File(getExecDirToUse());
201 // if (!dir.exists())
204 // Watchdog to check for lost processes
205 Executor executorToUse
;
206 if (executor
!= null)
207 executorToUse
= executor
;
209 executorToUse
= new DefaultExecutor();
210 executorToUse
.setWatchdog(createWatchdog());
212 if (redirectStreams
) {
213 // Redirect standard streams
214 executorToUse
.setStreamHandler(createExecuteStreamHandler(
215 stdOutWriter
, stdOutputStream
, stdErrWriter
, stdInStream
));
217 // Dummy stream handler (otherwise pump is used)
218 executorToUse
.setStreamHandler(new DummyexecuteStreamHandler());
221 executorToUse
.setProcessDestroyer(new ShutdownHookProcessDestroyer());
222 executorToUse
.setWorkingDirectory(dir
);
224 // Command line to use
225 final CommandLine commandLine
= createCommandLine();
227 log
.info("Execute command:\n" + commandLine
228 + "\n in working directory: \n" + dir
+ "\n");
231 Map
<String
, String
> environmentVariablesToUse
= null;
232 environmentVariablesToUse
= new HashMap
<String
, String
>();
233 if (mergeEnvironmentVariables
)
234 environmentVariablesToUse
.putAll(System
.getenv());
235 if (environmentVariables
.size() > 0)
236 environmentVariablesToUse
.putAll(environmentVariables
);
239 ExecuteResultHandler executeResultHandler
= createExecuteResultHandler(commandLine
);
242 // THE EXECUTION PROPER
247 int exitValue
= executorToUse
.execute(commandLine
,
248 environmentVariablesToUse
);
249 executeResultHandler
.onProcessComplete(exitValue
);
250 } catch (ExecuteException e1
) {
251 if (e1
.getExitValue() == Executor
.INVALID_EXITVALUE
) {
252 Thread
.currentThread().interrupt();
255 // Sleep 1s in order to make sure error logs are flushed
257 executeResultHandler
.onProcessFailed(e1
);
260 executorToUse
.execute(commandLine
, environmentVariablesToUse
,
261 executeResultHandler
);
262 } catch (SlcException e
) {
264 } catch (Exception e
) {
265 throw new SlcException("Could not execute command " + commandLine
,
268 IOUtils
.closeQuietly(stdOutWriter
);
269 IOUtils
.closeQuietly(stdErrWriter
);
270 IOUtils
.closeQuietly(stdInStream
);
271 IOUtils
.closeQuietly(stdInSink
);
276 public synchronized String
function() {
277 final StringBuffer buf
= new StringBuffer("");
278 SystemCallOutputListener tempOutputListener
= new SystemCallOutputListener() {
279 private Long lineCount
= 0l;
281 public void newLine(SystemCall systemCall
, String line
,
291 addOutputListener(tempOutputListener
);
293 removeOutputListener(tempOutputListener
);
294 return buf
.toString();
297 public String
asCommand() {
298 return createCommandLine().toString();
302 public String
toString() {
307 * Build a command line based on the properties. Can be overridden by
308 * specific command wrappers.
310 protected CommandLine
createCommandLine() {
311 // Check if an OS specific command overrides
312 String osName
= System
.getProperty("os.name");
313 List
<Object
> commandToUse
= null;
314 if (osCommands
.containsKey(osName
))
315 commandToUse
= osCommands
.get(osName
);
317 commandToUse
= command
;
318 String cmdToUse
= null;
319 if (osCmds
.containsKey(osName
))
320 cmdToUse
= osCmds
.get(osName
);
324 CommandLine commandLine
= null;
326 // Which command definition to use
327 if (commandToUse
== null && cmdToUse
== null)
328 throw new SlcException("Please specify a command.");
329 else if (commandToUse
!= null && cmdToUse
!= null)
330 throw new SlcException(
331 "Specify the command either as a line or as a list.");
332 else if (cmdToUse
!= null) {
333 if (chroot
!= null && !chroot
.trim().equals(""))
334 cmdToUse
= "chroot \"" + chroot
+ "\" " + cmdToUse
;
336 environmentVariables
.put("SUDO_ASKPASS", askPassProgram
);
337 if (!sudo
.trim().equals(""))
338 cmdToUse
= "sudo -p " + sudoPrompt
+ " -u " + sudo
+ " "
341 cmdToUse
= "sudo -p " + sudoPrompt
+ " " + cmdToUse
;
344 // GENERATE COMMAND LINE
345 commandLine
= CommandLine
.parse(cmdToUse
);
346 } else if (commandToUse
!= null) {
347 if (commandToUse
.size() == 0)
348 throw new SlcException("Command line is empty.");
350 if (chroot
!= null && sudo
!= null) {
351 commandToUse
.add(0, "chroot");
352 commandToUse
.add(1, chroot
);
356 environmentVariables
.put("SUDO_ASKPASS", askPassProgram
);
357 commandToUse
.add(0, "sudo");
358 commandToUse
.add(1, "-p");
359 commandToUse
.add(2, sudoPrompt
);
360 if (!sudo
.trim().equals("")) {
361 commandToUse
.add(3, "-u");
362 commandToUse
.add(4, sudo
);
366 // GENERATE COMMAND LINE
367 commandLine
= new CommandLine(commandToUse
.get(0).toString());
369 for (int i
= 1; i
< commandToUse
.size(); i
++) {
370 if (log
.isTraceEnabled())
371 log
.debug(commandToUse
.get(i
));
372 commandLine
.addArgument(commandToUse
.get(i
).toString());
375 // all cases covered previously
376 throw new UnsupportedException();
379 if (generateScript
!= null) {
380 File scriptFile
= new File(getExecDirToUse() + File
.separator
383 FileUtils
.writeStringToFile(scriptFile
,
384 (osConsole
!= null ? osConsole
+ " " : "")
385 + commandLine
.toString());
386 } catch (IOException e
) {
387 throw new SlcException("Could not generate script "
390 commandLine
= new CommandLine(scriptFile
);
392 if (osConsole
!= null)
393 commandLine
= CommandLine
.parse(osConsole
+ " "
394 + commandLine
.toString());
401 * Creates a {@link PumpStreamHandler} which redirects streams to the custom
404 protected ExecuteStreamHandler
createExecuteStreamHandler(
405 final Writer stdOutWriter
, final OutputStream stdOutputStream
,
406 final Writer stdErrWriter
, final InputStream stdInStream
) {
409 OutputStream stdout
= stdOutputStream
!= null ? stdOutputStream
410 : new LogOutputStream() {
411 protected void processLine(String line
, int level
) {
413 // if (sudo != null && callbackHandler != null
414 // && line.startsWith(sudoPrompt)) {
416 // PasswordCallback pc = new PasswordCallback(
417 // "sudo password", false);
418 // Callback[] cbs = { pc };
419 // callbackHandler.handle(cbs);
420 // char[] pwd = pc.getPassword();
421 // char[] arr = Arrays.copyOf(pwd,
423 // arr[arr.length - 1] = '\n';
424 // IOUtils.write(arr, stdInSink);
425 // stdInSink.flush();
426 // } catch (Exception e) {
427 // throw new SlcException(
428 // "Cannot retrieve sudo password", e);
431 // firstLine = false;
434 if (line
!= null && !line
.trim().equals(""))
437 if (stdOutWriter
!= null)
438 appendLineToFile(stdOutWriter
, line
);
442 OutputStream stderr
= new LogOutputStream() {
443 protected void processLine(String line
, int level
) {
444 if (line
!= null && !line
.trim().equals(""))
446 if (stdErrWriter
!= null)
447 appendLineToFile(stdErrWriter
, line
);
451 PumpStreamHandler pumpStreamHandler
= new PumpStreamHandler(stdout
,
452 stderr
, stdInStream
) {
456 // prevents the method to block when joining stdin
457 if (stdInSink
!= null)
458 IOUtils
.closeQuietly(stdInSink
);
463 return pumpStreamHandler
;
466 /** Creates the default {@link ExecuteResultHandler}. */
467 protected ExecuteResultHandler
createExecuteResultHandler(
468 final CommandLine commandLine
) {
469 return new ExecuteResultHandler() {
471 public void onProcessComplete(int exitValue
) {
472 String msg
= "System call '" + commandLine
473 + "' properly completed.";
474 if (log
.isTraceEnabled())
476 if (testResult
!= null) {
477 forwardPath(testResult
);
478 testResult
.addResultPart(new SimpleResultPart(
479 TestStatus
.PASSED
, msg
));
484 public void onProcessFailed(ExecuteException e
) {
486 String msg
= "System call '" + commandLine
+ "' failed.";
487 if (testResult
!= null) {
488 forwardPath(testResult
);
489 testResult
.addResultPart(new SimpleResultPart(
490 TestStatus
.ERROR
, msg
, e
));
492 if (exceptionOnFailed
)
493 throw new SlcException(msg
, e
);
503 protected void forwardPath(TestResult testResult
) {
504 // TODO: allocate a TreeSPath
508 * Shortcut method getting the execDir to use
510 protected String
getExecDirToUse() {
512 if (execDir
!= null) {
515 return System
.getProperty("user.dir");
516 } catch (Exception e
) {
517 throw new SlcException("Cannot find exec dir", e
);
521 protected void logStdOut(String line
) {
522 for (SystemCallOutputListener outputListener
: outputListeners
)
523 outputListener
.newLine(this, line
, false);
524 log(stdOutLogLevel
, line
);
527 protected void logStdErr(String line
) {
528 for (SystemCallOutputListener outputListener
: outputListeners
)
529 outputListener
.newLine(this, line
, true);
530 log(stdErrLogLevel
, line
);
533 /** Log from the underlying streams. */
534 protected void log(String logLevel
, String line
) {
536 if (SecurityContextHolder
.getContext().getAuthentication() == null) {
537 SecurityContextHolder
.getContext()
538 .setAuthentication(authentication
);
541 if ("ERROR".equals(logLevel
))
543 else if ("WARN".equals(logLevel
))
545 else if ("INFO".equals(logLevel
))
547 else if ("DEBUG".equals(logLevel
))
549 else if ("TRACE".equals(logLevel
))
551 else if (LOG_STDOUT
.equals(logLevel
))
552 System
.out
.println(line
);
553 else if ("System.err".equals(logLevel
))
554 System
.err
.println(line
);
556 throw new SlcException("Unknown log level " + logLevel
);
559 /** Append line to a log file. */
560 protected void appendLineToFile(Writer writer
, String line
) {
562 writer
.append(line
).append('\n');
563 } catch (IOException e
) {
564 log
.error("Cannot write to log file", e
);
568 /** Creates the writer for the output/err files. */
569 protected Writer
createWriter(Resource target
, Boolean append
) {
570 FileWriter writer
= null;
574 if (executionResources
!= null)
575 file
= new File(executionResources
.getAsOsPath(target
, true));
577 file
= target
.getFile();
578 writer
= new FileWriter(file
, append
);
579 } catch (IOException e
) {
580 log
.error("Cannot get file for " + target
, e
);
581 IOUtils
.closeQuietly(writer
);
586 /** Creates an outputstream for the output/err files. */
587 protected OutputStream
createOutputStream(Resource target
) {
588 FileOutputStream out
= null;
592 if (executionResources
!= null)
593 file
= new File(executionResources
.getAsOsPath(target
, true));
595 file
= target
.getFile();
596 out
= new FileOutputStream(file
, false);
597 } catch (IOException e
) {
598 log
.error("Cannot get file for " + target
, e
);
599 IOUtils
.closeQuietly(out
);
604 /** Append the argument (for chaining) */
605 public SystemCall
arg(String arg
) {
607 command
= new ArrayList
<Object
>();
612 /** Append the argument (for chaining) */
613 public SystemCall
arg(String arg
, String value
) {
615 command
= new ArrayList
<Object
>();
622 public synchronized Boolean
isRunning() {
623 return currentWatchdog
!= null;
626 private synchronized ExecuteWatchdog
createWatchdog() {
627 // if (currentWatchdog != null)
628 // throw new SlcException("A process is already being monitored");
629 currentWatchdog
= new ExecuteWatchdog(watchdogTimeout
);
630 return currentWatchdog
;
633 private synchronized void releaseWatchdog() {
634 currentWatchdog
= null;
637 public synchronized void kill() {
638 if (currentWatchdog
!= null)
639 currentWatchdog
.destroyProcess();
643 public void setCmd(String command
) {
647 public void setCommand(List
<Object
> command
) {
648 this.command
= command
;
651 public void setExecDir(String execdir
) {
652 this.execDir
= execdir
;
655 public void setStdErrLogLevel(String stdErrLogLevel
) {
656 this.stdErrLogLevel
= stdErrLogLevel
;
659 public void setStdOutLogLevel(String stdOutLogLevel
) {
660 this.stdOutLogLevel
= stdOutLogLevel
;
663 public void setSynchronous(Boolean synchronous
) {
664 this.synchronous
= synchronous
;
667 public void setOsCommands(Map
<String
, List
<Object
>> osCommands
) {
668 this.osCommands
= osCommands
;
671 public void setOsCmds(Map
<String
, String
> osCmds
) {
672 this.osCmds
= osCmds
;
675 public void setEnvironmentVariables(Map
<String
, String
> environmentVariables
) {
676 this.environmentVariables
= environmentVariables
;
679 public Map
<String
, String
> getEnvironmentVariables() {
680 return environmentVariables
;
683 public void setWatchdogTimeout(Long watchdogTimeout
) {
684 this.watchdogTimeout
= watchdogTimeout
;
687 public void setStdOutFile(Resource stdOutFile
) {
688 this.stdOutFile
= stdOutFile
;
691 public void setStdErrFile(Resource stdErrFile
) {
692 this.stdErrFile
= stdErrFile
;
695 public void setStdInFile(Resource stdInFile
) {
696 this.stdInFile
= stdInFile
;
699 public void setTestResult(TestResult testResult
) {
700 this.testResult
= testResult
;
703 public void setLogCommand(Boolean logCommand
) {
704 this.logCommand
= logCommand
;
707 public void setRedirectStreams(Boolean redirectStreams
) {
708 this.redirectStreams
= redirectStreams
;
711 public void setExceptionOnFailed(Boolean exceptionOnFailed
) {
712 this.exceptionOnFailed
= exceptionOnFailed
;
715 public void setMergeEnvironmentVariables(Boolean mergeEnvironmentVariables
) {
716 this.mergeEnvironmentVariables
= mergeEnvironmentVariables
;
719 public void setOsConsole(String osConsole
) {
720 this.osConsole
= osConsole
;
723 public void setGenerateScript(String generateScript
) {
724 this.generateScript
= generateScript
;
727 public void setExecutionResources(ExecutionResources executionResources
) {
728 this.executionResources
= executionResources
;
731 public void setRedirectStdOut(Boolean redirectStdOut
) {
732 this.redirectStdOut
= redirectStdOut
;
735 public void addOutputListener(SystemCallOutputListener outputListener
) {
736 outputListeners
.add(outputListener
);
739 public void removeOutputListener(SystemCallOutputListener outputListener
) {
740 outputListeners
.remove(outputListener
);
743 public void setOutputListeners(
744 List
<SystemCallOutputListener
> outputListeners
) {
745 this.outputListeners
= outputListeners
;
748 public void setExecutor(Executor executor
) {
749 this.executor
= executor
;
752 public void setSudo(String sudo
) {
756 public void setCallbackHandler(CallbackHandler callbackHandler
) {
757 this.callbackHandler
= callbackHandler
;
760 public void setChroot(String chroot
) {
761 this.chroot
= chroot
;
764 private class DummyexecuteStreamHandler
implements ExecuteStreamHandler
{
766 public void setProcessErrorStream(InputStream is
) throws IOException
{
769 public void setProcessInputStream(OutputStream os
) throws IOException
{
772 public void setProcessOutputStream(InputStream is
) throws IOException
{
775 public void start() throws IOException
{