|
16 | 16 | // under the License. |
17 | 17 | package com.cloud.vm; |
18 | 18 |
|
| 19 | +import static com.cloud.event.EventTypes.EVENT_NIC_CREATE; |
| 20 | +import static com.cloud.event.EventTypes.EVENT_NIC_DELETE; |
19 | 21 | import static com.cloud.hypervisor.Hypervisor.HypervisorType.Functionality; |
20 | 22 | import static com.cloud.storage.Volume.IOPS_LIMIT; |
21 | 23 | import static com.cloud.utils.NumbersUtil.toHumanReadableSize; |
| 24 | +import static com.cloud.vm.VirtualMachineManager.Topics.VM_LIFECYCLE; |
22 | 25 | import static org.apache.cloudstack.api.ApiConstants.MAX_IOPS; |
23 | 26 | import static org.apache.cloudstack.api.ApiConstants.MIN_IOPS; |
24 | 27 |
|
@@ -1435,7 +1438,7 @@ protected ResizeVolumeCmd prepareResizeVolumeCmd(VolumeVO rootVolume, DiskOfferi |
1435 | 1438 | } |
1436 | 1439 |
|
1437 | 1440 | @Override |
1438 | | - @ActionEvent(eventType = EventTypes.EVENT_NIC_CREATE, eventDescription = "Creating NIC", async = true) |
| 1441 | + @ActionEvent(eventType = EVENT_NIC_CREATE, eventDescription = "Creating NIC", async = true) |
1439 | 1442 | public UserVm addNicToVirtualMachine(AddNicToVMCmd cmd) throws InvalidParameterValueException, PermissionDeniedException, CloudRuntimeException { |
1440 | 1443 | Long vmId = cmd.getVmId(); |
1441 | 1444 | Long networkId = cmd.getNetworkId(); |
@@ -1537,9 +1540,41 @@ public UserVm addNicToVirtualMachine(AddNicToVMCmd cmd) throws InvalidParameterV |
1537 | 1540 | } |
1538 | 1541 | CallContext.current().putContextParameter(Nic.class, guestNic.getUuid()); |
1539 | 1542 | logger.debug(String.format("Successful addition of %s from %s through %s", network, vmInstance, guestNic)); |
| 1543 | + publishNicEventMessageBus(vmInstance.getId(), vmInstance.getAccountId(), guestNic.getId(), EVENT_NIC_CREATE); |
1540 | 1544 | return _vmDao.findById(vmInstance.getId()); |
1541 | 1545 | } |
1542 | 1546 |
|
| 1547 | + private void publishVmLifecycleMessageBus(UserVm instance, @Nullable VirtualMachine.State oldState, VirtualMachine.State newState) { |
| 1548 | + try { |
| 1549 | + Map<String, Object> event = new HashMap<>(); |
| 1550 | + event.put(ApiConstants.EVENT_ID, UUID.randomUUID().toString()); |
| 1551 | + event.put(ApiConstants.INSTANCE_ID, instance.getId()); |
| 1552 | + event.put(ApiConstants.ACCOUNT_ID, instance.getAccountId()); |
| 1553 | + event.put(ApiConstants.OLD_STATE, oldState != null ? oldState : State.Unknown); |
| 1554 | + event.put(ApiConstants.NEW_STATE, newState); |
| 1555 | + event.put(ApiConstants.TIME_STAMP, System.currentTimeMillis()); |
| 1556 | + messageBus.publish(_name, VM_LIFECYCLE, PublishScope.GLOBAL, event); |
| 1557 | + } catch (Exception ex) { |
| 1558 | + logger.warn("Failed to publish lifecycle event for Instance: {}", instance.getUuid(), ex); |
| 1559 | + } |
| 1560 | + } |
| 1561 | + |
| 1562 | + private void publishNicEventMessageBus(Long instanceId, Long accountId, Long nicId, String eventType) { |
| 1563 | + try { |
| 1564 | + Map<String, Object> event = new HashMap<>(); |
| 1565 | + event.put(ApiConstants.EVENT_ID, UUID.randomUUID().toString()); |
| 1566 | + event.put(ApiConstants.INSTANCE_ID, instanceId); |
| 1567 | + event.put(ApiConstants.ACCOUNT_ID, accountId); |
| 1568 | + event.put(ApiConstants.NIC_ID, nicId); |
| 1569 | + event.put(ApiConstants.EVENT_TYPE, eventType); // NIC_CREATE or NIC_DELETE |
| 1570 | + event.put(ApiConstants.TIME_STAMP, System.currentTimeMillis()); |
| 1571 | + |
| 1572 | + messageBus.publish(_name, Nic.Topics.NIC_LIFECYCLE, PublishScope.GLOBAL, event); |
| 1573 | + } catch (Exception ex) { |
| 1574 | + logger.error("Failed to publish lifecycle event for NIC with ID: {}", nicId, ex); |
| 1575 | + } |
| 1576 | + } |
| 1577 | + |
1543 | 1578 | /** |
1544 | 1579 | * Set NIC as default if VM has no default NIC |
1545 | 1580 | * @param vmInstance VM instance to be checked |
@@ -1654,6 +1689,7 @@ public UserVm removeNicFromVirtualMachine(RemoveNicFromVMCmd cmd) throws Invalid |
1654 | 1689 | } |
1655 | 1690 |
|
1656 | 1691 | logger.debug("Successful removal of " + network + " from " + vmInstance); |
| 1692 | + publishNicEventMessageBus(vmInstance.getId(), vmInstance.getAccountId(), nic.getId(), EVENT_NIC_DELETE); |
1657 | 1693 | return _vmDao.findById(vmInstance.getId()); |
1658 | 1694 | } |
1659 | 1695 |
|
@@ -3384,8 +3420,10 @@ public UserVm startVirtualMachine(StartVMCmd cmd) throws ExecutionException, Con |
3384 | 3420 | if (cmd.getConsiderLastHost() != null) { |
3385 | 3421 | additonalParams.put(VirtualMachineProfile.Param.ConsiderLastHost, cmd.getConsiderLastHost().toString()); |
3386 | 3422 | } |
3387 | | - |
3388 | | - return startVirtualMachine(cmd.getId(), cmd.getPodId(), cmd.getClusterId(), cmd.getHostId(), additonalParams, cmd.getDeploymentPlanner()).first(); |
| 3423 | + UserVmVO vm = _vmDao.findById(cmd.getId()); |
| 3424 | + UserVm userVm = startVirtualMachine(cmd.getId(), cmd.getPodId(), cmd.getClusterId(), cmd.getHostId(), additonalParams, cmd.getDeploymentPlanner()).first(); |
| 3425 | + publishVmLifecycleMessageBus(userVm, vm.getState(), VirtualMachine.State.Running); |
| 3426 | + return userVm; |
3389 | 3427 | } |
3390 | 3428 |
|
3391 | 3429 | @Override |
@@ -3570,7 +3608,7 @@ public UserVm destroyVm(DestroyVMCmd cmd) throws ResourceUnavailableException, C |
3570 | 3608 | logger.warn("Tried to destroy ROOT volume for VM [{}], but couldn't retrieve it.", vm); |
3571 | 3609 | } |
3572 | 3610 | } |
3573 | | - |
| 3611 | + publishVmLifecycleMessageBus(destroyedVm, vm.getState(), VirtualMachine.State.Destroyed); |
3574 | 3612 | return destroyedVm; |
3575 | 3613 | } |
3576 | 3614 |
|
@@ -5272,7 +5310,9 @@ public UserVm startVirtualMachine(DeployVMCmd cmd) throws ResourceUnavailableExc |
5272 | 5310 | additionalParams.put(VirtualMachineProfile.Param.VmPassword, cmd.getPassword()); |
5273 | 5311 | } |
5274 | 5312 |
|
5275 | | - return startVirtualMachine(vmId, podId, clusterId, hostId, diskOfferingMap, additionalParams, cmd.getDeploymentPlanner()); |
| 5313 | + UserVm userVm = startVirtualMachine(vmId, podId, clusterId, hostId, diskOfferingMap, additionalParams, cmd.getDeploymentPlanner()); |
| 5314 | + publishVmLifecycleMessageBus(userVm, null, VirtualMachine.State.Running); |
| 5315 | + return userVm; |
5276 | 5316 | } |
5277 | 5317 |
|
5278 | 5318 | private UserVm startVirtualMachine(long vmId, Long podId, Long clusterId, Long hostId, Map<Long, DiskOffering> diskOfferingMap |
@@ -5631,6 +5671,7 @@ public UserVm stopVirtualMachine(long vmId, boolean forced) throws ConcurrentOpe |
5631 | 5671 | status = vmEntity.stop(Long.toString(userId)); |
5632 | 5672 | } |
5633 | 5673 | if (status) { |
| 5674 | + publishVmLifecycleMessageBus(vm, vm.getState(), VirtualMachine.State.Stopped); |
5634 | 5675 | return _vmDao.findById(vmId); |
5635 | 5676 | } else { |
5636 | 5677 | return null; |
|
0 commit comments