Search in sources :

Example 26 with State

use of com.cloud.vm.VirtualMachine.State in project CloudStack-archive by CloudStack-extras.

the class AgentRoutingResource method initialize.

@Override
public StartupCommand[] initialize() {
    synchronized (_vms) {
        _vms.clear();
    }
    Map<String, State> changes = _simMgr.getVmStates(this.hostGuid);
    Map<String, MockVMVO> vmsMaps = _simMgr.getVms(this.hostGuid);
    totalCpu = agentHost.getCpuCount() * agentHost.getCpuSpeed();
    totalMem = agentHost.getMemorySize();
    for (Map.Entry<String, MockVMVO> entry : vmsMaps.entrySet()) {
        MockVMVO vm = entry.getValue();
        usedCpu += vm.getCpu();
        usedMem += vm.getMemory();
        _runningVms.put(entry.getKey(), new Pair<Long, Long>(Long.valueOf(vm.getCpu()), vm.getMemory()));
    }
    List<Object> info = getHostInfo();
    StartupRoutingCommand cmd = new StartupRoutingCommand((Integer) info.get(0), (Long) info.get(1), (Long) info.get(2), (Long) info.get(4), (String) info.get(3), HypervisorType.Simulator, RouterPrivateIpStrategy.HostLocal);
    cmd.setStateChanges(changes);
    Map<String, String> hostDetails = new HashMap<String, String>();
    hostDetails.put(RouterPrivateIpStrategy.class.getCanonicalName(), RouterPrivateIpStrategy.DcGlobal.toString());
    cmd.setHostDetails(hostDetails);
    cmd.setAgentTag("agent-simulator");
    cmd.setPrivateIpAddress(agentHost.getPrivateIpAddress());
    cmd.setPrivateNetmask(agentHost.getPrivateNetMask());
    cmd.setPrivateMacAddress(agentHost.getPrivateMacAddress());
    cmd.setStorageIpAddress(agentHost.getStorageIpAddress());
    cmd.setStorageNetmask(agentHost.getStorageNetMask());
    cmd.setStorageMacAddress(agentHost.getStorageMacAddress());
    cmd.setStorageIpAddressDeux(agentHost.getStorageIpAddress());
    cmd.setStorageNetmaskDeux(agentHost.getStorageNetMask());
    cmd.setStorageMacAddressDeux(agentHost.getStorageIpAddress());
    cmd.setName(agentHost.getName());
    cmd.setGuid(agentHost.getGuid());
    cmd.setVersion(agentHost.getVersion());
    cmd.setAgentTag("agent-simulator");
    cmd.setDataCenter(String.valueOf(agentHost.getDataCenterId()));
    cmd.setPod(String.valueOf(agentHost.getPodId()));
    cmd.setCluster(String.valueOf(agentHost.getClusterId()));
    StartupStorageCommand ssCmd = initializeLocalSR();
    return new StartupCommand[] { cmd, ssCmd };
}
Also used : HashMap(java.util.HashMap) StartupStorageCommand(com.cloud.agent.api.StartupStorageCommand) RouterPrivateIpStrategy(com.cloud.network.Networks.RouterPrivateIpStrategy) MockVMVO(com.cloud.simulator.MockVMVO) StartupCommand(com.cloud.agent.api.StartupCommand) State(com.cloud.vm.VirtualMachine.State) StartupRoutingCommand(com.cloud.agent.api.StartupRoutingCommand) HashMap(java.util.HashMap) Map(java.util.Map)

Example 27 with State

use of com.cloud.vm.VirtualMachine.State in project CloudStack-archive by CloudStack-extras.

the class AgentRoutingResource method sync.

protected HashMap<String, State> sync() {
    Map<String, State> newStates;
    Map<String, State> oldStates = null;
    HashMap<String, State> changes = new HashMap<String, State>();
    synchronized (_vms) {
        oldStates = new HashMap<String, State>(_vms.size());
        oldStates.putAll(_vms);
        newStates = new HashMap<String, State>(_vms.size());
        newStates.putAll(_vms);
        for (Map.Entry<String, State> entry : newStates.entrySet()) {
            String vm = entry.getKey();
            State newState = entry.getValue();
            State oldState = oldStates.remove(vm);
            if (s_logger.isTraceEnabled()) {
                s_logger.trace("VM " + vm + ": has state " + newState + " and we have state " + (oldState != null ? oldState.toString() : "null"));
            }
            if (oldState == null) {
                _vms.put(vm, newState);
                changes.put(vm, newState);
            } else if (oldState == State.Starting) {
                if (newState == State.Running) {
                    _vms.put(vm, newState);
                } else if (newState == State.Stopped) {
                    s_logger.debug("Ignoring vm " + vm + " because of a lag in starting the vm.");
                }
            } else if (oldState == State.Stopping) {
                if (newState == State.Stopped) {
                    _vms.put(vm, newState);
                } else if (newState == State.Running) {
                    s_logger.debug("Ignoring vm " + vm + " because of a lag in stopping the vm. ");
                }
            } else if (oldState != newState) {
                _vms.put(vm, newState);
                changes.put(vm, newState);
            }
        }
        for (Map.Entry<String, State> entry : oldStates.entrySet()) {
            String vm = entry.getKey();
            State oldState = entry.getValue();
            if (s_logger.isTraceEnabled()) {
                s_logger.trace("VM " + vm + " is now missing from simulator agent so reporting stopped");
            }
            if (oldState == State.Stopping) {
                s_logger.debug("Ignoring VM " + vm + " in transition state stopping.");
                _vms.remove(vm);
            } else if (oldState == State.Starting) {
                s_logger.debug("Ignoring VM " + vm + " in transition state starting.");
            } else if (oldState == State.Stopped) {
                _vms.remove(vm);
            } else {
                changes.put(entry.getKey(), State.Stopped);
            }
        }
    }
    return changes;
}
Also used : HashMap(java.util.HashMap) State(com.cloud.vm.VirtualMachine.State) HashMap(java.util.HashMap) Map(java.util.Map)

Example 28 with State

use of com.cloud.vm.VirtualMachine.State in project CloudStack-archive by CloudStack-extras.

the class AgentRoutingResource method execute.

protected synchronized StopAnswer execute(StopCommand cmd) {
    StopAnswer answer = null;
    String vmName = cmd.getVmName();
    State state = null;
    synchronized (_vms) {
        state = _vms.get(vmName);
        _vms.put(vmName, State.Stopping);
    }
    try {
        Answer result = _simMgr.simulate(cmd, hostGuid);
        if (!result.getResult()) {
            return new StopAnswer(cmd, result.getDetails());
        }
        answer = new StopAnswer(cmd, null, 0, new Long(100), new Long(200));
        Pair<Long, Long> data = _runningVms.get(vmName);
        if (data != null) {
            this.usedCpu -= data.first();
            this.usedMem -= data.second();
        }
        state = State.Stopped;
    } finally {
        synchronized (_vms) {
            _vms.put(vmName, state);
        }
    }
    return answer;
}
Also used : ReadyAnswer(com.cloud.agent.api.ReadyAnswer) StartAnswer(com.cloud.agent.api.StartAnswer) StopAnswer(com.cloud.agent.api.StopAnswer) Answer(com.cloud.agent.api.Answer) CheckVirtualMachineAnswer(com.cloud.agent.api.CheckVirtualMachineAnswer) State(com.cloud.vm.VirtualMachine.State) StopAnswer(com.cloud.agent.api.StopAnswer)

Example 29 with State

use of com.cloud.vm.VirtualMachine.State 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 30 with State

use of com.cloud.vm.VirtualMachine.State in project CloudStack-archive by CloudStack-extras.

the class MockVmManagerImpl method getVmStates.

@Override
public Map<String, State> getVmStates(String hostGuid) {
    Map<String, State> states = new HashMap<String, State>();
    List<MockVMVO> vms = _mockVmDao.findByHostGuid(hostGuid);
    if (vms.isEmpty()) {
        return states;
    }
    for (MockVm vm : vms) {
        states.put(vm.getName(), vm.getState());
    }
    return states;
}
Also used : MockVMVO(com.cloud.simulator.MockVMVO) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) State(com.cloud.vm.VirtualMachine.State) MockVm(com.cloud.simulator.MockVm)

Aggregations

State (com.cloud.vm.VirtualMachine.State)45 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)14 ConfigurationException (javax.naming.ConfigurationException)11 HashMap (java.util.HashMap)9 PowerState (com.cloud.vm.VirtualMachine.PowerState)8 LibvirtException (org.libvirt.LibvirtException)8 CheckVirtualMachineAnswer (com.cloud.agent.api.CheckVirtualMachineAnswer)7 StartAnswer (com.cloud.agent.api.StartAnswer)7 StopAnswer (com.cloud.agent.api.StopAnswer)7 Map (java.util.Map)7 Connect (org.libvirt.Connect)7 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)5 Ovm3ResourceException (com.cloud.hypervisor.ovm3.objects.Ovm3ResourceException)5 FileNotFoundException (java.io.FileNotFoundException)5 IOException (java.io.IOException)5 VmState (com.cloud.agent.api.StartupRoutingCommand.VmState)4 VirtualMachineTO (com.cloud.agent.api.to.VirtualMachineTO)4 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)4 Answer (com.cloud.agent.api.Answer)3 NicTO (com.cloud.agent.api.to.NicTO)3