From 5b7ea8b0feeea48dc5bbe7d9e0b45735b83d4404 Mon Sep 17 00:00:00 2001 From: Min Chen Date: Fri, 26 Apr 2013 17:52:50 -0700 Subject: [PATCH] Remove DeleteObjectFromSwiftCommand, which can be achieved through DeleteTemplateCommand. --- .../api/DeleteObjectFromSwiftCommand.java | 61 ------------ .../api/storage/DeleteVolumeCommand.java | 9 +- .../resource/NfsSecondaryStorageResource.java | 92 ++++++++----------- .../CloudStackImageStoreDriverImpl.java | 2 +- .../driver/S3ImageStoreDriverImpl.java | 2 +- .../driver/SwiftImageStoreDriverImpl.java | 2 +- .../com/cloud/storage/StorageManagerImpl.java | 2 +- .../cloud/storage/swift/SwiftManagerImpl.java | 8 +- 8 files changed, 55 insertions(+), 123 deletions(-) delete mode 100644 api/src/com/cloud/agent/api/DeleteObjectFromSwiftCommand.java diff --git a/api/src/com/cloud/agent/api/DeleteObjectFromSwiftCommand.java b/api/src/com/cloud/agent/api/DeleteObjectFromSwiftCommand.java deleted file mode 100644 index 3d62c507b89..00000000000 --- a/api/src/com/cloud/agent/api/DeleteObjectFromSwiftCommand.java +++ /dev/null @@ -1,61 +0,0 @@ -// 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.LogLevel.Log4jLevel; -import com.cloud.agent.api.to.SwiftTO; - -/** - * - * - */ - -public class DeleteObjectFromSwiftCommand extends Command { - @LogLevel(Log4jLevel.Off) - private SwiftTO swift; - private String container; - private String object; - - protected DeleteObjectFromSwiftCommand() { - - } - - public DeleteObjectFromSwiftCommand(SwiftTO swift, String container, String object) { - this.swift = swift; - this.container = container; - this.object = object; - } - - public SwiftTO getSwift() { - return this.swift; - } - - public String getContainer() { - return container; - } - - public String getObject() { - return object; - } - - @Override - public boolean executeInSequence() { - // TODO Auto-generated method stub - return true; - } - -} diff --git a/api/src/com/cloud/agent/api/storage/DeleteVolumeCommand.java b/api/src/com/cloud/agent/api/storage/DeleteVolumeCommand.java index eb17529e261..49f0a21967a 100755 --- a/api/src/com/cloud/agent/api/storage/DeleteVolumeCommand.java +++ b/api/src/com/cloud/agent/api/storage/DeleteVolumeCommand.java @@ -21,12 +21,14 @@ import com.cloud.agent.api.to.DataStoreTO; public class DeleteVolumeCommand extends ssCommand { private DataStoreTO store; private String volumePath; + private Long volumeId; public DeleteVolumeCommand() { } - public DeleteVolumeCommand(DataStoreTO store, String volumePath) { + public DeleteVolumeCommand(DataStoreTO store, Long volumeId, String volumePath) { this.store = store; + this.volumeId = volumeId; this.volumePath = volumePath; } @@ -43,5 +45,10 @@ public class DeleteVolumeCommand extends ssCommand { return store; } + public Long getVolumeId() { + return volumeId; + } + + } diff --git a/core/src/com/cloud/storage/resource/NfsSecondaryStorageResource.java b/core/src/com/cloud/storage/resource/NfsSecondaryStorageResource.java index 0806dcfa685..6cb7b9838b5 100755 --- a/core/src/com/cloud/storage/resource/NfsSecondaryStorageResource.java +++ b/core/src/com/cloud/storage/resource/NfsSecondaryStorageResource.java @@ -61,7 +61,6 @@ import com.cloud.agent.api.CheckHealthCommand; import com.cloud.agent.api.CleanupSnapshotBackupCommand; import com.cloud.agent.api.Command; import com.cloud.agent.api.ComputeChecksumCommand; -import com.cloud.agent.api.DeleteObjectFromSwiftCommand; import com.cloud.agent.api.DeleteSnapshotBackupCommand; import com.cloud.agent.api.DeleteSnapshotsDirCommand; import com.cloud.agent.api.DownloadSnapshotFromS3Command; @@ -215,8 +214,6 @@ public class NfsSecondaryStorageResource extends ServerResourceBase implements S return execute((uploadTemplateToSwiftFromSecondaryStorageCommand) cmd); } else if (cmd instanceof UploadTemplateToS3FromSecondaryStorageCommand) { return execute((UploadTemplateToS3FromSecondaryStorageCommand) cmd); - } else if (cmd instanceof DeleteObjectFromSwiftCommand) { - return execute((DeleteObjectFromSwiftCommand) cmd); } else if (cmd instanceof CleanupSnapshotBackupCommand) { return execute((CleanupSnapshotBackupCommand) cmd); } else if (cmd instanceof CopyCommand) { @@ -321,7 +318,7 @@ public class NfsSecondaryStorageResource extends ServerResourceBase implements S } @SuppressWarnings("unchecked") - private String determineS3TemplateNameFromKey(String key){ + private String determineS3TemplateNameFromKey(String key) { return StringUtils.substringAfterLast(StringUtils.substringBeforeLast(key, S3Utils.SEPARATOR), S3Utils.SEPARATOR); } @@ -330,7 +327,6 @@ public class NfsSecondaryStorageResource extends ServerResourceBase implements S return join(asList(VOLUME_ROOT_DIR, accountId, volId), S3Utils.SEPARATOR); } - @SuppressWarnings("unchecked") private String determineStorageTemplatePath(final String storagePath, String dataPath) { return join(asList(getRootDir(storagePath), dataPath), File.separator); @@ -413,7 +409,9 @@ public class NfsSecondaryStorageResource extends ServerResourceBase implements S // convention is no / in the end for install path based on // S3Utils implementation. // template key is - // TEMPLATE_ROOT_DIR/account_id/template_id/template_name, by adding template_name in the key, I can avoid generating a template.properties file + // TEMPLATE_ROOT_DIR/account_id/template_id/template_name, by + // adding template_name in the key, I can avoid generating a + // template.properties file // for listTemplateCommand. path = determineS3TemplateDirectory(cmd.getAccountId(), cmd.getResourceId(), cmd.getName()); } else { @@ -426,8 +424,8 @@ public class NfsSecondaryStorageResource extends ServerResourceBase implements S if (s3Obj == null || s3Obj.size() == 0) { return new Answer(cmd, false, "Failed to download to S3 bucket: " + bucket + " with key: " + key); } else { - return new DownloadAnswer(null, 100, null, Status.DOWNLOADED, path, path, s3Obj.get(0).getSize(), s3Obj.get(0).getSize(), s3Obj.get(0) - .getETag()); + return new DownloadAnswer(null, 100, null, Status.DOWNLOADED, path, path, s3Obj.get(0).getSize(), s3Obj.get(0).getSize(), s3Obj + .get(0).getETag()); } } else if (dstore instanceof SwiftTO) { // TODO: need to move code from @@ -548,31 +546,6 @@ public class NfsSecondaryStorageResource extends ServerResourceBase implements S return new Answer(cmd, false, "not supported "); } - private Answer execute(DeleteObjectFromSwiftCommand cmd) { - SwiftTO swift = cmd.getSwift(); - String container = cmd.getContainer(); - String object = cmd.getObject(); - if (object == null) { - object = ""; - } - try { - String result = swiftDelete(swift, container, object); - if (result != null) { - String errMsg = "failed to delete object " + container + "/" + object + " , 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); - } - - } - - - String swiftDownload(SwiftTO swift, String container, String rfilename, String lFullPath) { Script command = new Script("/bin/bash", s_logger); command.add("-c"); @@ -722,6 +695,10 @@ 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(); @@ -804,9 +781,6 @@ public class NfsSecondaryStorageResource extends ServerResourceBase implements S } - - - private String determineSnapshotS3Directory(final Long accountId, final Long volumeId) { return join(S3Utils.SEPARATOR, SNAPSHOT_ROOT_DIR, accountId, volumeId); } @@ -1014,8 +988,7 @@ public class NfsSecondaryStorageResource extends ServerResourceBase implements S } - private String deleteSnapshotBackupfromS3(final S3TO s3, final String secondaryStorageUrl, final Long accountId, final Long volumeId, - final String name, final Boolean deleteAllFlag) { + private String deleteSnapshotBackupfromS3(final S3TO s3, final Long accountId, final Long volumeId, final String name, final Boolean deleteAllFlag) { try { @@ -1026,11 +999,6 @@ public class NfsSecondaryStorageResource extends ServerResourceBase implements S @Override public String call() throws Exception { - final String innerResult = deleteSnapshotBackupFromLocalFileSystem(secondaryStorageUrl, accountId, volumeId, name, deleteAllFlag); - if (innerResult != null) { - return innerResult; - } - if (deleteAllFlag) { S3Utils.deleteDirectory(s3, bucket, determineSnapshotS3Directory(accountId, volumeId)); } else { @@ -1063,19 +1031,18 @@ public class NfsSecondaryStorageResource extends ServerResourceBase implements S } protected Answer execute(final DeleteSnapshotBackupCommand cmd) { - String secondaryStorageUrl = cmd.getSecondaryStorageUrl(); Long accountId = cmd.getAccountId(); Long volumeId = cmd.getVolumeId(); String name = cmd.getSnapshotUuid(); DataStoreTO dstore = cmd.getDataStore(); if (dstore instanceof NfsTO) { - final String result = deleteSnapshotBackupFromLocalFileSystem(secondaryStorageUrl, accountId, volumeId, name, cmd.isAll()); + final String result = deleteSnapshotBackupFromLocalFileSystem(((NfsTO) dstore).getUrl(), accountId, volumeId, name, cmd.isAll()); if (result != null) { s_logger.warn(result); return new Answer(cmd, false, result); } } else if (dstore instanceof S3TO) { - final String result = deleteSnapshotBackupfromS3((S3TO) dstore, secondaryStorageUrl, accountId, volumeId, name, cmd.isAll()); + final String result = deleteSnapshotBackupfromS3((S3TO) dstore, accountId, volumeId, name, cmd.isAll()); if (result != null) { s_logger.warn(result); return new Answer(cmd, false, result); @@ -1136,14 +1103,14 @@ public class NfsSecondaryStorageResource extends ServerResourceBase implements S String bucket = s3.getBucketName(); // List the objects in the source directory on S3 final List objectSummaries = S3Utils.getDirectory(s3, bucket, this.TEMPLATE_ROOT_DIR); - if ( objectSummaries == null ) + if (objectSummaries == null) return null; Map tmpltInfos = new HashMap(); - for (S3ObjectSummary objectSummary : objectSummaries){ + for (S3ObjectSummary objectSummary : objectSummaries) { String key = objectSummary.getKey(); String installPath = StringUtils.substringBeforeLast(key, S3Utils.SEPARATOR); String uniqueName = this.determineS3TemplateNameFromKey(key); - //TODO: isPublic value, where to get? + // TODO: isPublic value, where to get? TemplateProp tInfo = new TemplateProp(uniqueName, installPath, objectSummary.getSize(), objectSummary.getSize(), true, false); tmpltInfos.put(uniqueName, tInfo); } @@ -1158,7 +1125,7 @@ public class NfsSecondaryStorageResource extends ServerResourceBase implements S DataStoreTO store = cmd.getDataStore(); if (store instanceof NfsTO) { - NfsTO nfs = (NfsTO)store; + NfsTO nfs = (NfsTO) store; String root = getRootDir(nfs.getUrl()); Map templateInfos = _dlMgr.gatherTemplateInfo(root); return new ListTemplateAnswer(nfs.getUrl(), templateInfos); @@ -1324,7 +1291,7 @@ public class NfsSecondaryStorageResource extends ServerResourceBase implements S protected Answer execute(final DeleteTemplateCommand cmd) { DataStoreTO dstore = cmd.getDataStore(); if (dstore instanceof NfsTO) { - NfsTO nfs = (NfsTO)dstore; + NfsTO nfs = (NfsTO) dstore; String relativeTemplatePath = cmd.getTemplatePath(); String parent = getRootDir(nfs.getUrl()); @@ -1376,8 +1343,8 @@ public class NfsSecondaryStorageResource extends ServerResourceBase implements S S3Utils.deleteDirectory(s3, bucket, path); return new Answer(cmd, true, String.format("Deleted template %1%s from bucket %2$s.", path, bucket)); } catch (Exception e) { - final String errorMessage = String.format("Failed to delete template %1$s from bucket %2$s due to the following error: %3$s", - path, bucket, e.getMessage()); + final String errorMessage = String.format("Failed to delete template %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); } @@ -1465,6 +1432,25 @@ public class NfsSecondaryStorageResource extends ServerResourceBase implements S return new Answer(cmd, false, errorMessage); } } else if (dstore instanceof SwiftTO) { + Long volumeId = cmd.getVolumeId(); + String path = cmd.getVolumePath(); + String filename = StringUtils.substringAfterLast(path, "/"); // assuming + // that + // the + // filename + // is + // the + // last + // section + // in + // the + // path + String result = swiftDelete((SwiftTO) dstore, "V-" + volumeId.toString(), filename); + if (result != null) { + String errMsg = "failed to delete volume " + filename + " , err=" + result; + s_logger.warn(errMsg); + return new Answer(cmd, false, errMsg); + } return new Answer(cmd, false, "Swift is not currently support DeleteVolumeCommand"); } else { return new Answer(cmd, false, "Unsupported image data store: " + dstore); 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 1f29dadd74d..277cbf9f276 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 @@ -237,7 +237,7 @@ public class CloudStackImageStoreDriverImpl implements ImageStoreDriver { DataStore store = this._dataStoreMgr.getDataStore(volumeStore.getDataStoreId(), DataStoreRole.Image); EndPoint ep = _epSelector.select(store); DeleteVolumeCommand dtCommand = new DeleteVolumeCommand( - store.getTO(), volumeStore.getInstallPath()); + store.getTO(), volumeStore.getVolumeId(), volumeStore.getInstallPath()); Answer answer = ep.sendMessage(dtCommand); if (answer == null || !answer.getResult()) { s_logger.debug("Failed to delete " 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 ecd89c74e60..f3aefe06a0d 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 @@ -195,7 +195,7 @@ public class S3ImageStoreDriverImpl implements ImageStoreDriver { DataStore store = this._dataStoreMgr.getDataStore(volumeStore.getDataStoreId(), DataStoreRole.Image); EndPoint ep = _epSelector.select(store); DeleteVolumeCommand dtCommand = new DeleteVolumeCommand( - store.getTO(), volumeStore.getInstallPath()); + store.getTO(), volumeStore.getVolumeId(), volumeStore.getInstallPath()); Answer answer = ep.sendMessage(dtCommand); if (answer == null || !answer.getResult()) { s_logger.debug("Failed to delete " 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 ddc97866e54..e8009fc0239 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 @@ -189,7 +189,7 @@ public class SwiftImageStoreDriverImpl implements ImageStoreDriver { DataStore store = this._dataStoreMgr.getDataStore(volumeStore.getDataStoreId(), DataStoreRole.Image); EndPoint ep = _epSelector.select(store); DeleteVolumeCommand dtCommand = new DeleteVolumeCommand( - store.getTO(), volumeStore.getInstallPath()); + store.getTO(), volumeStore.getVolumeId(), volumeStore.getInstallPath()); Answer answer = ep.sendMessage(dtCommand); if (answer == null || !answer.getResult()) { s_logger.debug("Failed to delete " diff --git a/server/src/com/cloud/storage/StorageManagerImpl.java b/server/src/com/cloud/storage/StorageManagerImpl.java index ea737b86f5a..e6159679b63 100755 --- a/server/src/com/cloud/storage/StorageManagerImpl.java +++ b/server/src/com/cloud/storage/StorageManagerImpl.java @@ -1325,7 +1325,7 @@ public class StorageManagerImpl extends ManagerBase implements StorageManager, C if (installPath != null) { EndPoint ep = _epSelector.select(store); - DeleteVolumeCommand cmd = new DeleteVolumeCommand(store.getTO(), destroyedStoreVO.getInstallPath()); + DeleteVolumeCommand cmd = new DeleteVolumeCommand(store.getTO(), destroyedStoreVO.getVolumeId(), destroyedStoreVO.getInstallPath()); Answer answer = ep.sendMessage(cmd); if (answer == null || !answer.getResult()) { s_logger.debug("Failed to delete " + destroyedStoreVO + " due to " diff --git a/server/src/com/cloud/storage/swift/SwiftManagerImpl.java b/server/src/com/cloud/storage/swift/SwiftManagerImpl.java index 5a7f01ab20b..75780b8f158 100644 --- a/server/src/com/cloud/storage/swift/SwiftManagerImpl.java +++ b/server/src/com/cloud/storage/swift/SwiftManagerImpl.java @@ -34,7 +34,7 @@ import org.springframework.stereotype.Component; import com.cloud.agent.AgentManager; import com.cloud.agent.api.Answer; -import com.cloud.agent.api.DeleteObjectFromSwiftCommand; +import com.cloud.agent.api.storage.DeleteTemplateCommand; import com.cloud.agent.api.to.SwiftTO; import org.apache.cloudstack.api.command.admin.swift.AddSwiftCmd; import com.cloud.configuration.Config; @@ -144,7 +144,7 @@ public class SwiftManagerImpl extends ManagerBase implements SwiftManager { s_logger.warn(msg); throw new CloudRuntimeException(msg); } - Answer answer = _agentMgr.sendToSSVM(null, new DeleteObjectFromSwiftCommand(swift, "T-" + cmd.getId(), null)); + Answer answer = _agentMgr.sendToSSVM(null, new DeleteTemplateCommand(swift, null, cmd.getId(), null)); if (answer == null || !answer.getResult()) { msg = "Failed to delete " + tmpltSwiftRef + " due to " + ((answer == null) ? "answer is null" : answer.getDetails()); s_logger.warn(msg); @@ -170,7 +170,7 @@ public class SwiftManagerImpl extends ManagerBase implements SwiftManager { s_logger.warn(msg); throw new CloudRuntimeException(msg); } - Answer answer = _agentMgr.sendToSSVM(null, new DeleteObjectFromSwiftCommand(swift, "T-" + cmd.getId(), null)); + Answer answer = _agentMgr.sendToSSVM(null, new DeleteTemplateCommand(swift, null, cmd.getId(), null)); if (answer == null || !answer.getResult()) { msg = "Failed to delete " + tmpltSwiftRef + " due to " + ((answer == null) ? "answer is null" : answer.getDetails()); s_logger.warn(msg); @@ -236,7 +236,7 @@ public class SwiftManagerImpl extends ManagerBase implements SwiftManager { if (swift == null) { return null; } - + List tmpltHosts = _vmTmpltHostDao.listByOnlyTemplateId(tmpltId); if (tmpltHosts != null) { Collections.shuffle(tmpltHosts);