Search in sources :

Example 21 with Commands

use of com.cloud.agent.manager.Commands in project cloudstack by apache.

the class VirtualMachineManagerImpl method ensureVmRunningContext.

private void ensureVmRunningContext(final long hostId, VMInstanceVO vm, final Event cause) throws OperationTimedoutException, ResourceUnavailableException, NoTransitionException, InsufficientAddressCapacityException {
    final VirtualMachineGuru vmGuru = getVmGuru(vm);
    s_logger.debug("VM state is starting on full sync so updating it to running");
    vm = _vmDao.findById(vm.getId());
    // grab outstanding work item if any
    final ItWorkVO work = _workDao.findByOutstandingWork(vm.getId(), vm.getState());
    if (work != null) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Found an outstanding work item for this vm " + vm + " in state:" + vm.getState() + ", work id:" + work.getId());
        }
    }
    try {
        stateTransitTo(vm, cause, hostId);
    } catch (final NoTransitionException e1) {
        s_logger.warn(e1.getMessage());
    }
    s_logger.debug("VM's " + vm + " state is starting on full sync so updating it to Running");
    // this should ensure vm has the most
    vm = _vmDao.findById(vm.getId());
    // up to date info
    final VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm);
    final List<NicVO> nics = _nicsDao.listByVmId(profile.getId());
    for (final NicVO nic : nics) {
        final Network network = _networkModel.getNetwork(nic.getNetworkId());
        final NicProfile nicProfile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), null, _networkModel.isSecurityGroupSupportedInNetwork(network), _networkModel.getNetworkTag(profile.getHypervisorType(), network));
        profile.addNic(nicProfile);
    }
    final Commands cmds = new Commands(Command.OnError.Stop);
    s_logger.debug("Finalizing commands that need to be send to complete Start process for the vm " + vm);
    if (vmGuru.finalizeCommandsOnStart(cmds, profile)) {
        if (cmds.size() != 0) {
            _agentMgr.send(vm.getHostId(), cmds);
        }
        if (vmGuru.finalizeStart(profile, vm.getHostId(), cmds, null)) {
            stateTransitTo(vm, cause, vm.getHostId());
        } else {
            s_logger.error("Unable to finish finialization for running vm: " + vm);
        }
    } else {
        s_logger.error("Unable to finalize commands on start for vm: " + vm);
    }
    if (work != null) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Updating outstanding work item to Done, id:" + work.getId());
        }
        work.setStep(Step.Done);
        _workDao.update(work.getId(), work);
    }
}
Also used : NoTransitionException(com.cloud.utils.fsm.NoTransitionException) Network(com.cloud.network.Network) Commands(com.cloud.agent.manager.Commands)

Example 22 with Commands

use of com.cloud.agent.manager.Commands in project cloudstack by apache.

the class UserVmManagerImpl method handleManagedStorage.

private void handleManagedStorage(UserVmVO vm, VolumeVO root) {
    if (Volume.State.Allocated.equals(root.getState())) {
        return;
    }
    StoragePoolVO storagePool = _storagePoolDao.findById(root.getPoolId());
    if (storagePool != null && storagePool.isManaged()) {
        Long hostId = vm.getHostId() != null ? vm.getHostId() : vm.getLastHostId();
        if (hostId != null) {
            VolumeInfo volumeInfo = volFactory.getVolume(root.getId());
            Host host = _hostDao.findById(hostId);
            final Command cmd;
            if (host.getHypervisorType() == HypervisorType.XenServer) {
                DiskTO disk = new DiskTO(volumeInfo.getTO(), root.getDeviceId(), root.getPath(), root.getVolumeType());
                // it's OK in this case to send a detach command to the host for a root volume as this
                // will simply lead to the SR that supports the root volume being removed
                cmd = new DettachCommand(disk, vm.getInstanceName());
                DettachCommand detachCommand = (DettachCommand) cmd;
                detachCommand.setManaged(true);
                detachCommand.setStorageHost(storagePool.getHostAddress());
                detachCommand.setStoragePort(storagePool.getPort());
                detachCommand.set_iScsiName(root.get_iScsiName());
            } else if (host.getHypervisorType() == HypervisorType.VMware) {
                PrimaryDataStore primaryDataStore = (PrimaryDataStore) volumeInfo.getDataStore();
                Map<String, String> details = primaryDataStore.getDetails();
                if (details == null) {
                    details = new HashMap<String, String>();
                    primaryDataStore.setDetails(details);
                }
                details.put(DiskTO.MANAGED, Boolean.TRUE.toString());
                cmd = new DeleteCommand(volumeInfo.getTO());
            } else {
                throw new CloudRuntimeException("This hypervisor type is not supported on managed storage for this command.");
            }
            Commands cmds = new Commands(Command.OnError.Stop);
            cmds.addCommand(cmd);
            try {
                _agentMgr.send(hostId, cmds);
            } catch (Exception ex) {
                throw new CloudRuntimeException(ex.getMessage());
            }
            if (!cmds.isSuccessful()) {
                for (Answer answer : cmds.getAnswers()) {
                    if (!answer.getResult()) {
                        s_logger.warn("Failed to reset vm due to: " + answer.getDetails());
                        throw new CloudRuntimeException("Unable to reset " + vm + " due to " + answer.getDetails());
                    }
                }
            }
            // root.getPoolId() should be null if the VM we are detaching the disk from has never been started before
            DataStore dataStore = root.getPoolId() != null ? _dataStoreMgr.getDataStore(root.getPoolId(), DataStoreRole.Primary) : null;
            volumeMgr.revokeAccess(volFactory.getVolume(root.getId()), host, dataStore);
        }
    }
}
Also used : LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) Host(com.cloud.host.Host) ExecutionException(com.cloud.utils.exception.ExecutionException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) VirtualMachineMigrationException(com.cloud.exception.VirtualMachineMigrationException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) CloudException(com.cloud.exception.CloudException) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException) StorageUnavailableException(com.cloud.exception.StorageUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) ConfigurationException(javax.naming.ConfigurationException) ManagementServerException(com.cloud.exception.ManagementServerException) DeleteCommand(org.apache.cloudstack.storage.command.DeleteCommand) GetVmStatsAnswer(com.cloud.agent.api.GetVmStatsAnswer) Answer(com.cloud.agent.api.Answer) StartAnswer(com.cloud.agent.api.StartAnswer) RestoreVMSnapshotAnswer(com.cloud.agent.api.RestoreVMSnapshotAnswer) GetVmDiskStatsAnswer(com.cloud.agent.api.GetVmDiskStatsAnswer) PvlanSetupCommand(com.cloud.agent.api.PvlanSetupCommand) Command(com.cloud.agent.api.Command) GetVmStatsCommand(com.cloud.agent.api.GetVmStatsCommand) DettachCommand(org.apache.cloudstack.storage.command.DettachCommand) DeleteCommand(org.apache.cloudstack.storage.command.DeleteCommand) GetVmIpAddressCommand(com.cloud.agent.api.GetVmIpAddressCommand) GetVmDiskStatsCommand(com.cloud.agent.api.GetVmDiskStatsCommand) RestoreVMSnapshotCommand(com.cloud.agent.api.RestoreVMSnapshotCommand) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) PrimaryDataStore(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore) StoragePoolVO(org.apache.cloudstack.storage.datastore.db.StoragePoolVO) DettachCommand(org.apache.cloudstack.storage.command.DettachCommand) Commands(com.cloud.agent.manager.Commands) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) DiskTO(com.cloud.agent.api.to.DiskTO) PrimaryDataStore(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore)

Example 23 with Commands

use of com.cloud.agent.manager.Commands in project cloudstack by apache.

the class NetworkHelperImplTest method testSendCommandsToRouterWithTrueResult.

/**
     * The only way result can be true is if each and every command receive a true result
     *
     * @throws AgentUnavailableException
     * @throws OperationTimedoutException
     */
@Test
public void testSendCommandsToRouterWithTrueResult() throws AgentUnavailableException, OperationTimedoutException, ResourceUnavailableException {
    // Prepare
    NetworkHelperImpl nwHelperUT = spy(this.nwHelper);
    VirtualRouter vr = mock(VirtualRouter.class);
    when(vr.getHostId()).thenReturn(HOST_ID);
    doReturn(true).when(nwHelperUT).checkRouterVersion(vr);
    Commands commands = mock(Commands.class);
    when(commands.size()).thenReturn(3);
    Answer answer1 = mock(Answer.class);
    Answer answer2 = mock(Answer.class);
    Answer answer3 = mock(Answer.class);
    // In the second iteration it should match and return, without invoking the third
    Answer[] answers = { answer1, answer2, answer3 };
    when(answer1.getResult()).thenReturn(true);
    when(answer2.getResult()).thenReturn(true);
    when(answer3.getResult()).thenReturn(true);
    when(this.agentManager.send(HOST_ID, commands)).thenReturn(answers);
    // Execute
    final boolean result = nwHelperUT.sendCommandsToRouter(vr, commands);
    // Assert
    verify(this.agentManager, times(1)).send(HOST_ID, commands);
    verify(answer1, times(1)).getResult();
    verify(answer2, times(1)).getResult();
    verify(answer3, times(1)).getResult();
    assertTrue(result);
}
Also used : Answer(com.cloud.agent.api.Answer) Commands(com.cloud.agent.manager.Commands) Test(org.junit.Test)

Example 24 with Commands

use of com.cloud.agent.manager.Commands in project cloudstack by apache.

the class NetworkHelperImplTest method testSendCommandsToRouterWithNoAnswers.

/**
     * If the number of answers is different to the number of commands the result is false
     *
     * @throws AgentUnavailableException
     * @throws OperationTimedoutException
     */
@Test
public void testSendCommandsToRouterWithNoAnswers() throws AgentUnavailableException, OperationTimedoutException, ResourceUnavailableException {
    // Prepare
    NetworkHelperImpl nwHelperUT = spy(this.nwHelper);
    VirtualRouter vr = mock(VirtualRouter.class);
    when(vr.getHostId()).thenReturn(HOST_ID);
    doReturn(true).when(nwHelperUT).checkRouterVersion(vr);
    Commands commands = mock(Commands.class);
    when(commands.size()).thenReturn(3);
    Answer answer1 = mock(Answer.class);
    Answer answer2 = mock(Answer.class);
    // In the second iteration it should match and return, without invoking the third
    Answer[] answers = { answer1, answer2 };
    when(this.agentManager.send(HOST_ID, commands)).thenReturn(answers);
    // Execute
    final boolean result = nwHelperUT.sendCommandsToRouter(vr, commands);
    // Assert
    verify(this.agentManager, times(1)).send(HOST_ID, commands);
    verify(answer1, times(0)).getResult();
    assertFalse(result);
}
Also used : Answer(com.cloud.agent.api.Answer) Commands(com.cloud.agent.manager.Commands) Test(org.junit.Test)

Example 25 with Commands

use of com.cloud.agent.manager.Commands in project cloudstack by apache.

the class NetworkHelperImplTest method testSendCommandsToRouter.

@Test
public void testSendCommandsToRouter() throws AgentUnavailableException, OperationTimedoutException, ResourceUnavailableException {
    // Prepare
    NetworkHelperImpl nwHelperUT = spy(this.nwHelper);
    VirtualRouter vr = mock(VirtualRouter.class);
    when(vr.getHostId()).thenReturn(HOST_ID);
    doReturn(true).when(nwHelperUT).checkRouterVersion(vr);
    Commands commands = mock(Commands.class);
    when(commands.size()).thenReturn(3);
    Answer answer1 = mock(Answer.class);
    Answer answer2 = mock(Answer.class);
    Answer answer3 = mock(Answer.class);
    // In the second iteration it should match and return, without invoking the third
    Answer[] answers = { answer1, answer2, answer3 };
    when(answer1.getResult()).thenReturn(true);
    when(answer2.getResult()).thenReturn(false);
    when(answer3.getResult()).thenReturn(false);
    when(this.agentManager.send(HOST_ID, commands)).thenReturn(answers);
    // Execute
    final boolean result = nwHelperUT.sendCommandsToRouter(vr, commands);
    // Assert
    verify(this.agentManager, times(1)).send(HOST_ID, commands);
    verify(answer1, times(1)).getResult();
    verify(answer2, times(1)).getResult();
    verify(answer3, times(0)).getResult();
    assertFalse(result);
}
Also used : Answer(com.cloud.agent.api.Answer) Commands(com.cloud.agent.manager.Commands) Test(org.junit.Test)

Aggregations

Commands (com.cloud.agent.manager.Commands)64 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)22 VirtualRouter (com.cloud.network.router.VirtualRouter)19 OperationTimedoutException (com.cloud.exception.OperationTimedoutException)17 Answer (com.cloud.agent.api.Answer)16 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)13 Network (com.cloud.network.Network)13 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)11 NicVO (com.cloud.vm.NicVO)10 UnPlugNicAnswer (com.cloud.agent.api.UnPlugNicAnswer)9 ArrayList (java.util.ArrayList)9 PlugNicAnswer (com.cloud.agent.api.PlugNicAnswer)8 UserVmVO (com.cloud.vm.UserVmVO)8 DataCenter (com.cloud.dc.DataCenter)7 NoTransitionException (com.cloud.utils.fsm.NoTransitionException)7 VirtualMachineProfile (com.cloud.vm.VirtualMachineProfile)7 AgentControlAnswer (com.cloud.agent.api.AgentControlAnswer)6 RestoreVMSnapshotAnswer (com.cloud.agent.api.RestoreVMSnapshotAnswer)6 StartAnswer (com.cloud.agent.api.StartAnswer)6 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)6