@@ -295,6 +295,22 @@ public void handleSysTemplateDownload(HypervisorType hostHyper, Long dcId) {
295295 }
296296 }
297297
298+ private boolean hasReachedSecStorageCopyLimit (VMTemplateVO template , long zoneId ) {
299+ boolean isPrivate = !template .isPublicTemplate () && !template .isFeatured ()
300+ && !TemplateType .SYSTEM .equals (template .getTemplateType ());
301+ int copyLimit = isPrivate
302+ ? TemplateManager .PrivateTemplateSecStorageCopy .valueIn (zoneId )
303+ : TemplateManager .PublicTemplateSecStorageCopy .valueIn (zoneId );
304+ if (copyLimit <= 0 ) {
305+ return false ;
306+ }
307+ List <TemplateDataStoreVO > existing = _vmTemplateStoreDao .listByTemplateZoneDownloadStatus (
308+ template .getId (), zoneId ,
309+ Status .DOWNLOADED , Status .DOWNLOAD_IN_PROGRESS , Status .NOT_DOWNLOADED );
310+ int currentCopies = existing == null ? 0 : existing .size ();
311+ return currentCopies >= copyLimit ;
312+ }
313+
298314 protected boolean shouldDownloadTemplateToStore (VMTemplateVO template , DataStore store ) {
299315 Long zoneId = store .getScope ().getScopeId ();
300316 DataStore directedStore = _tmpltMgr .verifyHeuristicRulesForZone (template , zoneId );
@@ -304,6 +320,12 @@ protected boolean shouldDownloadTemplateToStore(VMTemplateVO template, DataStore
304320 return false ;
305321 }
306322
323+ if (zoneId != null && hasReachedSecStorageCopyLimit (template , zoneId )) {
324+ logger .info ("Skipping sync of template [{}] to image store [{}]: zone [{}] has reached the configured copy limit." ,
325+ template .getUniqueName (), store .getName (), zoneId );
326+ return false ;
327+ }
328+
307329 if (template .isPublicTemplate ()) {
308330 logger .debug ("Download of template [{}] to image store [{}] cannot be skipped, as it is public." , template .getUniqueName (),
309331 store .getName ());
0 commit comments