Search in sources :

Example 46 with DataTO

use of com.cloud.legacymodel.to.DataTO in project cosmic by MissionCriticalCloud.

the class XenServerGuru method finalizeExpungeVolumes.

@Override
public List<Command> finalizeExpungeVolumes(final VirtualMachine vm) {
    final List<Command> commands = new ArrayList<>();
    final List<VolumeVO> volumes = _volumeDao.findByInstance(vm.getId());
    // will simply lead to the SR that supports the root volume being removed
    if (volumes != null) {
        for (final VolumeVO volume : volumes) {
            final StoragePoolVO storagePool = _storagePoolDao.findById(volume.getPoolId());
            // so the volume was never assigned to a storage pool)
            if (storagePool != null && storagePool.isManaged()) {
                final DataTO volTO = _volFactory.getVolume(volume.getId()).getTO();
                final DiskTO disk = new DiskTO(volTO, volume.getDeviceId(), volume.getPath(), volume.getVolumeType());
                final DettachCommand cmd = new DettachCommand(disk, vm.getInstanceName());
                cmd.setManaged(true);
                cmd.setStorageHost(storagePool.getHostAddress());
                cmd.setStoragePort(storagePool.getPort());
                cmd.set_iScsiName(volume.get_iScsiName());
                commands.add(cmd);
            }
        }
    }
    return commands;
}
Also used : DataTO(com.cloud.legacymodel.to.DataTO) VolumeVO(com.cloud.storage.VolumeVO) DettachCommand(com.cloud.legacymodel.communication.command.DettachCommand) StorageSubSystemCommand(com.cloud.legacymodel.communication.command.StorageSubSystemCommand) Command(com.cloud.legacymodel.communication.command.Command) CopyCommand(com.cloud.legacymodel.communication.command.CopyCommand) ArrayList(java.util.ArrayList) StoragePoolVO(com.cloud.storage.datastore.db.StoragePoolVO) DettachCommand(com.cloud.legacymodel.communication.command.DettachCommand) DiskTO(com.cloud.legacymodel.to.DiskTO)

Example 47 with DataTO

use of com.cloud.legacymodel.to.DataTO in project cosmic by MissionCriticalCloud.

the class VolumeObject method getTO.

@Override
public DataTO getTO() {
    DataTO to = getDataStore().getDriver().getTO(this);
    if (to == null) {
        DataStoreTO dataStoreTO = null;
        if (null != this.getDataStore()) {
            dataStoreTO = this.getDataStore().getTO();
        }
        to = new VolumeObjectTO(this.getUuid(), this.getVolumeType(), dataStoreTO, this.getName(), this.getSize(), this.getPath(), this.getVolumeId(), this.getAttachedVmName(), this.getAccountId(), this.getChainInfo(), this.getFormat(), this.getProvisioningType(), this.getId(), this.getDeviceId(), this.getBytesReadRate(), this.getBytesWriteRate(), this.getIopsReadRate(), this.getIopsWriteRate(), this.getIopsTotalRate(), this.getIopsRatePerGb(), this.getCacheMode(), this.getHypervisorType());
    }
    return to;
}
Also used : DataStoreTO(com.cloud.legacymodel.to.DataStoreTO) DataTO(com.cloud.legacymodel.to.DataTO) VolumeObjectTO(com.cloud.legacymodel.to.VolumeObjectTO)

Example 48 with DataTO

use of com.cloud.legacymodel.to.DataTO in project cosmic by MissionCriticalCloud.

the class VolumeApiServiceImpl method sendAttachVolumeCommand.

private VolumeVO sendAttachVolumeCommand(final UserVmVO vm, VolumeVO volumeToAttach, Long deviceId, final DiskControllerType diskController) {
    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 = this._storagePoolDao.findById(volumeToAttach.getPoolId());
    if (hostId != null) {
        host = this._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 ? this.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 {
            this.volService.grantAccess(this.volFactory.getVolume(volumeToAttach.getId()), host, dataStore);
        } catch (final Exception e) {
            this.volService.revokeAccess(this.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());
            this._volsDao.update(volumeToAttach.getId(), volumeToAttach);
        }
        final DataTO volTO = this.volFactory.getVolume(volumeToAttach.getId()).getTO();
        deviceId = getDeviceId(vm, deviceId);
        if (diskController != null) {
            volumeToAttach.setDiskController(diskController);
        }
        final DiskTO disk = new DiskTO(volTO, deviceId, volumeToAttach.getPath(), volumeToAttach.getVolumeType(), volumeToAttach.getDiskController(), volumeToAttach.getFormat());
        final AttachCommand cmd = new AttachCommand(disk, vm.getInstanceName());
        final ChapInfo chapInfo = this.volService.getChapInfo(this.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());
        }
        this._userVmDao.loadDetails(vm);
        try {
            answer = (AttachAnswer) this._agentMgr.send(hostId, cmd);
        } catch (final Exception e) {
            if (host != null) {
                this.volService.revokeAccess(this.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();
            this._volsDao.attachVolume(volumeToAttach.getId(), vm.getId(), disk.getDiskSeq(), diskController);
            volumeToAttach = this._volsDao.findById(volumeToAttach.getId());
            if (volumeToAttachStoragePool.isManaged() && volumeToAttach.getPath() == null) {
                volumeToAttach.setPath(answer.getDisk().getPath());
                if (diskController != null) {
                    volumeToAttach.setDiskController(diskController);
                }
                this._volsDao.update(volumeToAttach.getId(), volumeToAttach);
            }
        } else {
            deviceId = getDeviceId(vm, deviceId);
            this._volsDao.attachVolume(volumeToAttach.getId(), vm.getId(), deviceId, diskController);
        }
        // insert record for disk I/O statistics
        VmDiskStatisticsVO diskstats = this._vmDiskStatsDao.findBy(vm.getAccountId(), vm.getDataCenterId(), vm.getId(), volumeToAttach.getId());
        if (diskstats == null) {
            diskstats = new VmDiskStatisticsVO(vm.getAccountId(), vm.getDataCenterId(), vm.getId(), volumeToAttach.getId());
            this._vmDiskStatsDao.persist(diskstats);
        }
        // Change MaintenancePolicy when adding disk with HOST scope
        if (volumeToAttachStoragePool != null && ScopeType.HOST.equals(volumeToAttachStoragePool.getScope())) {
            vm.setMaintenancePolicy(MaintenancePolicy.ShutdownAndStart);
            s_logger.debug("Setting MaintenancePolicy to '" + MaintenancePolicy.ShutdownAndStart.toString() + "' for VM " + vm.getInstanceName());
            _vmInstanceDao.persist(vm);
        }
        return this._volsDao.findById(volumeToAttach.getId());
    } else {
        if (answer != null) {
            final String details = answer.getDetails();
            if (details != null && !details.isEmpty()) {
                errorMsg += "; " + details;
            }
        }
        if (host != null) {
            this.volService.revokeAccess(this.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) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) PermissionDeniedException(com.cloud.legacymodel.exceptions.PermissionDeniedException) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) ResourceAllocationException(com.cloud.legacymodel.exceptions.ResourceAllocationException) CloudException(com.cloud.legacymodel.exceptions.CloudException) ConcurrentOperationException(com.cloud.legacymodel.exceptions.ConcurrentOperationException) NoTransitionException(com.cloud.legacymodel.exceptions.NoTransitionException) ExecutionException(java.util.concurrent.ExecutionException) StorageUnavailableException(com.cloud.legacymodel.exceptions.StorageUnavailableException) MalformedURLException(java.net.MalformedURLException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) AttachCommand(com.cloud.legacymodel.communication.command.AttachCommand) DataTO(com.cloud.legacymodel.to.DataTO) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) DataStore(com.cloud.engine.subsystem.api.storage.DataStore) StoragePoolVO(com.cloud.storage.datastore.db.StoragePoolVO) AttachAnswer(com.cloud.legacymodel.communication.answer.AttachAnswer) DiskTO(com.cloud.legacymodel.to.DiskTO)

Example 49 with DataTO

use of com.cloud.legacymodel.to.DataTO in project cosmic by MissionCriticalCloud.

the class VolumeApiServiceImpl method orchestrateDetachVolumeFromVM.

private Volume orchestrateDetachVolumeFromVM(final long vmId, final long volumeId) {
    final Volume volume = this._volsDao.findById(volumeId);
    final VMInstanceVO vm = this._vmInstanceDao.findById(vmId);
    String errorMsg = "Failed to detach volume " + volume.getName() + " from VM " + vm.getHostName();
    boolean sendCommand = vm.getState() == State.Running;
    final Long hostId = vm.getHostId();
    HostVO host = null;
    final StoragePoolVO volumePool = this._storagePoolDao.findByIdIncludingRemoved(volume.getPoolId());
    if (hostId != null) {
        host = this._hostDao.findById(hostId);
        if (host != null && host.getHypervisorType() == HypervisorType.XenServer && volumePool != null && volumePool.isManaged()) {
            sendCommand = true;
        }
    }
    Answer answer = null;
    if (sendCommand) {
        final DataTO volTO = this.volFactory.getVolume(volume.getId()).getTO();
        final DiskTO disk = new DiskTO(volTO, volume.getDeviceId(), volume.getPath(), volume.getVolumeType(), volume.getDiskController(), volume.getFormat());
        final DettachCommand cmd = new DettachCommand(disk, vm.getInstanceName());
        cmd.setManaged(volumePool.isManaged());
        cmd.setStorageHost(volumePool.getHostAddress());
        cmd.setStoragePort(volumePool.getPort());
        cmd.set_iScsiName(volume.get_iScsiName());
        try {
            answer = this._agentMgr.send(hostId, cmd);
        } catch (final Exception e) {
            throw new CloudRuntimeException(errorMsg + " due to: " + e.getMessage());
        }
    }
    if (!sendCommand || answer != null && answer.getResult()) {
        // Mark the volume as detached
        this._volsDao.detachVolume(volume.getId());
        // volume.getPoolId() should be null if the VM we are detaching the disk from has never been started before
        final DataStore dataStore = volume.getPoolId() != null ? this.dataStoreMgr.getDataStore(volume.getPoolId(), DataStoreRole.Primary) : null;
        this.volService.revokeAccess(this.volFactory.getVolume(volume.getId()), host, dataStore);
        return this._volsDao.findById(volumeId);
    } else {
        if (answer != null) {
            final String details = answer.getDetails();
            if (details != null && !details.isEmpty()) {
                errorMsg += "; " + details;
            }
        }
        throw new CloudRuntimeException(errorMsg);
    }
}
Also used : VMInstanceVO(com.cloud.vm.VMInstanceVO) HostVO(com.cloud.host.HostVO) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) PermissionDeniedException(com.cloud.legacymodel.exceptions.PermissionDeniedException) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) ResourceAllocationException(com.cloud.legacymodel.exceptions.ResourceAllocationException) CloudException(com.cloud.legacymodel.exceptions.CloudException) ConcurrentOperationException(com.cloud.legacymodel.exceptions.ConcurrentOperationException) NoTransitionException(com.cloud.legacymodel.exceptions.NoTransitionException) ExecutionException(java.util.concurrent.ExecutionException) StorageUnavailableException(com.cloud.legacymodel.exceptions.StorageUnavailableException) MalformedURLException(java.net.MalformedURLException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) Answer(com.cloud.legacymodel.communication.answer.Answer) AttachAnswer(com.cloud.legacymodel.communication.answer.AttachAnswer) DataTO(com.cloud.legacymodel.to.DataTO) Volume(com.cloud.legacymodel.storage.Volume) VmWorkDetachVolume(com.cloud.vm.VmWorkDetachVolume) VmWorkMigrateVolume(com.cloud.vm.VmWorkMigrateVolume) VmWorkResizeVolume(com.cloud.vm.VmWorkResizeVolume) VmWorkAttachVolume(com.cloud.vm.VmWorkAttachVolume) VmWorkExtractVolume(com.cloud.vm.VmWorkExtractVolume) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) DataStore(com.cloud.engine.subsystem.api.storage.DataStore) StoragePoolVO(com.cloud.storage.datastore.db.StoragePoolVO) DettachCommand(com.cloud.legacymodel.communication.command.DettachCommand) DiskTO(com.cloud.legacymodel.to.DiskTO)

Aggregations

DataTO (com.cloud.legacymodel.to.DataTO)49 DataStoreTO (com.cloud.legacymodel.to.DataStoreTO)30 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)29 NfsTO (com.cloud.legacymodel.to.NfsTO)26 CopyCmdAnswer (com.cloud.legacymodel.communication.answer.CopyCmdAnswer)25 PrimaryDataStoreTO (com.cloud.legacymodel.to.PrimaryDataStoreTO)20 InternalErrorException (com.cloud.legacymodel.exceptions.InternalErrorException)19 XenAPIException (com.xensource.xenapi.Types.XenAPIException)18 XmlRpcException (org.apache.xmlrpc.XmlRpcException)18 Connection (com.xensource.xenapi.Connection)17 VDI (com.xensource.xenapi.VDI)17 VolumeObjectTO (com.cloud.legacymodel.to.VolumeObjectTO)16 SR (com.xensource.xenapi.SR)12 Answer (com.cloud.legacymodel.communication.answer.Answer)11 DiskTO (com.cloud.legacymodel.to.DiskTO)11 TemplateObjectTO (com.cloud.legacymodel.to.TemplateObjectTO)11 URI (java.net.URI)10 SnapshotObjectTO (com.cloud.legacymodel.to.SnapshotObjectTO)9 AttachAnswer (com.cloud.legacymodel.communication.answer.AttachAnswer)7 IOException (java.io.IOException)5