Search in sources :

Example 6 with CheckVirtualMachineAnswer

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

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.getDetails());
            }
        }
    }
    return true;
}
Also used : CheckVirtualMachineAnswer(com.cloud.agent.api.CheckVirtualMachineAnswer) AgentControlAnswer(com.cloud.agent.api.AgentControlAnswer) RebootAnswer(com.cloud.agent.api.RebootAnswer) StartAnswer(com.cloud.agent.api.StartAnswer) RestoreVMSnapshotAnswer(com.cloud.agent.api.RestoreVMSnapshotAnswer) PlugNicAnswer(com.cloud.agent.api.PlugNicAnswer) StopAnswer(com.cloud.agent.api.StopAnswer) Answer(com.cloud.agent.api.Answer) UnPlugNicAnswer(com.cloud.agent.api.UnPlugNicAnswer) ClusterVMMetaDataSyncAnswer(com.cloud.agent.api.ClusterVMMetaDataSyncAnswer) CheckVirtualMachineAnswer(com.cloud.agent.api.CheckVirtualMachineAnswer) 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 7 with CheckVirtualMachineAnswer

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

the class CheckOnAgentInvestigator method isVmAlive.

@Override
public boolean isVmAlive(final VirtualMachine vm, final Host host) throws UnknownVM {
    final CheckVirtualMachineCommand cmd = new CheckVirtualMachineCommand(vm.getInstanceName());
    try {
        final 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 (final AgentUnavailableException e) {
        s_logger.debug("Unable to reach the agent for " + vm.toString() + ": " + e.getMessage());
        throw new UnknownVM();
    } catch (final 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 8 with CheckVirtualMachineAnswer

use of com.cloud.agent.api.CheckVirtualMachineAnswer in project CloudStack-archive by CloudStack-extras.

the class AgentRoutingResource method execute.

protected CheckVirtualMachineAnswer execute(final CheckVirtualMachineCommand cmd) {
    final String vmName = cmd.getVmName();
    CheckVirtualMachineAnswer result = (CheckVirtualMachineAnswer) _simMgr.simulate(cmd, hostGuid);
    State state = result.getState();
    if (state == State.Running) {
        synchronized (_vms) {
            _vms.put(vmName, State.Running);
        }
    }
    return result;
}
Also used : CheckVirtualMachineAnswer(com.cloud.agent.api.CheckVirtualMachineAnswer) State(com.cloud.vm.VirtualMachine.State)

Example 9 with CheckVirtualMachineAnswer

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

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<VirtualMachine.Type, VirtualMachineGuru>();
    //        UserVmManagerImpl userVmManager = mock(UserVmManagerImpl.class);
    //        _vmMgr.registerGuru(VirtualMachine.Type.User, userVmManager);
    // Mock the iteration over all the volumes of an instance.
    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.
    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.
    PrepareForMigrationAnswer prepAnswerMock = mock(PrepareForMigrationAnswer.class);
    when(prepAnswerMock.getResult()).thenReturn(true);
    when(_agentMgr.send(anyLong(), isA(PrepareForMigrationCommand.class))).thenReturn(prepAnswerMock);
    MigrateWithStorageAnswer migAnswerMock = mock(MigrateWithStorageAnswer.class);
    when(migAnswerMock.getResult()).thenReturn(true);
    when(_agentMgr.send(anyLong(), isA(MigrateWithStorageCommand.class))).thenReturn(migAnswerMock);
    MigrateWithStorageReceiveAnswer migRecAnswerMock = mock(MigrateWithStorageReceiveAnswer.class);
    when(migRecAnswerMock.getResult()).thenReturn(true);
    when(_agentMgr.send(anyLong(), isA(MigrateWithStorageReceiveCommand.class))).thenReturn(migRecAnswerMock);
    MigrateWithStorageSendAnswer migSendAnswerMock = mock(MigrateWithStorageSendAnswer.class);
    when(migSendAnswerMock.getResult()).thenReturn(true);
    when(_agentMgr.send(anyLong(), isA(MigrateWithStorageSendCommand.class))).thenReturn(migSendAnswerMock);
    MigrateWithStorageCompleteAnswer migCompleteAnswerMock = mock(MigrateWithStorageCompleteAnswer.class);
    when(migCompleteAnswerMock.getResult()).thenReturn(true);
    when(_agentMgr.send(anyLong(), isA(MigrateWithStorageCompleteCommand.class))).thenReturn(migCompleteAnswerMock);
    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.
    Pair<Long, Long> opaqueMock = new Pair<Long, Long>(_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) HypervisorType(com.cloud.hypervisor.Hypervisor.HypervisorType) ProvisioningType(com.cloud.storage.Storage.ProvisioningType) 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 10 with CheckVirtualMachineAnswer

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

the class LibvirtCheckVirtualMachineCommandWrapper method execute.

@Override
public Answer execute(final CheckVirtualMachineCommand command, final LibvirtComputingResource libvirtComputingResource) {
    try {
        final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
        final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(command.getVmName());
        final PowerState state = libvirtComputingResource.getVmState(conn, command.getVmName());
        Integer vncPort = null;
        if (state == PowerState.PowerOn) {
            vncPort = libvirtComputingResource.getVncPort(conn, command.getVmName());
        }
        return new CheckVirtualMachineAnswer(command, state, vncPort);
    } catch (final LibvirtException e) {
        return new CheckVirtualMachineAnswer(command, e.getMessage());
    }
}
Also used : CheckVirtualMachineAnswer(com.cloud.agent.api.CheckVirtualMachineAnswer) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) PowerState(com.cloud.vm.VirtualMachine.PowerState)

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