|
2 | 2 |
|
3 | 3 | import java.util.ArrayList; |
4 | 4 | import java.util.List; |
| 5 | +import java.util.Map; |
| 6 | +import java.util.concurrent.Executors; |
5 | 7 |
|
6 | 8 | import javax.ejb.Local; |
| 9 | +import javax.naming.ConfigurationException; |
7 | 10 |
|
8 | 11 | import org.apache.log4j.Logger; |
9 | 12 |
|
| 13 | +import com.cloud.agent.manager.Commands; |
10 | 14 | import com.cloud.api.commands.AttachVolumeCmd; |
11 | 15 | import com.cloud.api.commands.CreateTemplateCmd; |
12 | 16 | import com.cloud.api.commands.DeployVMCmd; |
13 | 17 | import com.cloud.api.commands.DetachVolumeCmd; |
14 | 18 | import com.cloud.api.commands.UpgradeVMCmd; |
15 | 19 | import com.cloud.configuration.ResourceCount.ResourceType; |
| 20 | +import com.cloud.configuration.dao.ConfigurationDao; |
16 | 21 | import com.cloud.dc.DataCenterVO; |
17 | 22 | import com.cloud.dc.DataCenter.NetworkType; |
18 | 23 | import com.cloud.deploy.DataCenterDeployment; |
| 24 | +import com.cloud.deploy.DeployDestination; |
19 | 25 | import com.cloud.domain.DomainVO; |
20 | 26 | import com.cloud.event.EventTypes; |
21 | 27 | import com.cloud.event.UsageEventVO; |
|
39 | 45 | import com.cloud.user.SSHKeyPair; |
40 | 46 | import com.cloud.user.UserContext; |
41 | 47 | import com.cloud.uservm.UserVm; |
| 48 | +import com.cloud.utils.NumbersUtil; |
42 | 49 | import com.cloud.utils.Pair; |
| 50 | +import com.cloud.utils.component.ComponentLocator; |
43 | 51 | import com.cloud.utils.component.Manager; |
| 52 | +import com.cloud.utils.concurrency.NamedThreadFactory; |
44 | 53 | import com.cloud.utils.db.DB; |
45 | 54 | import com.cloud.utils.exception.CloudRuntimeException; |
46 | 55 | import com.cloud.utils.net.NetUtils; |
| 56 | +import com.cloud.vm.VirtualMachine.Type; |
47 | 57 |
|
48 | 58 | @Local(value={BareMetalVmManager.class, BareMetalVmService.class}) |
49 | 59 | public class BareMetalVmManagerImpl extends UserVmManagerImpl implements BareMetalVmManager, BareMetalVmService, Manager { |
50 | 60 | private static final Logger s_logger = Logger.getLogger(BareMetalVmManagerImpl.class); |
| 61 | + private ConfigurationDao _configDao; |
51 | 62 |
|
52 | 63 | @Override |
53 | 64 | public boolean attachISOToVM(long vmId, long isoId, boolean attach) { |
@@ -275,6 +286,87 @@ public UserVm createVirtualMachine(DeployVMCmd cmd) throws InsufficientCapacityE |
275 | 286 | } |
276 | 287 |
|
277 | 288 | 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; |
279 | 371 | } |
280 | 372 | } |
0 commit comments