Skip to content

Commit dac73f4

Browse files
committed
Fix extra params execution
1 parent d9c0f74 commit dac73f4

File tree

3 files changed

+43
-26
lines changed

3 files changed

+43
-26
lines changed

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import java.net.URLEncoder;
2222
import java.nio.charset.Charset;
23+
import java.util.Arrays;
2324
import java.util.List;
2425
import java.util.UUID;
2526

@@ -238,16 +239,32 @@ protected boolean performInstanceConversion(String originalVMName, String source
238239
script.add("-v");
239240
}
240241
if (StringUtils.isNotBlank(extraParams)) {
241-
script.add(extraParams);
242+
addExtraParamsToScript(extraParams, script);
242243
}
243244

244245
String logPrefix = String.format("(%s) virt-v2v ovf source: %s progress", originalVMName, sourceOVFDirPath);
245246
OutputInterpreter.LineByLineOutputLogger outputLogger = new OutputInterpreter.LineByLineOutputLogger(logger, logPrefix);
246-
script.execute(outputLogger, serverResource.getConvertInstanceEnv());
247+
script.execute(outputLogger);
247248
int exitValue = script.getExitValue();
248249
return exitValue == 0;
249250
}
250251

252+
protected void addExtraParamsToScript(String extraParams, Script script) {
253+
List<String> separatedArgs = Arrays.asList(extraParams.split(" "));
254+
int i = 0;
255+
while (i < separatedArgs.size()) {
256+
String current = separatedArgs.get(i);
257+
String next = (i + 1) < separatedArgs.size() ? separatedArgs.get(i + 1) : null;
258+
if (next == null || next.startsWith("-")) {
259+
script.add(current);
260+
i = i + 1;
261+
} else {
262+
script.add(current, next);
263+
i = i + 2;
264+
}
265+
}
266+
}
267+
251268
protected String encodeUsername(String username) {
252269
return URLEncoder.encode(username, Charset.defaultCharset());
253270
}

plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtConvertInstanceCommandWrapperTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,22 @@ public void testExecuteConvertFailure() {
171171
Mockito.anyString(), Mockito.anyString(), Mockito.anyLong(), Mockito.anyBoolean(), Mockito.nullable(String.class), Mockito.any(LibvirtComputingResource.class));
172172
}
173173
}
174+
175+
@Test
176+
public void testAddExtraParamsToScriptSameKeysAndValues() {
177+
Script script = Mockito.mock(Script.class);
178+
String extraParams = "--mac 00:0c:29:e6:3d:9d:ip:192.168.0.89,192.168.0.1,24,192.168.0.254";
179+
convertInstanceCommandWrapper.addExtraParamsToScript(extraParams, script);
180+
Mockito.verify(script).add("--mac", "00:0c:29:e6:3d:9d:ip:192.168.0.89,192.168.0.1,24,192.168.0.254");
181+
}
182+
183+
@Test
184+
public void testAddExtraParamsToScriptDifferentArgs() {
185+
Script script = Mockito.mock(Script.class);
186+
String extraParams = "--mac 00:0c:29:e6:3d:9d:ip:192.168.0.89,192.168.0.1,24,192.168.0.254 -x -v";
187+
convertInstanceCommandWrapper.addExtraParamsToScript(extraParams, script);
188+
Mockito.verify(script).add("--mac", "00:0c:29:e6:3d:9d:ip:192.168.0.89,192.168.0.1,24,192.168.0.254");
189+
Mockito.verify(script).add("-x");
190+
Mockito.verify(script).add("-v");
191+
}
174192
}

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

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343

4444
import org.apache.cloudstack.utils.security.KeyStoreUtils;
4545
import org.apache.commons.io.IOUtils;
46-
import org.apache.commons.lang3.ArrayUtils;
4746
import org.apache.logging.log4j.LogManager;
4847
import org.apache.logging.log4j.Logger;
4948
import org.joda.time.Duration;
@@ -237,14 +236,6 @@ static String stackTraceAsString(Throwable throwable) {
237236
}
238237

239238
public String execute(OutputInterpreter interpreter) {
240-
return execute(interpreter, null);
241-
}
242-
243-
public String execute(OutputInterpreter interpreter, String[] environment) {
244-
return executeInternal(interpreter, environment);
245-
}
246-
247-
public String executeInternal(OutputInterpreter interpreter, String[] environment) {
248239
String[] command = _command.toArray(new String[_command.size()]);
249240
String commandLine = buildCommandLine(command);
250241
if (_logger.isDebugEnabled() && !avoidLoggingCommand) {
@@ -254,22 +245,13 @@ public String executeInternal(OutputInterpreter interpreter, String[] environmen
254245
try {
255246
_logger.trace(String.format("Creating process for command [%s].", commandLine));
256247

257-
if (ArrayUtils.isNotEmpty(environment)) {
258-
// Since Runtime.exec() does not support redirecting the error stream, then append 2>&1 to the command
259-
String[] commands = new String[] {"sh", "-c", String.format("%s 2>&1", commandLine)};
260-
// The PATH variable must be added for indirect calls within the running command
261-
// Example: virt-v2v invokes qemu-img, which cannot be found if PATH is not set
262-
String[] env = ArrayUtils.add(environment, String.format("PATH=%s", System.getenv("PATH")));
263-
_process = Runtime.getRuntime().exec(commands, env, _workDir != null ? new File(_workDir) : null);
264-
} else {
265-
ProcessBuilder pb = new ProcessBuilder(command);
266-
pb.redirectErrorStream(true);
267-
if (_workDir != null)
268-
pb.directory(new File(_workDir));
248+
ProcessBuilder pb = new ProcessBuilder(command);
249+
pb.redirectErrorStream(true);
250+
if (_workDir != null)
251+
pb.directory(new File(_workDir));
269252

270-
_logger.trace(String.format("Starting process for command [%s].", commandLine));
271-
_process = pb.start();
272-
}
253+
_logger.trace(String.format("Starting process for command [%s].", commandLine));
254+
_process = pb.start();
273255
if (_process == null) {
274256
_logger.warn(String.format("Unable to execute command [%s] because no process was created.", commandLine));
275257
return "Unable to execute the command: " + command[0];

0 commit comments

Comments
 (0)