Search in sources :

Example 81 with VirtualMachineTO

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

the class LibvirtComputingResourceTest method testCreateVMFromSpecLegacy.

/**
        This test tests if the Agent can handle a vmSpec coming
        from a <=4.1 management server.

        The overcommit feature has not been merged in there and thus
        only 'speed' is set.
     */
@Test
public void testCreateVMFromSpecLegacy() {
    final int id = random.nextInt(65534);
    final String name = "test-instance-1";
    final int cpus = random.nextInt(2) + 1;
    final int speed = 1024;
    final int minRam = 256 * 1024;
    final int maxRam = 512 * 1024;
    final String os = "Ubuntu";
    final String vncAddr = "";
    final String vncPassword = "mySuperSecretPassword";
    final LibvirtComputingResource lcr = new LibvirtComputingResource();
    final VirtualMachineTO to = new VirtualMachineTO(id, name, VirtualMachine.Type.User, cpus, speed, minRam, maxRam, BootloaderType.HVM, os, false, false, vncPassword);
    to.setVncAddr(vncAddr);
    to.setUuid("b0f0a72d-7efb-3cad-a8ff-70ebf30b3af9");
    final LibvirtVMDef vm = lcr.createVMFromSpec(to);
    vm.setHvsType(hyperVisorType);
    verifyVm(to, vm);
}
Also used : VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) Test(org.junit.Test)

Example 82 with VirtualMachineTO

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

the class LibvirtComputingResourceTest method testCreateVMFromSpec.

/**
        This test tests if the Agent can handle a vmSpec coming
        from a >4.1 management server.

        It tests if the Agent can handle a vmSpec with overcommit
        data like minSpeed and maxSpeed in there
     */
@Test
public void testCreateVMFromSpec() {
    final int id = random.nextInt(65534);
    final String name = "test-instance-1";
    final int cpus = random.nextInt(2) + 1;
    final int minSpeed = 1024;
    final int maxSpeed = 2048;
    final int minRam = 256 * 1024;
    final int maxRam = 512 * 1024;
    final String os = "Ubuntu";
    final String vncAddr = "";
    final String vncPassword = "mySuperSecretPassword";
    final LibvirtComputingResource lcr = new LibvirtComputingResource();
    final VirtualMachineTO to = new VirtualMachineTO(id, name, VirtualMachine.Type.User, cpus, minSpeed, maxSpeed, minRam, maxRam, BootloaderType.HVM, os, false, false, vncPassword);
    to.setVncAddr(vncAddr);
    to.setUuid("b0f0a72d-7efb-3cad-a8ff-70ebf30b3af9");
    final LibvirtVMDef vm = lcr.createVMFromSpec(to);
    vm.setHvsType(hyperVisorType);
    verifyVm(to, vm);
}
Also used : VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) Test(org.junit.Test)

Example 83 with VirtualMachineTO

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

the class VmwareResource method execute.

protected ScaleVmAnswer execute(ScaleVmCommand cmd) {
    VmwareContext context = getServiceContext();
    VirtualMachineTO vmSpec = cmd.getVirtualMachine();
    try {
        VmwareHypervisorHost hyperHost = getHyperHost(context);
        VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(cmd.getVmName());
        VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
        int ramMb = getReservedMemoryMb(vmSpec);
        long hotaddIncrementSizeInMb;
        long hotaddMemoryLimitInMb;
        long requestedMaxMemoryInMb = vmSpec.getMaxRam() / (1024 * 1024);
        // Check if VM is really running on hypervisor host
        if (getVmPowerState(vmMo) != PowerState.PowerOn) {
            throw new CloudRuntimeException("Found that the VM " + vmMo.getVmName() + " is not running. Unable to scale-up this VM");
        }
        // Check max hot add limit
        hotaddIncrementSizeInMb = vmMo.getHotAddMemoryIncrementSizeInMb();
        hotaddMemoryLimitInMb = vmMo.getHotAddMemoryLimitInMb();
        if (requestedMaxMemoryInMb > hotaddMemoryLimitInMb) {
            throw new CloudRuntimeException("Memory of VM " + vmMo.getVmName() + " cannot be scaled to " + requestedMaxMemoryInMb + "MB." + " Requested memory limit is beyond the hotadd memory limit for this VM at the moment is " + hotaddMemoryLimitInMb + "MB.");
        }
        // Check increment is multiple of increment size
        long reminder = requestedMaxMemoryInMb % hotaddIncrementSizeInMb;
        if (reminder != 0) {
            requestedMaxMemoryInMb = requestedMaxMemoryInMb + hotaddIncrementSizeInMb - reminder;
        }
        // Check if license supports the feature
        VmwareHelper.isFeatureLicensed(hyperHost, FeatureKeyConstants.HOTPLUG);
        VmwareHelper.setVmScaleUpConfig(vmConfigSpec, vmSpec.getCpus(), vmSpec.getMaxSpeed(), vmSpec.getMinSpeed(), (int) requestedMaxMemoryInMb, ramMb, vmSpec.getLimitCpuUse());
        if (!vmMo.configureVm(vmConfigSpec)) {
            throw new Exception("Unable to execute ScaleVmCommand");
        }
    } catch (Exception e) {
        s_logger.error("Unexpected exception: ", e);
        return new ScaleVmAnswer(cmd, false, "Unable to execute ScaleVmCommand due to " + e.toString());
    }
    return new ScaleVmAnswer(cmd, true, null);
}
Also used : VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) VirtualMachineConfigSpec(com.vmware.vim25.VirtualMachineConfigSpec) ScaleVmAnswer(com.cloud.agent.api.ScaleVmAnswer) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) ConnectException(java.net.ConnectException) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) InternalErrorException(com.cloud.exception.InternalErrorException) CloudException(com.cloud.exception.CloudException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ConfigurationException(javax.naming.ConfigurationException)

Example 84 with VirtualMachineTO

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

the class UserVmManagerImpl method finalizeStart.

@Override
public boolean finalizeStart(VirtualMachineProfile profile, long hostId, Commands cmds, ReservationContext context) {
    UserVmVO vm = _vmDao.findById(profile.getId());
    Answer[] answersToCmds = cmds.getAnswers();
    if (answersToCmds == null) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Returning from finalizeStart() since there are no answers to read");
        }
        return true;
    }
    Answer startAnswer = cmds.getAnswer(StartAnswer.class);
    String returnedIp = null;
    String originalIp = null;
    if (startAnswer != null) {
        StartAnswer startAns = (StartAnswer) startAnswer;
        VirtualMachineTO vmTO = startAns.getVirtualMachine();
        for (NicTO nicTO : vmTO.getNics()) {
            if (nicTO.getType() == TrafficType.Guest) {
                returnedIp = nicTO.getIp();
            }
        }
    }
    List<NicVO> nics = _nicDao.listByVmId(vm.getId());
    NicVO guestNic = null;
    NetworkVO guestNetwork = null;
    for (NicVO nic : nics) {
        NetworkVO network = _networkDao.findById(nic.getNetworkId());
        long isDefault = (nic.isDefaultNic()) ? 1 : 0;
        UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NETWORK_OFFERING_ASSIGN, vm.getAccountId(), vm.getDataCenterId(), vm.getId(), Long.toString(nic.getId()), network.getNetworkOfferingId(), null, isDefault, VirtualMachine.class.getName(), vm.getUuid(), vm.isDisplay());
        if (network.getTrafficType() == TrafficType.Guest) {
            originalIp = nic.getIPv4Address();
            guestNic = nic;
            guestNetwork = network;
            // In vmware, we will be effecting pvlan settings in portgroups in StartCommand.
            if (profile.getHypervisorType() != HypervisorType.VMware) {
                if (nic.getBroadcastUri().getScheme().equals("pvlan")) {
                    NicProfile nicProfile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), 0, false, "pvlan-nic");
                    if (!setupVmForPvlan(true, hostId, nicProfile)) {
                        return false;
                    }
                }
            }
        }
    }
    boolean ipChanged = false;
    if (originalIp != null && !originalIp.equalsIgnoreCase(returnedIp)) {
        if (returnedIp != null && guestNic != null) {
            guestNic.setIPv4Address(returnedIp);
            ipChanged = true;
        }
    }
    if (returnedIp != null && !returnedIp.equalsIgnoreCase(originalIp)) {
        if (guestNic != null) {
            guestNic.setIPv4Address(returnedIp);
            ipChanged = true;
        }
    }
    if (ipChanged) {
        _dcDao.findById(vm.getDataCenterId());
        UserVmVO userVm = _vmDao.findById(profile.getId());
        // dc.getDhcpProvider().equalsIgnoreCase(Provider.ExternalDhcpServer.getName())
        if (_ntwkSrvcDao.canProviderSupportServiceInNetwork(guestNetwork.getId(), Service.Dhcp, Provider.ExternalDhcpServer)) {
            _nicDao.update(guestNic.getId(), guestNic);
            userVm.setPrivateIpAddress(guestNic.getIPv4Address());
            _vmDao.update(userVm.getId(), userVm);
            s_logger.info("Detected that ip changed in the answer, updated nic in the db with new ip " + returnedIp);
        }
    }
    // get system ip and create static nat rule for the vm
    try {
        _rulesMgr.getSystemIpAndEnableStaticNatForVm(profile.getVirtualMachine(), false);
    } catch (Exception ex) {
        s_logger.warn("Failed to get system ip and enable static nat for the vm " + profile.getVirtualMachine() + " due to exception ", ex);
        return false;
    }
    Answer answer = cmds.getAnswer("restoreVMSnapshot");
    if (answer != null && answer instanceof RestoreVMSnapshotAnswer) {
        RestoreVMSnapshotAnswer restoreVMSnapshotAnswer = (RestoreVMSnapshotAnswer) answer;
        if (restoreVMSnapshotAnswer == null || !restoreVMSnapshotAnswer.getResult()) {
            s_logger.warn("Unable to restore the vm snapshot from image file to the VM: " + restoreVMSnapshotAnswer.getDetails());
        }
    }
    final VirtualMachineProfile vmProfile = profile;
    Transaction.execute(new TransactionCallbackNoReturn() {

        @Override
        public void doInTransactionWithoutResult(TransactionStatus status) {
            final UserVmVO vm = _vmDao.findById(vmProfile.getId());
            final List<NicVO> nics = _nicDao.listByVmId(vm.getId());
            for (NicVO nic : nics) {
                Network network = _networkModel.getNetwork(nic.getNetworkId());
                if (_networkModel.isSharedNetworkWithoutServices(network.getId())) {
                    vmIdCountMap.put(nic.getId(), new VmAndCountDetails(nic.getInstanceId(), VmIpFetchTrialMax.value()));
                }
            }
        }
    });
    return true;
}
Also used : NetworkVO(com.cloud.network.dao.NetworkVO) StartAnswer(com.cloud.agent.api.StartAnswer) RestoreVMSnapshotAnswer(com.cloud.agent.api.RestoreVMSnapshotAnswer) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) ExecutionException(com.cloud.utils.exception.ExecutionException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) VirtualMachineMigrationException(com.cloud.exception.VirtualMachineMigrationException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) NoTransitionException(com.cloud.utils.fsm.NoTransitionException) CloudException(com.cloud.exception.CloudException) OperationTimedoutException(com.cloud.exception.OperationTimedoutException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) InsufficientAddressCapacityException(com.cloud.exception.InsufficientAddressCapacityException) StorageUnavailableException(com.cloud.exception.StorageUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) ConfigurationException(javax.naming.ConfigurationException) ManagementServerException(com.cloud.exception.ManagementServerException) GetVmStatsAnswer(com.cloud.agent.api.GetVmStatsAnswer) Answer(com.cloud.agent.api.Answer) StartAnswer(com.cloud.agent.api.StartAnswer) RestoreVMSnapshotAnswer(com.cloud.agent.api.RestoreVMSnapshotAnswer) GetVmDiskStatsAnswer(com.cloud.agent.api.GetVmDiskStatsAnswer) Network(com.cloud.network.Network) PhysicalNetwork(com.cloud.network.PhysicalNetwork) ArrayList(java.util.ArrayList) ExcludeList(com.cloud.deploy.DeploymentPlanner.ExcludeList) List(java.util.List) NicTO(com.cloud.agent.api.to.NicTO)

Aggregations

VirtualMachineTO (com.cloud.agent.api.to.VirtualMachineTO)84 NicTO (com.cloud.agent.api.to.NicTO)42 Test (org.junit.Test)33 Answer (com.cloud.agent.api.Answer)32 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)25 HashMap (java.util.HashMap)18 LibvirtException (org.libvirt.LibvirtException)18 StartAnswer (com.cloud.agent.api.StartAnswer)17 AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)17 URISyntaxException (java.net.URISyntaxException)16 Connect (org.libvirt.Connect)16 InternalErrorException (com.cloud.exception.InternalErrorException)15 CheckRouterAnswer (com.cloud.agent.api.CheckRouterAnswer)14 KVMStoragePoolManager (com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager)14 Connection (com.xensource.xenapi.Connection)14 ConfigurationException (javax.naming.ConfigurationException)14 LibvirtRequestWrapper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper)13 LibvirtUtilitiesHelper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper)13 StartCommand (com.cloud.agent.api.StartCommand)12 ArrayList (java.util.ArrayList)11