Skip to content

Commit 5035778

Browse files
committed
Bug 8208 - bare metal provisioning
StartCommand cloud reach to BareMetalResrouce
1 parent f82dec9 commit 5035778

7 files changed

Lines changed: 158 additions & 4 deletions

File tree

‎api/src/com/cloud/network/Network.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ public static class Provider {
7373
public static final Provider DhcpServer = new Provider("DhcpServer");
7474
public static final Provider JuniperSRX = new Provider("JuniperSRX");
7575
public static final Provider F5BigIp = new Provider("F5BigIp");
76+
public static final Provider ExternalDhcpServer = new Provider("ExternalDhcpServer");
77+
public static final Provider ExternalGateWay = new Provider("ExternalGateWay");
7678

7779
private String name;
7880

‎api/src/com/cloud/vm/VirtualMachine.java‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,13 @@ public enum Type {
145145
User,
146146
DomainRouter,
147147
ConsoleProxy,
148-
SecondaryStorageVm
148+
SecondaryStorageVm,
149+
150+
/*
151+
* UserBareMetal is only used for selecting VirtualMachineGuru, there is no
152+
* VM with this type. UserBareMetal should treat exactly as User.
153+
*/
154+
UserBareMetal,
149155
}
150156

151157
public String getInstanceName();

‎client/tomcatconf/components.xml.in‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
<adapter name="XenServerGuru" class="com.cloud.hypervisor.XenServerGuru"/>
7070
<adapter name="KVMGuru" class="com.cloud.hypervisor.KVMGuru"/>
7171
<adapter name="VMwareGuru" class="com.cloud.hypervisor.VMwareGuru"/>
72+
<adapter name="BareMetalGuru" class="com.cloud.hypervisor.BareMetalGuru"/>
7273
</adapters>
7374
<adapters key="com.cloud.resource.Discoverer">
7475
<adapter name="XCP Agent" class="com.cloud.hypervisor.xen.discoverer.XcpServerDiscoverer"/>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.cloud.hypervisor;
2+
3+
import javax.ejb.Local;
4+
5+
import com.cloud.agent.api.to.VirtualMachineTO;
6+
import com.cloud.hypervisor.Hypervisor.HypervisorType;
7+
import com.cloud.storage.GuestOSVO;
8+
import com.cloud.storage.dao.GuestOSDao;
9+
import com.cloud.utils.component.Inject;
10+
import com.cloud.vm.VirtualMachine;
11+
import com.cloud.vm.VirtualMachineProfile;
12+
13+
@Local(value=HypervisorGuru.class)
14+
public class BareMetalGuru extends HypervisorGuruBase implements HypervisorGuru {
15+
@Inject GuestOSDao _guestOsDao;
16+
17+
protected BareMetalGuru() {
18+
super();
19+
}
20+
21+
@Override
22+
public HypervisorType getHypervisorType() {
23+
return HypervisorType.BareMetal;
24+
}
25+
26+
@Override
27+
public <T extends VirtualMachine> VirtualMachineTO implement(VirtualMachineProfile<T> vm) {
28+
VirtualMachineTO to = toVirtualMachineTO(vm);
29+
30+
// Determine the VM's OS description
31+
GuestOSVO guestOS = _guestOsDao.findById(vm.getVirtualMachine().getGuestOSId());
32+
to.setOs(guestOS.getDisplayName());
33+
34+
return to;
35+
}
36+
37+
}

‎server/src/com/cloud/network/element/DhcpElement.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ private boolean canHandle(GuestIpType ipType, DeployDestination dest, TrafficTyp
8080

8181
if (provider.equalsIgnoreCase(Provider.JuniperSRX.getName()) && ipType == GuestIpType.Virtual) {
8282
return true;
83+
} else if (dc.getDhcpProvider().equalsIgnoreCase(Provider.ExternalDhcpServer.getName())){
84+
return false;
8385
} else {
8486
if (dc.getNetworkType() == NetworkType.Basic) {
8587
return (ipType == GuestIpType.Direct && trafficType == TrafficType.Guest);

‎server/src/com/cloud/vm/BareMetalVmManagerImpl.java‎

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,26 @@
22

33
import java.util.ArrayList;
44
import java.util.List;
5+
import java.util.Map;
6+
import java.util.concurrent.Executors;
57

68
import javax.ejb.Local;
9+
import javax.naming.ConfigurationException;
710

811
import org.apache.log4j.Logger;
912

13+
import com.cloud.agent.manager.Commands;
1014
import com.cloud.api.commands.AttachVolumeCmd;
1115
import com.cloud.api.commands.CreateTemplateCmd;
1216
import com.cloud.api.commands.DeployVMCmd;
1317
import com.cloud.api.commands.DetachVolumeCmd;
1418
import com.cloud.api.commands.UpgradeVMCmd;
1519
import com.cloud.configuration.ResourceCount.ResourceType;
20+
import com.cloud.configuration.dao.ConfigurationDao;
1621
import com.cloud.dc.DataCenterVO;
1722
import com.cloud.dc.DataCenter.NetworkType;
1823
import com.cloud.deploy.DataCenterDeployment;
24+
import com.cloud.deploy.DeployDestination;
1925
import com.cloud.domain.DomainVO;
2026
import com.cloud.event.EventTypes;
2127
import com.cloud.event.UsageEventVO;
@@ -39,15 +45,20 @@
3945
import com.cloud.user.SSHKeyPair;
4046
import com.cloud.user.UserContext;
4147
import com.cloud.uservm.UserVm;
48+
import com.cloud.utils.NumbersUtil;
4249
import com.cloud.utils.Pair;
50+
import com.cloud.utils.component.ComponentLocator;
4351
import com.cloud.utils.component.Manager;
52+
import com.cloud.utils.concurrency.NamedThreadFactory;
4453
import com.cloud.utils.db.DB;
4554
import com.cloud.utils.exception.CloudRuntimeException;
4655
import com.cloud.utils.net.NetUtils;
56+
import com.cloud.vm.VirtualMachine.Type;
4757

4858
@Local(value={BareMetalVmManager.class, BareMetalVmService.class})
4959
public class BareMetalVmManagerImpl extends UserVmManagerImpl implements BareMetalVmManager, BareMetalVmService, Manager {
5060
private static final Logger s_logger = Logger.getLogger(BareMetalVmManagerImpl.class);
61+
private ConfigurationDao _configDao;
5162

5263
@Override
5364
public boolean attachISOToVM(long vmId, long isoId, boolean attach) {
@@ -275,6 +286,87 @@ public UserVm createVirtualMachine(DeployVMCmd cmd) throws InsufficientCapacityE
275286
}
276287

277288
public UserVm startVirtualMachine(DeployVMCmd cmd) throws ResourceUnavailableException, InsufficientCapacityException, ConcurrentOperationException {
278-
return null;
289+
return super.startVirtualMachine(cmd);
290+
}
291+
292+
@Override
293+
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
294+
_name = name;
295+
296+
ComponentLocator locator = ComponentLocator.getCurrentLocator();
297+
_configDao = locator.getDao(ConfigurationDao.class);
298+
if (_configDao == null) {
299+
throw new ConfigurationException("Unable to get the configuration dao.");
300+
}
301+
302+
Map<String, String> configs = _configDao.getConfiguration("AgentManager", params);
303+
304+
_instance = configs.get("instance.name");
305+
if (_instance == null) {
306+
_instance = "DEFAULT";
307+
}
308+
309+
String workers = configs.get("expunge.workers");
310+
int wrks = NumbersUtil.parseInt(workers, 10);
311+
312+
String time = configs.get("expunge.interval");
313+
_expungeInterval = NumbersUtil.parseInt(time, 86400);
314+
315+
time = configs.get("expunge.delay");
316+
_expungeDelay = NumbersUtil.parseInt(time, _expungeInterval);
317+
318+
_executor = Executors.newScheduledThreadPool(wrks, new NamedThreadFactory("UserVm-Scavenger"));
319+
320+
_itMgr.registerGuru(Type.UserBareMetal, this);
321+
322+
s_logger.info("User VM Manager is configured.");
323+
324+
return true;
325+
}
326+
327+
@Override
328+
public boolean finalizeVirtualMachineProfile(VirtualMachineProfile<UserVmVO> profile, DeployDestination dest, ReservationContext context) {
329+
UserVmVO vm = profile.getVirtualMachine();
330+
Account owner = _accountDao.findById(vm.getAccountId());
331+
332+
if (owner == null || owner.getState() == Account.State.disabled) {
333+
throw new PermissionDeniedException("The owner of " + vm + " either does not exist or is disabled: " + vm.getAccountId());
334+
}
335+
336+
return true;
337+
}
338+
339+
@Override
340+
public boolean finalizeDeployment(Commands cmds, VirtualMachineProfile<UserVmVO> profile, DeployDestination dest, ReservationContext context) {
341+
UserVmVO userVm = profile.getVirtualMachine();
342+
List<NicVO> nics = _nicDao.listByVmId(userVm.getId());
343+
for (NicVO nic : nics) {
344+
NetworkVO network = _networkDao.findById(nic.getNetworkId());
345+
if (network.getTrafficType() == TrafficType.Guest) {
346+
userVm.setPrivateIpAddress(nic.getIp4Address());
347+
userVm.setPrivateMacAddress(nic.getMacAddress());
348+
}
349+
}
350+
_vmDao.update(userVm.getId(), userVm);
351+
return true;
352+
}
353+
354+
@Override
355+
public boolean finalizeStart(VirtualMachineProfile<UserVmVO> profile, long hostId, Commands cmds, ReservationContext context) {
356+
UserVmVO vm = profile.getVirtualMachine();
357+
UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_VM_START, vm.getAccountId(), vm.getDataCenterId(), vm.getId(), vm.getName(),
358+
vm.getServiceOfferingId(), vm.getTemplateId(), null);
359+
_usageEventDao.persist(usageEvent);
360+
361+
List<NicVO> nics = _nicDao.listByVmId(vm.getId());
362+
for (NicVO nic : nics) {
363+
NetworkVO network = _networkDao.findById(nic.getNetworkId());
364+
long isDefault = (nic.isDefaultNic()) ? 1 : 0;
365+
usageEvent = new UsageEventVO(EventTypes.EVENT_NETWORK_OFFERING_CREATE, vm.getAccountId(), vm.getDataCenterId(), vm.getId(), vm.getName(),
366+
network.getNetworkOfferingId(), null, isDefault);
367+
_usageEventDao.persist(usageEvent);
368+
}
369+
370+
return true;
279371
}
280372
}

‎server/src/com/cloud/vm/VirtualMachineManagerImpl.java‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130
import com.cloud.vm.ItWorkVO.Step;
131131
import com.cloud.vm.VirtualMachine.Event;
132132
import com.cloud.vm.VirtualMachine.State;
133+
import com.cloud.vm.VirtualMachine.Type;
133134
import com.cloud.vm.dao.ConsoleProxyDao;
134135
import com.cloud.vm.dao.DomainRouterDao;
135136
import com.cloud.vm.dao.NicDao;
@@ -296,6 +297,11 @@ private <T extends VMInstanceVO> VirtualMachineGuru<T> getVmGuru(T vm) {
296297
return (VirtualMachineGuru<T>)_vmGurus.get(vm.getType());
297298
}
298299

300+
@SuppressWarnings("unchecked")
301+
private <T extends VMInstanceVO> VirtualMachineGuru<T> getBareMetalVmGuru(T vm) {
302+
return (VirtualMachineGuru<T>)_vmGurus.get(Type.UserBareMetal);
303+
}
304+
299305
@Override
300306
public <T extends VMInstanceVO> boolean expunge(T vm, User caller, Account account) throws ResourceUnavailableException {
301307
try {
@@ -521,7 +527,13 @@ protected <T extends VMInstanceVO> boolean changeState(T vm, Event event, Long h
521527
public <T extends VMInstanceVO> T advanceStart(T vm, Map<VirtualMachineProfile.Param, Object> params, User caller, Account account) throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException {
522528
long vmId = vm.getId();
523529

524-
VirtualMachineGuru<T> vmGuru = getVmGuru(vm);
530+
VirtualMachineGuru<T> vmGuru;
531+
if (vm.getHypervisorType() == HypervisorType.BareMetal) {
532+
vmGuru = getBareMetalVmGuru(vm);
533+
} else {
534+
vmGuru = getVmGuru(vm);
535+
}
536+
525537
vm = vmGuru.findById(vm.getId());
526538
Ternary<T, ReservationContext, ItWorkVO> start = changeToStartState(vmGuru, vm, caller, account);
527539
if (start == null) {
@@ -569,7 +581,9 @@ public <T extends VMInstanceVO> T advanceStart(T vm, Map<VirtualMachineProfile.P
569581

570582
try {
571583

572-
_storageMgr.prepare(vmProfile, dest);
584+
if (vm.getHypervisorType() != HypervisorType.BareMetal) {
585+
_storageMgr.prepare(vmProfile, dest);
586+
}
573587
_networkMgr.prepare(vmProfile, dest, ctx);
574588

575589
vmGuru.finalizeVirtualMachineProfile(vmProfile, dest, ctx);

0 commit comments

Comments
 (0)