Search in sources :

Example 6 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)

Example 7 with State

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

the class FakeComputingResource method execute.

protected CheckVirtualMachineAnswer execute(final CheckVirtualMachineCommand cmd) {
    VmMgr vmMgr = getVmManager();
    final String vmName = cmd.getVmName();
    final State state = vmMgr.checkVmState(vmName);
    Integer vncPort = null;
    if (state == State.Running) {
        vncPort = vmMgr.getVncPort(vmName);
        synchronized (_vms) {
            _vms.put(vmName, State.Running);
        }
    }
    return new CheckVirtualMachineAnswer(cmd, state, vncPort);
}
Also used : CheckVirtualMachineAnswer(com.cloud.agent.api.CheckVirtualMachineAnswer) VmState(com.cloud.agent.api.StartupRoutingCommand.VmState) State(com.cloud.vm.VirtualMachine.State) VmMgr(com.cloud.agent.mockvm.VmMgr) MockVmMgr(com.cloud.agent.mockvm.MockVmMgr)

Example 8 with State

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

the class LibvirtComputingResource method execute.

protected synchronized StartAnswer execute(StartCommand cmd) {
    VirtualMachineTO vmSpec = cmd.getVirtualMachine();
    String vmName = vmSpec.getName();
    LibvirtVMDef vm = null;
    State state = State.Stopped;
    Connect conn = null;
    try {
        conn = LibvirtConnection.getConnection();
        synchronized (_vms) {
            _vms.put(vmName, State.Starting);
        }
        vm = createVMFromSpec(vmSpec);
        createVbd(conn, vmSpec, vmName, vm);
        createVifs(conn, vmSpec, vm);
        s_logger.debug("starting " + vmName + ": " + vm.toString());
        startDomain(conn, vmName, vm.toString());
        Script.runSimpleBashScript("virsh schedinfo " + vmName + " --set cpu_shares=" + vmSpec.getCpus() * vmSpec.getSpeed());
        NicTO[] nics = vmSpec.getNics();
        for (NicTO nic : nics) {
            if (nic.getIsolationUri() != null && nic.getIsolationUri().getScheme().equalsIgnoreCase(IsolationType.Ec2.toString())) {
                if (vmSpec.getType() != VirtualMachine.Type.User) {
                    default_network_rules_for_systemvm(conn, vmName);
                    break;
                } else {
                    default_network_rules(conn, vmName, nic, vmSpec.getId());
                }
            }
        }
        state = State.Running;
        return new StartAnswer(cmd);
    } catch (Exception e) {
        s_logger.warn("Exception ", e);
        if (conn != null) {
            handleVmStartFailure(conn, vmName, vm);
        }
        return new StartAnswer(cmd, e.getMessage());
    } finally {
        synchronized (_vms) {
            if (state != State.Stopped) {
                _vms.put(vmName, state);
            } else {
                _vms.remove(vmName);
            }
        }
    }
}
Also used : StartAnswer(com.cloud.agent.api.StartAnswer) State(com.cloud.vm.VirtualMachine.State) Connect(org.libvirt.Connect) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) URISyntaxException(java.net.URISyntaxException) LibvirtException(org.libvirt.LibvirtException) FileNotFoundException(java.io.FileNotFoundException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException) NicTO(com.cloud.agent.api.to.NicTO)

Example 9 with State

use of com.cloud.vm.VirtualMachine.State in project cloudstack by apache.

the class Ovm3HypervisorResource method execute.

@Override
public synchronized StartAnswer execute(StartCommand cmd) {
    VirtualMachineTO vmSpec = cmd.getVirtualMachine();
    String vmName = vmSpec.getName();
    State state = State.Stopped;
    Xen xen = new Xen(c);
    try {
        hypervisorsupport.setVmStateStarting(vmName);
        Xen.Vm vm = xen.getVmConfig();
        /* max and min ? */
        vm.setVmCpus(vmSpec.getCpus());
        /* in mb not in bytes */
        vm.setVmMemory(vmSpec.getMinRam() / 1024 / 1024);
        vm.setVmUuid(UUID.nameUUIDFromBytes(vmSpec.getName().getBytes(Charset.defaultCharset())).toString());
        vm.setVmName(vmName);
        String domType = guesttypes.getOvm3GuestType(vmSpec.getOs());
        if (domType == null || domType.isEmpty()) {
            domType = "default";
            LOGGER.debug("VM Virt type missing setting to: " + domType);
        } else {
            LOGGER.debug("VM Virt type set to " + domType + " for " + vmSpec.getOs());
        }
        vm.setVmDomainType(domType);
        if (vmSpec.getBootloader() == BootloaderType.CD) {
            LOGGER.warn("CD booting is not supported");
        }
        /*
             * officially CD boot is only supported on HVM, although there is a
             * simple way around it..
             */
        vmsupport.createVbds(vm, vmSpec);
        if (vmSpec.getType() != VirtualMachine.Type.User) {
            // double check control network if we run a non user VM
            hypervisornetwork.configureNetworking();
            vm.setVmExtra(vmSpec.getBootArgs().replace(" ", "%"));
            String svmPath = configuration.getAgentOvmRepoPath() + "/" + ovmObject.deDash(vm.getPrimaryPoolUuid()) + "/ISOs";
            String svmIso = svmPath + "/" + storagepool.getSystemVMPatchIsoFile().getName();
            vm.addIso(svmIso);
        }
        /* OVS/Network stuff should go here! */
        vmsupport.createVifs(vm, vmSpec);
        vm.setupVifs();
        vm.setVnc("0.0.0.0", vmSpec.getVncPassword());
        xen.createVm(ovmObject.deDash(vm.getPrimaryPoolUuid()), vm.getVmUuid());
        xen.startVm(ovmObject.deDash(vm.getPrimaryPoolUuid()), vm.getVmUuid());
        state = State.Running;
        if (vmSpec.getType() != VirtualMachine.Type.User) {
            String controlIp = null;
            for (NicTO nic : vmSpec.getNics()) {
                if (nic.getType() == TrafficType.Control) {
                    controlIp = nic.getIp();
                }
            }
            /* fix is in cloudstack.py for xend restart timer */
            for (int count = 0; count < 60; count++) {
                CloudstackPlugin cSp = new CloudstackPlugin(c);
                /* skip a beat to make sure we didn't miss start */
                if (hypervisorsupport.getVmState(vmName) == null && count > 1) {
                    String msg = "VM " + vmName + " went missing on " + configuration.getAgentHostname() + ", returning stopped";
                    LOGGER.debug(msg);
                    state = State.Stopped;
                    return new StartAnswer(cmd, msg);
                }
                /* creative fix? */
                try {
                    Boolean res = cSp.domrCheckSsh(controlIp);
                    LOGGER.debug("connected to " + controlIp + " on attempt " + count + " result: " + res);
                    if (res) {
                        break;
                    }
                } catch (Exception x) {
                    LOGGER.trace("unable to connect to " + controlIp + " on attempt " + count + " " + x.getMessage(), x);
                }
                Thread.sleep(5000);
            }
        }
        /*
             * Can't remember if HA worked if we were only a pool ?
             */
        if (configuration.getAgentInOvm3Pool() && configuration.getAgentInOvm3Cluster()) {
            xen.configureVmHa(ovmObject.deDash(vm.getPrimaryPoolUuid()), vm.getVmUuid(), true);
        }
        /* should be starting no ? */
        state = State.Running;
        return new StartAnswer(cmd);
    } catch (Exception e) {
        LOGGER.debug("Start vm " + vmName + " failed", e);
        state = State.Stopped;
        return new StartAnswer(cmd, e.getMessage());
    } finally {
        hypervisorsupport.setVmState(vmName, state);
    }
}
Also used : Xen(com.cloud.hypervisor.ovm3.objects.Xen) StartAnswer(com.cloud.agent.api.StartAnswer) State(com.cloud.vm.VirtualMachine.State) CloudstackPlugin(com.cloud.hypervisor.ovm3.objects.CloudstackPlugin) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Ovm3ResourceException(com.cloud.hypervisor.ovm3.objects.Ovm3ResourceException) NicTO(com.cloud.agent.api.to.NicTO)

Example 10 with State

use of com.cloud.vm.VirtualMachine.State in project cloudstack by apache.

the class Ovm3HypervisorResource method execute.

/**
     * Removes the vm and its configuration from the hypervisor.
     */
@Override
public StopAnswer execute(StopCommand cmd) {
    String vmName = cmd.getVmName();
    State state = State.Error;
    hypervisorsupport.setVmState(vmName, State.Stopping);
    try {
        Xen vms = new Xen(c);
        Xen.Vm vm = null;
        vm = vms.getRunningVmConfig(vmName);
        if (vm == null) {
            state = State.Stopping;
            LOGGER.debug("Unable to get details of vm: " + vmName + ", treating it as Stopping");
            return new StopAnswer(cmd, "success", true);
        }
        String repoId = ovmObject.deDash(vm.getVmRootDiskPoolId());
        String vmId = vm.getVmUuid();
        /* can we do without the poolId ? */
        vms.stopVm(repoId, vmId);
        int tries = 30;
        while (vms.getRunningVmConfig(vmName) != null && tries > 0) {
            String msg = "Waiting for " + vmName + " to stop";
            LOGGER.debug(msg);
            tries--;
            Thread.sleep(10 * 1000);
        }
        vms.deleteVm(repoId, vmId);
        vmsupport.cleanup(vm);
        if (vms.getRunningVmConfig(vmName) != null) {
            String msg = "Stop " + vmName + " failed ";
            LOGGER.debug(msg);
            return new StopAnswer(cmd, msg, false);
        }
        state = State.Stopped;
        return new StopAnswer(cmd, "success", true);
    } catch (Exception e) {
        LOGGER.debug("Stop " + vmName + " failed ", e);
        return new StopAnswer(cmd, e.getMessage(), false);
    } finally {
        if (state != null) {
            hypervisorsupport.setVmState(vmName, state);
        } else {
            hypervisorsupport.revmoveVmState(vmName);
        }
    }
}
Also used : Xen(com.cloud.hypervisor.ovm3.objects.Xen) State(com.cloud.vm.VirtualMachine.State) StopAnswer(com.cloud.agent.api.StopAnswer) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Ovm3ResourceException(com.cloud.hypervisor.ovm3.objects.Ovm3ResourceException)

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