|
19 | 19 | package org.apache.cloudstack.hypervisor.external.provisioner.simpleprovisioner; |
20 | 20 |
|
21 | 21 | 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; |
22 | 27 | import java.util.ArrayList; |
23 | 28 | import java.util.HashMap; |
24 | 29 | import java.util.List; |
@@ -394,26 +399,43 @@ public void prepareScripts(String extensionName) { |
394 | 399 | logger.info("File already exists at {}, skipping copy.", destinationPath); |
395 | 400 | return; |
396 | 401 | } |
| 402 | + CloudRuntimeException exception = |
| 403 | + new CloudRuntimeException(String.format("Failed to prepare scripts for extension: %s", extensionName)); |
397 | 404 | if (!checkExtensionsDirectory()) { |
398 | | - throw new CloudRuntimeException(String.format("Failed to prepare scripts for extension: %s", extensionName)); |
| 405 | + throw exception; |
399 | 406 | } |
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); |
407 | 411 | } |
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; |
416 | 436 | } |
| 437 | + logger.debug("Successfully copied prepared script [{}] for extension: {}", destinationPath, |
| 438 | + extensionName); |
417 | 439 | } |
418 | 440 |
|
419 | 441 | public Pair<Boolean, String> runCustomActionOnExternalSystem(String filename, String actionName, Map<String, String> accessDetails, int wait) { |
|
0 commit comments