Search in sources :

Example 1 with AttachCommand

use of com.cloud.storage.command.AttachCommand 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)

Example 2 with AttachCommand

use of com.cloud.storage.command.AttachCommand in project cosmic by MissionCriticalCloud.

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) Answer(com.cloud.agent.api.Answer) AttachAnswer(com.cloud.storage.command.AttachAnswer) StorageSubsystemCommandHandler(com.cloud.storage.resource.StorageSubsystemCommandHandler) AttachCommand(com.cloud.storage.command.AttachCommand) DiskTO(com.cloud.agent.api.to.DiskTO) AttachAnswer(com.cloud.storage.command.AttachAnswer) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 3 with AttachCommand

use of com.cloud.storage.command.AttachCommand 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 = _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 = _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, Volume.Type.ISO);
    final Command cmd;
    if (attach) {
        cmd = new AttachCommand(disk, vmName);
    } else {
        cmd = new DettachCommand(disk, vmName);
    }
    final Answer a = _agentMgr.easySend(vm.getHostId(), cmd);
    return a != null && a.getResult();
}
Also used : Answer(com.cloud.agent.api.Answer) UserVmVO(com.cloud.vm.UserVmVO) TemplateInfo(com.cloud.engine.subsystem.api.storage.TemplateInfo) DataTO(com.cloud.agent.api.to.DataTO) DettachCommand(com.cloud.storage.command.DettachCommand) DestroyCommand(com.cloud.agent.api.storage.DestroyCommand) Command(com.cloud.agent.api.Command) TemplateOrVolumePostUploadCommand(com.cloud.storage.command.TemplateOrVolumePostUploadCommand) AttachCommand(com.cloud.storage.command.AttachCommand) ComputeChecksumCommand(com.cloud.agent.api.ComputeChecksumCommand) DettachCommand(com.cloud.storage.command.DettachCommand) StoragePoolHostVO(com.cloud.storage.StoragePoolHostVO) VMTemplateHostVO(com.cloud.storage.VMTemplateHostVO) HostVO(com.cloud.host.HostVO) AttachCommand(com.cloud.storage.command.AttachCommand) DiskTO(com.cloud.agent.api.to.DiskTO)

Example 4 with AttachCommand

use of com.cloud.storage.command.AttachCommand in project cosmic by MissionCriticalCloud.

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 : Answer(com.cloud.agent.api.Answer) CheckRouterAnswer(com.cloud.agent.api.CheckRouterAnswer) AttachAnswer(com.cloud.storage.command.AttachAnswer) LibvirtRequestWrapper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper) StorageSubsystemCommandHandler(com.cloud.storage.resource.StorageSubsystemCommandHandler) AttachCommand(com.cloud.storage.command.AttachCommand) DiskTO(com.cloud.agent.api.to.DiskTO) AttachAnswer(com.cloud.storage.command.AttachAnswer) Test(org.junit.Test)

Aggregations

DiskTO (com.cloud.agent.api.to.DiskTO)4 AttachCommand (com.cloud.storage.command.AttachCommand)4 Answer (com.cloud.agent.api.Answer)3 AttachAnswer (com.cloud.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 Test (org.junit.Test)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 CreateAnswer (com.cloud.agent.api.storage.CreateAnswer)1 DestroyCommand (com.cloud.agent.api.storage.DestroyCommand)1 ChapInfo (com.cloud.engine.subsystem.api.storage.ChapInfo)1 DataStore (com.cloud.engine.subsystem.api.storage.DataStore)1 TemplateInfo (com.cloud.engine.subsystem.api.storage.TemplateInfo)1 CloudException (com.cloud.exception.CloudException)1 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)1 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)1