Search in sources :

Example 1 with DettachCommand

use of com.cloud.legacymodel.communication.command.DettachCommand 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 : GetVmDiskStatsAnswer(com.cloud.legacymodel.communication.answer.GetVmDiskStatsAnswer) GetVmStatsAnswer(com.cloud.legacymodel.communication.answer.GetVmStatsAnswer) RestoreVMSnapshotAnswer(com.cloud.legacymodel.communication.answer.RestoreVMSnapshotAnswer) StartAnswer(com.cloud.legacymodel.communication.answer.StartAnswer) Answer(com.cloud.legacymodel.communication.answer.Answer) DettachCommand(com.cloud.legacymodel.communication.command.DettachCommand) RestoreVMSnapshotCommand(com.cloud.legacymodel.communication.command.RestoreVMSnapshotCommand) GetVmDiskStatsCommand(com.cloud.legacymodel.communication.command.GetVmDiskStatsCommand) Command(com.cloud.legacymodel.communication.command.Command) GetVmIpAddressCommand(com.cloud.legacymodel.communication.command.GetVmIpAddressCommand) GetVmStatsCommand(com.cloud.legacymodel.communication.command.GetVmStatsCommand) PvlanSetupCommand(com.cloud.legacymodel.communication.command.PvlanSetupCommand) 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) Commands(com.cloud.agent.manager.Commands) VolumeInfo(com.cloud.engine.subsystem.api.storage.VolumeInfo) Host(com.cloud.legacymodel.dc.Host) PermissionDeniedException(com.cloud.legacymodel.exceptions.PermissionDeniedException) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) ConcurrentOperationException(com.cloud.legacymodel.exceptions.ConcurrentOperationException) OperationTimedoutException(com.cloud.legacymodel.exceptions.OperationTimedoutException) InsufficientAddressCapacityException(com.cloud.legacymodel.exceptions.InsufficientAddressCapacityException) VirtualMachineMigrationException(com.cloud.legacymodel.exceptions.VirtualMachineMigrationException) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) ExecutionException(com.cloud.legacymodel.exceptions.ExecutionException) ResourceAllocationException(com.cloud.legacymodel.exceptions.ResourceAllocationException) CloudException(com.cloud.legacymodel.exceptions.CloudException) NoTransitionException(com.cloud.legacymodel.exceptions.NoTransitionException) InsufficientCapacityException(com.cloud.legacymodel.exceptions.InsufficientCapacityException) AgentUnavailableException(com.cloud.legacymodel.exceptions.AgentUnavailableException) ConfigurationException(javax.naming.ConfigurationException) StorageUnavailableException(com.cloud.legacymodel.exceptions.StorageUnavailableException) ResourceUnavailableException(com.cloud.legacymodel.exceptions.ResourceUnavailableException) ManagementServerException(com.cloud.legacymodel.exceptions.ManagementServerException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) DiskTO(com.cloud.legacymodel.to.DiskTO)

Example 2 with DettachCommand

use of com.cloud.legacymodel.communication.command.DettachCommand in project cosmic by MissionCriticalCloud.

the class TemplateManagerImpl method attachISOToVM.

private boolean attachISOToVM(final long vmId, final long isoId, final boolean attach) {
    final UserVmVO vm = this._userVmDao.findById(vmId);
    if (vm == null) {
        return false;
    } else if (vm.getState() != State.Running) {
        return true;
    }
    // prepare ISO ready to mount on hypervisor resource level
    final TemplateInfo tmplt = prepareIso(isoId, vm.getDataCenterId());
    final String vmName = vm.getInstanceName();
    final HostVO host = this._hostDao.findById(vm.getHostId());
    if (host == null) {
        s_logger.warn("Host: " + vm.getHostId() + " does not exist");
        return false;
    }
    final DataTO isoTO = tmplt.getTO();
    final DiskTO disk = new DiskTO(isoTO, null, null, VolumeType.ISO);
    final Command cmd;
    if (attach) {
        cmd = new AttachCommand(disk, vmName);
    } else {
        cmd = new DettachCommand(disk, vmName);
    }
    final Answer a = this._agentMgr.easySend(vm.getHostId(), cmd);
    return a != null && a.getResult();
}
Also used : Answer(com.cloud.legacymodel.communication.answer.Answer) UserVmVO(com.cloud.vm.UserVmVO) TemplateInfo(com.cloud.engine.subsystem.api.storage.TemplateInfo) DataTO(com.cloud.legacymodel.to.DataTO) DettachCommand(com.cloud.legacymodel.communication.command.DettachCommand) ComputeChecksumCommand(com.cloud.legacymodel.communication.command.ComputeChecksumCommand) TemplateOrVolumePostUploadCommand(com.cloud.legacymodel.communication.command.TemplateOrVolumePostUploadCommand) Command(com.cloud.legacymodel.communication.command.Command) AttachCommand(com.cloud.legacymodel.communication.command.AttachCommand) DestroyCommand(com.cloud.legacymodel.communication.command.DestroyCommand) DettachCommand(com.cloud.legacymodel.communication.command.DettachCommand) StoragePoolHostVO(com.cloud.storage.StoragePoolHostVO) VMTemplateHostVO(com.cloud.storage.VMTemplateHostVO) HostVO(com.cloud.host.HostVO) AttachCommand(com.cloud.legacymodel.communication.command.AttachCommand) DiskTO(com.cloud.legacymodel.to.DiskTO)

Example 3 with DettachCommand

use of com.cloud.legacymodel.communication.command.DettachCommand 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 4 with DettachCommand

use of com.cloud.legacymodel.communication.command.DettachCommand 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

DettachCommand (com.cloud.legacymodel.communication.command.DettachCommand)4 DiskTO (com.cloud.legacymodel.to.DiskTO)4 Answer (com.cloud.legacymodel.communication.answer.Answer)3 Command (com.cloud.legacymodel.communication.command.Command)3 DataTO (com.cloud.legacymodel.to.DataTO)3 DataStore (com.cloud.engine.subsystem.api.storage.DataStore)2 HostVO (com.cloud.host.HostVO)2 CloudException (com.cloud.legacymodel.exceptions.CloudException)2 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)2 ConcurrentOperationException (com.cloud.legacymodel.exceptions.ConcurrentOperationException)2 InvalidParameterValueException (com.cloud.legacymodel.exceptions.InvalidParameterValueException)2 NoTransitionException (com.cloud.legacymodel.exceptions.NoTransitionException)2 PermissionDeniedException (com.cloud.legacymodel.exceptions.PermissionDeniedException)2 ResourceAllocationException (com.cloud.legacymodel.exceptions.ResourceAllocationException)2 StorageUnavailableException (com.cloud.legacymodel.exceptions.StorageUnavailableException)2 StoragePoolVO (com.cloud.storage.datastore.db.StoragePoolVO)2 Commands (com.cloud.agent.manager.Commands)1 TemplateInfo (com.cloud.engine.subsystem.api.storage.TemplateInfo)1 VolumeInfo (com.cloud.engine.subsystem.api.storage.VolumeInfo)1 AttachAnswer (com.cloud.legacymodel.communication.answer.AttachAnswer)1