volume = _volumeDao.findById(volumeId);
zoneId = volume.getDataCenterId();
}
DataStore store = this._dataStoreMgr.getImageStore(zoneId);
if (store == null) {
throw new CloudRuntimeException("cannot find an image store for zone " + zoneId);
}
AsyncCallFuture<TemplateApiResult> future = null;
if (snapshotId != null) {
SnapshotInfo snapInfo = this._snapshotFactory.getSnapshot(snapshotId, DataStoreRole.Image);
DataStore snapStore = snapInfo.getDataStore();
if ( snapStore != null ){
store = snapStore; // pick snapshot image store to create template
}
future = this._tmpltSvr.createTemplateFromSnapshotAsync(snapInfo, tmplInfo, store);
} else if (volumeId != null) {
VolumeInfo volInfo = this._volFactory.getVolume(volumeId);
future = this._tmpltSvr.createTemplateFromVolumeAsync(volInfo, tmplInfo, store);
} else {
throw new CloudRuntimeException("Creating private Template need to specify snapshotId or volumeId");
}
CommandResult result = null;
try {
result = future.get();
if (result.isFailed()) {
privateTemplate = null;
s_logger.debug("Failed to create template" + result.getResult());
throw new CloudRuntimeException("Failed to create template" + result.getResult());
}
VMTemplateZoneVO templateZone = new VMTemplateZoneVO(zoneId, templateId, new Date());
this._tmpltZoneDao.persist(templateZone);
privateTemplate = this._tmpltDao.findById(templateId);
if (snapshotId != null) {
//getting the prent volume
long parentVolumeId=_snapshotDao.findById(snapshotId).getVolumeId();
VolumeVO parentVolume = _volumeDao.findById(parentVolumeId);
if (parentVolume != null && parentVolume.getIsoId() != null) {
privateTemplate.setSourceTemplateId(parentVolume.getIsoId());
_tmpltDao.update(privateTemplate.getId(), privateTemplate);
}
}
else if (volumeId != null) {
VolumeVO parentVolume = _volumeDao.findById(volumeId);
if (parentVolume.getIsoId() != null) {
privateTemplate.setSourceTemplateId(parentVolume.getIsoId());
_tmpltDao.update(privateTemplate.getId(), privateTemplate);
}
}
TemplateDataStoreVO srcTmpltStore = this._tmplStoreDao.findByStoreTemplate(store.getId(), templateId);
UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_TEMPLATE_CREATE, privateTemplate.getAccountId(), zoneId,
privateTemplate.getId(), privateTemplate.getName(), null, privateTemplate.getSourceTemplateId(), srcTmpltStore.getPhysicalSize(), privateTemplate.getSize());
_usageEventDao.persist(usageEvent);
} catch (InterruptedException e) {
s_logger.debug("Failed to create template", e);
throw new CloudRuntimeException("Failed to create template", e);
} catch (ExecutionException e) {
s_logger.debug("Failed to create template", e);
throw new CloudRuntimeException("Failed to create template", e);
}
} finally {
/*if (snapshot != null && snapshot.getSwiftId() != null
&& secondaryStorageURL != null && zoneId != null
&& accountId != null && volumeId != null) {
_snapshotMgr.deleteSnapshotsForVolume(secondaryStorageURL,
zoneId, accountId, volumeId);
}*/
if (privateTemplate == null) {
Transaction txn = Transaction.currentTxn();
txn.start();
// template_store_ref entries should have been removed using our
// DataObject.processEvent command in case of failure, but clean
// it up here to avoid
// some leftovers which will cause removing template from
// vm_template table fail.
this._tmplStoreDao.deletePrimaryRecordsForTemplate(templateId);
// Remove the template_zone_ref record
this._tmpltZoneDao.deletePrimaryRecordsForTemplate(templateId);
// Remove the template record
this._tmpltDao.expunge(templateId);
// decrement resource count
if (accountId != null) {
_resourceLimitMgr.decrementResourceCount(accountId, ResourceType.template);
_resourceLimitMgr.decrementResourceCount(accountId, ResourceType.secondary_storage, new Long(volume != null ? volume.getSize()
: snapshot.getSize()));
}
txn.commit();
}
}
if (privateTemplate != null) {
return privateTemplate;
} else {
throw new CloudRuntimeException("Failed to create a template");
}
}