@@ -1596,6 +1596,23 @@ public Answer forgetObject(final ForgetObjectCmd cmd) {
15961596 return new Answer (cmd , false , "not implememented yet" );
15971597 }
15981598
1599+ /**
1600+ * Get direct template downloader from direct download command and destination pool
1601+ */
1602+ private DirectTemplateDownloader getDirectTemplateDownloaderFromCommand (DirectDownloadCommand cmd , KVMStoragePool destPool ) {
1603+ if (cmd instanceof HttpDirectDownloadCommand ) {
1604+ return new HttpDirectTemplateDownloader (cmd .getUrl (), cmd .getTemplateId (), destPool .getLocalPath (), cmd .getChecksum (), cmd .getHeaders ());
1605+ } else if (cmd instanceof HttpsDirectDownloadCommand ) {
1606+ return new HttpsDirectTemplateDownloader (cmd .getUrl (), cmd .getTemplateId (), destPool .getLocalPath (), cmd .getChecksum (), cmd .getHeaders ());
1607+ } else if (cmd instanceof NfsDirectDownloadCommand ) {
1608+ return new NfsDirectTemplateDownloader (cmd .getUrl (), destPool .getLocalPath (), cmd .getTemplateId (), cmd .getChecksum ());
1609+ } else if (cmd instanceof MetalinkDirectDownloadCommand ) {
1610+ return new MetalinkDirectTemplateDownloader (cmd .getUrl (), destPool .getLocalPath (), cmd .getTemplateId (), cmd .getChecksum (), cmd .getHeaders ());
1611+ } else {
1612+ throw new IllegalArgumentException ("Unsupported protocol, please provide HTTP(S), NFS or a metalink" );
1613+ }
1614+ }
1615+
15991616 @ Override
16001617 public Answer handleDownloadTemplateToPrimaryStorage (DirectDownloadCommand cmd ) {
16011618 final PrimaryDataStoreTO pool = cmd .getDestPool ();
@@ -1606,30 +1623,39 @@ public Answer handleDownloadTemplateToPrimaryStorage(DirectDownloadCommand cmd)
16061623 DirectTemplateDownloader downloader ;
16071624
16081625 try {
1609- if (cmd instanceof HttpDirectDownloadCommand ) {
1610- downloader = new HttpDirectTemplateDownloader (cmd .getUrl (), cmd .getTemplateId (), destPool .getLocalPath (), cmd .getChecksum (), cmd .getHeaders ());
1611- } else if (cmd instanceof HttpsDirectDownloadCommand ) {
1612- downloader = new HttpsDirectTemplateDownloader (cmd .getUrl (), cmd .getTemplateId (), destPool .getLocalPath (), cmd .getChecksum (), cmd .getHeaders ());
1613- } else if (cmd instanceof NfsDirectDownloadCommand ) {
1614- downloader = new NfsDirectTemplateDownloader (cmd .getUrl (), destPool .getLocalPath (), cmd .getTemplateId (), cmd .getChecksum ());
1615- } else if (cmd instanceof MetalinkDirectDownloadCommand ) {
1616- downloader = new MetalinkDirectTemplateDownloader (cmd .getUrl (), destPool .getLocalPath (), cmd .getTemplateId (), cmd .getChecksum (), cmd .getHeaders ());
1617- } else {
1618- return new DirectDownloadAnswer (false , "Unsupported protocol, please provide HTTP(S), NFS or a metalink" );
1619- }
1620- } catch (CloudRuntimeException e ) {
1626+ downloader = getDirectTemplateDownloaderFromCommand (cmd , destPool );
1627+ } catch (IllegalArgumentException e ) {
16211628 return new DirectDownloadAnswer (false , "Unable to create direct downloader: " + e .getMessage ());
16221629 }
16231630
1624- if (!downloader .downloadTemplate ()) {
1625- return new DirectDownloadAnswer (false , "Could not download template " + cmd .getTemplateId () + " on " + destPool .getLocalPath ());
1626- }
1627- if (!downloader .validateChecksum ()) {
1628- return new DirectDownloadAnswer (false , "Checksum validation failed for template " + cmd .getTemplateId ());
1629- }
1630- if (!downloader .extractAndInstallDownloadedTemplate ()) {
1631- return new DirectDownloadAnswer (false , "Template downloaded but there was an error on installation" );
1631+ int retries = 3 ;
1632+ boolean ok = false ;
1633+ do {
1634+ try {
1635+ retries --;
1636+ s_logger .info ("Trying to download template, retries left: " + retries );
1637+ if (!downloader .downloadTemplate ()) {
1638+ s_logger .warn ("Couldn't download template" );
1639+ continue ;
1640+ }
1641+ if (!downloader .validateChecksum ()) {
1642+ s_logger .warn ("Couldn't validate template checksum" );
1643+ continue ;
1644+ }
1645+ if (!downloader .extractAndInstallDownloadedTemplate ()) {
1646+ s_logger .warn ("Couldn't extract and install template" );
1647+ continue ;
1648+ }
1649+ ok = true ;
1650+ } catch (CloudRuntimeException e ) {
1651+ s_logger .warn ("Error downloading template " + cmd .getTemplateId () + " due to: " + e .getMessage () + " retries left: " + retries );
1652+ }
1653+ } while (!ok && retries > 0 );
1654+
1655+ if (!ok ) {
1656+ return new DirectDownloadAnswer (false , "Unable to download template " + cmd .getTemplateId ());
16321657 }
1658+
16331659 DirectTemplateInformation info = downloader .getTemplateInformation ();
16341660 return new DirectDownloadAnswer (true , info .getSize (), info .getInstallPath ());
16351661 }
0 commit comments