Search in sources :

Example 1 with StartAnswer

use of com.cloud.agent.api.StartAnswer 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 2 with StartAnswer

use of com.cloud.agent.api.StartAnswer 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 3 with StartAnswer

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

the class AgentRoutingResource method execute.

protected synchronized Answer execute(StartCommand cmd) throws IllegalArgumentException {
    VirtualMachineTO vmSpec = cmd.getVirtualMachine();
    String vmName = vmSpec.getName();
    if (this.totalCpu < (vmSpec.getCpus() * vmSpec.getSpeed() + this.usedCpu) || this.totalMem < (vmSpec.getMaxRam() + this.usedMem)) {
        return new StartAnswer(cmd, "No enough resource to start the vm");
    }
    State state = State.Stopped;
    synchronized (_vms) {
        _vms.put(vmName, State.Starting);
    }
    try {
        Answer result = _simMgr.simulate(cmd, hostGuid);
        if (!result.getResult()) {
            return new StartAnswer(cmd, result.getDetails());
        }
        this.usedCpu += vmSpec.getCpus() * vmSpec.getSpeed();
        this.usedMem += vmSpec.getMaxRam();
        _runningVms.put(vmName, new Pair<Long, Long>(Long.valueOf(vmSpec.getCpus() * vmSpec.getSpeed()), vmSpec.getMaxRam()));
        state = State.Running;
    } finally {
        synchronized (_vms) {
            _vms.put(vmName, state);
        }
    }
    return new StartAnswer(cmd);
}
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) StartAnswer(com.cloud.agent.api.StartAnswer) State(com.cloud.vm.VirtualMachine.State) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO)

Example 4 with StartAnswer

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

the class OvmResourceBase method execute.

@Override
public synchronized StartAnswer execute(StartCommand cmd) {
    VirtualMachineTO vmSpec = cmd.getVirtualMachine();
    String vmName = vmSpec.getName();
    OvmVm.Details vmDetails = null;
    try {
        vmDetails = new OvmVm.Details();
        applySpecToVm(vmDetails, vmSpec);
        createVbds(vmDetails, vmSpec);
        createVifs(vmDetails, vmSpec);
        startVm(vmDetails);
        // Add security group rules
        NicTO[] nics = vmSpec.getNics();
        for (NicTO nic : nics) {
            if (nic.isSecurityGroupEnabled()) {
                if (vmSpec.getType().equals(VirtualMachine.Type.User)) {
                    defaultNetworkRulesForUserVm(vmName, vmSpec.getId(), nic);
                }
            }
        }
        return new StartAnswer(cmd);
    } catch (Exception e) {
        s_logger.debug("Start vm " + vmName + " failed", e);
        cleanup(vmDetails);
        return new StartAnswer(cmd, e.getMessage());
    }
}
Also used : StartAnswer(com.cloud.agent.api.StartAnswer) OvmVm(com.cloud.ovm.object.OvmVm) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) ConfigurationException(javax.naming.ConfigurationException) NicTO(com.cloud.agent.api.to.NicTO)

Example 5 with StartAnswer

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

the class HypervDirectConnectResourceTest method testStartCommandCorruptDiskImage.

@Test
public final void testStartCommandCorruptDiskImage() {
    String sampleStart = "{\"vm\":{\"id\":16,\"name\":\"i-3-17-VM\"," + "\"type\":\"User\",\"cpus\":1,\"speed\":500," + "\"minRam\":536870912,\"maxRam\":536870912," + "\"arch\":\"x86_64\"," + "\"os\":\"CentOS 6.0 (64-bit)\"," + "\"bootArgs\":\"\",\"rebootOnCrash\":false," + "\"enableHA\":false," + "\"limitCpuUse\":false," + "\"vncPassword\":\"31f82f29aff646eb\"," + "\"params\":{}," + "\"uuid\":\"8b030b6a-0243-440a-8cc5-45d08815ca11\"" + ",\"disks\":[" + "{\"id\":18,\"name\":\"" + s_testSampleVolumeCorruptUUID + "\"," + "\"mountPoint\":" + s_testSampleVolumeCorruptURIJSON + "," + "\"path\":" + s_testSampleVolumeCorruptURIJSON + ",\"size\":0," + "\"type\":\"ROOT\"," + "\"storagePoolType\":\"Filesystem\"," + "\"storagePoolUuid\":\"" + s_testLocalStoreUUID + "\"" + ",\"deviceId\":0}," + "{\"id\":16,\"name\":\"Hyper-V Sample2\",\"size\":0," + "\"type\":\"ISO\",\"storagePoolType\":\"ISO\"," + "\"deviceId\":3}]," + "\"nics\":[" + "{\"deviceId\":0,\"networkRateMbps\":100," + "\"defaultNic\":true," + "\"uuid\":\"99cb4813-23af-428c-a87a-2d1899be4f4b\"," + "\"ip\":\"10.1.1.67\",\"netmask\":\"255.255.255.0\"," + "\"gateway\":\"10.1.1.1\"," + "\"mac\":\"02:00:51:2c:00:0e\",\"dns1\":\"4.4.4.4\"," + "\"broadcastType\":\"Vlan\",\"type\":\"Guest\"," + "\"broadcastUri\":\"vlan://261\"," + "\"isolationUri\":\"vlan://261\"," + "\"isSecurityGroupEnabled\":false}" + "]},\"contextMap\":{},\"wait\":0}";
    StartCommand cmd = s_gson.fromJson(sampleStart, StartCommand.class);
    StartAnswer ans = (StartAnswer) s_hypervresource.executeRequest(cmd);
    Assert.assertFalse(ans.getDetails(), ans.getResult());
}
Also used : StartAnswer(com.cloud.agent.api.StartAnswer) StartCommand(com.cloud.agent.api.StartCommand) Test(org.junit.Test)

Aggregations

StartAnswer (com.cloud.agent.api.StartAnswer)19 VirtualMachineTO (com.cloud.agent.api.to.VirtualMachineTO)15 NicTO (com.cloud.agent.api.to.NicTO)9 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)7 ConfigurationException (javax.naming.ConfigurationException)7 StopAnswer (com.cloud.agent.api.StopAnswer)5 Answer (com.cloud.agent.api.Answer)4 State (com.cloud.vm.VirtualMachine.State)4 CheckVirtualMachineAnswer (com.cloud.agent.api.CheckVirtualMachineAnswer)3 StartCommand (com.cloud.agent.api.StartCommand)3 InternalErrorException (com.cloud.exception.InternalErrorException)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 ReadyAnswer (com.cloud.agent.api.ReadyAnswer)2 RestoreVMSnapshotAnswer (com.cloud.agent.api.RestoreVMSnapshotAnswer)2 StopCommand (com.cloud.agent.api.StopCommand)2 DiskTO (com.cloud.agent.api.to.DiskTO)2 GPUDeviceTO (com.cloud.agent.api.to.GPUDeviceTO)2 ExcludeList (com.cloud.deploy.DeploymentPlanner.ExcludeList)2 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)2