Search in sources :

Example 6 with HypervisorGuru

use of com.cloud.hypervisor.HypervisorGuru in project cloudstack by apache.

the class VirtualMachineManagerImpl method toVmTO.

@Override
public VirtualMachineTO toVmTO(final VirtualMachineProfile profile) {
    final HypervisorGuru hvGuru = _hvGuruMgr.getGuru(profile.getVirtualMachine().getHypervisorType());
    final VirtualMachineTO to = hvGuru.implement(profile);
    return to;
}
Also used : HypervisorGuru(com.cloud.hypervisor.HypervisorGuru) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO)

Example 7 with HypervisorGuru

use of com.cloud.hypervisor.HypervisorGuru in project cloudstack by apache.

the class VirtualMachineManagerImpl method orchestrateAddVmToNetwork.

private NicProfile orchestrateAddVmToNetwork(final VirtualMachine vm, final Network network, final NicProfile requested) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException {
    final CallContext cctx = CallContext.current();
    s_logger.debug("Adding vm " + vm + " to network " + network + "; requested nic profile " + requested);
    final VMInstanceVO vmVO = _vmDao.findById(vm.getId());
    final ReservationContext context = new ReservationContextImpl(null, null, cctx.getCallingUser(), cctx.getCallingAccount());
    final VirtualMachineProfileImpl vmProfile = new VirtualMachineProfileImpl(vmVO, null, null, null, null);
    final DataCenter dc = _entityMgr.findById(DataCenter.class, network.getDataCenterId());
    final Host host = _hostDao.findById(vm.getHostId());
    final DeployDestination dest = new DeployDestination(dc, null, null, host);
    //check vm state
    if (vm.getState() == State.Running) {
        //1) allocate and prepare nic
        final NicProfile nic = _networkMgr.createNicForVm(network, requested, context, vmProfile, true);
        //2) Convert vmProfile to vmTO
        final HypervisorGuru hvGuru = _hvGuruMgr.getGuru(vmProfile.getVirtualMachine().getHypervisorType());
        final VirtualMachineTO vmTO = hvGuru.implement(vmProfile);
        //3) Convert nicProfile to NicTO
        final NicTO nicTO = toNicTO(nic, vmProfile.getVirtualMachine().getHypervisorType());
        //4) plug the nic to the vm
        s_logger.debug("Plugging nic for vm " + vm + " in network " + network);
        boolean result = false;
        try {
            result = plugNic(network, nicTO, vmTO, context, dest);
            if (result) {
                s_logger.debug("Nic is plugged successfully for vm " + vm + " in network " + network + ". Vm  is a part of network now");
                final long isDefault = nic.isDefaultNic() ? 1 : 0;
                // insert nic's Id into DB as resource_name
                if (VirtualMachine.Type.User.equals(vmVO.getType())) {
                    //Log usage event for user Vms only
                    UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NETWORK_OFFERING_ASSIGN, vmVO.getAccountId(), vmVO.getDataCenterId(), vmVO.getId(), Long.toString(nic.getId()), network.getNetworkOfferingId(), null, isDefault, VirtualMachine.class.getName(), vmVO.getUuid(), vm.isDisplay());
                }
                return nic;
            } else {
                s_logger.warn("Failed to plug nic to the vm " + vm + " in network " + network);
                return null;
            }
        } finally {
            if (!result) {
                s_logger.debug("Removing nic " + nic + " from vm " + vmProfile.getVirtualMachine() + " as nic plug failed on the backend");
                _networkMgr.removeNic(vmProfile, _nicsDao.findById(nic.getId()));
            }
        }
    } else if (vm.getState() == State.Stopped) {
        //1) allocate nic
        return _networkMgr.createNicForVm(network, requested, context, vmProfile, false);
    } else {
        s_logger.warn("Unable to add vm " + vm + " to network  " + network);
        throw new ResourceUnavailableException("Unable to add vm " + vm + " to network, is not in the right state", DataCenter.class, vm.getDataCenterId());
    }
}
Also used : Host(com.cloud.host.Host) CallContext(org.apache.cloudstack.context.CallContext) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) DataCenter(com.cloud.dc.DataCenter) HypervisorGuru(com.cloud.hypervisor.HypervisorGuru) DeployDestination(com.cloud.deploy.DeployDestination) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) NicTO(com.cloud.agent.api.to.NicTO)

Example 8 with HypervisorGuru

use of com.cloud.hypervisor.HypervisorGuru in project cloudstack by apache.

the class VirtualMachineManagerImpl method orchestrateRemoveVmFromNetwork.

@DB
private boolean orchestrateRemoveVmFromNetwork(final VirtualMachine vm, final Network network, final URI broadcastUri) throws ConcurrentOperationException, ResourceUnavailableException {
    final CallContext cctx = CallContext.current();
    final VMInstanceVO vmVO = _vmDao.findById(vm.getId());
    final ReservationContext context = new ReservationContextImpl(null, null, cctx.getCallingUser(), cctx.getCallingAccount());
    final VirtualMachineProfileImpl vmProfile = new VirtualMachineProfileImpl(vmVO, null, null, null, null);
    final DataCenter dc = _entityMgr.findById(DataCenter.class, network.getDataCenterId());
    final Host host = _hostDao.findById(vm.getHostId());
    final DeployDestination dest = new DeployDestination(dc, null, null, host);
    final HypervisorGuru hvGuru = _hvGuruMgr.getGuru(vmProfile.getVirtualMachine().getHypervisorType());
    final VirtualMachineTO vmTO = hvGuru.implement(vmProfile);
    Nic nic = null;
    if (broadcastUri != null) {
        nic = _nicsDao.findByNetworkIdInstanceIdAndBroadcastUri(network.getId(), vm.getId(), broadcastUri.toString());
    } else {
        nic = _networkModel.getNicInNetwork(vm.getId(), network.getId());
    }
    if (nic == null) {
        s_logger.warn("Could not get a nic with " + network);
        return false;
    }
    // don't delete default NIC on a user VM
    if (nic.isDefaultNic() && vm.getType() == VirtualMachine.Type.User) {
        s_logger.warn("Failed to remove nic from " + vm + " in " + network + ", nic is default.");
        throw new CloudRuntimeException("Failed to remove nic from " + vm + " in " + network + ", nic is default.");
    }
    //Lock on nic is needed here
    final Nic lock = _nicsDao.acquireInLockTable(nic.getId());
    if (lock == null) {
        //check if nic is still there. Return if it was released already
        if (_nicsDao.findById(nic.getId()) == null) {
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Not need to remove the vm " + vm + " from network " + network + " as the vm doesn't have nic in this network");
            }
            return true;
        }
        throw new ConcurrentOperationException("Unable to lock nic " + nic.getId());
    }
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Lock is acquired for nic id " + lock.getId() + " as a part of remove vm " + vm + " from network " + network);
    }
    try {
        final NicProfile nicProfile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), _networkModel.getNetworkRate(network.getId(), vm.getId()), _networkModel.isSecurityGroupSupportedInNetwork(network), _networkModel.getNetworkTag(vmProfile.getVirtualMachine().getHypervisorType(), network));
        //1) Unplug the nic
        if (vm.getState() == State.Running) {
            final NicTO nicTO = toNicTO(nicProfile, vmProfile.getVirtualMachine().getHypervisorType());
            s_logger.debug("Un-plugging nic for vm " + vm + " from network " + network);
            final boolean result = unplugNic(network, nicTO, vmTO, context, dest);
            if (result) {
                s_logger.debug("Nic is unplugged successfully for vm " + vm + " in network " + network);
            } else {
                s_logger.warn("Failed to unplug nic for the vm " + vm + " from network " + network);
                return false;
            }
        } else if (vm.getState() != State.Stopped) {
            s_logger.warn("Unable to remove vm " + vm + " from network  " + network);
            throw new ResourceUnavailableException("Unable to remove vm " + vm + " from network, is not in the right state", DataCenter.class, vm.getDataCenterId());
        }
        //2) Release the nic
        _networkMgr.releaseNic(vmProfile, nic);
        s_logger.debug("Successfully released nic " + nic + "for vm " + vm);
        //3) Remove the nic
        _networkMgr.removeNic(vmProfile, nic);
        return true;
    } finally {
        if (lock != null) {
            _nicsDao.releaseFromLockTable(lock.getId());
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Lock is released for nic id " + lock.getId() + " as a part of remove vm " + vm + " from network " + network);
            }
        }
    }
}
Also used : Host(com.cloud.host.Host) CallContext(org.apache.cloudstack.context.CallContext) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) DataCenter(com.cloud.dc.DataCenter) HypervisorGuru(com.cloud.hypervisor.HypervisorGuru) DeployDestination(com.cloud.deploy.DeployDestination) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) NicTO(com.cloud.agent.api.to.NicTO) DB(com.cloud.utils.db.DB)

Example 9 with HypervisorGuru

use of com.cloud.hypervisor.HypervisorGuru in project cloudstack by apache.

the class VirtualMachineManagerImpl method toNicTO.

@Override
public NicTO toNicTO(final NicProfile nic, final HypervisorType hypervisorType) {
    final HypervisorGuru hvGuru = _hvGuruMgr.getGuru(hypervisorType);
    final NicTO nicTO = hvGuru.toNicTO(nic);
    return nicTO;
}
Also used : HypervisorGuru(com.cloud.hypervisor.HypervisorGuru) NicTO(com.cloud.agent.api.to.NicTO)

Example 10 with HypervisorGuru

use of com.cloud.hypervisor.HypervisorGuru in project cloudstack by apache.

the class VirtualMachineManagerImpl method orchestrateReConfigureVm.

private VMInstanceVO orchestrateReConfigureVm(final String vmUuid, final ServiceOffering oldServiceOffering, final boolean reconfiguringOnExistingHost) throws ResourceUnavailableException, ConcurrentOperationException {
    final VMInstanceVO vm = _vmDao.findByUuid(vmUuid);
    final long newServiceofferingId = vm.getServiceOfferingId();
    final ServiceOffering newServiceOffering = _offeringDao.findById(vm.getId(), newServiceofferingId);
    final HostVO hostVo = _hostDao.findById(vm.getHostId());
    final Float memoryOvercommitRatio = CapacityManager.MemOverprovisioningFactor.valueIn(hostVo.getClusterId());
    final Float cpuOvercommitRatio = CapacityManager.CpuOverprovisioningFactor.valueIn(hostVo.getClusterId());
    final long minMemory = (long) (newServiceOffering.getRamSize() / memoryOvercommitRatio);
    final ScaleVmCommand reconfigureCmd = new ScaleVmCommand(vm.getInstanceName(), newServiceOffering.getCpu(), (int) (newServiceOffering.getSpeed() / cpuOvercommitRatio), newServiceOffering.getSpeed(), minMemory * 1024L * 1024L, newServiceOffering.getRamSize() * 1024L * 1024L, newServiceOffering.getLimitCpuUse());
    final Long dstHostId = vm.getHostId();
    if (vm.getHypervisorType().equals(HypervisorType.VMware)) {
        final HypervisorGuru hvGuru = _hvGuruMgr.getGuru(vm.getHypervisorType());
        Map<String, String> details = null;
        details = hvGuru.getClusterSettings(vm.getId());
        reconfigureCmd.getVirtualMachine().setDetails(details);
    }
    final ItWorkVO work = new ItWorkVO(UUID.randomUUID().toString(), _nodeId, State.Running, vm.getType(), vm.getId());
    work.setStep(Step.Prepare);
    work.setResourceType(ItWorkVO.ResourceType.Host);
    work.setResourceId(vm.getHostId());
    _workDao.persist(work);
    boolean success = false;
    try {
        if (reconfiguringOnExistingHost) {
            vm.setServiceOfferingId(oldServiceOffering.getId());
            //release the old capacity
            _capacityMgr.releaseVmCapacity(vm, false, false, vm.getHostId());
            vm.setServiceOfferingId(newServiceofferingId);
            // lock the new capacity
            _capacityMgr.allocateVmCapacity(vm, false);
        }
        final Answer reconfigureAnswer = _agentMgr.send(vm.getHostId(), reconfigureCmd);
        if (reconfigureAnswer == null || !reconfigureAnswer.getResult()) {
            s_logger.error("Unable to scale vm due to " + (reconfigureAnswer == null ? "" : reconfigureAnswer.getDetails()));
            throw new CloudRuntimeException("Unable to scale vm due to " + (reconfigureAnswer == null ? "" : reconfigureAnswer.getDetails()));
        }
        success = true;
    } catch (final OperationTimedoutException e) {
        throw new AgentUnavailableException("Operation timed out on reconfiguring " + vm, dstHostId);
    } catch (final AgentUnavailableException e) {
        throw e;
    } finally {
        if (!success) {
            // release the new capacity
            _capacityMgr.releaseVmCapacity(vm, false, false, vm.getHostId());
            vm.setServiceOfferingId(oldServiceOffering.getId());
            // allocate the old capacity
            _capacityMgr.allocateVmCapacity(vm, false);
        }
    }
    return vm;
}
Also used : OperationTimedoutException(com.cloud.exception.OperationTimedoutException) ServiceOffering(com.cloud.offering.ServiceOffering) HostVO(com.cloud.host.HostVO) AgentControlAnswer(com.cloud.agent.api.AgentControlAnswer) RebootAnswer(com.cloud.agent.api.RebootAnswer) StartAnswer(com.cloud.agent.api.StartAnswer) RestoreVMSnapshotAnswer(com.cloud.agent.api.RestoreVMSnapshotAnswer) PlugNicAnswer(com.cloud.agent.api.PlugNicAnswer) StopAnswer(com.cloud.agent.api.StopAnswer) Answer(com.cloud.agent.api.Answer) UnPlugNicAnswer(com.cloud.agent.api.UnPlugNicAnswer) ClusterVMMetaDataSyncAnswer(com.cloud.agent.api.ClusterVMMetaDataSyncAnswer) CheckVirtualMachineAnswer(com.cloud.agent.api.CheckVirtualMachineAnswer) HypervisorGuru(com.cloud.hypervisor.HypervisorGuru) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) ScaleVmCommand(com.cloud.agent.api.ScaleVmCommand)

Aggregations

HypervisorGuru (com.cloud.hypervisor.HypervisorGuru)11 VirtualMachineTO (com.cloud.agent.api.to.VirtualMachineTO)7 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)6 DeployDestination (com.cloud.deploy.DeployDestination)5 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)5 CheckVirtualMachineAnswer (com.cloud.agent.api.CheckVirtualMachineAnswer)4 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)4 OperationTimedoutException (com.cloud.exception.OperationTimedoutException)4 CallContext (org.apache.cloudstack.context.CallContext)4 AgentControlAnswer (com.cloud.agent.api.AgentControlAnswer)3 Answer (com.cloud.agent.api.Answer)3 ClusterVMMetaDataSyncAnswer (com.cloud.agent.api.ClusterVMMetaDataSyncAnswer)3 PlugNicAnswer (com.cloud.agent.api.PlugNicAnswer)3 RebootAnswer (com.cloud.agent.api.RebootAnswer)3 RestoreVMSnapshotAnswer (com.cloud.agent.api.RestoreVMSnapshotAnswer)3 StartAnswer (com.cloud.agent.api.StartAnswer)3 StopAnswer (com.cloud.agent.api.StopAnswer)3 UnPlugNicAnswer (com.cloud.agent.api.UnPlugNicAnswer)3 NicTO (com.cloud.agent.api.to.NicTO)3 Commands (com.cloud.agent.manager.Commands)3