Search in sources :

Example 1 with PrepareForMigrationAnswer

use of com.cloud.legacymodel.communication.answer.PrepareForMigrationAnswer in project cosmic by MissionCriticalCloud.

the class CitrixPrepareForMigrationCommandWrapper method execute.

@Override
public Answer execute(final PrepareForMigrationCommand command, final CitrixResourceBase citrixResourceBase) {
    final Connection conn = citrixResourceBase.getConnection();
    final VirtualMachineTO vm = command.getVirtualMachine();
    final List<String[]> vmDataList = vm.getVmData();
    String configDriveLabel = vm.getConfigDriveLabel();
    if (configDriveLabel == null) {
        configDriveLabel = "config";
    }
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Preparing host for migrating " + vm);
    }
    final NicTO[] nics = vm.getNics();
    try {
        citrixResourceBase.prepareISO(conn, vm.getName(), vmDataList, configDriveLabel);
        for (final NicTO nic : nics) {
            citrixResourceBase.getNetwork(conn, nic);
        }
        s_logger.debug("4. The VM " + vm.getName() + " is in Migrating state");
        return new PrepareForMigrationAnswer(command);
    } catch (final Exception e) {
        s_logger.warn("Catch Exception " + e.getClass().getName() + " prepare for migration failed due to " + e.toString(), e);
        return new PrepareForMigrationAnswer(command, e);
    }
}
Also used : Connection(com.xensource.xenapi.Connection) VirtualMachineTO(com.cloud.legacymodel.to.VirtualMachineTO) NicTO(com.cloud.legacymodel.to.NicTO) PrepareForMigrationAnswer(com.cloud.legacymodel.communication.answer.PrepareForMigrationAnswer)

Example 2 with PrepareForMigrationAnswer

use of com.cloud.legacymodel.communication.answer.PrepareForMigrationAnswer in project cosmic by MissionCriticalCloud.

the class LibvirtPrepareForMigrationCommandWrapper method execute.

@Override
public Answer execute(final PrepareForMigrationCommand command, final LibvirtComputingResource libvirtComputingResource) {
    final VirtualMachineTO vm = command.getVirtualMachine();
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Preparing host for migrating " + vm);
    }
    final NicTO[] nics = vm.getNics();
    boolean skipDisconnect = false;
    final KvmStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
    try {
        final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
        final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(vm.getName());
        for (final NicTO nic : nics) {
            libvirtComputingResource.getVifDriver(nic.getType()).plug(nic, null, "");
        }
        /* setup disks, e.g for iso */
        final DiskTO[] volumes = vm.getDisks();
        for (final DiskTO volume : volumes) {
            if (volume.getType() == VolumeType.ISO) {
                libvirtComputingResource.getVolumePath(conn, volume);
            }
        }
        skipDisconnect = true;
        if (!storagePoolMgr.connectPhysicalDisksViaVmSpec(vm)) {
            return new PrepareForMigrationAnswer(command, "failed to connect physical disks to host");
        }
        return new PrepareForMigrationAnswer(command);
    } catch (final LibvirtException e) {
        return new PrepareForMigrationAnswer(command, e.toString());
    } catch (final InternalErrorException e) {
        return new PrepareForMigrationAnswer(command, e.toString());
    } catch (final URISyntaxException e) {
        return new PrepareForMigrationAnswer(command, e.toString());
    } finally {
        if (!skipDisconnect) {
            storagePoolMgr.disconnectPhysicalDisksViaVmSpec(vm);
        }
    }
}
Also used : LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) InternalErrorException(com.cloud.legacymodel.exceptions.InternalErrorException) URISyntaxException(java.net.URISyntaxException) VirtualMachineTO(com.cloud.legacymodel.to.VirtualMachineTO) PrepareForMigrationAnswer(com.cloud.legacymodel.communication.answer.PrepareForMigrationAnswer) KvmStoragePoolManager(com.cloud.agent.resource.kvm.storage.KvmStoragePoolManager) NicTO(com.cloud.legacymodel.to.NicTO) DiskTO(com.cloud.legacymodel.to.DiskTO)

Example 3 with PrepareForMigrationAnswer

use of com.cloud.legacymodel.communication.answer.PrepareForMigrationAnswer in project cosmic by MissionCriticalCloud.

the class VirtualMachineManagerImplTest method initializeMockConfigForMigratingVmWithVolumes.

private void initializeMockConfigForMigratingVmWithVolumes() throws OperationTimedoutException, ResourceUnavailableException {
    // Mock the source and destination hosts.
    when(_srcHostMock.getId()).thenReturn(5L);
    when(_destHostMock.getId()).thenReturn(6L);
    when(_hostDao.findById(5L)).thenReturn(_srcHostMock);
    when(_hostDao.findById(6L)).thenReturn(_destHostMock);
    // Mock the vm being migrated.
    when(_vmMock.getId()).thenReturn(1L);
    when(_vmMock.getHypervisorType()).thenReturn(HypervisorType.XenServer);
    when(_vmMock.getState()).thenReturn(State.Running).thenReturn(State.Running).thenReturn(State.Migrating).thenReturn(State.Migrating);
    when(_vmMock.getHostId()).thenReturn(5L);
    when(_vmInstance.getId()).thenReturn(1L);
    when(_vmInstance.getServiceOfferingId()).thenReturn(2L);
    when(_vmInstance.getInstanceName()).thenReturn("myVm");
    when(_vmInstance.getHostId()).thenReturn(5L);
    when(_vmInstance.getType()).thenReturn(VirtualMachineType.User);
    when(_vmInstance.getState()).thenReturn(State.Running).thenReturn(State.Running).thenReturn(State.Migrating).thenReturn(State.Migrating);
    // Mock the work item.
    when(_workDao.persist(any(ItWorkVO.class))).thenReturn(_work);
    when(_workDao.update("1", _work)).thenReturn(true);
    when(_work.getId()).thenReturn("1");
    doNothing().when(_work).setStep(ItWorkVO.Step.Done);
    // Mock the vm guru and the user vm object that gets returned.
    _vmMgr._vmGurus = new HashMap<>();
    // UserVmManagerImpl userVmManager = mock(UserVmManagerImpl.class);
    // _vmMgr.registerGuru(VirtualMachine.Type.User, userVmManager);
    // Mock the iteration over all the volumes of an instance.
    final Iterator<VolumeVO> volumeIterator = mock(Iterator.class);
    when(_volsDao.findUsableVolumesForInstance(anyLong())).thenReturn(_rootVols);
    when(_rootVols.iterator()).thenReturn(volumeIterator);
    when(volumeIterator.hasNext()).thenReturn(true, false);
    when(volumeIterator.next()).thenReturn(_volumeMock);
    // Mock the disk offering and pool objects for a volume.
    when(_volumeMock.getDiskOfferingId()).thenReturn(5L);
    when(_volumeMock.getPoolId()).thenReturn(200L);
    when(_volumeMock.getId()).thenReturn(5L);
    when(_diskOfferingDao.findById(anyLong())).thenReturn(_diskOfferingMock);
    when(_storagePoolDao.findById(200L)).thenReturn(_srcStoragePoolMock);
    when(_storagePoolDao.findById(201L)).thenReturn(_destStoragePoolMock);
    // Mock the volume to pool mapping.
    when(_volumeToPoolMock.get(5L)).thenReturn(201L);
    when(_destStoragePoolMock.getId()).thenReturn(201L);
    when(_srcStoragePoolMock.getId()).thenReturn(200L);
    when(_destStoragePoolMock.isLocal()).thenReturn(false);
    when(_diskOfferingMock.getUseLocalStorage()).thenReturn(false);
    when(_poolHostDao.findByPoolHost(anyLong(), anyLong())).thenReturn(mock(StoragePoolHostVO.class));
    // Mock hypervisor guru.
    final HypervisorGuru guruMock = mock(HypervisorGuru.class);
    when(_hvGuruMgr.getGuru(HypervisorType.XenServer)).thenReturn(guruMock);
    when(_srcHostMock.getClusterId()).thenReturn(3L);
    when(_destHostMock.getClusterId()).thenReturn(3L);
    // Mock the commands and answers to the agent.
    final PrepareForMigrationAnswer prepAnswerMock = mock(PrepareForMigrationAnswer.class);
    when(prepAnswerMock.getResult()).thenReturn(true);
    when(_agentMgr.send(anyLong(), isA(PrepareForMigrationCommand.class))).thenReturn(prepAnswerMock);
    final MigrateWithStorageAnswer migAnswerMock = mock(MigrateWithStorageAnswer.class);
    when(migAnswerMock.getResult()).thenReturn(true);
    when(_agentMgr.send(anyLong(), isA(MigrateWithStorageCommand.class))).thenReturn(migAnswerMock);
    final MigrateWithStorageReceiveAnswer migRecAnswerMock = mock(MigrateWithStorageReceiveAnswer.class);
    when(migRecAnswerMock.getResult()).thenReturn(true);
    when(_agentMgr.send(anyLong(), isA(MigrateWithStorageReceiveCommand.class))).thenReturn(migRecAnswerMock);
    final MigrateWithStorageSendAnswer migSendAnswerMock = mock(MigrateWithStorageSendAnswer.class);
    when(migSendAnswerMock.getResult()).thenReturn(true);
    when(_agentMgr.send(anyLong(), isA(MigrateWithStorageSendCommand.class))).thenReturn(migSendAnswerMock);
    final MigrateWithStorageCompleteAnswer migCompleteAnswerMock = mock(MigrateWithStorageCompleteAnswer.class);
    when(migCompleteAnswerMock.getResult()).thenReturn(true);
    when(_agentMgr.send(anyLong(), isA(MigrateWithStorageCompleteCommand.class))).thenReturn(migCompleteAnswerMock);
    final CheckVirtualMachineAnswer checkVmAnswerMock = mock(CheckVirtualMachineAnswer.class);
    when(checkVmAnswerMock.getResult()).thenReturn(true);
    when(checkVmAnswerMock.getState()).thenReturn(PowerState.PowerOn);
    when(_agentMgr.send(anyLong(), isA(CheckVirtualMachineCommand.class))).thenReturn(checkVmAnswerMock);
    // Mock the state transitions of vm.
    final Pair<Long, Long> opaqueMock = new Pair<>(_vmMock.getHostId(), _destHostMock.getId());
    when(_vmSnapshotMgr.hasActiveVMSnapshotTasks(anyLong())).thenReturn(false);
    when(_vmInstanceDao.updateState(State.Running, Event.MigrationRequested, State.Migrating, _vmMock, opaqueMock)).thenReturn(true);
    when(_vmInstanceDao.updateState(State.Migrating, Event.OperationSucceeded, State.Running, _vmMock, opaqueMock)).thenReturn(true);
}
Also used : MigrateWithStorageAnswer(com.cloud.legacymodel.communication.answer.MigrateWithStorageAnswer) CheckVirtualMachineAnswer(com.cloud.legacymodel.communication.answer.CheckVirtualMachineAnswer) MigrateWithStorageCommand(com.cloud.legacymodel.communication.command.MigrateWithStorageCommand) MigrateWithStorageSendAnswer(com.cloud.legacymodel.communication.answer.MigrateWithStorageSendAnswer) StoragePoolHostVO(com.cloud.storage.StoragePoolHostVO) MigrateWithStorageCompleteCommand(com.cloud.legacymodel.communication.command.MigrateWithStorageCompleteCommand) MigrateWithStorageCompleteAnswer(com.cloud.legacymodel.communication.answer.MigrateWithStorageCompleteAnswer) PrepareForMigrationAnswer(com.cloud.legacymodel.communication.answer.PrepareForMigrationAnswer) MigrateWithStorageReceiveCommand(com.cloud.legacymodel.communication.command.MigrateWithStorageReceiveCommand) MigrateWithStorageReceiveAnswer(com.cloud.legacymodel.communication.answer.MigrateWithStorageReceiveAnswer) HypervisorGuru(com.cloud.hypervisor.HypervisorGuru) VolumeVO(com.cloud.storage.VolumeVO) PrepareForMigrationCommand(com.cloud.legacymodel.communication.command.PrepareForMigrationCommand) Matchers.anyLong(org.mockito.Matchers.anyLong) MigrateWithStorageSendCommand(com.cloud.legacymodel.communication.command.MigrateWithStorageSendCommand) CheckVirtualMachineCommand(com.cloud.legacymodel.communication.command.CheckVirtualMachineCommand) Pair(com.cloud.legacymodel.utils.Pair)

Aggregations

PrepareForMigrationAnswer (com.cloud.legacymodel.communication.answer.PrepareForMigrationAnswer)3 NicTO (com.cloud.legacymodel.to.NicTO)2 VirtualMachineTO (com.cloud.legacymodel.to.VirtualMachineTO)2 KvmStoragePoolManager (com.cloud.agent.resource.kvm.storage.KvmStoragePoolManager)1 HypervisorGuru (com.cloud.hypervisor.HypervisorGuru)1 CheckVirtualMachineAnswer (com.cloud.legacymodel.communication.answer.CheckVirtualMachineAnswer)1 MigrateWithStorageAnswer (com.cloud.legacymodel.communication.answer.MigrateWithStorageAnswer)1 MigrateWithStorageCompleteAnswer (com.cloud.legacymodel.communication.answer.MigrateWithStorageCompleteAnswer)1 MigrateWithStorageReceiveAnswer (com.cloud.legacymodel.communication.answer.MigrateWithStorageReceiveAnswer)1 MigrateWithStorageSendAnswer (com.cloud.legacymodel.communication.answer.MigrateWithStorageSendAnswer)1 CheckVirtualMachineCommand (com.cloud.legacymodel.communication.command.CheckVirtualMachineCommand)1 MigrateWithStorageCommand (com.cloud.legacymodel.communication.command.MigrateWithStorageCommand)1 MigrateWithStorageCompleteCommand (com.cloud.legacymodel.communication.command.MigrateWithStorageCompleteCommand)1 MigrateWithStorageReceiveCommand (com.cloud.legacymodel.communication.command.MigrateWithStorageReceiveCommand)1 MigrateWithStorageSendCommand (com.cloud.legacymodel.communication.command.MigrateWithStorageSendCommand)1 PrepareForMigrationCommand (com.cloud.legacymodel.communication.command.PrepareForMigrationCommand)1 InternalErrorException (com.cloud.legacymodel.exceptions.InternalErrorException)1 DiskTO (com.cloud.legacymodel.to.DiskTO)1 Pair (com.cloud.legacymodel.utils.Pair)1 StoragePoolHostVO (com.cloud.storage.StoragePoolHostVO)1