@Override public boolean isTemplateMarkedForDirectDownload(long templateId) { VMTemplateVO templateVO = imageDataDao.findById(templateId); return templateVO.isDirectDownload(); }
@Override public String getUri() { if (url != null) { return url; } VMTemplateVO image = imageDao.findById(imageVO.getId()); return image.getUrl(); }
@Override public Long getSize() { if (dataStore == null) { return imageVO.getSize(); } /* * * // If the template that was passed into this allocator is not * installed in the storage pool, // add 3 * (template size on secondary * storage) to the running total VMTemplateHostVO templateHostVO = * _storageMgr.findVmTemplateHost(templateForVmCreation.getId(), null); * * if (templateHostVO == null) { VMTemplateSwiftVO templateSwiftVO = * _swiftMgr.findByTmpltId(templateForVmCreation.getId()); if * (templateSwiftVO != null) { long templateSize = * templateSwiftVO.getPhysicalSize(); if (templateSize == 0) { * templateSize = templateSwiftVO.getSize(); } totalAllocatedSize += * (templateSize + _extraBytesPerVolume); } } else { long templateSize = * templateHostVO.getPhysicalSize(); if ( templateSize == 0 ){ * templateSize = templateHostVO.getSize(); } totalAllocatedSize += * (templateSize + _extraBytesPerVolume); } */ VMTemplateVO image = imageDao.findById(imageVO.getId()); return image.getSize(); }
@Override public TemplateProfile prepareExtractTemplate(ExtractTemplateCmd cmd) { Long templateId = cmd.getId(); Long userId = CallContext.current().getCallingUserId(); Long zoneId = cmd.getZoneId(); VMTemplateVO template = _tmpltDao.findById(templateId); if (template == null) { throw new InvalidParameterValueException("unable to find template with id " + templateId); } return new TemplateProfile(userId, template, zoneId); }
@Override public boolean delete(long userId, long templateId, Long zoneId) { VMTemplateVO template = _tmpltDao.findById(templateId); if (template == null || template.getRemoved() != null) { throw new InvalidParameterValueException("Please specify a valid template."); } TemplateAdapter adapter = getAdapter(template.getHypervisorType()); return adapter.delete(new TemplateProfile(userId, template, zoneId)); }
private boolean attachISOToVM(long vmId, long userId, long isoId, boolean attach) { UserVmVO vm = _userVmDao.findById(vmId); VMTemplateVO iso = _tmpltDao.findById(isoId); boolean success = attachISOToVM(vmId, isoId, attach); if (success && attach) { vm.setIsoId(iso.getId()); _userVmDao.update(vmId, vm); } if (success && !attach) { vm.setIsoId(null); _userVmDao.update(vmId, vm); } return success; }
@Override public TemplateInfo getTemplate(long templateId, DataStore store) { VMTemplateVO templ = imageDataDao.findById(templateId); if (store == null) { TemplateObject tmpl = TemplateObject.getTemplate(templ, null); return tmpl; } // verify if the given input parameters are consistent with our db data. boolean found = false; if (store.getRole() == DataStoreRole.Primary) { VMTemplateStoragePoolVO templatePoolVO = templatePoolDao.findByPoolTemplate(store.getId(), templateId); if (templatePoolVO != null) { found = true; } } else { TemplateDataStoreVO templateStoreVO = templateStoreDao.findByStoreTemplate(store.getId(), templateId); if (templateStoreVO != null) { found = true; } } if (s_logger.isDebugEnabled()) { if (!found) { s_logger.debug("template " + templateId + " is not in store:" + store.getId() + ", type:" + store.getRole()); } else { s_logger.debug("template " + templateId + " is already in store:" + store.getId() + ", type:" + store.getRole()); } } TemplateObject tmpl = TemplateObject.getTemplate(templ, store); return tmpl; }
@Override @ActionEvent(eventType = EventTypes.EVENT_TEMPLATE_DELETE, eventDescription = "deleting template", async = true) public boolean deleteTemplate(DeleteTemplateCmd cmd) { Long templateId = cmd.getId(); Account caller = CallContext.current().getCallingAccount(); VMTemplateVO template = _tmpltDao.findById(templateId); if (template == null) { throw new InvalidParameterValueException("unable to find template with id " + templateId); } List<VMInstanceVO> vmInstanceVOList; if(cmd.getZoneId() != null) { vmInstanceVOList = _vmInstanceDao.listNonExpungedByZoneAndTemplate(cmd.getZoneId(), templateId); } else { vmInstanceVOList = _vmInstanceDao.listNonExpungedByTemplate(templateId); } if(!cmd.isForced() && CollectionUtils.isNotEmpty(vmInstanceVOList)) { final String message = String.format("Unable to delete template with id: %1$s because VM instances: [%2$s] are using it.", templateId, Joiner.on(",").join(vmInstanceVOList)); s_logger.warn(message); throw new InvalidParameterValueException(message); } _accountMgr.checkAccess(caller, AccessType.OperateEntry, true, template); if (template.getFormat() == ImageFormat.ISO) { throw new InvalidParameterValueException("Please specify a valid template."); } TemplateAdapter adapter = getAdapter(template.getHypervisorType()); TemplateProfile profile = adapter.prepareDelete(cmd); return adapter.delete(profile); }
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; }
@Override public VirtualMachineTemplate prepareTemplate(long templateId, long zoneId, Long storageId) { VMTemplateVO vmTemplate = _tmpltDao.findById(templateId); if (vmTemplate == null) { throw new InvalidParameterValueException("Unable to find template id=" + templateId); } _accountMgr.checkAccess(CallContext.current().getCallingAccount(), AccessType.OperateEntry, true, vmTemplate); if (storageId != null) { StoragePoolVO pool = _poolDao.findById(storageId); if (pool != null) { if (pool.getStatus() == StoragePoolStatus.Up && pool.getDataCenterId() == zoneId) { prepareTemplateInOneStoragePool(vmTemplate, pool); } else { s_logger.warn("Skip loading template " + vmTemplate.getId() + " into primary storage " + pool.getId() + " as either the pool zone " + pool.getDataCenterId() + " is different from the requested zone " + zoneId + " or the pool is currently not available."); } } } else { prepareTemplateInAllStoragePools(vmTemplate, zoneId); } return vmTemplate; }
@Override @ActionEvent(eventType = EventTypes.EVENT_TEMPLATE_EXTRACT, eventDescription = "extracting template", async = true) public String extract(ExtractTemplateCmd cmd) { Account caller = CallContext.current().getCallingAccount(); Long templateId = cmd.getId(); Long zoneId = cmd.getZoneId(); String url = cmd.getUrl(); String mode = cmd.getMode(); Long eventId = cmd.getStartEventId(); VirtualMachineTemplate template = _tmpltDao.findById(templateId); if (template == null) { throw new InvalidParameterValueException("unable to find template with id " + templateId); } return extract(caller, templateId, url, zoneId, mode, eventId, false); }
private boolean preparePxeInBasicZone(VirtualMachineProfile profile, NicProfile nic, DeployDestination dest, ReservationContext context) throws AgentUnavailableException, OperationTimedoutException { NetworkVO nwVO = _nwDao.findById(nic.getNetworkId()); QueryBuilder<BaremetalPxeVO> sc = QueryBuilder.create(BaremetalPxeVO.class); sc.and(sc.entity().getDeviceType(), Op.EQ, BaremetalPxeType.KICK_START.toString()); sc.and(sc.entity().getPhysicalNetworkId(), Op.EQ, nwVO.getPhysicalNetworkId()); BaremetalPxeVO pxeVo = sc.find(); if (pxeVo == null) { throw new CloudRuntimeException("No kickstart PXE server found in pod: " + dest.getPod().getId() + ", you need to add it before starting VM"); } VMTemplateVO template = _tmpDao.findById(profile.getTemplateId()); List<String> tuple = parseKickstartUrl(profile); String ks = tuple.get(0); String kernel = tuple.get(1); String initrd = tuple.get(2); PrepareKickstartPxeServerCommand cmd = new PrepareKickstartPxeServerCommand(); cmd.setKsFile(ks); cmd.setInitrd(initrd); cmd.setKernel(kernel); cmd.setMac(nic.getMacAddress()); cmd.setTemplateUuid(template.getUuid()); Answer aws = _agentMgr.send(pxeVo.getHostId(), cmd); if (!aws.getResult()) { s_logger.warn("Unable to set host: " + dest.getHost().getId() + " to PXE boot because " + aws.getDetails()); return false; } return true; }
@Override public TemplateProfile prepareDelete(DeleteIsoCmd cmd) { Long templateId = cmd.getId(); Long userId = CallContext.current().getCallingUserId(); Account account = CallContext.current().getCallingAccount(); Long zoneId = cmd.getZoneId(); VMTemplateVO template = _tmpltDao.findById(templateId); if (template == null) { throw new InvalidParameterValueException("unable to find iso with id " + templateId); } userId = accountAndUserValidation(account, userId, null, template, "Unable to delete iso "); UserVO user = _userDao.findById(userId); if (user == null) { throw new InvalidParameterValueException("Please specify a valid user."); } if (template.getFormat() != ImageFormat.ISO) { throw new InvalidParameterValueException("Please specify a valid iso."); } return new TemplateProfile(userId, template, zoneId); }
@Override public TemplateProfile prepareDelete(DeleteTemplateCmd cmd) { Long templateId = cmd.getId(); Long userId = CallContext.current().getCallingUserId(); Account account = CallContext.current().getCallingAccount(); Long zoneId = cmd.getZoneId(); VMTemplateVO template = _tmpltDao.findById(templateId); if (template == null) { throw new InvalidParameterValueException("unable to find template with id " + templateId); } userId = accountAndUserValidation(account, userId, null, template, "Unable to delete template "); UserVO user = _userDao.findById(userId); if (user == null) { throw new InvalidParameterValueException("Please specify a valid user."); } if (template.getFormat() == ImageFormat.ISO) { throw new InvalidParameterValueException("Please specify a valid template."); } return new TemplateProfile(userId, template, zoneId); }
@Override public TemplateInfo getReadyBypassedTemplateOnPrimaryStore(long templateId, Long poolId, Long hostId) { VMTemplateVO templateVO = imageDataDao.findById(templateId); if (templateVO == null || !templateVO.isDirectDownload()) { return null; } Long pool = poolId; if (poolId == null) { //Get ISO from existing pool ref HostVO host = hostDao.findById(hostId); List<StoragePoolVO> pools = getStoragePoolsFromClusterOrZone(host.getClusterId(), host.getDataCenterId(), host.getHypervisorType()); List<VMTemplateStoragePoolVO> existingRefs = templatePoolDao.listByTemplateId(templateId); pool = getOneMatchingPoolIdFromRefs(existingRefs, pools); } if (pool == null) { throw new CloudRuntimeException("No storage pool found where to download template: " + templateId); } VMTemplateStoragePoolVO spoolRef = templatePoolDao.findByPoolTemplate(pool, templateId); if (spoolRef == null) { directDownloadManager.downloadTemplate(templateId, pool, hostId); } DataStore store = storeMgr.getDataStore(pool, DataStoreRole.Primary); return this.getTemplate(templateId, store); }
VMTemplateVO iso = _tmpltDao.findById(isoId); if (iso == null || iso.getRemoved() != null) { throw new InvalidParameterValueException("Unable to find an ISO with id " + isoId);
@Override public void createAsync(DataStore dataStore, DataObject data, AsyncCompletionCallback<CreateCmdResult> callback) { Long maxTemplateSizeInBytes = getMaxTemplateSizeInBytes(); VirtualMachineTemplate tmpl = _templateDao.findById(data.getId()); DataStore cacheStore = cacheManager.getCacheStorage(dataStore.getScope()); DownloadCommand dcmd = new DownloadCommand((TemplateObjectTO)(data.getTO()), maxTemplateSizeInBytes); dcmd.setCacheStore(cacheStore.getTO()); dcmd.setProxy(getHttpProxy()); EndPoint ep = _epSelector.select(data); if (ep == null) { String errMsg = "No remote endpoint to send command, check if host or ssvm is down?"; s_logger.error(errMsg); throw new CloudRuntimeException(errMsg); } CreateContext<CreateCmdResult> context = new CreateContext<CreateCmdResult>(callback, data); AsyncCallbackDispatcher<SwiftImageStoreDriverImpl, DownloadAnswer> caller = AsyncCallbackDispatcher.create(this); caller.setContext(context); if (data.getType() == DataObjectType.TEMPLATE) { caller.setCallback(caller.getTarget().createTemplateAsyncCallback(null, null)); } else if (data.getType() == DataObjectType.VOLUME) { caller.setCallback(caller.getTarget().createVolumeAsyncCallback(null, null)); } ep.sendMessageAsync(dcmd, caller); }
@Override @ActionEvent(eventType = EventTypes.EVENT_ISO_DELETE, eventDescription = "deleting iso", async = true) public boolean deleteIso(DeleteIsoCmd cmd) { Long templateId = cmd.getId(); Account caller = CallContext.current().getCallingAccount(); Long zoneId = cmd.getZoneId(); VMTemplateVO template = _tmpltDao.findById(templateId); if (template == null) { throw new InvalidParameterValueException("unable to find iso with id " + templateId); } _accountMgr.checkAccess(caller, AccessType.OperateEntry, true, template); if (template.getFormat() != ImageFormat.ISO) { throw new InvalidParameterValueException("Please specify a valid iso."); } // check if there is any VM using this ISO. if (!templateIsDeleteable(templateId)) { throw new InvalidParameterValueException("Unable to delete iso, as it's used by other vms"); } if (zoneId != null && (_dataStoreMgr.getImageStore(zoneId) == null)) { throw new InvalidParameterValueException("Failed to find a secondary storage store in the specified zone."); } TemplateAdapter adapter = getAdapter(template.getHypervisorType()); TemplateProfile profile = adapter.prepareDelete(cmd); boolean result = adapter.delete(profile); if (result) { return true; } else { throw new CloudRuntimeException("Failed to delete ISO"); } }
protected VMTemplateVO persistTemplate(TemplateProfile profile, VirtualMachineTemplate.State initialState) { List<Long> zoneIdList = profile.getZoneIdList(); VMTemplateVO template = new VMTemplateVO(profile.getTemplateId(), profile.getName(), profile.getFormat(), profile.isPublic(), profile.isFeatured(), profile.isExtractable(), profile.getTemplateType(), profile.getUrl(), profile.isRequiresHVM(), profile.getBits(), profile.getAccountId(), profile.getCheckSum(), profile.getDisplayText(), profile.isPasswordEnabled(), profile.getGuestOsId(), profile.isBootable(), profile.getHypervisorType(), profile.getTemplateTag(), profile.getDetails(), profile.isSshKeyEnabled(), profile.IsDynamicallyScalable(), profile.isDirectDownload()); template.setState(initialState); if (profile.isDirectDownload()) { template.setSize(profile.getSize()); } if (zoneIdList == null) { List<DataCenterVO> dcs = _dcDao.listAll(); if (dcs.isEmpty()) { throw new CloudRuntimeException("No zones are present in the system, can't add template"); } template.setCrossZones(true); for (DataCenterVO dc : dcs) { _tmpltDao.addTemplateToZone(template, dc.getId()); } } else { for (Long zoneId: zoneIdList) { _tmpltDao.addTemplateToZone(template, zoneId); } } return _tmpltDao.findById(template.getId()); }
VMTemplateVO template = _templateDao.findById(volume.getTemplateId()); if (template != null && template.getTemplateType() == Storage.TemplateType.SYSTEM) { throw new InvalidParameterValueException("VolumeId: " + volumeId + " is for System VM , Creating snapshot against System VM volumes is not supported");