3030import java .util .ArrayList ;
3131import java .util .Arrays ;
3232import java .util .List ;
33+ import java .util .Map ;
3334import java .util .Properties ;
3435import java .util .concurrent .Callable ;
3536import java .util .concurrent .ExecutionException ;
4243import java .util .stream .Collectors ;
4344
4445import org .apache .cloudstack .utils .security .KeyStoreUtils ;
46+ import org .apache .commons .collections4 .MapUtils ;
4547import org .apache .commons .io .IOUtils ;
46- import org .apache .commons .lang3 .ArrayUtils ;
4748import org .apache .logging .log4j .LogManager ;
4849import org .apache .logging .log4j .Logger ;
4950import org .joda .time .Duration ;
@@ -240,11 +241,11 @@ public String execute(OutputInterpreter interpreter) {
240241 return execute (interpreter , null );
241242 }
242243
243- public String execute (OutputInterpreter interpreter , String [] environment ) {
244+ public String execute (OutputInterpreter interpreter , Map < String , String > environment ) {
244245 return executeInternal (interpreter , environment );
245246 }
246247
247- public String executeInternal (OutputInterpreter interpreter , String [] environment ) {
248+ private String executeInternal (OutputInterpreter interpreter , Map < String , String > environment ) {
248249 String [] command = _command .toArray (new String [_command .size ()]);
249250 String commandLine = buildCommandLine (command );
250251 if (_logger .isDebugEnabled () && !avoidLoggingCommand ) {
@@ -254,23 +255,21 @@ public String executeInternal(OutputInterpreter interpreter, String[] environmen
254255 try {
255256 _logger .trace (String .format ("Creating process for command [%s]." , commandLine ));
256257
257- if (ArrayUtils .isEmpty (environment )) {
258- ProcessBuilder pb = new ProcessBuilder (command );
259- pb .redirectErrorStream (true );
260- if (_workDir != null )
261- pb .directory (new File (_workDir ));
258+ ProcessBuilder pb = new ProcessBuilder (command );
259+ pb .redirectErrorStream (true );
262260
263- _logger .trace (String .format ("Starting process for command [%s]." , commandLine ));
264- _process = pb .start ();
265- } else {
266- // Since Runtime.exec() does not support redirecting the error stream, then append 2>&1 to the command
267- String [] commands = new String [] {"sh" , "-c" , String .format ("%s 2>&1" , commandLine )};
268- // The PATH variable must be added for indirect calls within the running command
269- // Example: virt-v2v invokes qemu-img, which cannot be found if PATH is not set
270- String [] env = ArrayUtils .add (environment , String .format ("PATH=%s" , System .getenv ("PATH" )));
271- _process = Runtime .getRuntime ().exec (commands , env , _workDir != null ? new File (_workDir ) : null );
261+ if (MapUtils .isNotEmpty (environment )) {
262+ Map <String , String > processEnvironment = pb .environment ();
263+ processEnvironment .putAll (environment );
264+ }
265+
266+ if (_workDir != null ) {
267+ pb .directory (new File (_workDir ));
272268 }
273269
270+ _logger .trace (String .format ("Starting process for command [%s]." , commandLine ));
271+ _process = pb .start ();
272+
274273 if (_process == null ) {
275274 _logger .warn (String .format ("Unable to execute command [%s] because no process was created." , commandLine ));
276275 return "Unable to execute the command: " + command [0 ];
0 commit comments