Skip to content

Commit 54c7073

Browse files
committed
use java code to prepare scripts
Signed-off-by: Abhishek Kumar <[email protected]>
1 parent f77a2e0 commit 54c7073

File tree

1 file changed

+38
-16
lines changed

1 file changed

+38
-16
lines changed

plugins/hypervisors/external/src/main/java/org/apache/cloudstack/hypervisor/external/provisioner/simpleprovisioner/SimpleExternalProvisioner.java

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
package org.apache.cloudstack.hypervisor.external.provisioner.simpleprovisioner;
2020

2121
import java.io.File;
22+
import java.io.IOException;
23+
import java.nio.file.Files;
24+
import java.nio.file.Path;
25+
import java.nio.file.Paths;
26+
import java.nio.file.StandardCopyOption;
2227
import java.util.ArrayList;
2328
import java.util.HashMap;
2429
import java.util.List;
@@ -394,26 +399,43 @@ public void prepareScripts(String extensionName) {
394399
logger.info("File already exists at {}, skipping copy.", destinationPath);
395400
return;
396401
}
402+
CloudRuntimeException exception =
403+
new CloudRuntimeException(String.format("Failed to prepare scripts for extension: %s", extensionName));
397404
if (!checkExtensionsDirectory()) {
398-
throw new CloudRuntimeException(String.format("Failed to prepare scripts for extension: %s", extensionName));
405+
throw exception;
399406
}
400-
String destinationDir = destinationPath.substring(0, destinationPath.lastIndexOf('/'));
401-
Script mkdirScript = new Script(true, "/bin/mkdir", 0, logger);
402-
mkdirScript.add("-p", destinationDir);
403-
String result = mkdirScript.execute();
404-
if (result != null) {
405-
logger.warn("Failed to create directory {} due to {}", destinationDir, result);
406-
throw new CloudRuntimeException(String.format("Failed to prepare scripts for extension: %s", extensionName));
407+
Path sourcePath = null;
408+
String sourceScriptPath = Script.findScript("", BASE_EXTERNAL_PROVISIONER_SCRIPT);
409+
if (sourceScriptPath != null) {
410+
sourcePath = Paths.get(sourceScriptPath);
407411
}
408-
409-
String prepareExternalScript = Script.findScript("", BASE_EXTERNAL_PROVISIONER_SCRIPT);
410-
Script copyScript = new Script(true, "/bin/cp", 0, logger);
411-
copyScript.add(prepareExternalScript, destinationPath);
412-
result = copyScript.execute();
413-
if (result != null) {
414-
logger.warn("Failed to copy script to {} due to {}", destinationPath, result);
415-
throw new CloudRuntimeException(String.format("Failed to prepare scripts for extension: %s", extensionName));
412+
if (sourcePath == null) {
413+
logger.error("Failed to find base script for preparing extension: {}",
414+
extensionName);
415+
throw exception;
416+
}
417+
Path destinationPathObj = Paths.get(destinationPath);
418+
Path destinationDirPath = destinationPathObj.getParent();
419+
if (destinationDirPath == null) {
420+
logger.error("Failed to find parent directory for extension: {} script path {}",
421+
extensionName, destinationPath);
422+
throw exception;
423+
}
424+
try {
425+
Files.createDirectories(destinationDirPath);
426+
} catch (IOException e) {
427+
logger.error("Failed to create directory: {} for extension: {}", destinationDirPath,
428+
extensionName, e);
429+
throw exception;
430+
}
431+
try {
432+
Files.copy(sourcePath, destinationPathObj, StandardCopyOption.REPLACE_EXISTING);
433+
} catch (IOException e) {
434+
logger.error("Failed to copy script from {} to {}", sourcePath, destinationPath, e);
435+
throw exception;
416436
}
437+
logger.debug("Successfully copied prepared script [{}] for extension: {}", destinationPath,
438+
extensionName);
417439
}
418440

419441
public Pair<Boolean, String> runCustomActionOnExternalSystem(String filename, String actionName, Map<String, String> accessDetails, int wait) {

0 commit comments

Comments
 (0)