|
18 | 18 |
|
19 | 19 | import java.util.Collections; |
20 | 20 | import java.util.HashSet; |
| 21 | +import java.util.LinkedList; |
21 | 22 | import java.util.List; |
22 | 23 | import java.util.Set; |
23 | 24 | import java.util.concurrent.ExecutionException; |
24 | 25 |
|
25 | 26 | import javax.ejb.Local; |
26 | 27 | import javax.inject.Inject; |
27 | 28 |
|
| 29 | +import org.apache.cloudstack.api.command.user.template.GetUploadParamsForTemplate; |
| 30 | +import org.apache.cloudstack.engine.subsystem.api.storage.DataObject; |
| 31 | +import org.apache.cloudstack.engine.subsystem.api.storage.EndPoint; |
| 32 | +import org.apache.cloudstack.storage.command.TemplateOrVolumePostUploadCommand; |
28 | 33 | import org.apache.log4j.Logger; |
29 | 34 |
|
30 | 35 | import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreDao; |
@@ -131,6 +136,15 @@ public TemplateProfile prepare(RegisterTemplateCmd cmd) throws ResourceAllocatio |
131 | 136 | return profile; |
132 | 137 | } |
133 | 138 |
|
| 139 | + @Override |
| 140 | + public TemplateProfile prepare(GetUploadParamsForTemplate cmd) throws ResourceAllocationException { |
| 141 | + TemplateProfile profile = super.prepare(cmd); |
| 142 | + |
| 143 | + // Check that the resource limit for secondary storage won't be exceeded |
| 144 | + _resourceLimitMgr.checkResourceLimit(_accountMgr.getAccount(cmd.getEntityOwnerId()), ResourceType.secondary_storage); |
| 145 | + return profile; |
| 146 | + } |
| 147 | + |
134 | 148 | @Override |
135 | 149 | public VMTemplateVO create(TemplateProfile profile) { |
136 | 150 | // persist entry in vm_template, vm_template_details and template_zone_ref tables, not that entry at template_store_ref is not created here, and created in createTemplateAsync. |
@@ -185,6 +199,70 @@ public VMTemplateVO create(TemplateProfile profile) { |
185 | 199 | return template; |
186 | 200 | } |
187 | 201 |
|
| 202 | + @Override |
| 203 | + public List<TemplateOrVolumePostUploadCommand> createTemplateForPostUpload(TemplateProfile profile) { |
| 204 | + // persist entry in vm_template, vm_template_details and template_zone_ref tables, not that entry at template_store_ref is not created here, and created in createTemplateAsync. |
| 205 | + VMTemplateVO template = persistTemplate(profile); |
| 206 | + |
| 207 | + if (template == null) { |
| 208 | + throw new CloudRuntimeException("Unable to persist the template " + profile.getTemplate()); |
| 209 | + } |
| 210 | + |
| 211 | + // find all eligible image stores for this zone scope |
| 212 | + List<DataStore> imageStores = storeMgr.getImageStoresByScope(new ZoneScope(profile.getZoneId())); |
| 213 | + if (imageStores == null || imageStores.size() == 0) { |
| 214 | + throw new CloudRuntimeException("Unable to find image store to download template " + profile.getTemplate()); |
| 215 | + } |
| 216 | + |
| 217 | + List<TemplateOrVolumePostUploadCommand> payloads = new LinkedList<>(); |
| 218 | + Set<Long> zoneSet = new HashSet<Long>(); |
| 219 | + Collections.shuffle(imageStores); // For private templates choose a random store. TODO - Have a better algorithm based on size, no. of objects, load etc. |
| 220 | + for (DataStore imageStore : imageStores) { |
| 221 | + // skip data stores for a disabled zone |
| 222 | + Long zoneId = imageStore.getScope().getScopeId(); |
| 223 | + if (zoneId != null) { |
| 224 | + DataCenterVO zone = _dcDao.findById(zoneId); |
| 225 | + if (zone == null) { |
| 226 | + s_logger.warn("Unable to find zone by id " + zoneId + ", so skip downloading template to its image store " + imageStore.getId()); |
| 227 | + continue; |
| 228 | + } |
| 229 | + |
| 230 | + // Check if zone is disabled |
| 231 | + if (Grouping.AllocationState.Disabled == zone.getAllocationState()) { |
| 232 | + s_logger.info("Zone " + zoneId + " is disabled, so skip downloading template to its image store " + imageStore.getId()); |
| 233 | + continue; |
| 234 | + } |
| 235 | + |
| 236 | + // We want to download private template to one of the image store in a zone |
| 237 | + if (isPrivateTemplate(template) && zoneSet.contains(zoneId)) { |
| 238 | + continue; |
| 239 | + } else { |
| 240 | + zoneSet.add(zoneId); |
| 241 | + } |
| 242 | + |
| 243 | + } |
| 244 | + |
| 245 | + TemplateInfo tmpl = imageFactory.getTemplate(template.getId(), imageStore); |
| 246 | + //imageService.createTemplateAsync(tmpl, imageStore, caller); |
| 247 | + |
| 248 | + // persist template_store_ref entry |
| 249 | + DataObject templateOnStore = imageStore.create(tmpl); |
| 250 | + // update template_store_ref and template state |
| 251 | + |
| 252 | + EndPoint ep = _epSelector.select(templateOnStore); |
| 253 | + if (ep == null) { |
| 254 | + String errMsg = "There is no secondary storage VM for downloading template to image store " + imageStore.getName(); |
| 255 | + s_logger.warn(errMsg); |
| 256 | + throw new CloudRuntimeException(errMsg); |
| 257 | + } |
| 258 | + |
| 259 | + TemplateOrVolumePostUploadCommand payload = new TemplateOrVolumePostUploadCommand(templateOnStore, ep); |
| 260 | + payloads.add(payload); |
| 261 | + } |
| 262 | + _resourceLimitMgr.incrementResourceCount(profile.getAccountId(), ResourceType.template); |
| 263 | + return payloads; |
| 264 | + } |
| 265 | + |
188 | 266 | private boolean isPrivateTemplate(VMTemplateVO template){ |
189 | 267 |
|
190 | 268 | // if public OR featured OR system template |
|
0 commit comments