Search in sources :

Example 31 with TemplateInfo

use of org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo in project cloudstack by apache.

the class SolidFirePrimaryDataStoreDriver method createSolidFireVolume.

private SolidFireUtil.SolidFireVolume createSolidFireVolume(SolidFireUtil.SolidFireConnection sfConnection, DataObject dataObject, long sfAccountId) {
    long storagePoolId = dataObject.getDataStore().getId();
    final Long minIops;
    final Long maxIops;
    final Long volumeSize;
    final String volumeName;
    final Map<String, String> mapAttributes;
    if (dataObject.getType() == DataObjectType.VOLUME) {
        VolumeInfo volumeInfo = (VolumeInfo) dataObject;
        minIops = volumeInfo.getMinIops();
        maxIops = volumeInfo.getMaxIops();
        volumeSize = getDataObjectSizeIncludingHypervisorSnapshotReserve(volumeInfo, storagePoolDao.findById(storagePoolId));
        volumeName = volumeInfo.getName();
        mapAttributes = getVolumeAttributes(volumeInfo);
    } else if (dataObject.getType() == DataObjectType.TEMPLATE) {
        TemplateInfo templateInfo = (TemplateInfo) dataObject;
        minIops = MIN_IOPS_FOR_TEMPLATE_VOLUME;
        maxIops = MAX_IOPS_FOR_TEMPLATE_VOLUME;
        volumeSize = getDataObjectSizeIncludingHypervisorSnapshotReserve(templateInfo, storagePoolDao.findById(storagePoolId));
        volumeName = templateInfo.getUniqueName();
        mapAttributes = getTemplateAttributes(templateInfo);
    } else {
        throw new CloudRuntimeException("Invalid type passed to createSolidFireVolume: " + dataObject.getType());
    }
    final Iops iops = getIops(minIops, maxIops, storagePoolId);
    long sfVolumeId = SolidFireUtil.createVolume(sfConnection, SolidFireUtil.getSolidFireVolumeName(volumeName), sfAccountId, volumeSize, true, mapAttributes, iops.getMinIops(), iops.getMaxIops(), iops.getBurstIops());
    return SolidFireUtil.getVolume(sfConnection, sfVolumeId);
}
Also used : TemplateInfo(org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo)

Example 32 with TemplateInfo

use of org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo in project cloudstack by apache.

the class VolumeServiceImpl method createManagedVolumeCopyTemplateAsync.

private void createManagedVolumeCopyTemplateAsync(VolumeInfo volumeInfo, PrimaryDataStore primaryDataStore, TemplateInfo srcTemplateInfo, Host destHost, AsyncCallFuture<VolumeApiResult> future) {
    try {
        // Create a volume on managed storage.
        TemplateInfo destTemplateInfo = (TemplateInfo) primaryDataStore.create(srcTemplateInfo, false);
        AsyncCallFuture<VolumeApiResult> createVolumeFuture = createVolumeAsync(volumeInfo, primaryDataStore);
        VolumeApiResult createVolumeResult = createVolumeFuture.get();
        if (createVolumeResult.isFailed()) {
            throw new CloudRuntimeException("Creation of a volume failed: " + createVolumeResult.getResult());
        }
        // Refresh the volume info from the DB.
        volumeInfo = volFactory.getVolume(volumeInfo.getId(), primaryDataStore);
        ManagedCreateBaseImageContext<CreateCmdResult> context = new ManagedCreateBaseImageContext<CreateCmdResult>(null, volumeInfo, primaryDataStore, srcTemplateInfo, future);
        AsyncCallbackDispatcher<VolumeServiceImpl, CopyCommandResult> caller = AsyncCallbackDispatcher.create(this);
        caller.setCallback(caller.getTarget().managedCopyBaseImageCallback(null, null)).setContext(context);
        Map<String, String> details = new HashMap<String, String>();
        details.put(PrimaryDataStore.MANAGED, Boolean.TRUE.toString());
        details.put(PrimaryDataStore.STORAGE_HOST, primaryDataStore.getHostAddress());
        details.put(PrimaryDataStore.STORAGE_PORT, String.valueOf(primaryDataStore.getPort()));
        // for managed storage, the storage repository (XenServer) or datastore (ESX) name is based off of the iScsiName property of a volume
        details.put(PrimaryDataStore.MANAGED_STORE_TARGET, volumeInfo.get_iScsiName());
        details.put(PrimaryDataStore.MANAGED_STORE_TARGET_ROOT_VOLUME, volumeInfo.getName());
        details.put(PrimaryDataStore.VOLUME_SIZE, String.valueOf(volumeInfo.getSize()));
        ChapInfo chapInfo = getChapInfo(volumeInfo, primaryDataStore);
        if (chapInfo != null) {
            details.put(PrimaryDataStore.CHAP_INITIATOR_USERNAME, chapInfo.getInitiatorUsername());
            details.put(PrimaryDataStore.CHAP_INITIATOR_SECRET, chapInfo.getInitiatorSecret());
            details.put(PrimaryDataStore.CHAP_TARGET_USERNAME, chapInfo.getTargetUsername());
            details.put(PrimaryDataStore.CHAP_TARGET_SECRET, chapInfo.getTargetSecret());
        }
        primaryDataStore.setDetails(details);
        grantAccess(volumeInfo, destHost, primaryDataStore);
        try {
            motionSrv.copyAsync(srcTemplateInfo, destTemplateInfo, destHost, caller);
        } finally {
            revokeAccess(volumeInfo, destHost, primaryDataStore);
        }
    } catch (Throwable t) {
        String errMsg = t.toString();
        volumeInfo.processEvent(Event.DestroyRequested);
        try {
            AsyncCallFuture<VolumeApiResult> expungeVolumeFuture = expungeVolumeAsync(volumeInfo);
            VolumeApiResult expungeVolumeResult = expungeVolumeFuture.get();
            if (expungeVolumeResult.isFailed()) {
                errMsg += " : Failed to expunge a volume that was created";
            }
        } catch (Exception ex) {
            errMsg += " : " + ex.getMessage();
        }
        VolumeApiResult result = new VolumeApiResult(volumeInfo);
        result.setResult(errMsg);
        future.complete(result);
    }
}
Also used : HashMap(java.util.HashMap) ChapInfo(org.apache.cloudstack.engine.subsystem.api.storage.ChapInfo) CreateCmdResult(org.apache.cloudstack.engine.subsystem.api.storage.CreateCmdResult) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) AsyncCallFuture(org.apache.cloudstack.framework.async.AsyncCallFuture) TemplateInfo(org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) CopyCommandResult(org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult)

Example 33 with TemplateInfo

use of org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo in project cloudstack by apache.

the class VolumeServiceImpl method createManagedStorageVolumeFromTemplateAsync.

@Override
public AsyncCallFuture<VolumeApiResult> createManagedStorageVolumeFromTemplateAsync(VolumeInfo volumeInfo, long destDataStoreId, TemplateInfo srcTemplateInfo, long destHostId) {
    PrimaryDataStore destPrimaryDataStore = dataStoreMgr.getPrimaryDataStore(destDataStoreId);
    Host destHost = _hostDao.findById(destHostId);
    if (destHost == null) {
        throw new CloudRuntimeException("Destination host should not be null.");
    }
    Boolean storageCanCloneVolume = new Boolean(destPrimaryDataStore.getDriver().getCapabilities().get(DataStoreCapabilities.CAN_CREATE_VOLUME_FROM_VOLUME.toString()));
    boolean computeZoneSupportsResign = computeZoneSupportsResign(destHost.getDataCenterId(), destHost.getHypervisorType());
    AsyncCallFuture<VolumeApiResult> future = new AsyncCallFuture<>();
    if (storageCanCloneVolume && computeZoneSupportsResign) {
        s_logger.debug("Storage " + destDataStoreId + " can support cloning using a cached template and host cluster can perform UUID resigning.");
        TemplateInfo templateOnPrimary = destPrimaryDataStore.getTemplate(srcTemplateInfo.getId());
        if (templateOnPrimary == null) {
            templateOnPrimary = createManagedTemplateVolume(srcTemplateInfo, destPrimaryDataStore);
            if (templateOnPrimary == null) {
                throw new CloudRuntimeException("Failed to create template " + srcTemplateInfo.getUniqueName() + " on primary storage: " + destDataStoreId);
            }
        }
        // Copy the template to the template volume.
        VMTemplateStoragePoolVO templatePoolRef = _tmpltPoolDao.findByPoolTemplate(destPrimaryDataStore.getId(), templateOnPrimary.getId());
        if (templatePoolRef == null) {
            throw new CloudRuntimeException("Failed to find template " + srcTemplateInfo.getUniqueName() + " in storage pool " + destPrimaryDataStore.getId());
        }
        if (templatePoolRef.getDownloadState() == Status.NOT_DOWNLOADED) {
            copyTemplateToManagedTemplateVolume(srcTemplateInfo, templateOnPrimary, templatePoolRef, destPrimaryDataStore, destHost);
        }
        // We have a template on primary storage. Clone it to new volume.
        s_logger.debug("Creating a clone from template on primary storage " + destDataStoreId);
        createManagedVolumeCloneTemplateAsync(volumeInfo, templateOnPrimary, destPrimaryDataStore, future);
    } else {
        s_logger.debug("Primary storage does not support cloning or no support for UUID resigning on the host side; copying the template normally");
        createManagedVolumeCopyTemplateAsync(volumeInfo, destPrimaryDataStore, srcTemplateInfo, destHost, future);
    }
    return future;
}
Also used : VMTemplateStoragePoolVO(com.cloud.storage.VMTemplateStoragePoolVO) AsyncCallFuture(org.apache.cloudstack.framework.async.AsyncCallFuture) TemplateInfo(org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Host(com.cloud.host.Host) PrimaryDataStore(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore)

Example 34 with TemplateInfo

use of org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo in project cloudstack by apache.

the class VolumeServiceImpl method createManagedTemplateVolume.

/**
     * Creates a template volume on managed storage, which will be used for creating ROOT volumes by cloning.
     *
     * @param srcTemplateInfo Source template on secondary storage
     * @param destPrimaryDataStore Managed storage on which we need to create the volume
     */
private TemplateInfo createManagedTemplateVolume(TemplateInfo srcTemplateInfo, PrimaryDataStore destPrimaryDataStore) {
    // create a template volume on primary storage
    AsyncCallFuture<VolumeApiResult> createTemplateFuture = new AsyncCallFuture<>();
    TemplateInfo templateOnPrimary = (TemplateInfo) destPrimaryDataStore.create(srcTemplateInfo);
    VMTemplateStoragePoolVO templatePoolRef = _tmpltPoolDao.findByPoolTemplate(destPrimaryDataStore.getId(), templateOnPrimary.getId());
    if (templatePoolRef == null) {
        throw new CloudRuntimeException("Failed to find template " + srcTemplateInfo.getUniqueName() + " in storage pool " + destPrimaryDataStore.getId());
    }
    // At this point, we have an entry in the DB that points to our cached template.
    // We need to lock it as there may be other VMs that may get started using the same template.
    // We want to avoid having to create multiple cache copies of the same template.
    int storagePoolMaxWaitSeconds = NumbersUtil.parseInt(configDao.getValue(Config.StoragePoolMaxWaitSeconds.key()), 3600);
    long templatePoolRefId = templatePoolRef.getId();
    templatePoolRef = _tmpltPoolDao.acquireInLockTable(templatePoolRefId, storagePoolMaxWaitSeconds);
    if (templatePoolRef == null) {
        throw new CloudRuntimeException("Unable to acquire lock on VMTemplateStoragePool: " + templatePoolRefId);
    }
    // Template already exists
    if (templatePoolRef.getState() == ObjectInDataStoreStateMachine.State.Ready) {
        _tmpltPoolDao.releaseFromLockTable(templatePoolRefId);
        return templateOnPrimary;
    }
    try {
        // create a cache volume on the back-end
        templateOnPrimary.processEvent(Event.CreateOnlyRequested);
        CreateVolumeContext<CreateCmdResult> createContext = new CreateVolumeContext<>(null, templateOnPrimary, createTemplateFuture);
        AsyncCallbackDispatcher<VolumeServiceImpl, CreateCmdResult> createCaller = AsyncCallbackDispatcher.create(this);
        createCaller.setCallback(createCaller.getTarget().createManagedTemplateImageCallback(null, null)).setContext(createContext);
        destPrimaryDataStore.getDriver().createAsync(destPrimaryDataStore, templateOnPrimary, createCaller);
        VolumeApiResult result = createTemplateFuture.get();
        if (result.isFailed()) {
            String errMesg = result.getResult();
            throw new CloudRuntimeException("Unable to create template " + templateOnPrimary.getId() + " on primary storage " + destPrimaryDataStore.getId() + ":" + errMesg);
        }
    } catch (Throwable e) {
        s_logger.debug("Failed to create template volume on storage", e);
        templateOnPrimary.processEvent(Event.OperationFailed);
        throw new CloudRuntimeException(e.getMessage());
    } finally {
        _tmpltPoolDao.releaseFromLockTable(templatePoolRefId);
    }
    return templateOnPrimary;
}
Also used : CreateCmdResult(org.apache.cloudstack.engine.subsystem.api.storage.CreateCmdResult) EndPoint(org.apache.cloudstack.engine.subsystem.api.storage.EndPoint) RemoteHostEndPoint(org.apache.cloudstack.storage.RemoteHostEndPoint) VMTemplateStoragePoolVO(com.cloud.storage.VMTemplateStoragePoolVO) AsyncCallFuture(org.apache.cloudstack.framework.async.AsyncCallFuture) TemplateInfo(org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 35 with TemplateInfo

use of org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo in project cloudstack by apache.

the class S3TemplateTest method registerTemplate.

@Test(priority = 1)
public void registerTemplate() {
    TemplateInfo template = templateFactory.getTemplate(templateId, DataStoreRole.Image);
    DataStore store = dataStoreMgr.getImageStore(dcId);
    AsyncCallFuture<TemplateApiResult> future = new AsyncCallFuture<TemplateApiResult>();
    templateSvr.createTemplateAsync(template, store, future);
    try {
        TemplateApiResult result = future.get();
        assertTrue(result.isSuccess(), "failed to register template: " + result.getResult());
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        assertTrue(false, e.getMessage());
    } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        assertTrue(false, e.getMessage());
    }
}
Also used : AsyncCallFuture(org.apache.cloudstack.framework.async.AsyncCallFuture) TemplateInfo(org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) ExecutionException(java.util.concurrent.ExecutionException) TemplateApiResult(org.apache.cloudstack.engine.subsystem.api.storage.TemplateService.TemplateApiResult) Test(org.testng.annotations.Test)

Aggregations

TemplateInfo (org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo)49 DataStore (org.apache.cloudstack.engine.subsystem.api.storage.DataStore)25 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)19 VMTemplateVO (com.cloud.storage.VMTemplateVO)17 TemplateDataStoreVO (org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO)15 ExecutionException (java.util.concurrent.ExecutionException)13 TemplateApiResult (org.apache.cloudstack.engine.subsystem.api.storage.TemplateService.TemplateApiResult)13 VolumeInfo (org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo)11 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)9 PrimaryDataStore (org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore)9 AsyncCallFuture (org.apache.cloudstack.framework.async.AsyncCallFuture)9 Test (org.testng.annotations.Test)9 ZoneScope (org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope)8 DB (com.cloud.utils.db.DB)7 NoTransitionException (com.cloud.utils.fsm.NoTransitionException)6 CreateCmdResult (org.apache.cloudstack.engine.subsystem.api.storage.CreateCmdResult)6 SnapshotInfo (org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo)6 VMTemplateStoragePoolVO (com.cloud.storage.VMTemplateStoragePoolVO)5 HashSet (java.util.HashSet)5 DataObject (org.apache.cloudstack.engine.subsystem.api.storage.DataObject)5