Skip to content

Commit f77a2e0

Browse files
committed
refactor check extensions dir
Signed-off-by: Abhishek Kumar <[email protected]>
1 parent 83f5cb8 commit f77a2e0

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public String getDescription() {
107107

108108
private String extensionsDirectory;
109109

110-
private Map<String, String> loadAccessDetails(Map<String, String> accessDetails, VirtualMachineTO virtualMachineTO) {
110+
protected Map<String, String> loadAccessDetails(Map<String, String> accessDetails, VirtualMachineTO virtualMachineTO) {
111111
Map<String, String> modifiedDetails = new HashMap<>();
112112
for (Map.Entry<String, String> entry : accessDetails.entrySet()) {
113113
String key = entry.getKey();
@@ -131,7 +131,7 @@ private Map<String, String> loadAccessDetails(Map<String, String> accessDetails,
131131
return modifiedDetails;
132132
}
133133

134-
private String getExtensionCheckedScriptPath(String extensionName) {
134+
protected String getExtensionCheckedScriptPath(String extensionName) {
135135
String path = getExtensionScriptPath(extensionName);
136136
File file = new File(path);
137137
if (!file.exists()) {
@@ -150,20 +150,24 @@ private String getExtensionCheckedScriptPath(String extensionName) {
150150

151151
}
152152

153+
protected boolean checkExtensionsDirectory() {
154+
File dir = new File(extensionsDirectory);
155+
if (!dir.exists() || !dir.isDirectory() || !dir.canWrite()) {
156+
logger.error("Extension directory [{}] specified by config - {} is not properly set up. It must exist, be a directory, and be writeable",
157+
extensionsDirectory, ExtensionsDirectory.key());
158+
return false;
159+
}
160+
return true;
161+
}
162+
153163
@Override
154164
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
155165
super.configure(name, params);
156166
extensionsDirectory = ExtensionsDirectory.value();
157167
if (StringUtils.isBlank(extensionsDirectory)) {
158168
throw new ConfigurationException("Extension directory path is blank");
159169
}
160-
161-
File dir = new File(extensionsDirectory);
162-
if (!dir.exists() || !dir.isDirectory() || !dir.canWrite()) {
163-
String msg = String.format("Extension directory [%s] is not properly set up", extensionsDirectory);
164-
logger.error("{}. It must exist, be a directory, and be writeable", msg);
165-
throw new ConfigurationException(msg);
166-
}
170+
checkExtensionsDirectory();
167171
return true;
168172
}
169173

@@ -390,14 +394,16 @@ public void prepareScripts(String extensionName) {
390394
logger.info("File already exists at {}, skipping copy.", destinationPath);
391395
return;
392396
}
393-
397+
if (!checkExtensionsDirectory()) {
398+
throw new CloudRuntimeException(String.format("Failed to prepare scripts for extension: %s", extensionName));
399+
}
394400
String destinationDir = destinationPath.substring(0, destinationPath.lastIndexOf('/'));
395401
Script mkdirScript = new Script(true, "/bin/mkdir", 0, logger);
396402
mkdirScript.add("-p", destinationDir);
397403
String result = mkdirScript.execute();
398404
if (result != null) {
399405
logger.warn("Failed to create directory {} due to {}", destinationDir, result);
400-
throw new CloudRuntimeException("Failed to create destination directory: " + result);
406+
throw new CloudRuntimeException(String.format("Failed to prepare scripts for extension: %s", extensionName));
401407
}
402408

403409
String prepareExternalScript = Script.findScript("", BASE_EXTERNAL_PROVISIONER_SCRIPT);
@@ -406,7 +412,7 @@ public void prepareScripts(String extensionName) {
406412
result = copyScript.execute();
407413
if (result != null) {
408414
logger.warn("Failed to copy script to {} due to {}", destinationPath, result);
409-
throw new CloudRuntimeException("Script copy failed: " + result);
415+
throw new CloudRuntimeException(String.format("Failed to prepare scripts for extension: %s", extensionName));
410416
}
411417
}
412418

0 commit comments

Comments
 (0)