Skip to content

Commit 1a64f64

Browse files
committed
Address review comments
1 parent f893d3d commit 1a64f64

File tree

3 files changed

+27
-27
lines changed

3 files changed

+27
-27
lines changed

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,7 @@ protected enum HealthCheckResult {
882882
protected StorageSubsystemCommandHandler storageHandler;
883883

884884
private boolean convertInstanceVerboseMode = false;
885-
private String[] convertInstanceEnv = null;
885+
private Map<String, String> convertInstanceEnv = null;
886886
protected boolean dpdkSupport = false;
887887
protected String dpdkOvsPath;
888888
protected String directDownloadTemporaryDownloadPath;
@@ -947,7 +947,7 @@ public boolean isConvertInstanceVerboseModeEnabled() {
947947
return convertInstanceVerboseMode;
948948
}
949949

950-
public String[] getConvertInstanceEnv() {
950+
public Map<String, String> getConvertInstanceEnv() {
951951
return convertInstanceEnv;
952952
}
953953

@@ -1437,14 +1437,14 @@ private void setConvertInstanceEnv(String convertEnvTmpDir, String convertEnvVir
14371437
return;
14381438
}
14391439
if (StringUtils.isNotBlank(convertEnvTmpDir) && StringUtils.isNotBlank(convertEnvVirtv2vTmpDir)) {
1440-
convertInstanceEnv = new String[2];
1441-
convertInstanceEnv[0] = String.format("%s=%s", "TMPDIR", convertEnvTmpDir);
1442-
convertInstanceEnv[1] = String.format("%s=%s", "VIRT_V2V_TMPDIR", convertEnvVirtv2vTmpDir);
1440+
convertInstanceEnv = new HashMap<>(2);
1441+
convertInstanceEnv.put("TMPDIR", convertEnvTmpDir);
1442+
convertInstanceEnv.put("VIRT_V2V_TMPDIR", convertEnvVirtv2vTmpDir);
14431443
} else {
1444-
convertInstanceEnv = new String[1];
1444+
convertInstanceEnv = new HashMap<>(1);
14451445
String key = StringUtils.isNotBlank(convertEnvTmpDir) ? "TMPDIR" : "VIRT_V2V_TMPDIR";
14461446
String value = StringUtils.isNotBlank(convertEnvTmpDir) ? convertEnvTmpDir : convertEnvVirtv2vTmpDir;
1447-
convertInstanceEnv[0] = String.format("%s=%s", key, value);
1447+
convertInstanceEnv.put(key, value);
14481448
}
14491449
}
14501450

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtConvertInstanceCommandWrapper.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@
2222
import java.nio.charset.Charset;
2323
import java.util.Arrays;
2424
import java.util.List;
25+
import java.util.Map;
2526
import java.util.UUID;
2627

2728
import org.apache.cloudstack.storage.to.PrimaryDataStoreTO;
28-
import org.apache.commons.lang3.ArrayUtils;
29+
import org.apache.commons.collections4.MapUtils;
2930
import org.apache.commons.lang3.StringUtils;
3031

3132
import com.cloud.agent.api.Answer;
@@ -245,8 +246,8 @@ protected boolean performInstanceConversion(String originalVMName, String source
245246

246247
String logPrefix = String.format("(%s) virt-v2v ovf source: %s progress", originalVMName, sourceOVFDirPath);
247248
OutputInterpreter.LineByLineOutputLogger outputLogger = new OutputInterpreter.LineByLineOutputLogger(logger, logPrefix);
248-
String[] convertInstanceEnv = serverResource.getConvertInstanceEnv();
249-
if (ArrayUtils.isEmpty(convertInstanceEnv)) {
249+
Map<String, String> convertInstanceEnv = serverResource.getConvertInstanceEnv();
250+
if (MapUtils.isEmpty(convertInstanceEnv)) {
250251
script.execute(outputLogger);
251252
} else {
252253
script.execute(outputLogger, convertInstanceEnv);

utils/src/main/java/com/cloud/utils/script/Script.java

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.ArrayList;
3131
import java.util.Arrays;
3232
import java.util.List;
33+
import java.util.Map;
3334
import java.util.Properties;
3435
import java.util.concurrent.Callable;
3536
import java.util.concurrent.ExecutionException;
@@ -42,8 +43,8 @@
4243
import java.util.stream.Collectors;
4344

4445
import org.apache.cloudstack.utils.security.KeyStoreUtils;
46+
import org.apache.commons.collections4.MapUtils;
4547
import org.apache.commons.io.IOUtils;
46-
import org.apache.commons.lang3.ArrayUtils;
4748
import org.apache.logging.log4j.LogManager;
4849
import org.apache.logging.log4j.Logger;
4950
import 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

Comments
 (0)