|
| 1 | +package com.cloud.deploy; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +import javax.ejb.Local; |
| 6 | + |
| 7 | +import org.apache.log4j.Logger; |
| 8 | + |
| 9 | +import com.cloud.exception.InsufficientServerCapacityException; |
| 10 | +import com.cloud.host.Host; |
| 11 | +import com.cloud.host.HostVO; |
| 12 | +import com.cloud.hypervisor.Hypervisor.HypervisorType; |
| 13 | +import com.cloud.offering.ServiceOffering; |
| 14 | +import com.cloud.service.ServiceOfferingVO; |
| 15 | +import com.cloud.utils.exception.CloudRuntimeException; |
| 16 | +import com.cloud.vm.VirtualMachine; |
| 17 | +import com.cloud.vm.VirtualMachineProfile; |
| 18 | +import com.cloud.vm.VirtualMachineProfileImpl; |
| 19 | + |
| 20 | +@Local(value=DeploymentPlanner.class) |
| 21 | +public class BareMetalPlanner extends FirstFitPlanner implements DeploymentPlanner { |
| 22 | + private static final Logger s_logger = Logger.getLogger(BareMetalPlanner.class); |
| 23 | + |
| 24 | + @Override |
| 25 | + public DeployDestination plan(VirtualMachineProfile vmProfile, DeploymentPlan plan, ExcludeList avoid) throws InsufficientServerCapacityException { |
| 26 | + VirtualMachine vm = vmProfile.getVirtualMachine(); |
| 27 | + ServiceOffering offering = vmProfile.getServiceOffering(); |
| 28 | + String hostTag = null; |
| 29 | + |
| 30 | + if (offering.getTags() != null) { |
| 31 | + String[] tags = offering.getTags().split(","); |
| 32 | + if (tags.length > 0) { |
| 33 | + hostTag = tags[0]; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + if (hostTag != null) { |
| 38 | + List<HostVO> hosts = _hostDao.listBy(Host.Type.Routing, vm.getDataCenterId()); |
| 39 | + HostVO target = null; |
| 40 | + for (HostVO h : hosts) { |
| 41 | + _hostDao.loadDetails(h); |
| 42 | + if (h.getDetail("hostTag") != null && h.getDetail("hostTag").equalsIgnoreCase(hostTag)) { |
| 43 | + target = h; |
| 44 | + break; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + if (target == null) { |
| 49 | + s_logger.warn("Cannot find host with tag " + hostTag); |
| 50 | + return null; |
| 51 | + } |
| 52 | + |
| 53 | + int cpu = target.getCpus(); |
| 54 | + int speed = target.getSpeed().intValue(); |
| 55 | + Long ramSize = target.getTotalMemory() / (1024L*1024L); |
| 56 | + ServiceOfferingVO newOffering = new ServiceOfferingVO(offering.getName(), cpu, ramSize.intValue(), speed, offering.getRateMbps(), |
| 57 | + offering.getMulticastRateMbps(), false, offering.getDisplayText(), offering.getUseLocalStorage(), false, offering.getTags(), false); |
| 58 | + ((VirtualMachineProfileImpl)vmProfile).setServiceOffering(newOffering); |
| 59 | + } |
| 60 | + |
| 61 | + DeployDestination dest = super.plan(vmProfile, plan, avoid); |
| 62 | + |
| 63 | + if (hostTag == null && dest != null) { |
| 64 | + Host h = dest.getHost(); |
| 65 | + if (h.getCpus() != offering.getCpu() || h.getTotalMemory() != offering.getRamSize() || h.getSpeed() != offering.getSpeed()) { |
| 66 | + throw new CloudRuntimeException(String.format("Bare Metal only allows one VM one host, " + |
| 67 | + "the offering capcacity doesn't equal to host capacity(offering: cpu number:%$1s, cpu speed:%$2s," + |
| 68 | + "ram size:%3$s; host: cpu number:%$4s, cpu speed:%$5s, ram size:%$6s)", offering.getCpu(), offering.getSpeed(), |
| 69 | + offering.getRamSize(), h.getCpus(), h.getSpeed(), h.getTotalMemory())); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return dest; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public boolean canHandle(VirtualMachineProfile<? extends VirtualMachine> vm, DeploymentPlan plan, ExcludeList avoid) { |
| 78 | + return vm.getHypervisorType() == HypervisorType.BareMetal; |
| 79 | + } |
| 80 | +} |
0 commit comments