Skip to content

Commit 7fa0533

Browse files
committed
Bug 8208 - bare metal provisioning
Add bare metal planner
1 parent ec7a6e6 commit 7fa0533

13 files changed

Lines changed: 136 additions & 11 deletions

File tree

‎api/src/com/cloud/api/ApiConstants.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ public class ApiConstants {
194194
public static final String HOST_CPU_NUM = "hostcpunum";
195195
public static final String HOST_MEM_CAPACITY = "hostmemcapacity";
196196
public static final String HOST_MAC = "hostmac";
197+
public static final String HOST_TAG = "hosttag";
197198
public static final String PXE_SERVER_TYPE = "pxeservertype";
198199
public static final String LINMIN_USERNAME = "linminusername";
199200
public static final String LINMIN_PASSWORD = "linminpassword";

‎api/src/com/cloud/api/commands/AddHostCmd.java‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,21 @@ public class AddHostCmd extends BaseCmd {
6666
@Parameter(name=ApiConstants.HYPERVISOR, type=CommandType.STRING, required=true, description="hypervisor type of the host")
6767
private String hypervisor;
6868

69-
@Parameter(name=ApiConstants.HOST_CPU_CAPACITY, type=CommandType.LONG, description="HZ per CPU of host")
69+
@Parameter(name=ApiConstants.HOST_CPU_CAPACITY, type=CommandType.LONG, description="Only for hypervisor is BareMetal, HZ per CPU of host")
7070
private Long cpuCapacity;
7171

72-
@Parameter(name=ApiConstants.HOST_CPU_NUM, type=CommandType.LONG, description="number of CPU on host")
72+
@Parameter(name=ApiConstants.HOST_CPU_NUM, type=CommandType.LONG, description="Only for hypervisor is BareMetal, number of CPU on host")
7373
private Long cpuNum;
7474

75-
@Parameter(name=ApiConstants.HOST_MEM_CAPACITY, type=CommandType.LONG, description="memory capacity of host")
75+
@Parameter(name=ApiConstants.HOST_MEM_CAPACITY, type=CommandType.LONG, description="Only for hypervisor is BareMetal, memory capacity of host(in MB)")
7676
private Long memCapacity;
7777

78-
@Parameter(name=ApiConstants.HOST_MAC, type=CommandType.STRING, required=true, description="Mac of PXE nic")
78+
@Parameter(name=ApiConstants.HOST_MAC, type=CommandType.STRING, description="Only for hypervisor is BareMetal, Mac of PXE nic")
7979
private String mac;
8080

81+
@Parameter(name=ApiConstants.HOST_TAG, type=CommandType.STRING, description="Only for hypervisor is BareMetal, Tag of host")
82+
private String hostTag;
83+
8184

8285

8386

@@ -133,6 +136,10 @@ public String getMac() {
133136
return mac;
134137
}
135138

139+
public String getHostTag() {
140+
return hostTag;
141+
}
142+
136143
/////////////////////////////////////////////////////
137144
/////////////// API Implementation///////////////////
138145
/////////////////////////////////////////////////////

‎api/src/com/cloud/deploy/DeploymentPlanner.java‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ public interface DeploymentPlanner extends Adapter {
4444
*/
4545
boolean check(VirtualMachineProfile<? extends VirtualMachine> vm, DeploymentPlan plan, DeployDestination dest, ExcludeList exclude);
4646

47+
/**
48+
* canHandle is called before plan to determine if the plan can do the allocation. Planers should be exclusive so planner writer must
49+
* make sure only one planer->canHandle return true in the planner list
50+
*
51+
* @param vm virtual machine.
52+
* @param plan deployment plan that tells you where it's being deployed to.
53+
* @param avoid avoid these data centers, pods, clusters, or hosts.
54+
* @return true if it's okay to allocate; false or not
55+
*/
56+
boolean canHandle(VirtualMachineProfile<? extends VirtualMachine> vm, DeploymentPlan plan, ExcludeList avoid);
57+
4758
public static class ExcludeList {
4859
Set<Long> _dcIds;
4960
Set<Long> _podIds;

‎api/src/com/cloud/offering/ServiceOffering.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,4 @@ public interface ServiceOffering {
7373
boolean getUseLocalStorage();
7474

7575
Long getDomainId();
76-
7776
}

‎client/tomcatconf/components.xml.in‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
</adapters>
7979
<adapters key="com.cloud.deploy.DeploymentPlanner">
8080
<adapter name="First Fit" class="com.cloud.deploy.FirstFitPlanner"/>
81+
<adapter name="BareMetal Fit" class="com.cloud.deploy.BareMetalPlanner"/>
8182
</adapters>
8283
<adapters key="com.cloud.network.element.NetworkElement">
8384
<adapter name="DomainRouter" class="com.cloud.network.element.VirtualRouterElement"/>

‎server/src/com/cloud/agent/manager/AgentManagerImpl.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,7 @@ public List<? extends Host> discoverHosts(AddHostCmd cmd)
754754
Long cpuCapacity = cmd.getCpuCapacity();
755755
Long cpuNum = cmd.getCpuNum();
756756
String mac = cmd.getMac();
757+
String hostTag = cmd.getHostTag();
757758
Map<String, String>bareMetalParams = new HashMap<String, String>();
758759

759760
// this is for standalone option
@@ -779,6 +780,9 @@ public List<? extends Host> discoverHosts(AddHostCmd cmd)
779780
bareMetalParams.put("cpuCapacity", cpuCapacity.toString());
780781
bareMetalParams.put("memCapacity", memCapacity.toString());
781782
bareMetalParams.put("mac", mac);
783+
if (hostTag != null) {
784+
bareMetalParams.put("hostTag", hostTag);
785+
}
782786
}
783787

784788

‎server/src/com/cloud/baremetal/LinMinPxeServerManagerImpl.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,15 @@ public Host addPxeServer(AddPxeServerCmd cmd) throws InvalidParameterValueExcept
6464
apiPassword = cmd.getLinMinPassword();
6565
apid = cmd.getLinMinApid();
6666
if (apiUsername == null) {
67-
throw new InvalidParameterValueException("No LinMin username specified, without it I can user LinMin API");
67+
throw new InvalidParameterValueException("No LinMin username specified, without it I can not use LinMin API");
6868
}
6969

7070
if (apiPassword == null) {
71-
throw new InvalidParameterValueException("No LinMin password specified, without it I can user LinMin API");
71+
throw new InvalidParameterValueException("No LinMin password specified, without it I can not use LinMin API");
7272
}
7373

7474
if (apid == null) {
75-
throw new InvalidParameterValueException("No LinMin apid specified, without it I can user LinMin API");
75+
throw new InvalidParameterValueException("No LinMin apid specified, without it I can not use LinMin API");
7676
}
7777

7878
String ipAddress = uri.getHost();
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
}

‎server/src/com/cloud/deploy/FirstFitPlanner.java‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@
2020
import com.cloud.dc.dao.ClusterDao;
2121
import com.cloud.dc.dao.DataCenterDao;
2222
import com.cloud.dc.dao.HostPodDao;
23+
import com.cloud.deploy.DeploymentPlanner.ExcludeList;
2324
import com.cloud.exception.InsufficientServerCapacityException;
2425
import com.cloud.host.DetailVO;
2526
import com.cloud.host.Host;
2627
import com.cloud.host.HostVO;
2728
import com.cloud.host.Status;
2829
import com.cloud.host.dao.DetailsDao;
2930
import com.cloud.host.dao.HostDao;
31+
import com.cloud.hypervisor.Hypervisor.HypervisorType;
3032
import com.cloud.offering.ServiceOffering;
3133
import com.cloud.org.Cluster;
3234
import com.cloud.storage.GuestOSCategoryVO;
@@ -43,7 +45,7 @@
4345
@Local(value=DeploymentPlanner.class)
4446
public class FirstFitPlanner extends PlannerBase implements DeploymentPlanner {
4547
private static final Logger s_logger = Logger.getLogger(FirstFitPlanner.class);
46-
@Inject private HostDao _hostDao;
48+
@Inject protected HostDao _hostDao;
4749
@Inject private CapacityDao _capacityDao;
4850
@Inject private DataCenterDao _dcDao;
4951
@Inject private HostPodDao _podDao;
@@ -286,4 +288,9 @@ protected String getTemplateGuestOSCategory(VirtualMachineTemplate template) {
286288
GuestOSCategoryVO guestOSCategory = _guestOSCategoryDao.findById(guestOSCategoryId);
287289
return guestOSCategory.getName();
288290
}
291+
292+
@Override
293+
public boolean canHandle(VirtualMachineProfile<? extends VirtualMachine> vm, DeploymentPlan plan, ExcludeList avoid) {
294+
return vm.getHypervisorType() != HypervisorType.BareMetal;
295+
}
289296
}

‎server/src/com/cloud/deploy/SimplePlanner.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@
2626
import com.cloud.dc.dao.ClusterDao;
2727
import com.cloud.dc.dao.DataCenterDao;
2828
import com.cloud.dc.dao.HostPodDao;
29+
import com.cloud.deploy.DeploymentPlanner.ExcludeList;
2930
import com.cloud.host.Host;
3031
import com.cloud.host.Host.Type;
3132
import com.cloud.host.HostVO;
3233
import com.cloud.host.dao.HostDao;
3334
import com.cloud.org.Cluster;
3435
import com.cloud.utils.component.Inject;
36+
import com.cloud.vm.VirtualMachine;
3537
import com.cloud.vm.VirtualMachineProfile;
3638

3739
@Local(value=DeploymentPlanner.class)
@@ -68,4 +70,10 @@ public boolean check(VirtualMachineProfile vm, DeploymentPlan plan, DeployDestin
6870
protected SimplePlanner() {
6971
super();
7072
}
73+
74+
//TODO: set it to true if you want to use it
75+
@Override
76+
public boolean canHandle(VirtualMachineProfile<? extends VirtualMachine> vm, DeploymentPlan plan, ExcludeList avoid) {
77+
return false;
78+
}
7179
}

0 commit comments

Comments
 (0)