@Override public long getId() { return templateInfo.getId(); }
private Map<String, String> getTemplateAttributes(TemplateInfo templateInfo) { Map<String, String> mapAttributes = new HashMap<>(); mapAttributes.put(SolidFireUtil.CloudStackTemplateId, String.valueOf(templateInfo.getId())); mapAttributes.put(SolidFireUtil.CloudStackTemplateSize, NumberFormat.getInstance().format(templateInfo.getSize())); return mapAttributes; }
private TemplateInfo waitForTemplateDownloaded(PrimaryDataStore store, TemplateInfo template) { int storagePoolMaxWaitSeconds = NumbersUtil.parseInt(configDao.getValue(Config.StoragePoolMaxWaitSeconds.key()), 3600); int sleepTime = 120; int tries = storagePoolMaxWaitSeconds / sleepTime; while (tries > 0) { TemplateInfo tmpl = store.getTemplate(template.getId()); if (tmpl != null) { return tmpl; } try { Thread.sleep(sleepTime * 1000); } catch (InterruptedException e) { s_logger.debug("waiting for template download been interrupted: " + e.toString()); } tries--; } return null; }
/** * This method is only relevant when storagePool is being used with a compute cluster that supports UUID resigning. */ @Override public long getBytesRequiredForTemplate(TemplateInfo templateInfo, StoragePool storagePool) { List<VMTemplateStoragePoolVO> lstTemplatePoolRefs = tmpltPoolDao.listByPoolId(storagePool.getId()); if (lstTemplatePoolRefs != null) { for (VMTemplateStoragePoolVO templatePoolRef : lstTemplatePoolRefs) { if (templatePoolRef.getTemplateId() == templateInfo.getId()) { // This indicates that we already have this template stored on this primary storage, so // we do not require additional space. return 0; } } } // This indicates that we do not have a copy of this template on this primary storage, so // we need to take it into consideration from a space standpoint (ex. when a new VM is spun // up and wants to use this particular template for its root disk). return getDataObjectSizeIncludingHypervisorSnapshotReserve(templateInfo, storagePool); }
@DB @Override public AsyncCallFuture<VolumeApiResult> createVolumeFromTemplateAsync(VolumeInfo volume, long dataStoreId, TemplateInfo template) { PrimaryDataStore pd = dataStoreMgr.getPrimaryDataStore(dataStoreId); TemplateInfo templateOnPrimaryStore = pd.getTemplate(template.getId()); AsyncCallFuture<VolumeApiResult> future = new AsyncCallFuture<VolumeApiResult>(); if (templateOnPrimaryStore == null) { createBaseImageAsync(volume, pd, template, future); return future; } createVolumeFromBaseImageAsync(volume, templateOnPrimaryStore, pd, future); return future; }
private void deleteTemplate(TemplateInfo template, long storagePoolId) { try { SolidFireUtil.SolidFireConnection sfConnection = SolidFireUtil.getSolidFireConnection(storagePoolId, storagePoolDetailsDao); long sfTemplateVolumeId = getVolumeIdFrom_iScsiPath(template.getInstallPath()); SolidFireUtil.deleteVolume(sfConnection, sfTemplateVolumeId); StoragePoolVO storagePool = storagePoolDao.findById(storagePoolId); // getUsedBytes(StoragePool) will not include the template to delete because the "template_spool_ref" table has already been updated by this point long usedBytes = getUsedBytes(storagePool); storagePool.setUsedBytes(usedBytes < 0 ? 0 : usedBytes); storagePoolDao.update(storagePoolId, storagePool); } catch (Exception ex) { LOGGER.debug(SolidFireUtil.LOG_PREFIX + "Failed to delete SolidFire template volume. CloudStack template ID: " + template.getId(), ex); throw ex; } }
dataDiskTemplates = tmpltStore.getDataDiskTemplates(parentTemplate); int diskCount = 0; VMTemplateVO templateVO = _templateDao.findById(parentTemplate.getId()); _templateDao.loadDetails(templateVO); DataStore imageStore = parentTemplate.getDataStore();
@Override public Void createDataDiskTemplateAsync(TemplateInfo dataDiskTemplate, String path, String diskId, boolean bootable, long fileSize, AsyncCompletionCallback<CreateCmdResult> callback) { Answer answer = null; String errMsg = null; if (s_logger.isDebugEnabled()) { s_logger.debug("Create Datadisk template: " + dataDiskTemplate.getId()); } CreateDatadiskTemplateCommand cmd = new CreateDatadiskTemplateCommand(dataDiskTemplate.getTO(), path, diskId, fileSize, bootable); EndPoint ep = _defaultEpSelector.select(dataDiskTemplate.getDataStore()); if (ep == null) { errMsg = "No remote endpoint to send command, check if host or ssvm is down?"; s_logger.error(errMsg); answer = new Answer(cmd, false, errMsg); } else { answer = ep.sendMessage(cmd); } if (answer != null && !answer.getResult()) { errMsg = answer.getDetails(); } CreateCmdResult result = new CreateCmdResult(null, answer); result.setResult(errMsg); callback.complete(result); return null; } }
private boolean finalizeParentTemplate(DatadiskTO dataDiskTemplate, VMTemplateVO templateVO, TemplateInfo parentTemplate, DataStore imageStore, int diskCount) throws ExecutionException, InterruptedException, CloudRuntimeException { TemplateInfo templateInfo = imageFactory.getTemplate(templateVO.getId(), imageStore); AsyncCallFuture<TemplateApiResult> templateFuture = createDatadiskTemplateAsync(parentTemplate, templateInfo, dataDiskTemplate.getPath(), dataDiskTemplate.getDiskId(), dataDiskTemplate.getFileSize(), dataDiskTemplate.isBootable()); TemplateApiResult result = null; result = templateFuture.get(); if (!result.isSuccess()) { s_logger.debug("Since creation of parent template: " + templateInfo.getId() + " failed, delete Datadisk templates that were created as part of parent" + " template download"); cleanupDatadiskTemplates(templateInfo); } return result.isSuccess(); }
TemplateApiResult result = callback.getResult(); if (result.isSuccess()) { VMTemplateVO tmplt = _templateDao.findById(template.getId()); TemplateDataStoreVO tmpltStore = _vmTemplateStoreDao.findByStoreTemplate(ds.getId(), template.getId()); if (tmpltStore != null) { physicalSize = tmpltStore.getPhysicalSize(); } else { s_logger.warn("No entry found in template_store_ref for template id: " + template.getId() + " and image store id: " + ds.getId() + " at the end of registering template!"); UsageEventUtils.publishUsageEvent(etype, template.getAccountId(), dsScope.getScopeId(), template.getId(), template.getName(), null, null, physicalSize, template.getSize(), VirtualMachineTemplate.class.getName(), template.getUuid()); } else {
TemplateInfo template = context.template; if (result.isSuccess()) { VMTemplateVO tmplt = _tmpltDao.findById(template.getId()); TemplateDataStoreVO tmpltStore = _tmpltStoreDao.findByStoreTemplate(ds.getId(), template.getId()); if (tmpltStore != null) { physicalSize = tmpltStore.getPhysicalSize(); } else { s_logger.warn("No entry found in template_store_ref for template id: " + template.getId() + " and image store id: " + ds.getId() + " at the end of registering template!"); if (dsScope.getScopeType() == ScopeType.ZONE) { if (dsScope.getScopeId() != null) { UsageEventUtils.publishUsageEvent(etype, template.getAccountId(), dsScope.getScopeId(), template.getId(), template.getName(), null, null, physicalSize, template.getSize(), VirtualMachineTemplate.class.getName(), template.getUuid()); } else { UsageEventUtils.publishUsageEvent(etype, template.getAccountId(), -1, template.getId(), template.getName(), null, null, physicalSize, template.getSize(), VirtualMachineTemplate.class.getName(), template.getUuid());
s_logger.debug("Storage " + destDataStoreId + " can support cloning using a cached template and compute side is OK with volume cloning."); TemplateInfo templateOnPrimary = destPrimaryDataStore.getTemplate(srcTemplateInfo.getId()); VMTemplateStoragePoolVO templatePoolRef = _tmpltPoolDao.findByPoolTemplate(destPrimaryDataStore.getId(), templateOnPrimary.getId());
private String generateCopyUrl(TemplateInfo srcTemplate) { DataStore srcStore = srcTemplate.getDataStore(); EndPoint ep = _epSelector.select(srcTemplate); if (ep != null) { if (ep.getPublicAddr() == null) { s_logger.warn("A running secondary storage vm has a null public ip?"); return null; } return generateCopyUrl(ep.getPublicAddr(), ((ImageStoreEntity)srcStore).getMountPoint(), srcTemplate.getInstallPath()); } VMTemplateVO tmplt = _templateDao.findById(srcTemplate.getId()); HypervisorType hyperType = tmplt.getHypervisorType(); /*No secondary storage vm yet*/ if (hyperType != null && hyperType == HypervisorType.KVM) { return "file://" + ((ImageStoreEntity)srcStore).getMountPoint() + "/" + srcTemplate.getInstallPath(); } return null; }
private void cleanupDatadiskTemplates(TemplateInfo parentTemplateInfo) { DataStore imageStore = parentTemplateInfo.getDataStore(); List<VMTemplateVO> datadiskTemplatesToDelete = _templateDao.listByParentTemplatetId(parentTemplateInfo.getId()); for (VMTemplateVO datadiskTemplateToDelete: datadiskTemplatesToDelete) { s_logger.info("Delete template: " + datadiskTemplateToDelete.getId() + " from image store: " + imageStore.getName()); AsyncCallFuture<TemplateApiResult> future = deleteTemplateAsync(imageFactory.getTemplate(datadiskTemplateToDelete.getId(), imageStore)); try { TemplateApiResult result = future.get(); if (!result.isSuccess()) { s_logger.warn("Failed to delete datadisk template: " + datadiskTemplateToDelete + " from image store: " + imageStore.getName() + " due to: " + result.getResult()); break; } _vmTemplateZoneDao.deletePrimaryRecordsForTemplate(datadiskTemplateToDelete.getId()); _resourceLimitMgr.decrementResourceCount(datadiskTemplateToDelete.getAccountId(), ResourceType.secondary_storage, datadiskTemplateToDelete.getSize()); } catch (Exception e) { s_logger.debug("Delete datadisk template failed", e); throw new CloudRuntimeException("Delete template Failed", e); } } }
@Override public AsyncCallFuture<TemplateApiResult> deleteTemplateOnPrimary(TemplateInfo template, StoragePool pool) { TemplateObject templateObject = (TemplateObject)_templateFactory.getTemplate(template.getId(), (DataStore)pool); templateObject.processEvent(ObjectInDataStoreStateMachine.Event.DestroyRequested); DataStore dataStore = _storeMgr.getPrimaryDataStore(pool.getId()); AsyncCallFuture<TemplateApiResult> future = new AsyncCallFuture<>(); TemplateOpContext<TemplateApiResult> context = new TemplateOpContext<>(null, templateObject, future); AsyncCallbackDispatcher<TemplateServiceImpl, CommandResult> caller = AsyncCallbackDispatcher.create(this); caller.setCallback(caller.getTarget().deleteTemplateCallback(null, null)).setContext(context); dataStore.getDriver().deleteAsync(dataStore, templateObject, caller); return future; }
private AsyncCallFuture<TemplateApiResult> syncToRegionStoreAsync(TemplateInfo template, DataStore store) { AsyncCallFuture<TemplateApiResult> future = new AsyncCallFuture<TemplateApiResult>(); // no need to create entry on template_store_ref here, since entries are already created when prepareSecondaryStorageForMigration is invoked. // But we need to set default install path so that sync can be done in the right s3 path TemplateInfo templateOnStore = _templateFactory.getTemplate(template, store); String installPath = TemplateConstants.DEFAULT_TMPLT_ROOT_DIR + "/" + TemplateConstants.DEFAULT_TMPLT_FIRST_LEVEL_DIR + template.getAccountId() + "/" + template.getId() + "/" + template.getUniqueName(); ((TemplateObject)templateOnStore).setInstallPath(installPath); TemplateOpContext<TemplateApiResult> context = new TemplateOpContext<TemplateApiResult>(null, (TemplateObject)templateOnStore, future); AsyncCallbackDispatcher<TemplateServiceImpl, CopyCommandResult> caller = AsyncCallbackDispatcher.create(this); caller.setCallback(caller.getTarget().syncTemplateCallBack(null, null)).setContext(context); _motionSrv.copyAsync(template, templateOnStore, caller); return future; }
/** * Clones the template volume on managed storage to the ROOT volume * * @param volumeInfo ROOT volume to create * @param templateOnPrimary Template from which to clone the ROOT volume * @param destPrimaryDataStore Primary storage of the volume * @param future For async */ private void createManagedVolumeCloneTemplateAsync(VolumeInfo volumeInfo, TemplateInfo templateOnPrimary, PrimaryDataStore destPrimaryDataStore, AsyncCallFuture<VolumeApiResult> future) { VMTemplateStoragePoolVO templatePoolRef = _tmpltPoolDao.findByPoolTemplate(destPrimaryDataStore.getId(), templateOnPrimary.getId()); if (templatePoolRef == null) { throw new CloudRuntimeException("Failed to find template " + templateOnPrimary.getUniqueName() + " in storage pool " + destPrimaryDataStore.getId()); } //XXX: not sure if this the right thing to do here. We can always fallback to the "copy from sec storage" if (templatePoolRef.getDownloadState() == Status.NOT_DOWNLOADED) { throw new CloudRuntimeException("Template " + templateOnPrimary.getUniqueName() + " has not been downloaded to primary storage."); } try { volumeInfo.processEvent(Event.CreateOnlyRequested); CreateVolumeFromBaseImageContext<VolumeApiResult> context = new CreateVolumeFromBaseImageContext<>(null, volumeInfo, destPrimaryDataStore, templateOnPrimary, future, null); AsyncCallbackDispatcher<VolumeServiceImpl, CopyCommandResult> caller = AsyncCallbackDispatcher.create(this); caller.setCallback(caller.getTarget().createVolumeFromBaseImageCallBack(null, null)); caller.setContext(context); motionSrv.copyAsync(templateOnPrimary, volumeInfo, caller); } catch (Throwable e) { s_logger.debug("Failed to clone template on primary storage", e); volumeInfo.processEvent(Event.OperationFailed); throw new CloudRuntimeException(e.getMessage()); } }
private String createTemplateVolume(TemplateInfo templateInfo, long storagePoolId) { verifySufficientBytesForStoragePool(templateInfo, storagePoolId); SolidFireUtil.SolidFireConnection sfConnection = SolidFireUtil.getSolidFireConnection(storagePoolId, storagePoolDetailsDao); long sfAccountId = getCreateSolidFireAccountId(sfConnection, templateInfo.getAccountId(), storagePoolId); SolidFireUtil.SolidFireVolume sfVolume = createSolidFireVolume(sfConnection, templateInfo, storagePoolId, sfAccountId); String iqn = sfVolume.getIqn(); VMTemplateStoragePoolVO templatePoolRef = tmpltPoolDao.findByPoolTemplate(storagePoolId, templateInfo.getId()); templatePoolRef.setInstallPath(iqn); templatePoolRef.setLocalDownloadPath(Long.toString(sfVolume.getId())); templatePoolRef.setTemplateSize(sfVolume.getTotalSize()); tmpltPoolDao.update(templatePoolRef.getId(), templatePoolRef); StoragePoolVO storagePool = storagePoolDao.findById(storagePoolId); long capacityBytes = storagePool.getCapacityBytes(); // getUsedBytes(StoragePool) will include the bytes of the newly created template volume because // _tmpltPoolDao.update(Long, VMTemplateStoragePoolVO) has already been invoked long usedBytes = getUsedBytes(storagePool); storagePool.setUsedBytes(usedBytes > capacityBytes ? capacityBytes : usedBytes); storagePoolDao.update(storagePoolId, storagePool); return iqn; }
public TemplateObjectTO(TemplateInfo template) { this.path = template.getInstallPath(); this.uuid = template.getUuid(); this.id = template.getId(); this.origUrl = template.getUrl(); this.displayText = template.getDisplayText(); this.checksum = template.getChecksum(); this.hvm = template.isRequiresHvm(); this.accountId = template.getAccountId(); this.name = template.getUniqueName(); this.format = template.getFormat(); this.uniqueName = template.getUniqueName(); this.size = template.getSize(); if (template.getDataStore() != null) { this.imageDataStore = template.getDataStore().getTO(); } this.hypervisorType = template.getHypervisorType(); }
} catch (CloudRuntimeException ex) { TemplateDataStoreVO templateStoreVO = _vmTemplateStoreDao.findByStoreTemplate(store.getId(), template.getId()); if (templateStoreVO != null) { TemplateInfo tmplObj = _templateFactory.getTemplate(template, store);