Search in sources :

Example 1 with DataStoreRole

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());
}
Also used : DataStoreRole(com.cloud.storage.DataStoreRole) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) HashMap(java.util.HashMap) URISyntaxException(java.net.URISyntaxException) ImageStoreVO(org.apache.cloudstack.storage.datastore.db.ImageStoreVO) HashMap(java.util.HashMap) Map(java.util.Map) URI(java.net.URI)

Example 2 with DataStoreRole

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());
}
Also used : ScopeType(com.cloud.storage.ScopeType) DataStoreRole(com.cloud.storage.DataStoreRole) HashMap(java.util.HashMap) ImageStoreVO(org.apache.cloudstack.storage.datastore.db.ImageStoreVO) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with DataStoreRole

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;
}
Also used : ArrayList(java.util.ArrayList) VMInstanceVO(com.cloud.vm.VMInstanceVO) DataStoreRole(com.cloud.storage.DataStoreRole) SnapshotInfo(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo) DataCenter(com.cloud.dc.DataCenter) ResourceTag(com.cloud.server.ResourceTag) VolumeVO(com.cloud.storage.VolumeVO) SnapshotResponse(org.apache.cloudstack.api.response.SnapshotResponse) VMSnapshotResponse(org.apache.cloudstack.api.response.VMSnapshotResponse) ResourceTagResponse(org.apache.cloudstack.api.response.ResourceTagResponse) GuestOS(com.cloud.storage.GuestOS)

Example 4 with DataStoreRole

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;
    }
}
Also used : DataStoreRole(com.cloud.storage.DataStoreRole)

Example 5 with DataStoreRole

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");
    }
}
Also used : VMTemplateZoneVO(com.cloud.storage.VMTemplateZoneVO) VMTemplateVO(com.cloud.storage.VMTemplateVO) TransactionStatus(com.cloud.utils.db.TransactionStatus) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) UsageEventVO(com.cloud.event.UsageEventVO) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) TemplateDataStoreVO(org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO) TemplateApiResult(org.apache.cloudstack.engine.subsystem.api.storage.TemplateService.TemplateApiResult) Date(java.util.Date) CommandResult(org.apache.cloudstack.storage.command.CommandResult) DataStoreRole(com.cloud.storage.DataStoreRole) TemplateInfo(org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo) SnapshotInfo(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo) SnapshotVO(com.cloud.storage.SnapshotVO) VolumeVO(com.cloud.storage.VolumeVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) PrimaryDataStore(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) ExecutionException(java.util.concurrent.ExecutionException) SnapshotStrategy(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotStrategy) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Aggregations

DataStoreRole (com.cloud.storage.DataStoreRole)11 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 SnapshotInfo (org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo)4 ImageStoreVO (org.apache.cloudstack.storage.datastore.db.ImageStoreVO)4 VolumeVO (com.cloud.storage.VolumeVO)3 DB (com.cloud.utils.db.DB)3 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)3 SnapshotStrategy (org.apache.cloudstack.engine.subsystem.api.storage.SnapshotStrategy)3 DataCenter (com.cloud.dc.DataCenter)2 StorageUnavailableException (com.cloud.exception.StorageUnavailableException)2 ScopeType (com.cloud.storage.ScopeType)2 Account (com.cloud.user.Account)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 ExecutionException (java.util.concurrent.ExecutionException)2 ConfigurationException (javax.naming.ConfigurationException)2 DataStore (org.apache.cloudstack.engine.subsystem.api.storage.DataStore)2 PrimaryDataStore (org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore)2