Search in sources :

Example 1 with State

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

the class LibvirtComputingResource method execute.

protected Answer execute(StopCommand cmd) {
    final String vmName = cmd.getVmName();
    Long bytesReceived = new Long(0);
    Long bytesSent = new Long(0);
    State state = null;
    synchronized (_vms) {
        state = _vms.get(vmName);
        _vms.put(vmName, State.Stopping);
    }
    try {
        Connect conn = LibvirtConnection.getConnection();
        List<DiskDef> disks = getDisks(conn, vmName);
        destroy_network_rules_for_vm(conn, vmName);
        String result = stopVM(conn, vmName, defineOps.UNDEFINE_VM);
        if (result == null) {
            for (DiskDef disk : disks) {
                if (disk.getDeviceType() == DiskDef.deviceType.CDROM && disk.getDiskPath() != null)
                    cleanupDisk(conn, disk);
            }
        }
        final String result2 = cleanupVnet(conn, cmd.getVnet());
        if (result != null && result2 != null) {
            result = result2 + result;
        }
        state = State.Stopped;
        return new StopAnswer(cmd, result, 0, bytesSent, bytesReceived);
    } catch (LibvirtException e) {
        return new StopAnswer(cmd, e.getMessage());
    } finally {
        synchronized (_vms) {
            if (state != null) {
                _vms.put(vmName, state);
            } else {
                _vms.remove(vmName);
            }
        }
    }
}
Also used : DiskDef(com.cloud.agent.resource.computing.LibvirtVMDef.DiskDef) LibvirtException(org.libvirt.LibvirtException) State(com.cloud.vm.VirtualMachine.State) Connect(org.libvirt.Connect) StopAnswer(com.cloud.agent.api.StopAnswer)

Example 2 with State

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

the class LibvirtComputingResource method execute.

private Answer execute(CheckVirtualMachineCommand cmd) {
    try {
        Connect conn = LibvirtConnection.getConnection();
        final State state = getVmState(conn, cmd.getVmName());
        Integer vncPort = null;
        if (state == State.Running) {
            vncPort = getVncPort(conn, cmd.getVmName());
            synchronized (_vms) {
                _vms.put(cmd.getVmName(), State.Running);
            }
        }
        return new CheckVirtualMachineAnswer(cmd, state, vncPort);
    } catch (LibvirtException e) {
        return new CheckVirtualMachineAnswer(cmd, e.getMessage());
    }
}
Also used : CheckVirtualMachineAnswer(com.cloud.agent.api.CheckVirtualMachineAnswer) LibvirtException(org.libvirt.LibvirtException) State(com.cloud.vm.VirtualMachine.State) Connect(org.libvirt.Connect)

Example 3 with State

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

the class LibvirtComputingResource method sync.

protected HashMap<String, State> sync() {
    HashMap<String, State> newStates;
    HashMap<String, State> oldStates = null;
    final HashMap<String, State> changes = new HashMap<String, State>();
    synchronized (_vms) {
        newStates = getAllVms();
        if (newStates == null) {
            s_logger.debug("Unable to get the vm states so no state sync at this point.");
            return changes;
        }
        oldStates = new HashMap<String, State>(_vms.size());
        oldStates.putAll(_vms);
        for (final Map.Entry<String, State> entry : newStates.entrySet()) {
            final String vm = entry.getKey();
            State newState = entry.getValue();
            final State oldState = oldStates.remove(vm);
            if (newState == State.Stopped && oldState != State.Stopping && oldState != null && oldState != State.Stopped) {
                newState = getRealPowerState(vm);
            }
            if (s_logger.isTraceEnabled()) {
                s_logger.trace("VM " + vm + ": libvirt has state " + newState + " and we have state " + (oldState != null ? oldState.toString() : "null"));
            }
            if (vm.startsWith("migrating")) {
                s_logger.debug("Migration detected.  Skipping");
                continue;
            }
            if (oldState == null) {
                _vms.put(vm, newState);
                s_logger.debug("Detecting a new state but couldn't find a old state so adding it to the changes: " + vm);
                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.Migrating) {
                if (newState == State.Running) {
                    s_logger.debug("Detected that an migrating VM is now running: " + vm);
                    _vms.put(vm, newState);
                }
            } 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);
                if (newState == State.Stopped) {
                    if (_vmsKilled.remove(vm)) {
                        s_logger.debug("VM " + vm + " has been killed for storage. ");
                        newState = State.Error;
                    }
                }
                changes.put(vm, newState);
            }
        }
        for (final Map.Entry<String, State> entry : oldStates.entrySet()) {
            final String vm = entry.getKey();
            final State oldState = entry.getValue();
            if (s_logger.isTraceEnabled()) {
                s_logger.trace("VM " + vm + " is now missing from libvirt 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 if (oldState == State.Migrating) {
                s_logger.debug("Ignoring VM " + vm + " in migrating state.");
            } else {
                _vms.remove(vm);
                State state = State.Stopped;
                if (_vmsKilled.remove(entry.getKey())) {
                    s_logger.debug("VM " + vm + " has been killed by storage monitor");
                    state = State.Error;
                }
                changes.put(entry.getKey(), state);
            }
        }
    }
    return changes;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) State(com.cloud.vm.VirtualMachine.State) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 4 with State

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

the class CloudZonesComputingResource method execute.

protected Answer execute(StopCommand cmd) {
    final String vmName = cmd.getVmName();
    Long bytesReceived = new Long(0);
    Long bytesSent = new Long(0);
    State state = null;
    synchronized (_vms) {
        state = _vms.get(vmName);
        _vms.put(vmName, State.Stopping);
    }
    try {
        Connect conn = LibvirtConnection.getConnection();
        try {
            Domain dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName.getBytes()));
        } catch (LibvirtException e) {
            state = State.Stopped;
            return new StopAnswer(cmd, null, 0, bytesSent, bytesReceived);
        }
        String macAddress = null;
        if (vmName.startsWith("i-")) {
            List<InterfaceDef> nics = getInterfaces(conn, vmName);
            if (!nics.isEmpty()) {
                macAddress = nics.get(0).getMacAddress();
            }
        }
        destroy_network_rules_for_vm(conn, vmName);
        String result = stopVM(conn, vmName, defineOps.UNDEFINE_VM);
        try {
            cleanupVnet(conn, cmd.getVnet());
            _dhcpSnooper.cleanup(macAddress, vmName);
            _vmDataServer.handleVmStopped(cmd.getVmName());
        } catch (Exception e) {
        }
        state = State.Stopped;
        return new StopAnswer(cmd, result, 0, bytesSent, bytesReceived);
    } catch (LibvirtException e) {
        return new StopAnswer(cmd, e.getMessage());
    } finally {
        synchronized (_vms) {
            if (state != null) {
                _vms.put(vmName, state);
            } else {
                _vms.remove(vmName);
            }
        }
    }
}
Also used : InterfaceDef(com.cloud.agent.resource.computing.LibvirtVMDef.InterfaceDef) LibvirtException(org.libvirt.LibvirtException) State(com.cloud.vm.VirtualMachine.State) Connect(org.libvirt.Connect) Domain(org.libvirt.Domain) StopAnswer(com.cloud.agent.api.StopAnswer) ConfigurationException(javax.naming.ConfigurationException) LibvirtException(org.libvirt.LibvirtException)

Example 5 with State

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

the class FakeComputingResource method execute.

protected synchronized StartAnswer execute(StartCommand cmd) {
    VmMgr vmMgr = getVmManager();
    VirtualMachineTO vmSpec = cmd.getVirtualMachine();
    String vmName = vmSpec.getName();
    State state = State.Stopped;
    try {
        if (!_vms.containsKey(vmName)) {
            synchronized (_vms) {
                _vms.put(vmName, State.Starting);
            }
            MockVm vm = vmMgr.createVmFromSpec(vmSpec);
            vmMgr.createVbd(vmSpec, vmName, vm);
            vmMgr.createVif(vmSpec, vmName, vm);
            state = State.Running;
            for (NicTO nic : cmd.getVirtualMachine().getNics()) {
                if (nic.getType() == TrafficType.Guest) {
                    InetAddress addr = _dhcpSnooper.getIPAddr(nic.getMac(), vmName);
                    nic.setIp(addr.getHostAddress());
                }
            }
            _vmDataServer.handleVmStarted(cmd.getVirtualMachine());
            return new StartAnswer(cmd);
        } else {
            String msg = "There is already a VM having the same name " + vmName;
            s_logger.warn(msg);
            return new StartAnswer(cmd, msg);
        }
    } catch (Exception ex) {
    } finally {
        synchronized (_vms) {
            _vms.put(vmName, state);
        }
    }
    return new StartAnswer(cmd);
}
Also used : StartAnswer(com.cloud.agent.api.StartAnswer) VmState(com.cloud.agent.api.StartupRoutingCommand.VmState) State(com.cloud.vm.VirtualMachine.State) InetAddress(java.net.InetAddress) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) MockVm(com.cloud.agent.mockvm.MockVm) FileNotFoundException(java.io.FileNotFoundException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) VmMgr(com.cloud.agent.mockvm.VmMgr) MockVmMgr(com.cloud.agent.mockvm.MockVmMgr) NicTO(com.cloud.agent.api.to.NicTO)

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