Search in sources :

Example 1 with AttachCommand

use of org.apache.cloudstack.storage.command.AttachCommand in project cloudstack by apache.

the class LibvirtComputingResourceTest method testStorageSubSystemCommand.

@Test
public void testStorageSubSystemCommand() {
    final DiskTO disk = Mockito.mock(DiskTO.class);
    final String vmName = "Test";
    final AttachCommand command = new AttachCommand(disk, vmName);
    final StorageSubsystemCommandHandler handler = Mockito.mock(StorageSubsystemCommandHandler.class);
    when(libvirtComputingResource.getStorageHandler()).thenReturn(handler);
    when(handler.handleStorageCommands(command)).thenReturn(new AttachAnswer(disk));
    final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, libvirtComputingResource);
    assertTrue(answer.getResult());
}
Also used : UnsupportedAnswer(com.cloud.agent.api.UnsupportedAnswer) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) Answer(com.cloud.agent.api.Answer) CheckRouterAnswer(com.cloud.agent.api.CheckRouterAnswer) LibvirtRequestWrapper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper) StorageSubsystemCommandHandler(com.cloud.storage.resource.StorageSubsystemCommandHandler) AttachCommand(org.apache.cloudstack.storage.command.AttachCommand) DiskTO(com.cloud.agent.api.to.DiskTO) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 2 with AttachCommand

use of org.apache.cloudstack.storage.command.AttachCommand in project cloudstack by apache.

the class TemplateManagerImpl method attachISOToVM.

private boolean attachISOToVM(long vmId, long isoId, boolean attach, boolean forced) {
    UserVmVO vm = _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
    TemplateInfo tmplt = prepareIso(isoId, vm.getDataCenterId(), vm.getHostId(), null);
    if (tmplt == null) {
        s_logger.error("Failed to prepare ISO ready to mount on hypervisor resource level");
        throw new CloudRuntimeException("Failed to prepare ISO ready to mount on hypervisor resource level");
    }
    String vmName = vm.getInstanceName();
    HostVO host = _hostDao.findById(vm.getHostId());
    if (host == null) {
        s_logger.warn("Host: " + vm.getHostId() + " does not exist");
        return false;
    }
    DataTO isoTO = tmplt.getTO();
    DiskTO disk = new DiskTO(isoTO, null, null, Volume.Type.ISO);
    HypervisorGuru hvGuru = _hvGuruMgr.getGuru(vm.getHypervisorType());
    VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm);
    VirtualMachineTO vmTO = hvGuru.implement(profile);
    Command cmd = null;
    if (attach) {
        cmd = new AttachCommand(disk, vmName, vmTO.getDetails());
        ((AttachCommand) cmd).setForced(forced);
    } else {
        cmd = new DettachCommand(disk, vmName, vmTO.getDetails());
        ((DettachCommand) cmd).setForced(forced);
    }
    Answer a = _agentMgr.easySend(vm.getHostId(), cmd);
    return (a != null && a.getResult());
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) VirtualMachineProfileImpl(com.cloud.vm.VirtualMachineProfileImpl) StoragePoolHostVO(com.cloud.storage.StoragePoolHostVO) HostVO(com.cloud.host.HostVO) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) AttachCommand(org.apache.cloudstack.storage.command.AttachCommand) Answer(com.cloud.agent.api.Answer) TemplateInfo(org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo) DataTO(com.cloud.agent.api.to.DataTO) HypervisorGuru(com.cloud.hypervisor.HypervisorGuru) DettachCommand(org.apache.cloudstack.storage.command.DettachCommand) DestroyCommand(com.cloud.agent.api.storage.DestroyCommand) Command(com.cloud.agent.api.Command) AttachCommand(org.apache.cloudstack.storage.command.AttachCommand) TemplateOrVolumePostUploadCommand(org.apache.cloudstack.storage.command.TemplateOrVolumePostUploadCommand) ComputeChecksumCommand(com.cloud.agent.api.ComputeChecksumCommand) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DettachCommand(org.apache.cloudstack.storage.command.DettachCommand) VirtualMachineProfile(com.cloud.vm.VirtualMachineProfile) DiskTO(com.cloud.agent.api.to.DiskTO)

Example 3 with AttachCommand

use of org.apache.cloudstack.storage.command.AttachCommand in project cloudstack by apache.

the class VolumeApiServiceImpl method sendAttachVolumeCommand.

private VolumeVO sendAttachVolumeCommand(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;
    StoragePoolVO volumeToAttachStoragePool = _storagePoolDao.findById(volumeToAttach.getPoolId());
    HostVO host = getHostForVmVolumeAttach(vm, volumeToAttachStoragePool);
    Long hostId = host == null ? null : host.getId();
    if (host != null && host.getHypervisorType() == HypervisorType.VMware) {
        sendCommand = true;
    }
    if (host != null && host.getHypervisorType() == HypervisorType.XenServer && volumeToAttachStoragePool != null && volumeToAttachStoragePool.isManaged()) {
        sendCommand = true;
    }
    if (volumeToAttachStoragePool != null) {
        verifyManagedStorage(volumeToAttachStoragePool.getId(), hostId);
    }
    // volumeToAttachStoragePool should be null if the VM we are attaching the disk to has never been started before
    DataStore dataStore = volumeToAttachStoragePool != null ? dataStoreMgr.getDataStore(volumeToAttachStoragePool.getId(), DataStoreRole.Primary) : null;
    checkAndSetAttaching(volumeToAttach.getId());
    boolean attached = false;
    try {
        // 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 (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);
            }
            DataTO volTO = volFactory.getVolume(volumeToAttach.getId()).getTO();
            deviceId = getDeviceId(vm, deviceId);
            DiskTO disk = storageMgr.getDiskWithThrottling(volTO, volumeToAttach.getVolumeType(), deviceId, volumeToAttach.getPath(), vm.getServiceOfferingId(), volumeToAttach.getDiskOfferingId());
            AttachCommand cmd = new AttachCommand(disk, vm.getInstanceName());
            ChapInfo chapInfo = volService.getChapInfo(volFactory.getVolume(volumeToAttach.getId()), dataStore);
            Map<String, String> details = new HashMap<String, String>();
            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);
            details.put(StorageManager.STORAGE_POOL_DISK_WAIT.toString(), String.valueOf(StorageManager.STORAGE_POOL_DISK_WAIT.valueIn(volumeToAttachStoragePool.getId())));
            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());
            }
            if (volumeToAttach.getPoolId() != null) {
                StoragePoolVO poolVO = _storagePoolDao.findById(volumeToAttach.getPoolId());
                if (poolVO.getParent() != 0L) {
                    details.put(DiskTO.PROTOCOL_TYPE, Storage.StoragePoolType.DatastoreCluster.toString());
                }
            }
            _userVmDao.loadDetails(vm);
            Map<String, String> controllerInfo = new HashMap<String, String>();
            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);
            s_logger.debug("Attach volume id:" + volumeToAttach.getId() + " on VM id:" + vm.getId() + " has controller info:" + controllerInfo);
            try {
                answer = (AttachAnswer) _agentMgr.send(hostId, cmd);
            } catch (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) {
                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);
                }
                String chainInfo = answer.getContextParam("chainInfo");
                if (chainInfo != null) {
                    volumeToAttach = _volsDao.findById(volumeToAttach.getId());
                    volumeToAttach.setChainInfo(chainInfo);
                    _volsDao.update(volumeToAttach.getId(), volumeToAttach);
                }
            } else {
                deviceId = getDeviceId(vm, deviceId);
                _volsDao.attachVolume(volumeToAttach.getId(), vm.getId(), deviceId);
                volumeToAttach = _volsDao.findById(volumeToAttach.getId());
                if (vm.getHypervisorType() == HypervisorType.KVM && volumeToAttachStoragePool != null && volumeToAttachStoragePool.isManaged() && volumeToAttach.getPath() == null && volumeToAttach.get_iScsiName() != null) {
                    volumeToAttach.setPath(volumeToAttach.get_iScsiName());
                    _volsDao.update(volumeToAttach.getId(), volumeToAttach);
                }
                if (host != null && volumeToAttachStoragePool.getPoolType() == Storage.StoragePoolType.PowerFlex) {
                    // Unmap the volume on PowerFlex/ScaleIO pool for stopped VM
                    volService.revokeAccess(volFactory.getVolume(volumeToAttach.getId()), host, dataStore);
                }
            }
            // 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);
            }
            attached = true;
        } else {
            if (answer != null) {
                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);
        }
    } finally {
        Volume.Event ev = Volume.Event.OperationFailed;
        VolumeInfo volInfo = volFactory.getVolume(volumeToAttach.getId());
        if (attached) {
            ev = Volume.Event.OperationSucceeded;
            s_logger.debug("Volume: " + volInfo.getName() + " successfully attached to VM: " + volInfo.getAttachedVmName());
        } else {
            s_logger.debug("Volume: " + volInfo.getName() + " failed to attach to VM: " + volInfo.getAttachedVmName());
        }
        volInfo.stateTransit(ev);
    }
    return _volsDao.findById(volumeToAttach.getId());
}
Also used : HashMap(java.util.HashMap) ChapInfo(org.apache.cloudstack.engine.subsystem.api.storage.ChapInfo) VmDiskStatisticsVO(com.cloud.user.VmDiskStatisticsVO) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) HostVO(com.cloud.host.HostVO) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) ServerApiException(org.apache.cloudstack.api.ServerApiException) StorageUnavailableException(com.cloud.exception.StorageUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ExecutionException(java.util.concurrent.ExecutionException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) JsonParseException(com.google.gson.JsonParseException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) MalformedURLException(java.net.MalformedURLException) AttachCommand(org.apache.cloudstack.storage.command.AttachCommand) DataTO(com.cloud.agent.api.to.DataTO) 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.utils.exception.CloudRuntimeException) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) StoragePoolVO(org.apache.cloudstack.storage.datastore.db.StoragePoolVO) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) DiskTO(com.cloud.agent.api.to.DiskTO)

Example 4 with AttachCommand

use of org.apache.cloudstack.storage.command.AttachCommand in project cloudstack by apache.

the class NotAValidCommand method testStorageSubSystemCommand.

@Test
public void testStorageSubSystemCommand() {
    final DiskTO disk = Mockito.mock(DiskTO.class);
    final String vmName = "Test";
    final AttachCommand command = new AttachCommand(disk, vmName);
    final StorageSubsystemCommandHandler handler = Mockito.mock(StorageSubsystemCommandHandler.class);
    when(citrixResourceBase.getStorageHandler()).thenReturn(handler);
    when(handler.handleStorageCommands(command)).thenReturn(new AttachAnswer(disk));
    final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, citrixResourceBase);
    assertTrue(answer.getResult());
}
Also used : RebootAnswer(com.cloud.agent.api.RebootAnswer) CreateAnswer(com.cloud.agent.api.storage.CreateAnswer) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) Answer(com.cloud.agent.api.Answer) StorageSubsystemCommandHandler(com.cloud.storage.resource.StorageSubsystemCommandHandler) AttachCommand(org.apache.cloudstack.storage.command.AttachCommand) DiskTO(com.cloud.agent.api.to.DiskTO) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

DiskTO (com.cloud.agent.api.to.DiskTO)4 AttachCommand (org.apache.cloudstack.storage.command.AttachCommand)4 Answer (com.cloud.agent.api.Answer)3 AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)3 DataTO (com.cloud.agent.api.to.DataTO)2 HostVO (com.cloud.host.HostVO)2 StorageSubsystemCommandHandler (com.cloud.storage.resource.StorageSubsystemCommandHandler)2 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)2 CheckRouterAnswer (com.cloud.agent.api.CheckRouterAnswer)1 Command (com.cloud.agent.api.Command)1 ComputeChecksumCommand (com.cloud.agent.api.ComputeChecksumCommand)1 RebootAnswer (com.cloud.agent.api.RebootAnswer)1 UnsupportedAnswer (com.cloud.agent.api.UnsupportedAnswer)1 CreateAnswer (com.cloud.agent.api.storage.CreateAnswer)1 DestroyCommand (com.cloud.agent.api.storage.DestroyCommand)1 VirtualMachineTO (com.cloud.agent.api.to.VirtualMachineTO)1 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)1 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)1 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)1 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)1