Search in sources :

Example 6 with DataStore

use of com.cloud.engine.subsystem.api.storage.DataStore in project cosmic by MissionCriticalCloud.

the class UploadMonitorImpl method extractTemplate.

@Override
public Long extractTemplate(final VMTemplateVO template, final String url, final TemplateDataStoreVO vmTemplateHost, final Long dataCenterId, final long eventId, final long asyncJobId, final AsyncJobManager asyncMgr) {
    final Type type = (template.getFormat() == ImageFormat.ISO) ? Type.ISO : Type.TEMPLATE;
    final DataStore secStore = storeMgr.getImageStore(dataCenterId);
    final UploadVO uploadTemplateObj = new UploadVO(secStore.getId(), template.getId(), new Date(), Upload.Status.NOT_UPLOADED, type, url, Mode.FTP_UPLOAD);
    _uploadDao.persist(uploadTemplateObj);
    if (vmTemplateHost != null) {
        start();
        final UploadCommand ucmd = new UploadCommand(template, url, vmTemplateHost.getInstallPath(), vmTemplateHost.getSize());
        final UploadListener ul = new UploadListener(secStore, _timer, _uploadDao, uploadTemplateObj, this, ucmd, template.getAccountId(), template.getName(), type, eventId, asyncJobId, asyncMgr);
        _listenerMap.put(uploadTemplateObj, ul);
        try {
            final EndPoint ep = _epSelector.select(secStore);
            if (ep == null) {
                final String errMsg = "No remote endpoint to send command, check if host or ssvm is down?";
                s_logger.error(errMsg);
                return null;
            }
            ep.sendMessageAsync(ucmd, new UploadListener.Callback(ep.getId(), ul));
        } catch (final Exception e) {
            s_logger.warn("Unable to start upload of " + template.getUniqueName() + " from " + secStore.getName() + " to " + url, e);
            ul.setDisconnected();
            ul.scheduleStatusCheck(RequestType.GET_OR_RESTART);
        }
        return uploadTemplateObj.getId();
    }
    return null;
}
Also used : Type(com.cloud.storage.Upload.Type) RequestType(com.cloud.agent.api.storage.UploadProgressCommand.RequestType) UploadCommand(com.cloud.agent.api.storage.UploadCommand) DataStore(com.cloud.engine.subsystem.api.storage.DataStore) UploadVO(com.cloud.storage.UploadVO) EndPoint(com.cloud.engine.subsystem.api.storage.EndPoint) Date(java.util.Date) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 7 with DataStore

use of com.cloud.engine.subsystem.api.storage.DataStore in project cosmic by MissionCriticalCloud.

the class UploadMonitorImpl method cleanupStorage.

public void cleanupStorage() {
    final int EXTRACT_URL_LIFE_LIMIT_IN_SECONDS = _urlExpirationInterval;
    final List<UploadVO> extractJobs = _uploadDao.listByModeAndStatus(Mode.HTTP_DOWNLOAD, Status.DOWNLOAD_URL_CREATED);
    for (final UploadVO extractJob : extractJobs) {
        if (getTimeDiff(extractJob.getLastUpdated()) > EXTRACT_URL_LIFE_LIMIT_IN_SECONDS) {
            final String path = extractJob.getInstallPath();
            final DataStore secStore = storeMgr.getDataStore(extractJob.getDataStoreId(), DataStoreRole.Image);
            // Would delete the symlink for the Type and if Type == VOLUME then also the volume
            final DeleteEntityDownloadURLCommand cmd = new DeleteEntityDownloadURLCommand(path, extractJob.getType(), extractJob.getUploadUrl(), ((ImageStoreVO) secStore).getParent());
            final EndPoint ep = _epSelector.select(secStore);
            if (ep == null) {
                s_logger.warn("UploadMonitor cleanup: There is no secondary storage VM for secondary storage host " + extractJob.getDataStoreId());
                // TODO: why continue? why not break?
                continue;
            }
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("UploadMonitor cleanup: Sending deletion of extract URL " + extractJob.getUploadUrl() + " to ssvm " + ep.getHostAddr());
            }
            final Answer ans = ep.sendMessage(cmd);
            if (ans != null && ans.getResult()) {
                _uploadDao.remove(extractJob.getId());
            } else {
                s_logger.warn("UploadMonitor cleanup: Unable to delete the link for " + extractJob.getType() + " id=" + extractJob.getTypeId() + " url=" + extractJob.getUploadUrl() + " on ssvm " + ep.getHostAddr());
            }
        }
    }
}
Also used : Answer(com.cloud.agent.api.Answer) DataStore(com.cloud.engine.subsystem.api.storage.DataStore) UploadVO(com.cloud.storage.UploadVO) EndPoint(com.cloud.engine.subsystem.api.storage.EndPoint) EndPoint(com.cloud.engine.subsystem.api.storage.EndPoint) DeleteEntityDownloadURLCommand(com.cloud.agent.api.storage.DeleteEntityDownloadURLCommand)

Example 8 with DataStore

use of com.cloud.engine.subsystem.api.storage.DataStore in project cosmic by MissionCriticalCloud.

the class HypervisorTemplateAdapter method delete.

@Override
@DB
public boolean delete(final TemplateProfile profile) {
    boolean success = true;
    final VMTemplateVO template = profile.getTemplate();
    // find all eligible image stores for this template
    final List<DataStore> imageStores = templateMgr.getImageStoreByTemplate(template.getId(), profile.getZoneId());
    if (imageStores == null || imageStores.size() == 0) {
        // already destroyed on image stores
        s_logger.info("Unable to find image store still having template: " + template.getName() + ", so just mark the template removed");
    } else {
        // Make sure the template is downloaded to all found image stores
        for (final DataStore store : imageStores) {
            final long storeId = store.getId();
            final List<TemplateDataStoreVO> templateStores = _tmpltStoreDao.listByTemplateStore(template.getId(), storeId);
            for (final TemplateDataStoreVO templateStore : templateStores) {
                if (templateStore.getDownloadState() == Status.DOWNLOAD_IN_PROGRESS) {
                    final String errorMsg = "Please specify a template that is not currently being downloaded.";
                    s_logger.debug("Template: " + template.getName() + " is currently being downloaded to secondary storage host: " + store.getName() + "; cant' delete it.");
                    throw new CloudRuntimeException(errorMsg);
                }
            }
        }
        for (final DataStore imageStore : imageStores) {
            // publish zone-wide usage event
            final Long sZoneId = ((ImageStoreEntity) imageStore).getDataCenterId();
            s_logger.info("Delete template from image store: " + imageStore.getName());
            final AsyncCallFuture<TemplateApiResult> future = imageService.deleteTemplateAsync(imageFactory.getTemplate(template.getId(), imageStore));
            try {
                final TemplateApiResult result = future.get();
                success = result.isSuccess();
                if (!success) {
                    s_logger.warn("Failed to delete the template " + template + " from the image store: " + imageStore.getName() + " due to: " + result.getResult());
                    break;
                }
                // remove from template_zone_ref
                final List<VMTemplateZoneVO> templateZones = templateZoneDao.listByZoneTemplate(sZoneId, template.getId());
                if (templateZones != null) {
                    for (final VMTemplateZoneVO templateZone : templateZones) {
                        templateZoneDao.remove(templateZone.getId());
                    }
                }
                // mark all the occurrences of this template in the given store as destroyed.
                templateDataStoreDao.removeByTemplateStore(template.getId(), imageStore.getId());
            } catch (final InterruptedException | ExecutionException e) {
                s_logger.debug("delete template Failed", e);
                throw new CloudRuntimeException("delete template Failed", e);
            }
        }
    }
    if (success) {
        if ((imageStores.size() > 1) && (profile.getZoneId() != null)) {
            // if template is stored in more than one image stores, and the zone id is not null, then don't delete other templates.
            return success;
        }
        // delete all cache entries for this template
        final List<TemplateInfo> cacheTmpls = imageFactory.listTemplateOnCache(template.getId());
        for (final TemplateInfo tmplOnCache : cacheTmpls) {
            s_logger.info("Delete template from image cache store: " + tmplOnCache.getDataStore().getName());
            tmplOnCache.delete();
        }
        // find all eligible image stores for this template
        final List<DataStore> iStores = templateMgr.getImageStoreByTemplate(template.getId(), null);
        if (iStores == null || iStores.size() == 0) {
            // Mark template as Inactive.
            template.setState(VirtualMachineTemplate.State.Inactive);
            _tmpltDao.update(template.getId(), template);
            // Decrement the number of templates and total secondary storage
            // space used by the account
            final Account account = _accountDao.findByIdIncludingRemoved(template.getAccountId());
            _resourceLimitMgr.decrementResourceCount(template.getAccountId(), ResourceType.template);
            _resourceLimitMgr.recalculateResourceCount(template.getAccountId(), account.getDomainId(), ResourceType.secondary_storage.getOrdinal());
        }
        // remove its related ACL permission
        final Pair<Class<?>, Long> tmplt = new Pair<>(VirtualMachineTemplate.class, template.getId());
        _messageBus.publish(_name, EntityManager.MESSAGE_REMOVE_ENTITY_EVENT, PublishScope.LOCAL, tmplt);
    }
    return success;
}
Also used : Account(com.cloud.user.Account) VMTemplateZoneVO(com.cloud.storage.VMTemplateZoneVO) VMTemplateVO(com.cloud.storage.VMTemplateVO) TemplateDataStoreVO(com.cloud.storage.datastore.db.TemplateDataStoreVO) TemplateApiResult(com.cloud.engine.subsystem.api.storage.TemplateService.TemplateApiResult) TemplateInfo(com.cloud.engine.subsystem.api.storage.TemplateInfo) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DataStore(com.cloud.engine.subsystem.api.storage.DataStore) ImageStoreEntity(com.cloud.storage.image.datastore.ImageStoreEntity) ExecutionException(java.util.concurrent.ExecutionException) Pair(com.cloud.utils.Pair) DB(com.cloud.utils.db.DB)

Example 9 with DataStore

use of com.cloud.engine.subsystem.api.storage.DataStore in project cosmic by MissionCriticalCloud.

the class UserVmManagerImpl method handleManagedStorage.

private void handleManagedStorage(final UserVmVO vm, final VolumeVO root) {
    if (Volume.State.Allocated.equals(root.getState())) {
        return;
    }
    final StoragePoolVO storagePool = _storagePoolDao.findById(root.getPoolId());
    if (storagePool != null && storagePool.isManaged()) {
        final Long hostId = vm.getHostId() != null ? vm.getHostId() : vm.getLastHostId();
        if (hostId != null) {
            final VolumeInfo volumeInfo = volFactory.getVolume(root.getId());
            final Host host = _hostDao.findById(hostId);
            final Command cmd;
            if (host.getHypervisorType() == HypervisorType.XenServer) {
                final 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());
                final DettachCommand detachCommand = (DettachCommand) cmd;
                detachCommand.setManaged(true);
                detachCommand.setStorageHost(storagePool.getHostAddress());
                detachCommand.setStoragePort(storagePool.getPort());
                detachCommand.set_iScsiName(root.get_iScsiName());
            } else {
                throw new CloudRuntimeException("This hypervisor type is not supported on managed storage for this command.");
            }
            final Commands cmds = new Commands(Command.OnError.Stop);
            cmds.addCommand(cmd);
            try {
                _agentMgr.send(hostId, cmds);
            } catch (final Exception ex) {
                throw new CloudRuntimeException(ex.getMessage());
            }
            if (!cmds.isSuccessful()) {
                for (final 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
            final DataStore dataStore = root.getPoolId() != null ? _dataStoreMgr.getDataStore(root.getPoolId(), DataStoreRole.Primary) : null;
            volumeMgr.revokeAccess(volFactory.getVolume(root.getId()), host, dataStore);
        }
    }
}
Also used : 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) DettachCommand(com.cloud.storage.command.DettachCommand) Command(com.cloud.agent.api.Command) GetVmStatsCommand(com.cloud.agent.api.GetVmStatsCommand) 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(com.cloud.engine.subsystem.api.storage.DataStore) StoragePoolVO(com.cloud.storage.datastore.db.StoragePoolVO) DettachCommand(com.cloud.storage.command.DettachCommand) Commands(com.cloud.agent.manager.Commands) VolumeInfo(com.cloud.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) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) ConfigurationException(javax.naming.ConfigurationException) ManagementServerException(com.cloud.exception.ManagementServerException) DiskTO(com.cloud.agent.api.to.DiskTO)

Example 10 with DataStore

use of com.cloud.engine.subsystem.api.storage.DataStore in project cosmic by MissionCriticalCloud.

the class VolumeApiServiceImpl method sendAttachVolumeCommand.

private VolumeVO sendAttachVolumeCommand(final UserVmVO vm, VolumeVO volumeToAttach, Long deviceId) {
    String errorMsg = "Failed to attach volume " + volumeToAttach.getName() + " to VM " + vm.getHostName();
    boolean sendCommand = vm.getState() == State.Running;
    AttachAnswer answer = null;
    final Long hostId = vm.getHostId();
    HostVO host = null;
    final StoragePoolVO volumeToAttachStoragePool = _storagePoolDao.findById(volumeToAttach.getPoolId());
    if (hostId != null) {
        host = _hostDao.findById(hostId);
        if (host != null && host.getHypervisorType() == HypervisorType.XenServer && volumeToAttachStoragePool != null && volumeToAttachStoragePool.isManaged()) {
            sendCommand = true;
        }
    }
    // volumeToAttachStoragePool should be null if the VM we are attaching the disk to has never been started before
    final DataStore dataStore = volumeToAttachStoragePool != null ? dataStoreMgr.getDataStore(volumeToAttachStoragePool.getId(), DataStoreRole.Primary) : null;
    // if we don't have a host, the VM we are attaching the disk to has never been started before
    if (host != null) {
        try {
            volService.grantAccess(volFactory.getVolume(volumeToAttach.getId()), host, dataStore);
        } catch (final Exception e) {
            volService.revokeAccess(volFactory.getVolume(volumeToAttach.getId()), host, dataStore);
            throw new CloudRuntimeException(e.getMessage());
        }
    }
    if (sendCommand) {
        if (host != null && host.getHypervisorType() == HypervisorType.KVM && volumeToAttachStoragePool.isManaged() && volumeToAttach.getPath() == null) {
            volumeToAttach.setPath(volumeToAttach.get_iScsiName());
            _volsDao.update(volumeToAttach.getId(), volumeToAttach);
        }
        final DataTO volTO = volFactory.getVolume(volumeToAttach.getId()).getTO();
        deviceId = getDeviceId(vm, deviceId);
        final DiskTO disk = new DiskTO(volTO, deviceId, volumeToAttach.getPath(), volumeToAttach.getVolumeType());
        final AttachCommand cmd = new AttachCommand(disk, vm.getInstanceName());
        final ChapInfo chapInfo = volService.getChapInfo(volFactory.getVolume(volumeToAttach.getId()), dataStore);
        final Map<String, String> details = new HashMap<>();
        disk.setDetails(details);
        details.put(DiskTO.MANAGED, String.valueOf(volumeToAttachStoragePool.isManaged()));
        details.put(DiskTO.STORAGE_HOST, volumeToAttachStoragePool.getHostAddress());
        details.put(DiskTO.STORAGE_PORT, String.valueOf(volumeToAttachStoragePool.getPort()));
        details.put(DiskTO.VOLUME_SIZE, String.valueOf(volumeToAttach.getSize()));
        details.put(DiskTO.IQN, volumeToAttach.get_iScsiName());
        details.put(DiskTO.MOUNT_POINT, volumeToAttach.get_iScsiName());
        details.put(DiskTO.PROTOCOL_TYPE, volumeToAttach.getPoolType() != null ? volumeToAttach.getPoolType().toString() : null);
        if (chapInfo != null) {
            details.put(DiskTO.CHAP_INITIATOR_USERNAME, chapInfo.getInitiatorUsername());
            details.put(DiskTO.CHAP_INITIATOR_SECRET, chapInfo.getInitiatorSecret());
            details.put(DiskTO.CHAP_TARGET_USERNAME, chapInfo.getTargetUsername());
            details.put(DiskTO.CHAP_TARGET_SECRET, chapInfo.getTargetSecret());
        }
        _userVmDao.loadDetails(vm);
        final Map<String, String> controllerInfo = new HashMap<>();
        controllerInfo.put(VmDetailConstants.ROOT_DISK_CONTROLLER, vm.getDetail(VmDetailConstants.ROOT_DISK_CONTROLLER));
        controllerInfo.put(VmDetailConstants.DATA_DISK_CONTROLLER, vm.getDetail(VmDetailConstants.DATA_DISK_CONTROLLER));
        cmd.setControllerInfo(controllerInfo);
        try {
            answer = (AttachAnswer) _agentMgr.send(hostId, cmd);
        } catch (final Exception e) {
            if (host != null) {
                volService.revokeAccess(volFactory.getVolume(volumeToAttach.getId()), host, dataStore);
            }
            throw new CloudRuntimeException(errorMsg + " due to: " + e.getMessage());
        }
    }
    if (!sendCommand || answer != null && answer.getResult()) {
        // Mark the volume as attached
        if (sendCommand) {
            final DiskTO disk = answer.getDisk();
            _volsDao.attachVolume(volumeToAttach.getId(), vm.getId(), disk.getDiskSeq());
            volumeToAttach = _volsDao.findById(volumeToAttach.getId());
            if (volumeToAttachStoragePool.isManaged() && volumeToAttach.getPath() == null) {
                volumeToAttach.setPath(answer.getDisk().getPath());
                _volsDao.update(volumeToAttach.getId(), volumeToAttach);
            }
        } else {
            deviceId = getDeviceId(vm, deviceId);
            _volsDao.attachVolume(volumeToAttach.getId(), vm.getId(), deviceId);
        }
        // insert record for disk I/O statistics
        VmDiskStatisticsVO diskstats = _vmDiskStatsDao.findBy(vm.getAccountId(), vm.getDataCenterId(), vm.getId(), volumeToAttach.getId());
        if (diskstats == null) {
            diskstats = new VmDiskStatisticsVO(vm.getAccountId(), vm.getDataCenterId(), vm.getId(), volumeToAttach.getId());
            _vmDiskStatsDao.persist(diskstats);
        }
        return _volsDao.findById(volumeToAttach.getId());
    } else {
        if (answer != null) {
            final String details = answer.getDetails();
            if (details != null && !details.isEmpty()) {
                errorMsg += "; " + details;
            }
        }
        if (host != null) {
            volService.revokeAccess(volFactory.getVolume(volumeToAttach.getId()), host, dataStore);
        }
        throw new CloudRuntimeException(errorMsg);
    }
}
Also used : HashMap(java.util.HashMap) ChapInfo(com.cloud.engine.subsystem.api.storage.ChapInfo) VmDiskStatisticsVO(com.cloud.user.VmDiskStatisticsVO) HostVO(com.cloud.host.HostVO) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) CloudException(com.cloud.exception.CloudException) StorageUnavailableException(com.cloud.exception.StorageUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ExecutionException(java.util.concurrent.ExecutionException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) MalformedURLException(java.net.MalformedURLException) AttachCommand(com.cloud.storage.command.AttachCommand) DataTO(com.cloud.agent.api.to.DataTO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DataStore(com.cloud.engine.subsystem.api.storage.DataStore) StoragePoolVO(com.cloud.storage.datastore.db.StoragePoolVO) AttachAnswer(com.cloud.storage.command.AttachAnswer) DiskTO(com.cloud.agent.api.to.DiskTO)

Aggregations

DataStore (com.cloud.engine.subsystem.api.storage.DataStore)96 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)43 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)23 ExecutionException (java.util.concurrent.ExecutionException)23 VolumeInfo (com.cloud.engine.subsystem.api.storage.VolumeInfo)19 ZoneScope (com.cloud.engine.subsystem.api.storage.ZoneScope)17 TemplateDataStoreVO (com.cloud.storage.datastore.db.TemplateDataStoreVO)17 ArrayList (java.util.ArrayList)17 VolumeVO (com.cloud.storage.VolumeVO)16 StorageUnavailableException (com.cloud.exception.StorageUnavailableException)15 EndPoint (com.cloud.engine.subsystem.api.storage.EndPoint)14 PrimaryDataStore (com.cloud.engine.subsystem.api.storage.PrimaryDataStore)14 DB (com.cloud.utils.db.DB)14 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)12 SnapshotDataStoreVO (com.cloud.storage.datastore.db.SnapshotDataStoreVO)12 StoragePoolVO (com.cloud.storage.datastore.db.StoragePoolVO)12 VolumeDataStoreVO (com.cloud.storage.datastore.db.VolumeDataStoreVO)12 ConfigurationException (javax.naming.ConfigurationException)12 Answer (com.cloud.agent.api.Answer)10 VMTemplateVO (com.cloud.storage.VMTemplateVO)10