1818
1919import static com .cloud .utils .S3Utils .deleteDirectory ;
2020import static com .cloud .utils .S3Utils .getDirectory ;
21- import static com .cloud .utils .S3Utils .putDirectory ;
2221import static com .cloud .utils .StringUtils .join ;
2322import static com .cloud .utils .db .GlobalLock .executeWithNoWaitLock ;
2423import static java .lang .String .format ;
2928import java .io .File ;
3029import java .io .FileInputStream ;
3130import java .io .FileWriter ;
32- import java .io .FilenameFilter ;
3331import java .io .IOException ;
3432import java .io .InputStream ;
3533import java .math .BigInteger ;
4947
5048import javax .naming .ConfigurationException ;
5149
50+ import org .apache .cloudstack .engine .subsystem .api .storage .DataTO ;
5251import org .apache .cloudstack .storage .command .CopyCmd ;
5352import org .apache .log4j .Logger ;
5453
6362import com .cloud .agent .api .DeleteSnapshotsDirCommand ;
6463import com .cloud .agent .api .DeleteTemplateFromS3Command ;
6564import com .cloud .agent .api .DownloadSnapshotFromS3Command ;
66- import com .cloud .agent .api .DownloadTemplateFromS3ToSecondaryStorageCommand ;
6765import com .cloud .agent .api .GetStorageStatsAnswer ;
6866import com .cloud .agent .api .GetStorageStatsCommand ;
6967import com .cloud .agent .api .PingCommand ;
102100import com .cloud .host .Host ;
103101import com .cloud .host .Host .Type ;
104102import com .cloud .resource .ServerResourceBase ;
103+ import com .cloud .storage .DataStoreRole ;
105104import com .cloud .storage .StorageLayer ;
106105import com .cloud .storage .template .DownloadManager ;
107106import com .cloud .storage .template .DownloadManagerImpl ;
108107import com .cloud .storage .template .DownloadManagerImpl .ZfsPathParser ;
109- import com .cloud .storage .template .TemplateProp ;
110108import com .cloud .storage .template .TemplateLocation ;
109+ import com .cloud .storage .template .TemplateProp ;
111110import com .cloud .storage .template .UploadManager ;
112111import com .cloud .storage .template .UploadManagerImpl ;
113112import com .cloud .utils .NumbersUtil ;
114113import com .cloud .utils .S3Utils ;
115- import com .cloud .utils .UriUtils ;
116114import com .cloud .utils .S3Utils .FileNamingStrategy ;
117- import com .cloud .utils .S3Utils .ObjectNamingStrategy ;
118- import com .cloud .utils .component .ComponentContext ;
115+ import com .cloud .utils .UriUtils ;
119116import com .cloud .utils .exception .CloudRuntimeException ;
120117import com .cloud .utils .net .NetUtils ;
121118import com .cloud .utils .script .OutputInterpreter ;
@@ -208,8 +205,6 @@ public Answer executeRequest(Command cmd) {
208205 return execute ((DeleteSnapshotsDirCommand )cmd );
209206 } else if (cmd instanceof downloadTemplateFromSwiftToSecondaryStorageCommand ) {
210207 return execute ((downloadTemplateFromSwiftToSecondaryStorageCommand ) cmd );
211- } else if (cmd instanceof DownloadTemplateFromS3ToSecondaryStorageCommand ) {
212- return execute ((DownloadTemplateFromS3ToSecondaryStorageCommand ) cmd );
213208 } else if (cmd instanceof uploadTemplateToSwiftFromSecondaryStorageCommand ) {
214209 return execute ((uploadTemplateToSwiftFromSecondaryStorageCommand ) cmd );
215210 } else if (cmd instanceof UploadTemplateToS3FromSecondaryStorageCommand ) {
@@ -227,7 +222,80 @@ public Answer executeRequest(Command cmd) {
227222 }
228223 }
229224
225+ protected Answer downloadFromS3ToNfs (CopyCmd cmd , DataTO srcData , S3TO s3 ,
226+ DataTO destData , NfsTO destImageStore ) {
227+ final String storagePath = destImageStore .getUrl ();
228+ final String destPath = destData .getPath ();
229+
230+ try {
231+
232+ final File downloadDirectory = _storage
233+ .getFile (determineStorageTemplatePath (storagePath ,
234+ destPath ));
235+ downloadDirectory .mkdirs ();
236+
237+ if (!downloadDirectory .exists ()) {
238+ final String errMsg = format (
239+ "Unable to create directory "
240+ + "download directory %1$s for download from S3." , downloadDirectory .getName ()
241+ );
242+ s_logger .error (errMsg );
243+ return new Answer (cmd , false , errMsg );
244+ }
245+
246+ getDirectory (s3 , s3 .getBucketName (),
247+ destPath ,
248+ downloadDirectory , new FileNamingStrategy () {
249+ @ Override
250+ public String determineFileName (final String key ) {
251+ return substringAfterLast (key , S3Utils .SEPARATOR );
252+ }
253+ });
254+
255+ return new Answer (cmd , true , format ("Successfully downloaded "
256+ + "from S3 to directory %2$s" ,
257+ downloadDirectory .getName ()));
258+
259+ } catch (Exception e ) {
260+
261+ final String errMsg = format ("Failed to download"
262+ + "due to $2%s" , e .getMessage ());
263+ s_logger .error (errMsg , e );
264+ return new Answer (cmd , false , errMsg );
265+ }
266+ }
267+
268+ protected Answer downloadFromSwiftToNfs (CopyCmd cmd , DataTO srcData , SwiftTO srcImageStore ,
269+ DataTO destData , NfsTO destImageStore ) {
270+ return Answer .createUnsupportedCommandAnswer (cmd );
271+ }
272+
230273 protected Answer execute (CopyCmd cmd ) {
274+ DataTO srcData = cmd .getSrcTO ();
275+ DataTO destData = cmd .getDestTO ();
276+ DataStoreTO srcDataStore = srcData .getDataStore ();
277+ DataStoreTO destDataStore = destData .getDataStore ();
278+
279+ if (srcDataStore .getRole () == DataStoreRole .Image
280+ && destDataStore .getRole () == DataStoreRole .ImageCache
281+ ) {
282+
283+ if (!(destDataStore instanceof NfsTO )) {
284+ s_logger .debug ("only support nfs as cache storage" );
285+ return Answer .createUnsupportedCommandAnswer (cmd );
286+ }
287+
288+ if (srcDataStore instanceof S3TO ) {
289+ return downloadFromS3ToNfs (cmd , srcData , (S3TO )srcDataStore ,
290+ destData , (NfsTO )destDataStore );
291+ } else if (srcDataStore instanceof SwiftTO ) {
292+ return downloadFromSwiftToNfs (cmd , srcData , (SwiftTO )srcDataStore ,
293+ destData , (NfsTO )destDataStore );
294+ } else {
295+ return Answer .createUnsupportedCommandAnswer (cmd );
296+ }
297+
298+ }
231299 return Answer .createUnsupportedCommandAnswer (cmd );
232300 }
233301
@@ -240,59 +308,9 @@ private String determineS3TemplateDirectory(final Long accountId,
240308
241309 @ SuppressWarnings ("unchecked" )
242310 private String determineStorageTemplatePath (final String storagePath ,
243- final Long accountId , final Long templateId ) {
311+ String dataPath ) {
244312 return join (
245- asList (getRootDir (storagePath ), TEMPLATE_ROOT_DIR , accountId ,
246- templateId ), File .separator );
247- }
248-
249- private Answer execute (
250- final DownloadTemplateFromS3ToSecondaryStorageCommand cmd ) {
251-
252- final S3TO s3 = cmd .getS3 ();
253- final String storagePath = cmd .getStoragePath ();
254- final Long accountId = cmd .getAccountId ();
255- final Long templateId = cmd .getTemplateId ();
256-
257- try {
258-
259- final File downloadDirectory = _storage
260- .getFile (determineStorageTemplatePath (storagePath ,
261- accountId , templateId ));
262- downloadDirectory .mkdirs ();
263-
264- if (!downloadDirectory .exists ()) {
265- final String errMsg = format (
266- "Unable to create directory "
267- + "download directory %1$s for download of template id "
268- + "%2$s from S3." , downloadDirectory .getName (),
269- templateId );
270- s_logger .error (errMsg );
271- return new Answer (cmd , false , errMsg );
272- }
273-
274- getDirectory (s3 , s3 .getBucketName (),
275- determineS3TemplateDirectory (accountId , templateId ),
276- downloadDirectory , new FileNamingStrategy () {
277- @ Override
278- public String determineFileName (final String key ) {
279- return substringAfterLast (key , S3Utils .SEPARATOR );
280- }
281- });
282-
283- return new Answer (cmd , true , format ("Successfully downloaded "
284- + "template id %1$s from S3 to directory %2$s" , templateId ,
285- downloadDirectory .getName ()));
286-
287- } catch (Exception e ) {
288-
289- final String errMsg = format ("Failed to upload template id %1$s "
290- + "due to $2%s" , templateId , e .getMessage ());
291- s_logger .error (errMsg , e );
292- return new Answer (cmd , false , errMsg );
293-
294- }
295-
313+ asList (getRootDir (storagePath ), dataPath ), File .separator );
296314 }
297315
298316 private Answer execute (downloadTemplateFromSwiftToSecondaryStorageCommand cmd ) {
@@ -411,7 +429,7 @@ private Answer execute(uploadTemplateToSwiftFromSecondaryStorageCommand cmd) {
411429 }
412430
413431 private Answer execute (UploadTemplateToS3FromSecondaryStorageCommand cmd ) {
414-
432+ /*
415433 final S3TO s3 = cmd.getS3();
416434 final Long accountId = cmd.getAccountId();
417435 final Long templateId = cmd.getTemplateId();
@@ -485,7 +503,8 @@ public String determineKey(final File file) {
485503 return new Answer(cmd, false, errMsg);
486504
487505 }
488-
506+ */
507+ return new Answer (cmd , false , "not supported " );
489508 }
490509
491510 private Answer execute (DeleteObjectFromSwiftCommand cmd ) {
0 commit comments