Skip to content

Commit 6941097

Browse files
committed
refactor
Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>
1 parent 21419d1 commit 6941097

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

services/secondary-storage/server/src/main/java/org/apache/cloudstack/storage/template/UploadManagerImpl.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@
6262

6363
public class UploadManagerImpl extends ManagerBase implements UploadManager {
6464

65-
protected static final String BASE_EXTRACT_DIR = "/var/www/html/userdata/";
66-
protected static final String BASE_MOUNT_DIR = "/mnt/SecStorage/";
65+
protected static final String EXTRACT_USERDATA_DIR = "userdata";
66+
protected static final String BASE_EXTRACT_PATH = String.format("/var/www/html/%s/", EXTRACT_USERDATA_DIR);
67+
protected static final String BASE_MOUNT_PATH = "/mnt/SecStorage/";
6768

6869
public class Completion implements UploadCompleteCallback {
6970
private final String jobId;
@@ -106,7 +107,7 @@ public void cleanup() {
106107
private ExecutorService threadPool;
107108
private final Map<String, UploadJob> jobs = new ConcurrentHashMap<String, UploadJob>();
108109
private String parentDir;
109-
private final String extractMountPoint = BASE_MOUNT_DIR + "extractmnt";
110+
private final String extractMountPoint = BASE_MOUNT_PATH + "extractmnt";
110111
private StorageLayer _storage;
111112
private boolean hvm;
112113

@@ -274,7 +275,7 @@ public CreateEntityDownloadURLAnswer handleCreateEntityURLCommand(CreateEntityDo
274275
return new CreateEntityDownloadURLAnswer(errorString, CreateEntityDownloadURLAnswer.RESULT_FAILURE);
275276
}
276277
// Create the directory structure so that its visible under apache server root
277-
String extractDir = BASE_EXTRACT_DIR + cmd.getFilepathInExtractURL() + File.separator;
278+
String extractDir = BASE_EXTRACT_PATH + cmd.getFilepathInExtractURL() + File.separator;
278279
Script command = new Script("/bin/su", logger);
279280
command.add("-s");
280281
command.add("/bin/bash");
@@ -288,7 +289,7 @@ public CreateEntityDownloadURLAnswer handleCreateEntityURLCommand(CreateEntityDo
288289
return new CreateEntityDownloadURLAnswer(errorString, CreateEntityDownloadURLAnswer.RESULT_FAILURE);
289290
}
290291

291-
File file = new File(BASE_MOUNT_DIR + cmd.getParent() + File.separator + cmd.getInstallPath());
292+
File file = new File(BASE_MOUNT_PATH + cmd.getParent() + File.separator + cmd.getInstallPath());
292293
// Return error if the file does not exist or is a directory
293294
if (!file.exists() || file.isDirectory()) {
294295
String errorString = "Error in finding the file " + file.getAbsolutePath();
@@ -301,7 +302,7 @@ public CreateEntityDownloadURLAnswer handleCreateEntityURLCommand(CreateEntityDo
301302
// Create a symbolic link from the actual directory to the template location. The entity would be directly visible under /var/www/html/userdata/cmd.getInstallPath();
302303
command = new Script("/bin/bash", logger);
303304
command.add("-c");
304-
command.add("ln -sf " + BASE_MOUNT_DIR + cmd.getParent() + File.separator + cmd.getInstallPath() + " " + extractDir + filename);
305+
command.add("ln -sf " + BASE_MOUNT_PATH + cmd.getParent() + File.separator + cmd.getInstallPath() + " " + extractDir + filename);
305306
result = command.execute();
306307
if (result != null) {
307308
String errorString = "Error in linking err=" + result;
@@ -339,24 +340,24 @@ public Answer handleDeleteEntityDownloadURLCommand(DeleteEntityDownloadURLComman
339340
if (extractUrl != null) {
340341
URI uri = URI.create(extractUrl);
341342
String uriPath = uri.getPath();
342-
String marker = "/userdata/";
343+
String marker = String.format("/%s/", EXTRACT_USERDATA_DIR);
343344
String linkPath = uriPath.startsWith(marker)
344345
? uriPath.substring(marker.length())
345346
: uriPath.substring(uriPath.indexOf(marker) + marker.length());
346-
command.add("unlink " + BASE_EXTRACT_DIR + linkPath);
347+
command.add("unlink " + BASE_EXTRACT_PATH + linkPath);
347348
result = command.execute();
348349
if (result != null) {
349350
// FIXME - Ideally should bail out if you can't delete symlink. Not doing it right now.
350351
// This is because the ssvm might already be destroyed and the symlinks do not exist.
351-
logger.warn("Error in deleting symlink :" + result);
352+
logger.warn("Error in deleting symlink :{}", result);
352353
} else {
353-
deleteEntitySymlinkRootPathIfNeeded(cmd, linkPath);
354+
deleteEntitySymlinkRootDirectoryIfNeeded(cmd, linkPath);
354355
}
355356
}
356357

357358
// If its a volume or archive also delete the Hard link since it was created only for the purpose of download.
358359
if (cmd.getType() == Upload.Type.VOLUME || cmd.getType() == Upload.Type.ARCHIVE) {
359-
String targetPath = BASE_MOUNT_DIR + cmd.getParentPath() + File.separator + path;
360+
String targetPath = BASE_MOUNT_PATH + cmd.getParentPath() + File.separator + path;
360361
command = new Script("/bin/bash", logger);
361362
command.add("-c");
362363
command.add("rm -rf " + targetPath);
@@ -373,7 +374,7 @@ public Answer handleDeleteEntityDownloadURLCommand(DeleteEntityDownloadURLComman
373374
if (Upload.Type.SNAPSHOT.equals(cmd.getType())) {
374375
try {
375376
path = path.replace(ConvertSnapshotCommand.TEMP_SNAPSHOT_NAME, "");
376-
String fullPath = String.format("%s%s%s%s%s", BASE_EXTRACT_DIR, cmd.getParentPath(), File.separator, path, ConvertSnapshotCommand.TEMP_SNAPSHOT_NAME);
377+
String fullPath = String.format("%s%s%s%s%s", BASE_MOUNT_PATH, cmd.getParentPath(), File.separator, path, ConvertSnapshotCommand.TEMP_SNAPSHOT_NAME);
377378
Files.deleteIfExists(Path.of(fullPath));
378379
} catch (IOException e) {
379380
String errorString = String.format("Error deleting temporary snapshot %s%s.", path, ConvertSnapshotCommand.TEMP_SNAPSHOT_NAME);
@@ -385,7 +386,7 @@ public Answer handleDeleteEntityDownloadURLCommand(DeleteEntityDownloadURLComman
385386
return new Answer(cmd, true, "");
386387
}
387388

388-
protected void deleteEntitySymlinkRootPathIfNeeded(DeleteEntityDownloadURLCommand cmd, String linkPath) {
389+
protected void deleteEntitySymlinkRootDirectoryIfNeeded(DeleteEntityDownloadURLCommand cmd, String linkPath) {
389390
if (StringUtils.isEmpty(linkPath)) {
390391
return;
391392
}
@@ -398,7 +399,7 @@ protected void deleteEntitySymlinkRootPathIfNeeded(DeleteEntityDownloadURLComman
398399
return;
399400
}
400401
logger.info("Deleting symlink root directory: {} for {}", rootDir, cmd.getExtractUrl());
401-
Path rootDirPath = Path.of(BASE_EXTRACT_DIR + rootDir);
402+
Path rootDirPath = Path.of(BASE_EXTRACT_PATH + rootDir);
402403
String failMsg = "Failed to delete symlink root directory: {} for {}";
403404
try {
404405
if (!FileUtil.deleteRecursively(rootDirPath)) {

services/secondary-storage/server/src/test/java/org/apache/cloudstack/storage/template/UploadManagerImplTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,21 @@ public void tearDown() {
5858
@Test
5959
public void doesNotDeleteWhenLinkPathIsEmpty() {
6060
String emptyLinkPath = "";
61-
uploadManager.deleteEntitySymlinkRootPathIfNeeded(mock(DeleteEntityDownloadURLCommand.class), emptyLinkPath);
61+
uploadManager.deleteEntitySymlinkRootDirectoryIfNeeded(mock(DeleteEntityDownloadURLCommand.class), emptyLinkPath);
6262
fileUtilMock.verify(() -> FileUtil.deleteRecursively(any(Path.class)), never());
6363
}
6464

6565
@Test
6666
public void doesNotDeleteWhenRootDirIsNotUuid() {
6767
String invalidLinkPath = "invalidRootDir/file";
68-
uploadManager.deleteEntitySymlinkRootPathIfNeeded(mock(DeleteEntityDownloadURLCommand.class), invalidLinkPath);
68+
uploadManager.deleteEntitySymlinkRootDirectoryIfNeeded(mock(DeleteEntityDownloadURLCommand.class), invalidLinkPath);
6969
fileUtilMock.verify(() -> FileUtil.deleteRecursively(any(Path.class)), never());
7070
}
7171

7272
@Test
7373
public void deletesSymlinkRootDirectoryWhenValidUuid() {
7474
String validLinkPath = "123e4567-e89b-12d3-a456-426614174000/file";
75-
uploadManager.deleteEntitySymlinkRootPathIfNeeded(mock(DeleteEntityDownloadURLCommand.class), validLinkPath);
75+
uploadManager.deleteEntitySymlinkRootDirectoryIfNeeded(mock(DeleteEntityDownloadURLCommand.class), validLinkPath);
7676
fileUtilMock.verify(() -> FileUtil.deleteRecursively(any(Path.class)), times(1));
7777
}
7878
}

0 commit comments

Comments
 (0)