Search in sources :

Example 16 with StartAnswer

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

the class HypervDirectConnectResourceTest method simpleVmStart.

private StartAnswer simpleVmStart(final String sample) {
    StartCommand cmd = s_gson.fromJson(sample, StartCommand.class);
    s_logger.info("StartCommand sample " + s_gson.toJson(cmd));
    StartAnswer ans = (StartAnswer) s_hypervresource.executeRequest(cmd);
    return ans;
}
Also used : StartAnswer(com.cloud.agent.api.StartAnswer) StartCommand(com.cloud.agent.api.StartCommand)

Example 17 with StartAnswer

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

the class HypervDirectConnectResourceTest method testStartStopCommand.

@Test
public final void testStartStopCommand() {
    String sample = getSampleStartCommand();
    StartAnswer sans = simpleVmStart(sample);
    Assert.assertTrue(sans.getDetails(), sans.getResult());
    StopAnswer stopAns = simpleVmStop();
    Assert.assertTrue(stopAns.getDetails(), stopAns.getResult());
}
Also used : StartAnswer(com.cloud.agent.api.StartAnswer) StopAnswer(com.cloud.agent.api.StopAnswer) Test(org.junit.Test)

Example 18 with StartAnswer

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

the class LibvirtStartCommandWrapper method execute.

@Override
public Answer execute(final StartCommand command, final LibvirtComputingResource libvirtComputingResource) {
    final VirtualMachineTO vmSpec = command.getVirtualMachine();
    vmSpec.setVncAddr(command.getHostIp());
    final String vmName = vmSpec.getName();
    LibvirtVMDef vm = null;
    DomainState state = DomainState.VIR_DOMAIN_SHUTOFF;
    final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
    Connect conn = null;
    try {
        vm = libvirtComputingResource.createVMFromSpec(vmSpec);
        conn = libvirtUtilitiesHelper.getConnectionByType(vm.getHvsType());
        Long remainingMem = getFreeMemory(conn, libvirtComputingResource);
        if (remainingMem == null) {
            return new StartAnswer(command, "failed to get free memory");
        } else if (remainingMem < vmSpec.getMinRam()) {
            return new StartAnswer(command, "Not enough memory on the host, remaining: " + remainingMem + ", asking: " + vmSpec.getMinRam());
        }
        final NicTO[] nics = vmSpec.getNics();
        for (final NicTO nic : nics) {
            if (vmSpec.getType() != VirtualMachine.Type.User) {
                nic.setPxeDisable(true);
            }
        }
        libvirtComputingResource.createVbd(conn, vmSpec, vmName, vm);
        if (!storagePoolMgr.connectPhysicalDisksViaVmSpec(vmSpec)) {
            return new StartAnswer(command, "Failed to connect physical disks to host");
        }
        libvirtComputingResource.createVifs(vmSpec, vm);
        s_logger.debug("starting " + vmName + ": " + vm.toString());
        libvirtComputingResource.startVM(conn, vmName, vm.toString());
        for (final NicTO nic : nics) {
            if (nic.isSecurityGroupEnabled() || nic.getIsolationUri() != null && nic.getIsolationUri().getScheme().equalsIgnoreCase(IsolationType.Ec2.toString())) {
                if (vmSpec.getType() != VirtualMachine.Type.User) {
                    libvirtComputingResource.configureDefaultNetworkRulesForSystemVm(conn, vmName);
                    break;
                } else {
                    final List<String> nicSecIps = nic.getNicSecIps();
                    String secIpsStr;
                    final StringBuilder sb = new StringBuilder();
                    if (nicSecIps != null) {
                        for (final String ip : nicSecIps) {
                            sb.append(ip).append(":");
                        }
                        secIpsStr = sb.toString();
                    } else {
                        secIpsStr = "0:";
                    }
                    libvirtComputingResource.defaultNetworkRules(conn, vmName, nic, vmSpec.getId(), secIpsStr);
                }
            }
        }
        // pass cmdline info to system vms
        if (vmSpec.getType() != VirtualMachine.Type.User) {
            //wait and try passCmdLine for 5 minutes at most for CLOUDSTACK-2823
            String controlIp = null;
            for (final NicTO nic : nics) {
                if (nic.getType() == TrafficType.Control) {
                    controlIp = nic.getIp();
                    break;
                }
            }
            for (int count = 0; count < 30; count++) {
                libvirtComputingResource.passCmdLine(vmName, vmSpec.getBootArgs());
                //check router is up?
                final VirtualRoutingResource virtRouterResource = libvirtComputingResource.getVirtRouterResource();
                final boolean result = virtRouterResource.connect(controlIp, 1, 5000);
                if (result) {
                    break;
                }
            }
        }
        state = DomainState.VIR_DOMAIN_RUNNING;
        return new StartAnswer(command);
    } catch (final LibvirtException e) {
        s_logger.warn("LibvirtException ", e);
        if (conn != null) {
            libvirtComputingResource.handleVmStartFailure(conn, vmName, vm);
        }
        return new StartAnswer(command, e.getMessage());
    } catch (final InternalErrorException e) {
        s_logger.warn("InternalErrorException ", e);
        if (conn != null) {
            libvirtComputingResource.handleVmStartFailure(conn, vmName, vm);
        }
        return new StartAnswer(command, e.getMessage());
    } catch (final URISyntaxException e) {
        s_logger.warn("URISyntaxException ", e);
        if (conn != null) {
            libvirtComputingResource.handleVmStartFailure(conn, vmName, vm);
        }
        return new StartAnswer(command, e.getMessage());
    } finally {
        if (state != DomainState.VIR_DOMAIN_RUNNING) {
            storagePoolMgr.disconnectPhysicalDisksViaVmSpec(vmSpec);
        }
    }
}
Also used : KVMStoragePoolManager(com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager) StartAnswer(com.cloud.agent.api.StartAnswer) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) VirtualRoutingResource(com.cloud.agent.resource.virtualnetwork.VirtualRoutingResource) InternalErrorException(com.cloud.exception.InternalErrorException) URISyntaxException(java.net.URISyntaxException) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) LibvirtVMDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef) DomainState(org.libvirt.DomainInfo.DomainState) NicTO(com.cloud.agent.api.to.NicTO)

Example 19 with StartAnswer

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

the class MockVmManagerImpl method startVM.

@Override
public StartAnswer startVM(final StartCommand cmd, final SimulatorInfo info) {
    final VirtualMachineTO vm = cmd.getVirtualMachine();
    final String result = startVM(vm.getName(), vm.getNics(), vm.getCpus() * vm.getMaxSpeed(), vm.getMaxRam(), vm.getBootArgs(), info.getHostUuid());
    if (result != null) {
        return new StartAnswer(cmd, result);
    } else {
        return new StartAnswer(cmd);
    }
}
Also used : StartAnswer(com.cloud.agent.api.StartAnswer) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO)

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