Search in sources :

Example 11 with CheckVirtualMachineAnswer

use of com.cloud.agent.api.CheckVirtualMachineAnswer 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(VirtualMachine.Type.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.agent.api.MigrateWithStorageAnswer) CheckVirtualMachineAnswer(com.cloud.agent.api.CheckVirtualMachineAnswer) MigrateWithStorageCommand(com.cloud.agent.api.MigrateWithStorageCommand) MigrateWithStorageSendAnswer(com.cloud.agent.api.MigrateWithStorageSendAnswer) StoragePoolHostVO(com.cloud.storage.StoragePoolHostVO) MigrateWithStorageCompleteCommand(com.cloud.agent.api.MigrateWithStorageCompleteCommand) MigrateWithStorageCompleteAnswer(com.cloud.agent.api.MigrateWithStorageCompleteAnswer) PrepareForMigrationAnswer(com.cloud.agent.api.PrepareForMigrationAnswer) MigrateWithStorageReceiveCommand(com.cloud.agent.api.MigrateWithStorageReceiveCommand) MigrateWithStorageReceiveAnswer(com.cloud.agent.api.MigrateWithStorageReceiveAnswer) HypervisorGuru(com.cloud.hypervisor.HypervisorGuru) VolumeVO(com.cloud.storage.VolumeVO) PrepareForMigrationCommand(com.cloud.agent.api.PrepareForMigrationCommand) Matchers.anyLong(org.mockito.Matchers.anyLong) MigrateWithStorageSendCommand(com.cloud.agent.api.MigrateWithStorageSendCommand) CheckVirtualMachineCommand(com.cloud.agent.api.CheckVirtualMachineCommand) Pair(com.cloud.utils.Pair)

Example 12 with CheckVirtualMachineAnswer

use of com.cloud.agent.api.CheckVirtualMachineAnswer in project cosmic by MissionCriticalCloud.

the class CitrixCheckVirtualMachineCommandWrapper method execute.

@Override
public Answer execute(final CheckVirtualMachineCommand command, final CitrixResourceBase citrixResourceBase) {
    final Connection conn = citrixResourceBase.getConnection();
    final String vmName = command.getVmName();
    final PowerState powerState = citrixResourceBase.getVmState(conn, vmName);
    final Integer vncPort = null;
    if (powerState == PowerState.PowerOn) {
        s_logger.debug("3. The VM " + vmName + " is in Running state");
    }
    return new CheckVirtualMachineAnswer(command, powerState, vncPort);
}
Also used : CheckVirtualMachineAnswer(com.cloud.agent.api.CheckVirtualMachineAnswer) Connection(com.xensource.xenapi.Connection) PowerState(com.cloud.vm.VirtualMachine.PowerState)

Example 13 with CheckVirtualMachineAnswer

use of com.cloud.agent.api.CheckVirtualMachineAnswer in project cloudstack by apache.

the class VirtualMachineManagerImpl method checkVmOnHost.

protected boolean checkVmOnHost(final VirtualMachine vm, final long hostId) throws AgentUnavailableException, OperationTimedoutException {
    final Answer answer = _agentMgr.send(hostId, new CheckVirtualMachineCommand(vm.getInstanceName()));
    if (answer == null || !answer.getResult()) {
        return false;
    }
    if (answer instanceof CheckVirtualMachineAnswer) {
        final CheckVirtualMachineAnswer vmAnswer = (CheckVirtualMachineAnswer) answer;
        if (vmAnswer.getState() == PowerState.PowerOff) {
            return false;
        }
    }
    UserVmVO userVm = _userVmDao.findById(vm.getId());
    if (userVm != null) {
        List<VMSnapshotVO> vmSnapshots = _vmSnapshotDao.findByVm(vm.getId());
        RestoreVMSnapshotCommand command = _vmSnapshotMgr.createRestoreCommand(userVm, vmSnapshots);
        if (command != null) {
            RestoreVMSnapshotAnswer restoreVMSnapshotAnswer = (RestoreVMSnapshotAnswer) _agentMgr.send(hostId, command);
            if (restoreVMSnapshotAnswer == null || !restoreVMSnapshotAnswer.getResult()) {
                s_logger.warn("Unable to restore the vm snapshot from image file after live migration of vm with vmsnapshots: " + restoreVMSnapshotAnswer == null ? "null answer" : restoreVMSnapshotAnswer.getDetails());
            }
        }
    }
    return true;
}
Also used : CheckVirtualMachineAnswer(com.cloud.agent.api.CheckVirtualMachineAnswer) MigrateVmToPoolAnswer(com.cloud.agent.api.MigrateVmToPoolAnswer) StopAnswer(com.cloud.agent.api.StopAnswer) Answer(com.cloud.agent.api.Answer) UnPlugNicAnswer(com.cloud.agent.api.UnPlugNicAnswer) AgentControlAnswer(com.cloud.agent.api.AgentControlAnswer) RebootAnswer(com.cloud.agent.api.RebootAnswer) StartAnswer(com.cloud.agent.api.StartAnswer) ReplugNicAnswer(com.cloud.agent.api.ReplugNicAnswer) RestoreVMSnapshotAnswer(com.cloud.agent.api.RestoreVMSnapshotAnswer) PlugNicAnswer(com.cloud.agent.api.PlugNicAnswer) ClusterVMMetaDataSyncAnswer(com.cloud.agent.api.ClusterVMMetaDataSyncAnswer) CheckVirtualMachineAnswer(com.cloud.agent.api.CheckVirtualMachineAnswer) PrepareForMigrationAnswer(com.cloud.agent.api.PrepareForMigrationAnswer) VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) RestoreVMSnapshotCommand(com.cloud.agent.api.RestoreVMSnapshotCommand) RestoreVMSnapshotAnswer(com.cloud.agent.api.RestoreVMSnapshotAnswer) CheckVirtualMachineCommand(com.cloud.agent.api.CheckVirtualMachineCommand)

Example 14 with CheckVirtualMachineAnswer

use of com.cloud.agent.api.CheckVirtualMachineAnswer in project cloudstack by apache.

the class CheckOnAgentInvestigator method isVmAlive.

@Override
public boolean isVmAlive(VirtualMachine vm, Host host) throws UnknownVM {
    CheckVirtualMachineCommand cmd = new CheckVirtualMachineCommand(vm.getInstanceName());
    try {
        CheckVirtualMachineAnswer answer = (CheckVirtualMachineAnswer) _agentMgr.send(vm.getHostId(), cmd);
        if (!answer.getResult()) {
            s_logger.debug("Unable to get vm state on " + vm.toString());
            throw new UnknownVM();
        }
        s_logger.debug("Agent responded with state " + answer.getState().toString());
        return answer.getState() == PowerState.PowerOn;
    } catch (AgentUnavailableException e) {
        s_logger.debug("Unable to reach the agent for " + vm.toString() + ": " + e.getMessage());
        throw new UnknownVM();
    } catch (OperationTimedoutException e) {
        s_logger.debug("Operation timed out for " + vm.toString() + ": " + e.getMessage());
        throw new UnknownVM();
    }
}
Also used : CheckVirtualMachineAnswer(com.cloud.agent.api.CheckVirtualMachineAnswer) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) CheckVirtualMachineCommand(com.cloud.agent.api.CheckVirtualMachineCommand)

Example 15 with CheckVirtualMachineAnswer

use of com.cloud.agent.api.CheckVirtualMachineAnswer in project cloudstack by apache.

the class SimulatorInvestigator method isVmAlive.

@Override
public boolean isVmAlive(VirtualMachine vm, Host host) throws UnknownVM {
    if (haManager.isHAEligible(host)) {
        return haManager.isVMAliveOnHost(host);
    }
    CheckVirtualMachineCommand cmd = new CheckVirtualMachineCommand(vm.getInstanceName());
    try {
        Answer answer = _agentMgr.send(vm.getHostId(), cmd);
        if (!answer.getResult()) {
            s_logger.debug("Unable to get vm state on " + vm.toString());
            throw new UnknownVM();
        }
        CheckVirtualMachineAnswer cvmAnswer = (CheckVirtualMachineAnswer) answer;
        s_logger.debug("Agent responded with state " + cvmAnswer.getState().toString());
        return cvmAnswer.getState() == PowerState.PowerOn;
    } catch (AgentUnavailableException e) {
        s_logger.debug("Unable to reach the agent for " + vm.toString() + ": " + e.getMessage());
        throw new UnknownVM();
    } catch (OperationTimedoutException e) {
        s_logger.debug("Operation timed out for " + vm.toString() + ": " + e.getMessage());
        throw new UnknownVM();
    }
}
Also used : CheckVirtualMachineAnswer(com.cloud.agent.api.CheckVirtualMachineAnswer) CheckVirtualMachineAnswer(com.cloud.agent.api.CheckVirtualMachineAnswer) Answer(com.cloud.agent.api.Answer) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) CheckVirtualMachineCommand(com.cloud.agent.api.CheckVirtualMachineCommand)

Aggregations

CheckVirtualMachineAnswer (com.cloud.agent.api.CheckVirtualMachineAnswer)19 CheckVirtualMachineCommand (com.cloud.agent.api.CheckVirtualMachineCommand)7 PowerState (com.cloud.vm.VirtualMachine.PowerState)7 Answer (com.cloud.agent.api.Answer)3 PrepareForMigrationAnswer (com.cloud.agent.api.PrepareForMigrationAnswer)3 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)3 OperationTimedoutException (com.cloud.exception.OperationTimedoutException)3 State (com.cloud.vm.VirtualMachine.State)3 AgentControlAnswer (com.cloud.agent.api.AgentControlAnswer)2 ClusterVMMetaDataSyncAnswer (com.cloud.agent.api.ClusterVMMetaDataSyncAnswer)2 MigrateWithStorageAnswer (com.cloud.agent.api.MigrateWithStorageAnswer)2 MigrateWithStorageCommand (com.cloud.agent.api.MigrateWithStorageCommand)2 MigrateWithStorageCompleteAnswer (com.cloud.agent.api.MigrateWithStorageCompleteAnswer)2 MigrateWithStorageCompleteCommand (com.cloud.agent.api.MigrateWithStorageCompleteCommand)2 MigrateWithStorageReceiveAnswer (com.cloud.agent.api.MigrateWithStorageReceiveAnswer)2 MigrateWithStorageReceiveCommand (com.cloud.agent.api.MigrateWithStorageReceiveCommand)2 MigrateWithStorageSendAnswer (com.cloud.agent.api.MigrateWithStorageSendAnswer)2 MigrateWithStorageSendCommand (com.cloud.agent.api.MigrateWithStorageSendCommand)2 PlugNicAnswer (com.cloud.agent.api.PlugNicAnswer)2 PrepareForMigrationCommand (com.cloud.agent.api.PrepareForMigrationCommand)2