Updated Branches:
  refs/heads/object_store 27133fba7 -> 8a1a51c6f

Clean up deleteSnapshot agent command, since snapshot path is already
stored in snapshot_store_ref, no need to construct path in resource
side.

Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/8a1a51c6
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/8a1a51c6
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/8a1a51c6

Branch: refs/heads/object_store
Commit: 8a1a51c6fc74549f167c65798cad8ad3473a23bf
Parents: 27133fb
Author: Min Chen <[email protected]>
Authored: Tue May 28 13:36:54 2013 -0700
Committer: Min Chen <[email protected]>
Committed: Tue May 28 13:36:54 2013 -0700

----------------------------------------------------------------------
 .../agent/api/DeleteSnapshotBackupCommand2.java    |   56 ++++++
 .../cloud/agent/api/DeleteSnapshotsDirCommand.java |   34 +---
 .../driver/CloudStackImageStoreDriverImpl.java     |   14 +-
 .../datastore/driver/S3ImageStoreDriverImpl.java   |   10 +-
 .../driver/SwiftImageStoreDriverImpl.java          |   11 +-
 .../src/com/cloud/storage/StorageManagerImpl.java  |    4 +-
 .../cloud/storage/snapshot/SnapshotManager.java    |    8 +-
 .../storage/snapshot/SnapshotManagerImpl.java      |   20 +--
 .../resource/NfsSecondaryStorageResource.java      |  153 +++++++++++++--
 9 files changed, 229 insertions(+), 81 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/8a1a51c6/core/src/com/cloud/agent/api/DeleteSnapshotBackupCommand2.java
----------------------------------------------------------------------
diff --git a/core/src/com/cloud/agent/api/DeleteSnapshotBackupCommand2.java 
b/core/src/com/cloud/agent/api/DeleteSnapshotBackupCommand2.java
new file mode 100644
index 0000000..2fcb62a
--- /dev/null
+++ b/core/src/com/cloud/agent/api/DeleteSnapshotBackupCommand2.java
@@ -0,0 +1,56 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+package com.cloud.agent.api;
+
+import com.cloud.agent.api.to.DataStoreTO;
+
+/**
+ * This command encapsulates a primitive operation which enables coalescing 
the backed up VHD snapshots on the secondary server
+ * This currently assumes that the secondary storage are mounted on the 
XenServer.
+ */
+public class DeleteSnapshotBackupCommand2 extends Command {
+    private DataStoreTO store;
+    private String snapshotPath;
+
+
+    public DeleteSnapshotBackupCommand2() {
+    }
+
+
+    public DeleteSnapshotBackupCommand2(DataStoreTO store,
+                                       String snapshotPath)
+    {
+        this.store = store;
+        this.snapshotPath = snapshotPath;
+    }
+
+
+    public DataStoreTO getDataStore(){
+        return store;
+    }
+
+
+    public String getSnapshotPath() {
+        return snapshotPath;
+    }
+
+
+    @Override
+    public boolean executeInSequence() {
+        return true;
+    }
+}

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/8a1a51c6/core/src/com/cloud/agent/api/DeleteSnapshotsDirCommand.java
----------------------------------------------------------------------
diff --git a/core/src/com/cloud/agent/api/DeleteSnapshotsDirCommand.java 
b/core/src/com/cloud/agent/api/DeleteSnapshotsDirCommand.java
index e2071d7..26d7586 100644
--- a/core/src/com/cloud/agent/api/DeleteSnapshotsDirCommand.java
+++ b/core/src/com/cloud/agent/api/DeleteSnapshotsDirCommand.java
@@ -16,27 +16,23 @@
 // under the License.
 package com.cloud.agent.api;
 
+import com.cloud.agent.api.to.DataStoreTO;
+
 /**
  * This command encapsulates a primitive operation which enables coalescing 
the backed up VHD snapshots on the secondary server
  * This currently assumes that the secondary storage are mounted on the 
XenServer.
  */
 public class DeleteSnapshotsDirCommand extends Command {
-    String secondaryStorageUrl;
-    Long dcId;
-    Long accountId;
-    Long volumeId;
+    DataStoreTO store;
+    String directory;
 
     protected DeleteSnapshotsDirCommand() {
 
     }
 
-    public DeleteSnapshotsDirCommand(String secondaryStorageUrl,
- Long dcId, Long accountId, Long volumeId)
-    {
-        this.secondaryStorageUrl = secondaryStorageUrl;
-        this.dcId = dcId;
-        this.accountId = accountId;
-        this.volumeId = volumeId;
+    public DeleteSnapshotsDirCommand(DataStoreTO store, String dir) {
+        this.store = store;
+        this.directory = dir;
     }
 
     @Override
@@ -44,20 +40,12 @@ public class DeleteSnapshotsDirCommand extends Command {
         return true;
     }
 
-    public String getSecondaryStorageUrl() {
-        return secondaryStorageUrl;
+    public DataStoreTO getDataStore() {
+        return store;
     }
 
-    public Long getDcId() {
-        return dcId;
-    }
 
-    public Long getAccountId() {
-        return accountId;
+    public String getDirectory() {
+        return directory;
     }
-
-    public Long getVolumeId() {
-        return volumeId;
-    }
-
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/8a1a51c6/plugins/storage/image/default/src/org/apache/cloudstack/storage/datastore/driver/CloudStackImageStoreDriverImpl.java
----------------------------------------------------------------------
diff --git 
a/plugins/storage/image/default/src/org/apache/cloudstack/storage/datastore/driver/CloudStackImageStoreDriverImpl.java
 
b/plugins/storage/image/default/src/org/apache/cloudstack/storage/datastore/driver/CloudStackImageStoreDriverImpl.java
index a6c4696..bc64250 100644
--- 
a/plugins/storage/image/default/src/org/apache/cloudstack/storage/datastore/driver/CloudStackImageStoreDriverImpl.java
+++ 
b/plugins/storage/image/default/src/org/apache/cloudstack/storage/datastore/driver/CloudStackImageStoreDriverImpl.java
@@ -49,6 +49,7 @@ import org.apache.cloudstack.storage.snapshot.SnapshotObject;
 import com.cloud.agent.AgentManager;
 import com.cloud.agent.api.Answer;
 import com.cloud.agent.api.DeleteSnapshotBackupCommand;
+import com.cloud.agent.api.DeleteSnapshotBackupCommand2;
 import com.cloud.agent.api.storage.DeleteTemplateCommand;
 import com.cloud.agent.api.storage.DeleteVolumeCommand;
 import com.cloud.agent.api.storage.DownloadAnswer;
@@ -247,7 +248,7 @@ public class CloudStackImageStoreDriverImpl implements 
ImageStoreDriver {
                 throw new CloudRuntimeException(
                         "Please specify a volume that is not currently being 
uploaded.");
             }
-          
+
             CommandResult result = new CommandResult();
             callback.complete(result);
             return;
@@ -326,11 +327,14 @@ public class CloudStackImageStoreDriverImpl implements 
ImageStoreDriver {
        }
 
        try {
-               String secondaryStoragePoolUrl = secStore.getUri();
+            String backupOfSnapshot = snapshotObj.getPath();
+            if (backupOfSnapshot == null) {
+                callback.complete(result);
+                return;
+            }
 
-               DeleteSnapshotBackupCommand cmd = new 
DeleteSnapshotBackupCommand(
-                               secStore.getTO(), secondaryStoragePoolUrl, 
null, null, null,
-                               snapshotObj.getPath(), false);
+               DeleteSnapshotBackupCommand2 cmd = new 
DeleteSnapshotBackupCommand2(
+                               secStore.getTO(), backupOfSnapshot);
                EndPoint ep = _epSelector.select(secStore);
                Answer answer = ep.sendMessage(cmd);
 

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/8a1a51c6/plugins/storage/image/s3/src/org/apache/cloudstack/storage/datastore/driver/S3ImageStoreDriverImpl.java
----------------------------------------------------------------------
diff --git 
a/plugins/storage/image/s3/src/org/apache/cloudstack/storage/datastore/driver/S3ImageStoreDriverImpl.java
 
b/plugins/storage/image/s3/src/org/apache/cloudstack/storage/datastore/driver/S3ImageStoreDriverImpl.java
index 3fd5930..1798ded 100644
--- 
a/plugins/storage/image/s3/src/org/apache/cloudstack/storage/datastore/driver/S3ImageStoreDriverImpl.java
+++ 
b/plugins/storage/image/s3/src/org/apache/cloudstack/storage/datastore/driver/S3ImageStoreDriverImpl.java
@@ -52,6 +52,7 @@ import org.apache.cloudstack.storage.snapshot.SnapshotObject;
 import com.cloud.agent.AgentManager;
 import com.cloud.agent.api.Answer;
 import com.cloud.agent.api.DeleteSnapshotBackupCommand;
+import com.cloud.agent.api.DeleteSnapshotBackupCommand2;
 import com.cloud.agent.api.storage.DeleteTemplateCommand;
 import com.cloud.agent.api.storage.DeleteVolumeCommand;
 import com.cloud.agent.api.storage.DownloadAnswer;
@@ -337,10 +338,6 @@ public class S3ImageStoreDriverImpl implements 
ImageStoreDriver {
         }
 
         try {
-            String secondaryStoragePoolUrl = secStore.getUri();
-            Long dcId = snapshot.getDataCenterId();
-            Long accountId = snapshot.getAccountId();
-            Long volumeId = snapshot.getVolumeId();
 
             String backupOfSnapshot = snapshotObj.getPath();
             if (backupOfSnapshot == null) {
@@ -348,9 +345,8 @@ public class S3ImageStoreDriverImpl implements 
ImageStoreDriver {
                 return;
             }
 
-            DeleteSnapshotBackupCommand cmd = new DeleteSnapshotBackupCommand(
-                    secStore.getTO(), secondaryStoragePoolUrl, dcId, 
accountId, volumeId,
-                    backupOfSnapshot, false);
+            DeleteSnapshotBackupCommand2 cmd = new 
DeleteSnapshotBackupCommand2(
+                    secStore.getTO(), backupOfSnapshot);
             EndPoint ep = _epSelector.select(secStore);
             Answer answer = ep.sendMessage(cmd);
 

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/8a1a51c6/plugins/storage/image/swift/src/org/apache/cloudstack/storage/datastore/driver/SwiftImageStoreDriverImpl.java
----------------------------------------------------------------------
diff --git 
a/plugins/storage/image/swift/src/org/apache/cloudstack/storage/datastore/driver/SwiftImageStoreDriverImpl.java
 
b/plugins/storage/image/swift/src/org/apache/cloudstack/storage/datastore/driver/SwiftImageStoreDriverImpl.java
index b8f666e..cdfe63e 100644
--- 
a/plugins/storage/image/swift/src/org/apache/cloudstack/storage/datastore/driver/SwiftImageStoreDriverImpl.java
+++ 
b/plugins/storage/image/swift/src/org/apache/cloudstack/storage/datastore/driver/SwiftImageStoreDriverImpl.java
@@ -51,6 +51,7 @@ import org.apache.log4j.Logger;
 import com.cloud.agent.AgentManager;
 import com.cloud.agent.api.Answer;
 import com.cloud.agent.api.DeleteSnapshotBackupCommand;
+import com.cloud.agent.api.DeleteSnapshotBackupCommand2;
 import com.cloud.agent.api.storage.DeleteTemplateCommand;
 import com.cloud.agent.api.storage.DeleteVolumeCommand;
 import com.cloud.agent.api.storage.DownloadAnswer;
@@ -331,20 +332,14 @@ public class SwiftImageStoreDriverImpl implements 
ImageStoreDriver {
         }
 
         try {
-            String secondaryStoragePoolUrl = secStore.getUri();
-            Long dcId = snapshot.getDataCenterId();
-            Long accountId = snapshot.getAccountId();
-            Long volumeId = snapshot.getVolumeId();
-
             String backupOfSnapshot = snapshotObj.getPath();
             if (backupOfSnapshot == null) {
                 callback.complete(result);
                 return;
             }
 
-            DeleteSnapshotBackupCommand cmd = new DeleteSnapshotBackupCommand(
-                    secStore.getTO(), secondaryStoragePoolUrl, dcId, 
accountId, volumeId,
-                    backupOfSnapshot, false);
+            DeleteSnapshotBackupCommand2 cmd = new 
DeleteSnapshotBackupCommand2(
+                    secStore.getTO(), backupOfSnapshot);
             EndPoint ep = _epSelector.select(secStore);
             Answer answer = ep.sendMessage(cmd);
 

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/8a1a51c6/server/src/com/cloud/storage/StorageManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/storage/StorageManagerImpl.java 
b/server/src/com/cloud/storage/StorageManagerImpl.java
index 3cc9bef..bb613b6 100755
--- a/server/src/com/cloud/storage/StorageManagerImpl.java
+++ b/server/src/com/cloud/storage/StorageManagerImpl.java
@@ -89,6 +89,7 @@ import com.cloud.agent.AgentManager;
 import com.cloud.agent.api.Answer;
 import com.cloud.agent.api.Command;
 import com.cloud.agent.api.DeleteSnapshotBackupCommand;
+import com.cloud.agent.api.DeleteSnapshotBackupCommand2;
 import com.cloud.agent.api.StoragePoolInfo;
 import com.cloud.agent.api.storage.DeleteTemplateCommand;
 import com.cloud.agent.api.storage.DeleteVolumeCommand;
@@ -1197,8 +1198,7 @@ public class StorageManagerImpl extends ManagerBase 
implements StorageManager, C
 
                         if (installPath != null) {
                             EndPoint ep = _epSelector.select(store);
-                            DeleteSnapshotBackupCommand cmd = new 
DeleteSnapshotBackupCommand(store.getTO(), store.getUri(),
-                                    null, null, null, 
destroyedSnapshotStoreVO.getInstallPath(), false);
+                            DeleteSnapshotBackupCommand2 cmd = new 
DeleteSnapshotBackupCommand2(store.getTO(), 
destroyedSnapshotStoreVO.getInstallPath());
                             Answer answer = ep.sendMessage(cmd);
                             if (answer == null || !answer.getResult()) {
                                 s_logger.debug("Failed to delete " + 
destroyedSnapshotStoreVO + " due to "

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/8a1a51c6/server/src/com/cloud/storage/snapshot/SnapshotManager.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/storage/snapshot/SnapshotManager.java 
b/server/src/com/cloud/storage/snapshot/SnapshotManager.java
index d6e0af6..027fad6 100755
--- a/server/src/com/cloud/storage/snapshot/SnapshotManager.java
+++ b/server/src/com/cloud/storage/snapshot/SnapshotManager.java
@@ -34,8 +34,8 @@ import com.cloud.utils.db.Filter;
 import com.cloud.utils.fsm.NoTransitionException;
 
 /**
- * 
- * 
+ *
+ *
  */
 public interface SnapshotManager {
 
@@ -51,7 +51,7 @@ public interface SnapshotManager {
      * For each of the volumes in the account, (which can span across multiple 
zones and multiple secondary storages), delete
      * the dir on the secondary storage which contains the backed up snapshots 
for that volume. This is called during
      * deleteAccount.
-     * 
+     *
      * @param accountId
      *            The account which is to be deleted.
      */
@@ -59,7 +59,7 @@ public interface SnapshotManager {
 
     String getSecondaryStorageURL(SnapshotVO snapshot);
 
-    void deleteSnapshotsDirForVolume(String secondaryStoragePoolUrl, Long 
dcId, Long accountId, Long volumeId);
+   //void deleteSnapshotsDirForVolume(String secondaryStoragePoolUrl, Long 
dcId, Long accountId, Long volumeId);
 
        boolean canOperateOnVolume(Volume volume);
 

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/8a1a51c6/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java 
b/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java
index 67b6062..a87f542 100755
--- a/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java
+++ b/server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java
@@ -53,6 +53,7 @@ import com.cloud.agent.AgentManager;
 import com.cloud.agent.api.Answer;
 import com.cloud.agent.api.Command;
 import com.cloud.agent.api.DeleteSnapshotBackupCommand;
+import com.cloud.agent.api.DeleteSnapshotBackupCommand2;
 import com.cloud.agent.api.DeleteSnapshotsDirCommand;
 import com.cloud.agent.api.DownloadSnapshotFromS3Command;
 import com.cloud.agent.api.DownloadSnapshotFromSwiftCommand;
@@ -109,6 +110,7 @@ import com.cloud.storage.dao.VolumeDao;
 import com.cloud.storage.s3.S3Manager;
 import com.cloud.storage.secondary.SecondaryStorageVmManager;
 import com.cloud.storage.swift.SwiftManager;
+import com.cloud.storage.template.TemplateConstants;
 import com.cloud.tags.ResourceTagVO;
 import com.cloud.tags.dao.ResourceTagDao;
 import com.cloud.template.TemplateManager;
@@ -323,20 +325,6 @@ public class SnapshotManagerImpl extends ManagerBase 
implements SnapshotManager,
     }
 
 
-    @Override
-    public void deleteSnapshotsDirForVolume(String secondaryStoragePoolUrl, 
Long dcId, Long accountId, Long volumeId) {
-        DeleteSnapshotsDirCommand cmd = new 
DeleteSnapshotsDirCommand(secondaryStoragePoolUrl, dcId, accountId, volumeId);
-        try {
-            DataStore store = this.dataStoreMgr.getImageStore(dcId);
-            EndPoint ep = _epSelector.select(store);
-            Answer ans = ep.sendMessage(cmd);
-            if (ans == null || !ans.getResult()) {
-                s_logger.warn("DeleteSnapshotsDirCommand failed due to " + 
ans.getDetails() + " volume id: " + volumeId);
-            }
-        } catch (Exception e) {
-            s_logger.warn("DeleteSnapshotsDirCommand failed due to" + 
e.toString() + " volume id: " + volumeId);
-        }
-    }
 
     @Override
     public Snapshot backupSnapshot(Long snapshotId) {
@@ -689,8 +677,8 @@ public class SnapshotManagerImpl extends ManagerBase 
implements SnapshotManager,
             }
             List<DataStore> ssHosts = 
this.dataStoreMgr.getImageStoresByScope(new ZoneScope(dcId));
             for (DataStore ssHost : ssHosts) {
-                DeleteSnapshotBackupCommand cmd = new 
DeleteSnapshotBackupCommand(ssHost.getTO(), ssHost.getUri(), dcId, accountId, 
volumeId, "",
-                        true);
+                String snapshotDir = 
TemplateConstants.DEFAULT_SNAPSHOT_ROOT_DIR + "/" + accountId + "/" + volumeId;
+                DeleteSnapshotsDirCommand cmd = new 
DeleteSnapshotsDirCommand(ssHost.getTO(), snapshotDir);
                 EndPoint ep = _epSelector.select(ssHost);
                 Answer answer = ep.sendMessage(cmd);
                 if ((answer != null) && answer.getResult()) {

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/8a1a51c6/services/secondary-storage/src/org/apache/cloudstack/storage/resource/NfsSecondaryStorageResource.java
----------------------------------------------------------------------
diff --git 
a/services/secondary-storage/src/org/apache/cloudstack/storage/resource/NfsSecondaryStorageResource.java
 
b/services/secondary-storage/src/org/apache/cloudstack/storage/resource/NfsSecondaryStorageResource.java
index 27a15cc..d6a2af4 100755
--- 
a/services/secondary-storage/src/org/apache/cloudstack/storage/resource/NfsSecondaryStorageResource.java
+++ 
b/services/secondary-storage/src/org/apache/cloudstack/storage/resource/NfsSecondaryStorageResource.java
@@ -68,6 +68,7 @@ import com.cloud.agent.api.CheckHealthCommand;
 import com.cloud.agent.api.Command;
 import com.cloud.agent.api.ComputeChecksumCommand;
 import com.cloud.agent.api.DeleteSnapshotBackupCommand;
+import com.cloud.agent.api.DeleteSnapshotBackupCommand2;
 import com.cloud.agent.api.DeleteSnapshotsDirCommand;
 import com.cloud.agent.api.DownloadSnapshotFromS3Command;
 import com.cloud.agent.api.DownloadSnapshotFromSwiftCommand;
@@ -211,6 +212,8 @@ public class NfsSecondaryStorageResource extends 
ServerResourceBase implements S
             return execute((DownloadSnapshotFromS3Command) cmd);
         } else if (cmd instanceof DeleteSnapshotBackupCommand) {
             return execute((DeleteSnapshotBackupCommand) cmd);
+        } else if (cmd instanceof DeleteSnapshotBackupCommand2) {
+            return execute((DeleteSnapshotBackupCommand2) cmd);
         } else if (cmd instanceof DeleteSnapshotsDirCommand) {
             return execute((DeleteSnapshotsDirCommand) cmd);
         } else if (cmd instanceof 
DownloadTemplateFromSwiftToSecondaryStorageCommand) {
@@ -838,28 +841,78 @@ public class NfsSecondaryStorageResource extends 
ServerResourceBase implements S
         return null;
     }
 
-    // TODO: this DeleteSnapshotsDirCommand should be removed after
-    // SnapshotManager refactor, this is used to delete those snapshot 
directory
-    // in the cachestorage. This should be able to be done through
-    // DeleteSnapshotBackupCommand with deleteAll flag set to true.
     public Answer execute(DeleteSnapshotsDirCommand cmd) {
-        String secondaryStorageUrl = cmd.getSecondaryStorageUrl();
-        Long accountId = cmd.getAccountId();
-        Long volumeId = cmd.getVolumeId();
-        try {
-            String parent = getRootDir(secondaryStorageUrl);
-            String lPath = parent + "/snapshots/" + String.valueOf(accountId) 
+ "/" + String.valueOf(volumeId) + "/*";
+        DataStoreTO dstore = cmd.getDataStore();
+        if (dstore instanceof NfsTO) {
+            NfsTO nfs = (NfsTO) dstore;
+            String relativeSnapshotPath = cmd.getDirectory();
+            String parent = getRootDir(nfs.getUrl());
+
+            if (relativeSnapshotPath.startsWith(File.separator)) {
+                relativeSnapshotPath = relativeSnapshotPath.substring(1);
+            }
+
+            if (!parent.endsWith(File.separator)) {
+                parent += File.separator;
+            }
+            String absoluteSnapshotPath = parent + relativeSnapshotPath;
+            File snapshotDir = new File(absoluteSnapshotPath);
+            String details = null;
+            if (!snapshotDir.exists()) {
+                details = "snapshot directory " + snapshotDir.getName() + " 
doesn't exist";
+                s_logger.debug(details);
+                return new Answer(cmd, true, details);
+            }
+            // delete all files in the directory
+            String lPath = absoluteSnapshotPath + "/*";
             String result = deleteLocalFile(lPath);
             if (result != null) {
                 String errMsg = "failed to delete all snapshots " + lPath + " 
, err=" + result;
                 s_logger.warn(errMsg);
                 return new Answer(cmd, false, errMsg);
             }
-            return new Answer(cmd, true, "success");
-        } catch (Exception e) {
-            String errMsg = cmd + " Command failed due to " + e.toString();
-            s_logger.warn(errMsg, e);
-            return new Answer(cmd, false, errMsg);
+            // delete the directory
+            if (!snapshotDir.delete()) {
+                details = "Unable to delete directory " + 
snapshotDir.getName() + " under snapshot path " + relativeSnapshotPath;
+                s_logger.debug(details);
+                return new Answer(cmd, false, details);
+            }
+            return new Answer(cmd, true, null);
+        } else if (dstore instanceof S3TO) {
+            final S3TO s3 = (S3TO) dstore;
+            final String path = cmd.getDirectory();
+            final String bucket = s3.getBucketName();
+            try {
+                S3Utils.deleteDirectory(s3, bucket, path);
+                return new Answer(cmd, true, String.format("Deleted snapshot 
%1%s from bucket %2$s.", path, bucket));
+            } catch (Exception e) {
+                final String errorMessage = String.format("Failed to delete 
snapshot %1$s from bucket %2$s due to the following error: %3$s", path,
+                        bucket, e.getMessage());
+                s_logger.error(errorMessage, e);
+                return new Answer(cmd, false, errorMessage);
+            }
+        } else if (dstore instanceof SwiftTO) {
+            String path = cmd.getDirectory();
+            String volumeId = StringUtils.substringAfterLast(path, "/"); // 
assuming
+                                                                         // 
that
+                                                                         // the
+                                                                         // 
filename
+                                                                         // is
+                                                                         // the
+                                                                         // 
last
+                                                                         // 
section
+                                                                         // in
+                                                                         // the
+                                                                         // 
path
+            String result = swiftDelete((SwiftTO) dstore, "V-" + 
volumeId.toString(), "");
+            if (result != null) {
+                String errMsg = "failed to delete snapshot for volume " + 
volumeId + " , err=" + result;
+                s_logger.warn(errMsg);
+                return new Answer(cmd, false, errMsg);
+            }
+            return new Answer(cmd, true, "Deleted snapshot " + path + " from 
swift");
+        } else {
+            return new Answer(cmd, false, "Unsupported image data store: " + 
dstore);
         }
     }
 
@@ -1183,6 +1236,74 @@ public class NfsSecondaryStorageResource extends 
ServerResourceBase implements S
         return join("_", "SNAPSHOT", accountId, volumeId);
     }
 
+    protected Answer execute(final DeleteSnapshotBackupCommand2 cmd) {
+        DataStoreTO dstore = cmd.getDataStore();
+        if (dstore instanceof NfsTO) {
+            NfsTO nfs = (NfsTO) dstore;
+            String relativeSnapshotPath = cmd.getSnapshotPath();
+            String parent = getRootDir(nfs.getUrl());
+
+            if (relativeSnapshotPath.startsWith(File.separator)) {
+                relativeSnapshotPath = relativeSnapshotPath.substring(1);
+            }
+
+            if (!parent.endsWith(File.separator)) {
+                parent += File.separator;
+            }
+            String absoluteSnapshotPath = parent + relativeSnapshotPath;
+            File snapshot = new File(absoluteSnapshotPath);
+            String details = null;
+            if (!snapshot.exists()) {
+                details = "snapshot file " + snapshot.getName() + " doesn't 
exist";
+                s_logger.debug(details);
+                return new Answer(cmd, true, details);
+            }
+
+            if (!snapshot.delete()) {
+                return new Answer(cmd, false, "Unable to delete file " + 
snapshot.getName() + " under install path " + relativeSnapshotPath);
+            }
+
+            return new Answer(cmd, true, null);
+        } else if (dstore instanceof S3TO) {
+            final S3TO s3 = (S3TO) dstore;
+            final String path = cmd.getSnapshotPath();
+            final String bucket = s3.getBucketName();
+            try {
+                S3Utils.deleteObject(s3, bucket, path);
+                return new Answer(cmd, true, String.format("Deleted snapshot 
%1%s from bucket %2$s.", path, bucket));
+            } catch (Exception e) {
+                final String errorMessage = String.format("Failed to delete 
snapshot %1$s from bucket %2$s due to the following error: %3$s", path,
+                        bucket, e.getMessage());
+                s_logger.error(errorMessage, e);
+                return new Answer(cmd, false, errorMessage);
+            }
+        } else if (dstore instanceof SwiftTO) {
+            String path = cmd.getSnapshotPath();
+            String filename = StringUtils.substringAfterLast(path, "/"); // 
assuming
+                                                                         // 
that
+                                                                         // the
+                                                                         // 
filename
+                                                                         // is
+                                                                         // the
+                                                                         // 
last
+                                                                         // 
section
+                                                                         // in
+                                                                         // the
+                                                                         // 
path
+            String volumeId = 
StringUtils.substringAfterLast(StringUtils.substringBeforeLast(path, "/"), "/");
+            String result = swiftDelete((SwiftTO) dstore, "V-" + volumeId, 
filename);
+            if (result != null) {
+                String errMsg = "failed to delete snapshot " + filename + " , 
err=" + result;
+                s_logger.warn(errMsg);
+                return new Answer(cmd, false, errMsg);
+            }
+            return new Answer(cmd, true, "Deleted snapshot " + path + " from 
swift");
+        } else {
+            return new Answer(cmd, false, "Unsupported image data store: " + 
dstore);
+        }
+
+    }
+
     protected Answer execute(final DeleteSnapshotBackupCommand cmd) {
         Long accountId = cmd.getAccountId();
         Long volumeId = cmd.getVolumeId();
@@ -1660,7 +1781,7 @@ public class NfsSecondaryStorageResource extends 
ServerResourceBase implements S
                 s_logger.warn(errMsg);
                 return new Answer(cmd, false, errMsg);
             }
-            return new Answer(cmd, false, "Swift is not currently support 
DeleteVolumeCommand");
+            return new Answer(cmd, true, "Deleted volume " + path + " from 
swift");
         } else {
             return new Answer(cmd, false, "Unsupported image data store: " + 
dstore);
         }

Reply via email to