11package com .cloud .deploy ;
22
33import java .util .List ;
4+ import java .util .Map ;
45
56import javax .ejb .Local ;
7+ import javax .naming .ConfigurationException ;
68
79import org .apache .log4j .Logger ;
810
11+ import com .cloud .capacity .CapacityManager ;
12+ import com .cloud .dc .ClusterVO ;
13+ import com .cloud .dc .DataCenter ;
14+ import com .cloud .dc .Pod ;
15+ import com .cloud .dc .dao .ClusterDao ;
16+ import com .cloud .dc .dao .DataCenterDao ;
17+ import com .cloud .dc .dao .HostPodDao ;
918import com .cloud .exception .InsufficientServerCapacityException ;
1019import com .cloud .host .Host ;
1120import com .cloud .host .HostVO ;
21+ import com .cloud .host .Status ;
22+ import com .cloud .host .dao .HostDao ;
1223import com .cloud .hypervisor .Hypervisor .HypervisorType ;
1324import com .cloud .offering .ServiceOffering ;
14- import com .cloud .service .ServiceOfferingVO ;
25+ import com .cloud .org .Cluster ;
26+ import com .cloud .utils .component .Inject ;
1527import com .cloud .utils .exception .CloudRuntimeException ;
1628import com .cloud .vm .VirtualMachine ;
1729import com .cloud .vm .VirtualMachineProfile ;
1830import com .cloud .vm .VirtualMachineProfileImpl ;
1931
2032@ Local (value =DeploymentPlanner .class )
21- public class BareMetalPlanner extends FirstFitPlanner implements DeploymentPlanner {
33+ public class BareMetalPlanner implements DeploymentPlanner {
2234 private static final Logger s_logger = Logger .getLogger (BareMetalPlanner .class );
35+ @ Inject protected DataCenterDao _dcDao ;
36+ @ Inject protected HostPodDao _podDao ;
37+ @ Inject protected ClusterDao _clusterDao ;
38+ @ Inject protected HostDao _hostDao ;
39+ @ Inject protected CapacityManager _capacityMgr ;
40+ String _name ;
2341
2442 @ Override
2543 public DeployDestination plan (VirtualMachineProfile vmProfile , DeploymentPlan plan , ExcludeList avoid ) throws InsufficientServerCapacityException {
2644 VirtualMachine vm = vmProfile .getVirtualMachine ();
27- ServiceOffering offering = vmProfile .getServiceOffering ();
45+ ServiceOffering offering = vmProfile .getServiceOffering ();
2846 String hostTag = null ;
2947
3048 if (offering .getTags () != null ) {
@@ -34,47 +52,79 @@ public DeployDestination plan(VirtualMachineProfile vmProfile, DeploymentPlan pl
3452 }
3553 }
3654
55+ List <ClusterVO > clusters = _clusterDao .listByHyTypeWithoutGuid (HypervisorType .BareMetal .name ());
56+ if (clusters .size () != 1 ) {
57+ throw new CloudRuntimeException ("Invaild baremetal cluster number " + clusters .size ());
58+ }
59+ Cluster cluster = clusters .get (0 );
60+
61+ int cpu_requested ;
62+ long ram_requested ;
63+ HostVO target = null ;
64+ List <HostVO > hosts = _hostDao .listByCluster (cluster .getId ());
3765 if (hostTag != null ) {
38- List <HostVO > hosts = _hostDao .listBy (Host .Type .Routing , vm .getDataCenterId ());
39- HostVO target = null ;
4066 for (HostVO h : hosts ) {
4167 _hostDao .loadDetails (h );
4268 if (h .getDetail ("hostTag" ) != null && h .getDetail ("hostTag" ).equalsIgnoreCase (hostTag )) {
4369 target = h ;
4470 break ;
4571 }
4672 }
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 );
73+ }
74+
75+ if (target == null ) {
76+ s_logger .warn ("Cannot find host with tag " + hostTag + " use capacity from service offering" );
77+ cpu_requested = offering .getCpu () * offering .getSpeed ();
78+ ram_requested = offering .getRamSize () * 1024 * 1024 ;
79+ } else {
80+ cpu_requested = target .getCpus () * target .getSpeed ().intValue ();
81+ ram_requested = target .getTotalMemory ();
5982 }
6083
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 ()));
84+ for (HostVO h : hosts ) {
85+ if (h .getStatus () == Status .Up ) {
86+ if (_capacityMgr .checkIfHostHasCapacity (h .getId (), cpu_requested , ram_requested , false )){
87+ s_logger .debug ("Find host " + h .getId () + " has enough capacity" );
88+ DataCenter dc = _dcDao .findById (h .getDataCenterId ());
89+ Pod pod = _podDao .findById (h .getPodId ());
90+ return new DeployDestination (dc , pod , cluster , h );
91+ }
7092 }
7193 }
7294
73- return dest ;
95+ s_logger .warn (String .format ("Cannot find enough capacity(requested cpu=%1$s memory=%2$s)" , cpu_requested , ram_requested ));
96+ return null ;
7497 }
7598
7699 @ Override
77100 public boolean canHandle (VirtualMachineProfile <? extends VirtualMachine > vm , DeploymentPlan plan , ExcludeList avoid ) {
78101 return vm .getHypervisorType () == HypervisorType .BareMetal ;
79102 }
103+
104+ @ Override
105+ public boolean configure (String name , Map <String , Object > params ) throws ConfigurationException {
106+ _name = name ;
107+ return true ;
108+ }
109+
110+ @ Override
111+ public String getName () {
112+ return _name ;
113+ }
114+
115+ @ Override
116+ public boolean start () {
117+ return true ;
118+ }
119+
120+ @ Override
121+ public boolean stop () {
122+ return true ;
123+ }
124+
125+ @ Override
126+ public boolean check (VirtualMachineProfile <? extends VirtualMachine > vm , DeploymentPlan plan , DeployDestination dest , ExcludeList exclude ) {
127+ // TODO Auto-generated method stub
128+ return false ;
129+ }
80130}
0 commit comments