Skip to content

Commit 5b7ea8b

Browse files
committed
Remove DeleteObjectFromSwiftCommand, which can be achieved through
DeleteTemplateCommand.
1 parent c2e9be8 commit 5b7ea8b

8 files changed

Lines changed: 55 additions & 123 deletions

File tree

api/src/com/cloud/agent/api/DeleteObjectFromSwiftCommand.java

Lines changed: 0 additions & 61 deletions
This file was deleted.

api/src/com/cloud/agent/api/storage/DeleteVolumeCommand.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121
public class DeleteVolumeCommand extends ssCommand {
2222
private DataStoreTO store;
2323
private String volumePath;
24+
private Long volumeId;
2425

2526
public DeleteVolumeCommand() {
2627
}
2728

28-
public DeleteVolumeCommand(DataStoreTO store, String volumePath) {
29+
public DeleteVolumeCommand(DataStoreTO store, Long volumeId, String volumePath) {
2930
this.store = store;
31+
this.volumeId = volumeId;
3032
this.volumePath = volumePath;
3133
}
3234

@@ -43,5 +45,10 @@ public DataStoreTO getDataStore() {
4345
return store;
4446
}
4547

48+
public Long getVolumeId() {
49+
return volumeId;
50+
}
51+
52+
4653

4754
}

core/src/com/cloud/storage/resource/NfsSecondaryStorageResource.java

Lines changed: 39 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
import com.cloud.agent.api.CleanupSnapshotBackupCommand;
6262
import com.cloud.agent.api.Command;
6363
import com.cloud.agent.api.ComputeChecksumCommand;
64-
import com.cloud.agent.api.DeleteObjectFromSwiftCommand;
6564
import com.cloud.agent.api.DeleteSnapshotBackupCommand;
6665
import com.cloud.agent.api.DeleteSnapshotsDirCommand;
6766
import com.cloud.agent.api.DownloadSnapshotFromS3Command;
@@ -215,8 +214,6 @@ public Answer executeRequest(Command cmd) {
215214
return execute((uploadTemplateToSwiftFromSecondaryStorageCommand) cmd);
216215
} else if (cmd instanceof UploadTemplateToS3FromSecondaryStorageCommand) {
217216
return execute((UploadTemplateToS3FromSecondaryStorageCommand) cmd);
218-
} else if (cmd instanceof DeleteObjectFromSwiftCommand) {
219-
return execute((DeleteObjectFromSwiftCommand) cmd);
220217
} else if (cmd instanceof CleanupSnapshotBackupCommand) {
221218
return execute((CleanupSnapshotBackupCommand) cmd);
222219
} else if (cmd instanceof CopyCommand) {
@@ -321,7 +318,7 @@ protected String determineS3TemplateDirectory(final Long accountId, final Long t
321318
}
322319

323320
@SuppressWarnings("unchecked")
324-
private String determineS3TemplateNameFromKey(String key){
321+
private String determineS3TemplateNameFromKey(String key) {
325322
return StringUtils.substringAfterLast(StringUtils.substringBeforeLast(key, S3Utils.SEPARATOR), S3Utils.SEPARATOR);
326323
}
327324

@@ -330,7 +327,6 @@ protected String determineS3VolumeDirectory(final Long accountId, final Long vol
330327
return join(asList(VOLUME_ROOT_DIR, accountId, volId), S3Utils.SEPARATOR);
331328
}
332329

333-
334330
@SuppressWarnings("unchecked")
335331
private String determineStorageTemplatePath(final String storagePath, String dataPath) {
336332
return join(asList(getRootDir(storagePath), dataPath), File.separator);
@@ -413,7 +409,9 @@ private Answer execute(DownloadCommand cmd) {
413409
// convention is no / in the end for install path based on
414410
// S3Utils implementation.
415411
// template key is
416-
// TEMPLATE_ROOT_DIR/account_id/template_id/template_name, by adding template_name in the key, I can avoid generating a template.properties file
412+
// TEMPLATE_ROOT_DIR/account_id/template_id/template_name, by
413+
// adding template_name in the key, I can avoid generating a
414+
// template.properties file
417415
// for listTemplateCommand.
418416
path = determineS3TemplateDirectory(cmd.getAccountId(), cmd.getResourceId(), cmd.getName());
419417
} else {
@@ -426,8 +424,8 @@ private Answer execute(DownloadCommand cmd) {
426424
if (s3Obj == null || s3Obj.size() == 0) {
427425
return new Answer(cmd, false, "Failed to download to S3 bucket: " + bucket + " with key: " + key);
428426
} else {
429-
return new DownloadAnswer(null, 100, null, Status.DOWNLOADED, path, path, s3Obj.get(0).getSize(), s3Obj.get(0).getSize(), s3Obj.get(0)
430-
.getETag());
427+
return new DownloadAnswer(null, 100, null, Status.DOWNLOADED, path, path, s3Obj.get(0).getSize(), s3Obj.get(0).getSize(), s3Obj
428+
.get(0).getETag());
431429
}
432430
} else if (dstore instanceof SwiftTO) {
433431
// TODO: need to move code from
@@ -548,31 +546,6 @@ public String determineKey(final File file) {
548546
return new Answer(cmd, false, "not supported ");
549547
}
550548

551-
private Answer execute(DeleteObjectFromSwiftCommand cmd) {
552-
SwiftTO swift = cmd.getSwift();
553-
String container = cmd.getContainer();
554-
String object = cmd.getObject();
555-
if (object == null) {
556-
object = "";
557-
}
558-
try {
559-
String result = swiftDelete(swift, container, object);
560-
if (result != null) {
561-
String errMsg = "failed to delete object " + container + "/" + object + " , err=" + result;
562-
s_logger.warn(errMsg);
563-
return new Answer(cmd, false, errMsg);
564-
}
565-
return new Answer(cmd, true, "success");
566-
} catch (Exception e) {
567-
String errMsg = cmd + " Command failed due to " + e.toString();
568-
s_logger.warn(errMsg, e);
569-
return new Answer(cmd, false, errMsg);
570-
}
571-
572-
}
573-
574-
575-
576549
String swiftDownload(SwiftTO swift, String container, String rfilename, String lFullPath) {
577550
Script command = new Script("/bin/bash", s_logger);
578551
command.add("-c");
@@ -722,6 +695,10 @@ String swiftDelete(SwiftTO swift, String container, String object) {
722695
return null;
723696
}
724697

698+
// TODO: this DeleteSnapshotsDirCommand should be removed after
699+
// SnapshotManager refactor, this is used to delete those snapshot directory
700+
// in the cachestorage. This should be able to be done through
701+
// DeleteSnapshotBackupCommand with deleteAll flag set to true.
725702
public Answer execute(DeleteSnapshotsDirCommand cmd) {
726703
String secondaryStorageUrl = cmd.getSecondaryStorageUrl();
727704
Long accountId = cmd.getAccountId();
@@ -804,9 +781,6 @@ public String determineFileName(String key) {
804781

805782
}
806783

807-
808-
809-
810784
private String determineSnapshotS3Directory(final Long accountId, final Long volumeId) {
811785
return join(S3Utils.SEPARATOR, SNAPSHOT_ROOT_DIR, accountId, volumeId);
812786
}
@@ -1014,8 +988,7 @@ private String deleteSnapshotBackupFromLocalFileSystem(final String secondarySto
1014988

1015989
}
1016990

1017-
private String deleteSnapshotBackupfromS3(final S3TO s3, final String secondaryStorageUrl, final Long accountId, final Long volumeId,
1018-
final String name, final Boolean deleteAllFlag) {
991+
private String deleteSnapshotBackupfromS3(final S3TO s3, final Long accountId, final Long volumeId, final String name, final Boolean deleteAllFlag) {
1019992

1020993
try {
1021994

@@ -1026,11 +999,6 @@ private String deleteSnapshotBackupfromS3(final S3TO s3, final String secondaryS
1026999
@Override
10271000
public String call() throws Exception {
10281001

1029-
final String innerResult = deleteSnapshotBackupFromLocalFileSystem(secondaryStorageUrl, accountId, volumeId, name, deleteAllFlag);
1030-
if (innerResult != null) {
1031-
return innerResult;
1032-
}
1033-
10341002
if (deleteAllFlag) {
10351003
S3Utils.deleteDirectory(s3, bucket, determineSnapshotS3Directory(accountId, volumeId));
10361004
} else {
@@ -1063,19 +1031,18 @@ private String determineSnapshotLockId(final Long accountId, final Long volumeId
10631031
}
10641032

10651033
protected Answer execute(final DeleteSnapshotBackupCommand cmd) {
1066-
String secondaryStorageUrl = cmd.getSecondaryStorageUrl();
10671034
Long accountId = cmd.getAccountId();
10681035
Long volumeId = cmd.getVolumeId();
10691036
String name = cmd.getSnapshotUuid();
10701037
DataStoreTO dstore = cmd.getDataStore();
10711038
if (dstore instanceof NfsTO) {
1072-
final String result = deleteSnapshotBackupFromLocalFileSystem(secondaryStorageUrl, accountId, volumeId, name, cmd.isAll());
1039+
final String result = deleteSnapshotBackupFromLocalFileSystem(((NfsTO) dstore).getUrl(), accountId, volumeId, name, cmd.isAll());
10731040
if (result != null) {
10741041
s_logger.warn(result);
10751042
return new Answer(cmd, false, result);
10761043
}
10771044
} else if (dstore instanceof S3TO) {
1078-
final String result = deleteSnapshotBackupfromS3((S3TO) dstore, secondaryStorageUrl, accountId, volumeId, name, cmd.isAll());
1045+
final String result = deleteSnapshotBackupfromS3((S3TO) dstore, accountId, volumeId, name, cmd.isAll());
10791046
if (result != null) {
10801047
s_logger.warn(result);
10811048
return new Answer(cmd, false, result);
@@ -1136,14 +1103,14 @@ Map<String, TemplateProp> s3ListTemplate(S3TO s3) {
11361103
String bucket = s3.getBucketName();
11371104
// List the objects in the source directory on S3
11381105
final List<S3ObjectSummary> objectSummaries = S3Utils.getDirectory(s3, bucket, this.TEMPLATE_ROOT_DIR);
1139-
if ( objectSummaries == null )
1106+
if (objectSummaries == null)
11401107
return null;
11411108
Map<String, TemplateProp> tmpltInfos = new HashMap<String, TemplateProp>();
1142-
for (S3ObjectSummary objectSummary : objectSummaries){
1109+
for (S3ObjectSummary objectSummary : objectSummaries) {
11431110
String key = objectSummary.getKey();
11441111
String installPath = StringUtils.substringBeforeLast(key, S3Utils.SEPARATOR);
11451112
String uniqueName = this.determineS3TemplateNameFromKey(key);
1146-
//TODO: isPublic value, where to get?
1113+
// TODO: isPublic value, where to get?
11471114
TemplateProp tInfo = new TemplateProp(uniqueName, installPath, objectSummary.getSize(), objectSummary.getSize(), true, false);
11481115
tmpltInfos.put(uniqueName, tInfo);
11491116
}
@@ -1158,7 +1125,7 @@ private Answer execute(ListTemplateCommand cmd) {
11581125

11591126
DataStoreTO store = cmd.getDataStore();
11601127
if (store instanceof NfsTO) {
1161-
NfsTO nfs = (NfsTO)store;
1128+
NfsTO nfs = (NfsTO) store;
11621129
String root = getRootDir(nfs.getUrl());
11631130
Map<String, TemplateProp> templateInfos = _dlMgr.gatherTemplateInfo(root);
11641131
return new ListTemplateAnswer(nfs.getUrl(), templateInfos);
@@ -1324,7 +1291,7 @@ protected GetStorageStatsAnswer execute(final GetStorageStatsCommand cmd) {
13241291
protected Answer execute(final DeleteTemplateCommand cmd) {
13251292
DataStoreTO dstore = cmd.getDataStore();
13261293
if (dstore instanceof NfsTO) {
1327-
NfsTO nfs = (NfsTO)dstore;
1294+
NfsTO nfs = (NfsTO) dstore;
13281295
String relativeTemplatePath = cmd.getTemplatePath();
13291296
String parent = getRootDir(nfs.getUrl());
13301297

@@ -1376,8 +1343,8 @@ protected Answer execute(final DeleteTemplateCommand cmd) {
13761343
S3Utils.deleteDirectory(s3, bucket, path);
13771344
return new Answer(cmd, true, String.format("Deleted template %1%s from bucket %2$s.", path, bucket));
13781345
} catch (Exception e) {
1379-
final String errorMessage = String.format("Failed to delete template %1$s from bucket %2$s due to the following error: %3$s",
1380-
path, bucket, e.getMessage());
1346+
final String errorMessage = String.format("Failed to delete template %1$s from bucket %2$s due to the following error: %3$s", path,
1347+
bucket, e.getMessage());
13811348
s_logger.error(errorMessage, e);
13821349
return new Answer(cmd, false, errorMessage);
13831350
}
@@ -1465,6 +1432,25 @@ protected Answer execute(final DeleteVolumeCommand cmd) {
14651432
return new Answer(cmd, false, errorMessage);
14661433
}
14671434
} else if (dstore instanceof SwiftTO) {
1435+
Long volumeId = cmd.getVolumeId();
1436+
String path = cmd.getVolumePath();
1437+
String filename = StringUtils.substringAfterLast(path, "/"); // assuming
1438+
// that
1439+
// the
1440+
// filename
1441+
// is
1442+
// the
1443+
// last
1444+
// section
1445+
// in
1446+
// the
1447+
// path
1448+
String result = swiftDelete((SwiftTO) dstore, "V-" + volumeId.toString(), filename);
1449+
if (result != null) {
1450+
String errMsg = "failed to delete volume " + filename + " , err=" + result;
1451+
s_logger.warn(errMsg);
1452+
return new Answer(cmd, false, errMsg);
1453+
}
14681454
return new Answer(cmd, false, "Swift is not currently support DeleteVolumeCommand");
14691455
} else {
14701456
return new Answer(cmd, false, "Unsupported image data store: " + dstore);

plugins/storage/image/default/src/org/apache/cloudstack/storage/datastore/driver/CloudStackImageStoreDriverImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ private void deleteVolume(DataObject data, AsyncCompletionCallback<CommandResult
237237
DataStore store = this._dataStoreMgr.getDataStore(volumeStore.getDataStoreId(), DataStoreRole.Image);
238238
EndPoint ep = _epSelector.select(store);
239239
DeleteVolumeCommand dtCommand = new DeleteVolumeCommand(
240-
store.getTO(), volumeStore.getInstallPath());
240+
store.getTO(), volumeStore.getVolumeId(), volumeStore.getInstallPath());
241241
Answer answer = ep.sendMessage(dtCommand);
242242
if (answer == null || !answer.getResult()) {
243243
s_logger.debug("Failed to delete "

plugins/storage/image/s3/src/org/apache/cloudstack/storage/datastore/driver/S3ImageStoreDriverImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ private void deleteVolume(DataObject data, AsyncCompletionCallback<CommandResult
195195
DataStore store = this._dataStoreMgr.getDataStore(volumeStore.getDataStoreId(), DataStoreRole.Image);
196196
EndPoint ep = _epSelector.select(store);
197197
DeleteVolumeCommand dtCommand = new DeleteVolumeCommand(
198-
store.getTO(), volumeStore.getInstallPath());
198+
store.getTO(), volumeStore.getVolumeId(), volumeStore.getInstallPath());
199199
Answer answer = ep.sendMessage(dtCommand);
200200
if (answer == null || !answer.getResult()) {
201201
s_logger.debug("Failed to delete "

plugins/storage/image/swift/src/org/apache/cloudstack/storage/datastore/driver/SwiftImageStoreDriverImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ private void deleteVolume(DataObject data, AsyncCompletionCallback<CommandResult
189189
DataStore store = this._dataStoreMgr.getDataStore(volumeStore.getDataStoreId(), DataStoreRole.Image);
190190
EndPoint ep = _epSelector.select(store);
191191
DeleteVolumeCommand dtCommand = new DeleteVolumeCommand(
192-
store.getTO(), volumeStore.getInstallPath());
192+
store.getTO(), volumeStore.getVolumeId(), volumeStore.getInstallPath());
193193
Answer answer = ep.sendMessage(dtCommand);
194194
if (answer == null || !answer.getResult()) {
195195
s_logger.debug("Failed to delete "

server/src/com/cloud/storage/StorageManagerImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1325,7 +1325,7 @@ public void cleanupSecondaryStorage(boolean recurring) {
13251325

13261326
if (installPath != null) {
13271327
EndPoint ep = _epSelector.select(store);
1328-
DeleteVolumeCommand cmd = new DeleteVolumeCommand(store.getTO(), destroyedStoreVO.getInstallPath());
1328+
DeleteVolumeCommand cmd = new DeleteVolumeCommand(store.getTO(), destroyedStoreVO.getVolumeId(), destroyedStoreVO.getInstallPath());
13291329
Answer answer = ep.sendMessage(cmd);
13301330
if (answer == null || !answer.getResult()) {
13311331
s_logger.debug("Failed to delete " + destroyedStoreVO + " due to "

server/src/com/cloud/storage/swift/SwiftManagerImpl.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
import com.cloud.agent.AgentManager;
3636
import com.cloud.agent.api.Answer;
37-
import com.cloud.agent.api.DeleteObjectFromSwiftCommand;
37+
import com.cloud.agent.api.storage.DeleteTemplateCommand;
3838
import com.cloud.agent.api.to.SwiftTO;
3939
import org.apache.cloudstack.api.command.admin.swift.AddSwiftCmd;
4040
import com.cloud.configuration.Config;
@@ -144,7 +144,7 @@ public void deleteIso(DeleteIsoCmd cmd) {
144144
s_logger.warn(msg);
145145
throw new CloudRuntimeException(msg);
146146
}
147-
Answer answer = _agentMgr.sendToSSVM(null, new DeleteObjectFromSwiftCommand(swift, "T-" + cmd.getId(), null));
147+
Answer answer = _agentMgr.sendToSSVM(null, new DeleteTemplateCommand(swift, null, cmd.getId(), null));
148148
if (answer == null || !answer.getResult()) {
149149
msg = "Failed to delete " + tmpltSwiftRef + " due to " + ((answer == null) ? "answer is null" : answer.getDetails());
150150
s_logger.warn(msg);
@@ -170,7 +170,7 @@ public void deleteTemplate(DeleteTemplateCmd cmd) {
170170
s_logger.warn(msg);
171171
throw new CloudRuntimeException(msg);
172172
}
173-
Answer answer = _agentMgr.sendToSSVM(null, new DeleteObjectFromSwiftCommand(swift, "T-" + cmd.getId(), null));
173+
Answer answer = _agentMgr.sendToSSVM(null, new DeleteTemplateCommand(swift, null, cmd.getId(), null));
174174
if (answer == null || !answer.getResult()) {
175175
msg = "Failed to delete " + tmpltSwiftRef + " due to " + ((answer == null) ? "answer is null" : answer.getDetails());
176176
s_logger.warn(msg);
@@ -236,7 +236,7 @@ public Long chooseZoneForTmpltExtract(Long tmpltId) {
236236
if (swift == null) {
237237
return null;
238238
}
239-
239+
240240
List<VMTemplateHostVO> tmpltHosts = _vmTmpltHostDao.listByOnlyTemplateId(tmpltId);
241241
if (tmpltHosts != null) {
242242
Collections.shuffle(tmpltHosts);

0 commit comments

Comments
 (0)