Search in sources :

Example 61 with DataStore

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

the class TemplateManagerImpl method getImageStoreByTemplate.

// find image store where this template is located
@Override
public List<DataStore> getImageStoreByTemplate(long templateId, Long zoneId) {
    // find all eligible image stores for this zone scope
    List<DataStore> imageStores = _dataStoreMgr.getImageStoresByScope(new ZoneScope(zoneId));
    if (imageStores == null || imageStores.size() == 0) {
        return null;
    }
    List<DataStore> stores = new ArrayList<DataStore>();
    for (DataStore store : imageStores) {
        // check if the template is stored there
        List<TemplateDataStoreVO> storeTmpl = _tmplStoreDao.listByTemplateStore(templateId, store.getId());
        if (storeTmpl != null && storeTmpl.size() > 0) {
            stores.add(store);
        }
    }
    return stores;
}
Also used : ZoneScope(org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope) PrimaryDataStore(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) ArrayList(java.util.ArrayList) TemplateDataStoreVO(org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO)

Example 62 with DataStore

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

the class TemplateManagerImpl method extract.

private String extract(Account caller, Long templateId, String url, Long zoneId, String mode, Long eventId, boolean isISO) {
    String desc = Upload.Type.TEMPLATE.toString();
    if (isISO) {
        desc = Upload.Type.ISO.toString();
    }
    if (!_accountMgr.isRootAdmin(caller.getId()) && _disableExtraction) {
        throw new PermissionDeniedException("Extraction has been disabled by admin");
    }
    VMTemplateVO template = _tmpltDao.findById(templateId);
    if (template == null || template.getRemoved() != null) {
        throw new InvalidParameterValueException("Unable to find " + desc + " with id " + templateId);
    }
    if (template.getTemplateType() == Storage.TemplateType.SYSTEM) {
        throw new InvalidParameterValueException("Unable to extract the " + desc + " " + template.getName() + " as it is a default System template");
    } else if (template.getTemplateType() == Storage.TemplateType.PERHOST) {
        throw new InvalidParameterValueException("Unable to extract the " + desc + " " + template.getName() + " as it resides on host and not on SSVM");
    }
    if (isISO) {
        if (template.getFormat() != ImageFormat.ISO) {
            throw new InvalidParameterValueException("Unsupported format, could not extract the ISO");
        }
    } else {
        if (template.getFormat() == ImageFormat.ISO) {
            throw new InvalidParameterValueException("Unsupported format, could not extract the template");
        }
    }
    if (zoneId != null && _dcDao.findById(zoneId) == null) {
        throw new IllegalArgumentException("Please specify a valid zone.");
    }
    if (!_accountMgr.isRootAdmin(caller.getId()) && !template.isExtractable()) {
        throw new InvalidParameterValueException("Unable to extract template id=" + templateId + " as it's not extractable");
    }
    _accountMgr.checkAccess(caller, AccessType.OperateEntry, true, template);
    List<DataStore> ssStores = _dataStoreMgr.getImageStoresByScope(new ZoneScope(null));
    TemplateDataStoreVO tmpltStoreRef = null;
    ImageStoreEntity tmpltStore = null;
    if (ssStores != null) {
        for (DataStore store : ssStores) {
            tmpltStoreRef = _tmplStoreDao.findByStoreTemplate(store.getId(), templateId);
            if (tmpltStoreRef != null) {
                if (tmpltStoreRef.getDownloadState() == com.cloud.storage.VMTemplateStorageResourceAssoc.Status.DOWNLOADED) {
                    tmpltStore = (ImageStoreEntity) store;
                    break;
                }
            }
        }
    }
    if (tmpltStore == null) {
        throw new InvalidParameterValueException("The " + desc + " has not been downloaded ");
    }
    // Check if the url already exists
    if (tmpltStoreRef.getExtractUrl() != null) {
        return tmpltStoreRef.getExtractUrl();
    }
    // Handle NFS to S3 object store migration case, we trigger template sync from NFS to S3 during extract template or copy template
    _tmpltSvr.syncTemplateToRegionStore(templateId, tmpltStore);
    TemplateInfo templateObject = _tmplFactory.getTemplate(templateId, tmpltStore);
    String extractUrl = tmpltStore.createEntityExtractUrl(tmpltStoreRef.getInstallPath(), template.getFormat(), templateObject);
    tmpltStoreRef.setExtractUrl(extractUrl);
    tmpltStoreRef.setExtractUrlCreated(DateUtil.now());
    _tmplStoreDao.update(tmpltStoreRef.getId(), tmpltStoreRef);
    return extractUrl;
}
Also used : ZoneScope(org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope) TemplateInfo(org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) PrimaryDataStore(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) VMTemplateVO(com.cloud.storage.VMTemplateVO) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) ImageStoreEntity(org.apache.cloudstack.storage.image.datastore.ImageStoreEntity) TemplateDataStoreVO(org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO)

Example 63 with DataStore

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

the class UserVmManagerImpl method handleManagedStorage.

private void handleManagedStorage(UserVmVO vm, VolumeVO root) {
    if (Volume.State.Allocated.equals(root.getState())) {
        return;
    }
    StoragePoolVO storagePool = _storagePoolDao.findById(root.getPoolId());
    if (storagePool != null && storagePool.isManaged()) {
        Long hostId = vm.getHostId() != null ? vm.getHostId() : vm.getLastHostId();
        if (hostId != null) {
            VolumeInfo volumeInfo = volFactory.getVolume(root.getId());
            Host host = _hostDao.findById(hostId);
            final Command cmd;
            if (host.getHypervisorType() == HypervisorType.XenServer) {
                DiskTO disk = new DiskTO(volumeInfo.getTO(), root.getDeviceId(), root.getPath(), root.getVolumeType());
                // it's OK in this case to send a detach command to the host for a root volume as this
                // will simply lead to the SR that supports the root volume being removed
                cmd = new DettachCommand(disk, vm.getInstanceName());
                DettachCommand detachCommand = (DettachCommand) cmd;
                detachCommand.setManaged(true);
                detachCommand.setStorageHost(storagePool.getHostAddress());
                detachCommand.setStoragePort(storagePool.getPort());
                detachCommand.set_iScsiName(root.get_iScsiName());
            } else if (host.getHypervisorType() == HypervisorType.VMware) {
                PrimaryDataStore primaryDataStore = (PrimaryDataStore) volumeInfo.getDataStore();
                Map<String, String> details = primaryDataStore.getDetails();
                if (details == null) {
                    details = new HashMap<String, String>();
                    primaryDataStore.setDetails(details);
                }
                details.put(DiskTO.MANAGED, Boolean.TRUE.toString());
                cmd = new DeleteCommand(volumeInfo.getTO());
            } else {
                throw new CloudRuntimeException("This hypervisor type is not supported on managed storage for this command.");
            }
            Commands cmds = new Commands(Command.OnError.Stop);
            cmds.addCommand(cmd);
            try {
                _agentMgr.send(hostId, cmds);
            } catch (Exception ex) {
                throw new CloudRuntimeException(ex.getMessage());
            }
            if (!cmds.isSuccessful()) {
                for (Answer answer : cmds.getAnswers()) {
                    if (!answer.getResult()) {
                        s_logger.warn("Failed to reset vm due to: " + answer.getDetails());
                        throw new CloudRuntimeException("Unable to reset " + vm + " due to " + answer.getDetails());
                    }
                }
            }
            // root.getPoolId() should be null if the VM we are detaching the disk from has never been started before
            DataStore dataStore = root.getPoolId() != null ? _dataStoreMgr.getDataStore(root.getPoolId(), DataStoreRole.Primary) : null;
            volumeMgr.revokeAccess(volFactory.getVolume(root.getId()), host, dataStore);
        }
    }
}
Also used : LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) Host(com.cloud.host.Host) ExecutionException(com.cloud.utils.exception.ExecutionException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) VirtualMachineMigrationException(com.cloud.exception.VirtualMachineMigrationException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) CloudException(com.cloud.exception.CloudException) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException) StorageUnavailableException(com.cloud.exception.StorageUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) ConfigurationException(javax.naming.ConfigurationException) ManagementServerException(com.cloud.exception.ManagementServerException) DeleteCommand(org.apache.cloudstack.storage.command.DeleteCommand) GetVmStatsAnswer(com.cloud.agent.api.GetVmStatsAnswer) Answer(com.cloud.agent.api.Answer) StartAnswer(com.cloud.agent.api.StartAnswer) RestoreVMSnapshotAnswer(com.cloud.agent.api.RestoreVMSnapshotAnswer) GetVmDiskStatsAnswer(com.cloud.agent.api.GetVmDiskStatsAnswer) PvlanSetupCommand(com.cloud.agent.api.PvlanSetupCommand) Command(com.cloud.agent.api.Command) GetVmStatsCommand(com.cloud.agent.api.GetVmStatsCommand) DettachCommand(org.apache.cloudstack.storage.command.DettachCommand) DeleteCommand(org.apache.cloudstack.storage.command.DeleteCommand) GetVmIpAddressCommand(com.cloud.agent.api.GetVmIpAddressCommand) GetVmDiskStatsCommand(com.cloud.agent.api.GetVmDiskStatsCommand) RestoreVMSnapshotCommand(com.cloud.agent.api.RestoreVMSnapshotCommand) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) PrimaryDataStore(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore) StoragePoolVO(org.apache.cloudstack.storage.datastore.db.StoragePoolVO) DettachCommand(org.apache.cloudstack.storage.command.DettachCommand) Commands(com.cloud.agent.manager.Commands) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) DiskTO(com.cloud.agent.api.to.DiskTO) PrimaryDataStore(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore)

Example 64 with DataStore

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

the class VolumeOrchestrator method revokeAccess.

@Override
public void revokeAccess(long vmId, long hostId) {
    HostVO host = _hostDao.findById(hostId);
    List<VolumeVO> volumesForVm = _volsDao.findByInstance(vmId);
    if (volumesForVm != null) {
        for (VolumeVO volumeForVm : volumesForVm) {
            VolumeInfo volumeInfo = volFactory.getVolume(volumeForVm.getId());
            // pool id can be null for the VM's volumes in Allocated state
            if (volumeForVm.getPoolId() != null) {
                DataStore dataStore = dataStoreMgr.getDataStore(volumeForVm.getPoolId(), DataStoreRole.Primary);
                volService.revokeAccess(volumeInfo, host, dataStore);
            }
        }
    }
}
Also used : VolumeVO(com.cloud.storage.VolumeVO) PrimaryDataStore(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) HostVO(com.cloud.host.HostVO)

Example 65 with DataStore

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

the class VolumeOrchestrator method createVolume.

@DB
public VolumeInfo createVolume(VolumeInfo volume, VirtualMachine vm, VirtualMachineTemplate template, DataCenter dc, Pod pod, Long clusterId, ServiceOffering offering, DiskOffering diskOffering, List<StoragePool> avoids, long size, HypervisorType hyperType) {
    // update the volume's hv_ss_reserve (hypervisor snapshot reserve) from a disk offering (used for managed storage)
    volume = volService.updateHypervisorSnapshotReserveForVolume(diskOffering, volume.getId(), hyperType);
    StoragePool pool = null;
    DiskProfile dskCh = null;
    if (volume.getVolumeType() == Type.ROOT && Storage.ImageFormat.ISO != template.getFormat()) {
        dskCh = createDiskCharacteristics(volume, template, dc, offering);
        storageMgr.setDiskProfileThrottling(dskCh, offering, diskOffering);
    } else {
        dskCh = createDiskCharacteristics(volume, template, dc, diskOffering);
        storageMgr.setDiskProfileThrottling(dskCh, null, diskOffering);
    }
    if (diskOffering != null && diskOffering.isCustomized()) {
        dskCh.setSize(size);
    }
    dskCh.setHyperType(hyperType);
    final HashSet<StoragePool> avoidPools = new HashSet<StoragePool>(avoids);
    pool = findStoragePool(dskCh, dc, pod, clusterId, vm.getHostId(), vm, avoidPools);
    if (pool == null) {
        s_logger.warn("Unable to find suitable primary storage when creating volume " + volume.getName());
        throw new CloudRuntimeException("Unable to find suitable primary storage when creating volume " + volume.getName());
    }
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Trying to create " + volume + " on " + pool);
    }
    DataStore store = dataStoreMgr.getDataStore(pool.getId(), DataStoreRole.Primary);
    for (int i = 0; i < 2; i++) {
        // retry one more time in case of template reload is required for Vmware case
        AsyncCallFuture<VolumeApiResult> future = null;
        boolean isNotCreatedFromTemplate = volume.getTemplateId() == null ? true : false;
        if (isNotCreatedFromTemplate) {
            future = volService.createVolumeAsync(volume, store);
        } else {
            TemplateInfo templ = tmplFactory.getTemplate(template.getId(), DataStoreRole.Image);
            future = volService.createVolumeFromTemplateAsync(volume, store.getId(), templ);
        }
        try {
            VolumeApiResult result = future.get();
            if (result.isFailed()) {
                if (result.getResult().contains("request template reload") && (i == 0)) {
                    s_logger.debug("Retry template re-deploy for vmware");
                    continue;
                } else {
                    s_logger.debug("create volume failed: " + result.getResult());
                    throw new CloudRuntimeException("create volume failed:" + result.getResult());
                }
            }
            return result.getVolume();
        } catch (InterruptedException e) {
            s_logger.error("create volume failed", e);
            throw new CloudRuntimeException("create volume failed", e);
        } catch (ExecutionException e) {
            s_logger.error("create volume failed", e);
            throw new CloudRuntimeException("create volume failed", e);
        }
    }
    throw new CloudRuntimeException("create volume failed even after template re-deploy");
}
Also used : StoragePool(com.cloud.storage.StoragePool) VolumeApiResult(org.apache.cloudstack.engine.subsystem.api.storage.VolumeService.VolumeApiResult) DiskProfile(com.cloud.vm.DiskProfile) TemplateInfo(org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo) 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) HashSet(java.util.HashSet) DB(com.cloud.utils.db.DB)

Aggregations

DataStore (org.apache.cloudstack.engine.subsystem.api.storage.DataStore)158 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)52 VolumeInfo (org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo)49 ExecutionException (java.util.concurrent.ExecutionException)37 VolumeVO (com.cloud.storage.VolumeVO)30 TemplateInfo (org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo)25 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)24 ArrayList (java.util.ArrayList)24 HashMap (java.util.HashMap)24 VMTemplateVO (com.cloud.storage.VMTemplateVO)22 PrimaryDataStore (org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore)22 TemplateDataStoreVO (org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO)22 StoragePoolVO (org.apache.cloudstack.storage.datastore.db.StoragePoolVO)18 EndPoint (org.apache.cloudstack.engine.subsystem.api.storage.EndPoint)17 VolumeApiResult (org.apache.cloudstack.engine.subsystem.api.storage.VolumeService.VolumeApiResult)17 ZoneScope (org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope)16 StorageUnavailableException (com.cloud.exception.StorageUnavailableException)15 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)13 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)13 Pair (com.cloud.utils.Pair)13