6161import com .cloud .agent .api .CleanupSnapshotBackupCommand ;
6262import com .cloud .agent .api .Command ;
6363import com .cloud .agent .api .ComputeChecksumCommand ;
64- import com .cloud .agent .api .DeleteObjectFromSwiftCommand ;
6564import com .cloud .agent .api .DeleteSnapshotBackupCommand ;
6665import com .cloud .agent .api .DeleteSnapshotsDirCommand ;
6766import 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 );
0 commit comments