Search in sources :

Example 1 with VirtualMachineTO

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

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

the class SimulatorGuru method implement.

@Override
public <T extends VirtualMachine> VirtualMachineTO implement(VirtualMachineProfile<T> vm) {
    VirtualMachineTO to = toVirtualMachineTO(vm);
    // Determine the VM's OS description
    GuestOSVO guestOS = _guestOsDao.findById(vm.getVirtualMachine().getGuestOSId());
    to.setOs(guestOS.getDisplayName());
    return to;
}
Also used : GuestOSVO(com.cloud.storage.GuestOSVO) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO)

Example 3 with VirtualMachineTO

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

the class LibvirtComputingResource method execute.

private synchronized Answer execute(PrepareForMigrationCommand cmd) {
    VirtualMachineTO vm = cmd.getVirtualMachine();
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Preparing host for migrating " + vm);
    }
    NicTO[] nics = vm.getNics();
    try {
        Connect conn = LibvirtConnection.getConnection();
        for (NicTO nic : nics) {
            String vlanId = null;
            if (nic.getBroadcastType() == BroadcastDomainType.Vlan) {
                URI broadcastUri = nic.getBroadcastUri();
                vlanId = broadcastUri.getHost();
            }
            if (nic.getType() == TrafficType.Guest) {
                if (nic.getBroadcastType() == BroadcastDomainType.Vlan && !vlanId.equalsIgnoreCase("untagged")) {
                    createVlanBr(vlanId, _pifs.first());
                }
            } else if (nic.getType() == TrafficType.Control) {
                /* Make sure the network is still there */
                createControlNetwork(conn);
            } else if (nic.getType() == TrafficType.Public) {
                if (nic.getBroadcastType() == BroadcastDomainType.Vlan && !vlanId.equalsIgnoreCase("untagged")) {
                    createVlanBr(vlanId, _pifs.second());
                }
            }
        }
        /* setup disks, e.g for iso */
        VolumeTO[] volumes = vm.getDisks();
        for (VolumeTO volume : volumes) {
            if (volume.getType() == Volume.Type.ISO) {
                getVolumePath(conn, volume);
            }
        }
        synchronized (_vms) {
            _vms.put(vm.getName(), State.Migrating);
        }
        return new PrepareForMigrationAnswer(cmd);
    } catch (LibvirtException e) {
        return new PrepareForMigrationAnswer(cmd, e.toString());
    } catch (InternalErrorException e) {
        return new PrepareForMigrationAnswer(cmd, e.toString());
    } catch (URISyntaxException e) {
        return new PrepareForMigrationAnswer(cmd, e.toString());
    }
}
Also used : VolumeTO(com.cloud.agent.api.to.VolumeTO) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) InternalErrorException(com.cloud.exception.InternalErrorException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) NicTO(com.cloud.agent.api.to.NicTO) PrepareForMigrationAnswer(com.cloud.agent.api.PrepareForMigrationAnswer)

Example 4 with VirtualMachineTO

use of com.cloud.agent.api.to.VirtualMachineTO 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 5 with VirtualMachineTO

use of com.cloud.agent.api.to.VirtualMachineTO 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)

Aggregations

VirtualMachineTO (com.cloud.agent.api.to.VirtualMachineTO)179 Test (org.junit.Test)92 NicTO (com.cloud.agent.api.to.NicTO)69 Answer (com.cloud.agent.api.Answer)62 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)52 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)41 LibvirtException (org.libvirt.LibvirtException)38 Connect (org.libvirt.Connect)35 HashMap (java.util.HashMap)30 CheckRouterAnswer (com.cloud.agent.api.CheckRouterAnswer)28 Connection (com.xensource.xenapi.Connection)28 URISyntaxException (java.net.URISyntaxException)28 LibvirtRequestWrapper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper)27 LibvirtUtilitiesHelper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper)27 InternalErrorException (com.cloud.exception.InternalErrorException)25 StartAnswer (com.cloud.agent.api.StartAnswer)24 StartCommand (com.cloud.agent.api.StartCommand)21 ArrayList (java.util.ArrayList)20 DiskTO (com.cloud.agent.api.to.DiskTO)18 AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)18