use of com.cloud.engine.subsystem.api.storage.TemplateInfo in project cosmic by MissionCriticalCloud.
the class TemplateServiceImpl method createTemplateAsync.
@Override
public void createTemplateAsync(final TemplateInfo template, final DataStore store, final AsyncCompletionCallback<TemplateApiResult> callback) {
// persist template_store_ref entry
final TemplateObject templateOnStore = (TemplateObject) store.create(template);
// update template_store_ref and template state
try {
templateOnStore.processEvent(ObjectInDataStoreStateMachine.Event.CreateOnlyRequested);
} catch (final Exception e) {
final TemplateApiResult result = new TemplateApiResult(templateOnStore);
result.setResult(e.toString());
result.setSuccess(false);
if (callback != null) {
callback.complete(result);
}
return;
}
try {
final TemplateOpContext<TemplateApiResult> context = new TemplateOpContext<>(callback, templateOnStore, null);
final AsyncCallbackDispatcher<TemplateServiceImpl, CreateCmdResult> caller = AsyncCallbackDispatcher.create(this);
caller.setCallback(caller.getTarget().createTemplateCallback(null, null)).setContext(context);
store.getDriver().createAsync(store, templateOnStore, caller);
} catch (final CloudRuntimeException ex) {
// clean up already persisted template_store_ref entry in case of createTemplateCallback is never called
final TemplateDataStoreVO templateStoreVO = _vmTemplateStoreDao.findByStoreTemplate(store.getId(), template.getId());
if (templateStoreVO != null) {
final TemplateInfo tmplObj = _templateFactory.getTemplate(template, store);
tmplObj.processEvent(ObjectInDataStoreStateMachine.Event.OperationFailed);
}
final TemplateApiResult result = new TemplateApiResult(template);
result.setResult(ex.getMessage());
if (callback != null) {
callback.complete(result);
}
}
}
use of com.cloud.engine.subsystem.api.storage.TemplateInfo in project cosmic by MissionCriticalCloud.
the class HypervisorTemplateAdapter method createTemplateAsyncCallBack.
protected Void createTemplateAsyncCallBack(final AsyncCallbackDispatcher<HypervisorTemplateAdapter, TemplateApiResult> callback, final CreateTemplateContext<TemplateApiResult> context) {
final TemplateApiResult result = callback.getResult();
final TemplateInfo template = context.template;
if (result.isSuccess()) {
final VMTemplateVO tmplt = _tmpltDao.findById(template.getId());
// need to grant permission for public templates
if (tmplt.isPublicTemplate()) {
_messageBus.publish(_name, TemplateManager.MESSAGE_REGISTER_PUBLIC_TEMPLATE_EVENT, PublishScope.LOCAL, tmplt.getId());
}
final long accountId = tmplt.getAccountId();
if (template.getSize() != null) {
_resourceLimitMgr.incrementResourceCount(accountId, ResourceType.secondary_storage, template.getSize());
}
}
return null;
}
use of com.cloud.engine.subsystem.api.storage.TemplateInfo in project cosmic by MissionCriticalCloud.
the class HypervisorTemplateAdapter method create.
@Override
public VMTemplateVO create(final TemplateProfile profile) {
// 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.
final VMTemplateVO template = persistTemplate(profile, State.Active);
if (template == null) {
throw new CloudRuntimeException("Unable to persist the template " + profile.getTemplate());
}
// find all eligible image stores for this zone scope
final List<DataStore> imageStores = storeMgr.getImageStoresByScope(new ZoneScope(profile.getZoneId()));
if (imageStores == null || imageStores.size() == 0) {
throw new CloudRuntimeException("Unable to find image store to download template " + profile.getTemplate());
}
final Set<Long> zoneSet = new HashSet<>();
// For private templates choose a random store. TODO - Have a better algorithm based on size, no. of objects, load etc.
Collections.shuffle(imageStores);
for (final DataStore imageStore : imageStores) {
// skip data stores for a disabled zone
final Long zoneId = imageStore.getScope().getScopeId();
if (zoneId != null) {
final DataCenterVO zone = _dcDao.findById(zoneId);
if (zone == null) {
s_logger.warn("Unable to find zone by id " + zoneId + ", so skip downloading template to its image store " + imageStore.getId());
continue;
}
// Check if zone is disabled
if (AllocationState.Disabled == zone.getAllocationState()) {
s_logger.info("Zone " + zoneId + " is disabled, so skip downloading template to its image store " + imageStore.getId());
continue;
}
// We want to download private template to one of the image store in a zone
if (isPrivateTemplate(template) && zoneSet.contains(zoneId)) {
continue;
} else {
zoneSet.add(zoneId);
}
}
final TemplateInfo tmpl = imageFactory.getTemplate(template.getId(), imageStore);
final CreateTemplateContext<TemplateApiResult> context = new CreateTemplateContext<>(null, tmpl);
final AsyncCallbackDispatcher<HypervisorTemplateAdapter, TemplateApiResult> caller = AsyncCallbackDispatcher.create(this);
caller.setCallback(caller.getTarget().createTemplateAsyncCallBack(null, null));
caller.setContext(context);
imageService.createTemplateAsync(tmpl, imageStore, caller);
}
_resourceLimitMgr.incrementResourceCount(profile.getAccountId(), ResourceType.template);
return template;
}
use of com.cloud.engine.subsystem.api.storage.TemplateInfo in project cosmic by MissionCriticalCloud.
the class TemplateManagerImpl method prepareIso.
// for ISO, we need to consider whether to copy to cache storage or not if it is not on NFS, since our hypervisor resource always assumes that they are in NFS
@Override
public TemplateInfo prepareIso(final long isoId, final long dcId) {
final TemplateInfo tmplt = _tmplFactory.getTemplate(isoId, DataStoreRole.Image, dcId);
if (tmplt == null || tmplt.getFormat() != ImageFormat.ISO) {
s_logger.warn("ISO: " + isoId + " does not exist in vm_template table");
return null;
}
if (tmplt.getDataStore() != null && !(tmplt.getDataStore().getTO() instanceof NfsTO)) {
// if it is s3, need to download into cache storage first
final Scope destScope = new ZoneScope(dcId);
final TemplateInfo cacheData = (TemplateInfo) cacheMgr.createCacheObject(tmplt, destScope);
if (cacheData == null) {
s_logger.error("Failed in copy iso from S3 to cache storage");
return null;
}
return cacheData;
} else {
return tmplt;
}
}
use of com.cloud.engine.subsystem.api.storage.TemplateInfo in project cosmic by MissionCriticalCloud.
the class TemplateManagerImpl method prepareIsoForVmProfile.
@Override
public void prepareIsoForVmProfile(final VirtualMachineProfile profile) {
final UserVmVO vm = _userVmDao.findById(profile.getId());
if (vm.getIsoId() != null) {
final TemplateInfo template = prepareIso(vm.getIsoId(), vm.getDataCenterId());
if (template == null) {
s_logger.error("Failed to prepare ISO on secondary or cache storage");
throw new CloudRuntimeException("Failed to prepare ISO on secondary or cache storage");
}
if (template.isBootable()) {
profile.setBootLoaderType(BootloaderType.CD);
}
final GuestOSVO guestOS = _guestOSDao.findById(template.getGuestOSId());
String displayName = null;
if (guestOS != null) {
displayName = guestOS.getDisplayName();
}
final TemplateObjectTO iso = (TemplateObjectTO) template.getTO();
iso.setGuestOsType(displayName);
final DiskTO disk = new DiskTO(iso, 3L, null, Volume.Type.ISO);
profile.addDisk(disk);
} else {
final TemplateObjectTO iso = new TemplateObjectTO();
iso.setFormat(ImageFormat.ISO);
final DiskTO disk = new DiskTO(iso, 3L, null, Volume.Type.ISO);
profile.addDisk(disk);
}
}
Aggregations