use of com.cloud.storage.DataStoreRole in project cloudstack by apache.
the class CloudStackImageStoreLifeCycleImpl method initialize.
@SuppressWarnings("unchecked")
@Override
public DataStore initialize(Map<String, Object> dsInfos) {
Long dcId = (Long) dsInfos.get("zoneId");
String url = (String) dsInfos.get("url");
String name = (String) dsInfos.get("name");
if (name == null) {
name = url;
}
String providerName = (String) dsInfos.get("providerName");
DataStoreRole role = (DataStoreRole) dsInfos.get("role");
Map<String, String> details = (Map<String, String>) dsInfos.get("details");
String logString = "";
if (url.contains("cifs")) {
logString = cleanPassword(url);
} else {
logString = StringUtils.cleanString(url);
}
s_logger.info("Trying to add a new data store at " + logString + " to data center " + dcId);
URI uri = null;
try {
uri = new URI(UriUtils.encodeURIComponent(url));
if (uri.getScheme() == null) {
throw new InvalidParameterValueException("uri.scheme is null " + StringUtils.cleanString(url) + ", add nfs:// (or cifs://) as a prefix");
} else if (uri.getScheme().equalsIgnoreCase("nfs")) {
if (uri.getHost() == null || uri.getHost().equalsIgnoreCase("") || uri.getPath() == null || uri.getPath().equalsIgnoreCase("")) {
throw new InvalidParameterValueException("Your host and/or path is wrong. Make sure it's of the format nfs://hostname/path");
}
} else if (uri.getScheme().equalsIgnoreCase("cifs")) {
// Don't validate against a URI encoded URI.
URI cifsUri = new URI(url);
String warnMsg = UriUtils.getCifsUriParametersProblems(cifsUri);
if (warnMsg != null) {
throw new InvalidParameterValueException(warnMsg);
}
}
} catch (URISyntaxException e) {
throw new InvalidParameterValueException(url + " is not a valid uri");
}
if (dcId == null) {
throw new InvalidParameterValueException("DataCenter id is null, and cloudstack default image store has to be associated with a data center");
}
Map<String, Object> imageStoreParameters = new HashMap<String, Object>();
imageStoreParameters.put("name", name);
imageStoreParameters.put("zoneId", dcId);
imageStoreParameters.put("url", url);
imageStoreParameters.put("protocol", uri.getScheme().toLowerCase());
// default cloudstack
imageStoreParameters.put("scope", ScopeType.ZONE);
// provider only
// supports zone-wide
// image store
imageStoreParameters.put("providerName", providerName);
imageStoreParameters.put("role", role);
ImageStoreVO ids = imageStoreHelper.createImageStore(imageStoreParameters, details);
return imageStoreMgr.getImageStore(ids.getId());
}
use of com.cloud.storage.DataStoreRole in project cloudstack by apache.
the class SwiftImageStoreLifeCycleImpl method initialize.
@Override
public DataStore initialize(Map<String, Object> dsInfos) {
Long dcId = (Long) dsInfos.get("zoneId");
String url = (String) dsInfos.get("url");
String name = (String) dsInfos.get("name");
ScopeType scope = (ScopeType) dsInfos.get("scope");
String providerName = (String) dsInfos.get("providerName");
DataStoreRole role = (DataStoreRole) dsInfos.get("role");
Map<String, String> details = (Map<String, String>) dsInfos.get("details");
s_logger.info("Trying to add a swift store at " + url + " in data center " + dcId);
// just need to insert an entry in DB
Map<String, Object> imageStoreParameters = new HashMap<String, Object>();
imageStoreParameters.put("name", name);
imageStoreParameters.put("zoneId", dcId);
imageStoreParameters.put("url", url);
imageStoreParameters.put("protocol", "http");
if (scope != null) {
imageStoreParameters.put("scope", scope);
} else {
imageStoreParameters.put("scope", ScopeType.REGION);
}
imageStoreParameters.put("providerName", providerName);
imageStoreParameters.put("role", role);
ImageStoreVO ids = imageStoreHelper.createImageStore(imageStoreParameters, details);
return imageStoreMgr.getImageStore(ids.getId());
}
use of com.cloud.storage.DataStoreRole in project cloudstack by apache.
the class ApiResponseHelper method createSnapshotResponse.
@Override
public SnapshotResponse createSnapshotResponse(Snapshot snapshot) {
SnapshotResponse snapshotResponse = new SnapshotResponse();
snapshotResponse.setId(snapshot.getUuid());
populateOwner(snapshotResponse, snapshot);
VolumeVO volume = findVolumeById(snapshot.getVolumeId());
String snapshotTypeStr = snapshot.getRecurringType().name();
snapshotResponse.setSnapshotType(snapshotTypeStr);
if (volume != null) {
snapshotResponse.setVolumeId(volume.getUuid());
snapshotResponse.setVolumeName(volume.getName());
snapshotResponse.setVolumeType(volume.getVolumeType().name());
DataCenter zone = ApiDBUtils.findZoneById(volume.getDataCenterId());
if (zone != null) {
snapshotResponse.setZoneId(zone.getUuid());
}
if (volume.getVolumeType() == Volume.Type.ROOT) {
//TODO combine lines and 489 into a join in the volume dao
VMInstanceVO instance = ApiDBUtils.findVMInstanceById(volume.getInstanceId());
if (instance != null) {
GuestOS guestOs = ApiDBUtils.findGuestOSById(instance.getGuestOSId());
if (guestOs != null) {
snapshotResponse.setOsTypeId(guestOs.getUuid());
snapshotResponse.setOsDisplayName(guestOs.getDisplayName());
}
}
}
}
snapshotResponse.setCreated(snapshot.getCreated());
snapshotResponse.setName(snapshot.getName());
snapshotResponse.setIntervalType(ApiDBUtils.getSnapshotIntervalTypes(snapshot.getId()));
snapshotResponse.setState(snapshot.getState());
snapshotResponse.setLocationType(ApiDBUtils.getSnapshotLocationType(snapshot.getId()));
SnapshotInfo snapshotInfo = null;
if (snapshot instanceof SnapshotInfo) {
snapshotInfo = (SnapshotInfo) snapshot;
} else {
DataStoreRole dataStoreRole = getDataStoreRole(snapshot, _snapshotStoreDao, _dataStoreMgr);
snapshotInfo = snapshotfactory.getSnapshot(snapshot.getId(), dataStoreRole);
}
if (snapshotInfo == null) {
s_logger.debug("Unable to find info for image store snapshot with uuid " + snapshot.getUuid());
snapshotResponse.setRevertable(false);
} else {
snapshotResponse.setRevertable(snapshotInfo.isRevertable());
snapshotResponse.setPhysicaSize(snapshotInfo.getPhysicalSize());
}
// set tag information
List<? extends ResourceTag> tags = ApiDBUtils.listByResourceTypeAndId(ResourceObjectType.Snapshot, snapshot.getId());
List<ResourceTagResponse> tagResponses = new ArrayList<ResourceTagResponse>();
for (ResourceTag tag : tags) {
ResourceTagResponse tagResponse = createResourceTagResponse(tag, true);
CollectionUtils.addIgnoreNull(tagResponses, tagResponse);
}
snapshotResponse.setTags(tagResponses);
snapshotResponse.setObjectName("snapshot");
return snapshotResponse;
}
use of com.cloud.storage.DataStoreRole in project cloudstack by apache.
the class DefaultEndPointSelector method moveBetweenCacheAndImage.
protected boolean moveBetweenCacheAndImage(DataStore srcStore, DataStore destStore) {
DataStoreRole srcRole = srcStore.getRole();
DataStoreRole destRole = destStore.getRole();
if (srcRole == DataStoreRole.Image && destRole == DataStoreRole.ImageCache || srcRole == DataStoreRole.ImageCache && destRole == DataStoreRole.Image) {
return true;
} else {
return false;
}
}
use of com.cloud.storage.DataStoreRole in project cloudstack by apache.
the class TemplateManagerImpl method createPrivateTemplate.
@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_TEMPLATE_CREATE, eventDescription = "creating template", async = true)
public VirtualMachineTemplate createPrivateTemplate(CreateTemplateCmd command) throws CloudRuntimeException {
final long templateId = command.getEntityId();
Long volumeId = command.getVolumeId();
Long snapshotId = command.getSnapshotId();
VMTemplateVO privateTemplate = null;
final Long accountId = CallContext.current().getCallingAccountId();
SnapshotVO snapshot = null;
VolumeVO volume = null;
try {
TemplateInfo tmplInfo = _tmplFactory.getTemplate(templateId, DataStoreRole.Image);
long zoneId = 0;
if (snapshotId != null) {
snapshot = _snapshotDao.findById(snapshotId);
zoneId = snapshot.getDataCenterId();
} else if (volumeId != null) {
volume = _volumeDao.findById(volumeId);
zoneId = volume.getDataCenterId();
}
DataStore store = _dataStoreMgr.getImageStore(zoneId);
if (store == null) {
throw new CloudRuntimeException("cannot find an image store for zone " + zoneId);
}
AsyncCallFuture<TemplateApiResult> future = null;
if (snapshotId != null) {
DataStoreRole dataStoreRole = ApiResponseHelper.getDataStoreRole(snapshot, _snapshotStoreDao, _dataStoreMgr);
SnapshotInfo snapInfo = _snapshotFactory.getSnapshot(snapshotId, dataStoreRole);
if (dataStoreRole == DataStoreRole.Image) {
if (snapInfo == null) {
snapInfo = _snapshotFactory.getSnapshot(snapshotId, DataStoreRole.Primary);
if (snapInfo == null) {
throw new CloudRuntimeException("Cannot find snapshot " + snapshotId);
}
// We need to copy the snapshot onto secondary.
SnapshotStrategy snapshotStrategy = _storageStrategyFactory.getSnapshotStrategy(snapshot, SnapshotOperation.BACKUP);
snapshotStrategy.backupSnapshot(snapInfo);
// Attempt to grab it again.
snapInfo = _snapshotFactory.getSnapshot(snapshotId, dataStoreRole);
if (snapInfo == null) {
throw new CloudRuntimeException("Cannot find snapshot " + snapshotId + " on secondary and could not create backup");
}
}
DataStore snapStore = snapInfo.getDataStore();
if (snapStore != null) {
// pick snapshot image store to create template
store = snapStore;
}
}
future = _tmpltSvr.createTemplateFromSnapshotAsync(snapInfo, tmplInfo, store);
} else if (volumeId != null) {
VolumeInfo volInfo = _volFactory.getVolume(volumeId);
future = _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());
}
// create entries in template_zone_ref table
if (_dataStoreMgr.isRegionStore(store)) {
// template created on region store
_tmpltSvr.associateTemplateToZone(templateId, null);
} else {
VMTemplateZoneVO templateZone = new VMTemplateZoneVO(zoneId, templateId, new Date());
_tmpltZoneDao.persist(templateZone);
}
privateTemplate = _tmpltDao.findById(templateId);
TemplateDataStoreVO srcTmpltStore = _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 (privateTemplate == null) {
final VolumeVO volumeFinal = volume;
final SnapshotVO snapshotFinal = snapshot;
Transaction.execute(new TransactionCallbackNoReturn() {
@Override
public void doInTransactionWithoutResult(TransactionStatus status) {
// 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.
_tmplStoreDao.deletePrimaryRecordsForTemplate(templateId);
// Remove the template_zone_ref record
_tmpltZoneDao.deletePrimaryRecordsForTemplate(templateId);
// Remove the template record
_tmpltDao.expunge(templateId);
// decrement resource count
if (accountId != null) {
_resourceLimitMgr.decrementResourceCount(accountId, ResourceType.template);
_resourceLimitMgr.decrementResourceCount(accountId, ResourceType.secondary_storage, new Long(volumeFinal != null ? volumeFinal.getSize() : snapshotFinal.getSize()));
}
}
});
}
}
if (privateTemplate != null) {
return privateTemplate;
} else {
throw new CloudRuntimeException("Failed to create a template");
}
}
Aggregations