Search in sources :

Example 1 with Event

use of com.cloud.legacymodel.vm.VirtualMachine.Event in project cosmic by MissionCriticalCloud.

the class UserVmStateListener method pubishOnEventBus.

private void pubishOnEventBus(final String event, final String status, final VirtualMachine vo, final VirtualMachine.State oldState, final VirtualMachine.State newState) {
    final String configKey = Config.PublishResourceStateEvent.key();
    final String value = _configDao.getValue(configKey);
    final boolean configValue = Boolean.parseBoolean(value);
    if (!configValue) {
        return;
    }
    try {
        s_eventBus = ComponentContext.getComponent(EventBus.class);
    } catch (final NoSuchBeanDefinitionException nbe) {
        // no provider is configured to provide events bus, so just return
        return;
    }
    final Map<String, String> eventDescription = new HashMap<>();
    final String resourceName = getEntityFromClassName(VirtualMachine.class.getName());
    String topic = "cosmic";
    eventDescription.put("resource", resourceName);
    eventDescription.put("id", vo.getUuid());
    eventDescription.put("status", status);
    eventDescription.put("event", event);
    eventDescription.put("vm_name", vo.getHostName());
    // State changed: Publish to DNS topic
    if (oldState != null && !oldState.equals(newState)) {
        topic = "cosmic_dns";
        eventDescription.put("ip_address", vo.getPrivateIpAddress());
        eventDescription.put("mac_address", vo.getPrivateMacAddress());
        eventDescription.put("state", newState.name());
    } else {
        // State unchanged: publish to default topic
        eventDescription.put("old-state", oldState.name());
        eventDescription.put("new-state", newState.name());
    }
    final com.cloud.framework.events.Event eventMsg = new com.cloud.framework.events.Event(ManagementService.Name, EventCategory.RESOURCE_STATE_CHANGE_EVENT.getName(), event, resourceName, vo.getUuid(), topic);
    final String eventDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z").format(new Date());
    eventDescription.put("eventDateTime", eventDate);
    eventMsg.setDescription(eventDescription);
    try {
        s_eventBus.publish(eventMsg);
    } catch (final EventBusException e) {
        s_logger.warn("Failed to publish state change event on the the event bus.");
    }
}
Also used : HashMap(java.util.HashMap) EventBus(com.cloud.framework.events.EventBus) Date(java.util.Date) Event(com.cloud.legacymodel.vm.VirtualMachine.Event) EventBusException(com.cloud.framework.events.EventBusException) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) SimpleDateFormat(java.text.SimpleDateFormat) VirtualMachine(com.cloud.legacymodel.vm.VirtualMachine)

Example 2 with Event

use of com.cloud.legacymodel.vm.VirtualMachine.Event in project cosmic by MissionCriticalCloud.

the class UserVmStateListener method postStateTransitionEvent.

@Override
public boolean postStateTransitionEvent(final Transition<State, Event> transition, final VirtualMachine vo, final boolean status, final Object opaque) {
    if (!status) {
        return false;
    }
    final Event event = transition.getEvent();
    final State oldState = transition.getCurrentState();
    final State newState = transition.getToState();
    pubishOnEventBus(event.name(), "postStateTransitionEvent", vo, oldState, newState);
    if (vo.getType() != VirtualMachineType.User) {
        return true;
    }
    return true;
}
Also used : State(com.cloud.legacymodel.vm.VirtualMachine.State) Event(com.cloud.legacymodel.vm.VirtualMachine.Event)

Example 3 with Event

use of com.cloud.legacymodel.vm.VirtualMachine.Event in project cosmic by MissionCriticalCloud.

the class CapacityManagerImpl method postStateTransitionEvent.

@Override
public boolean postStateTransitionEvent(final Transition<State, Event> transition, final VirtualMachine vm, final boolean status, final Object opaque) {
    if (!status) {
        return false;
    }
    final Pair<Long, Long> hosts = (Pair<Long, Long>) opaque;
    final Long oldHostId = hosts.first();
    final State oldState = transition.getCurrentState();
    final State newState = transition.getToState();
    final Event event = transition.getEvent();
    s_logger.debug("VM state transitted from :" + oldState + " to " + newState + " with event: " + event + "vm's original host id: " + vm.getLastHostId() + " new host id: " + vm.getHostId() + " host id before state transition: " + oldHostId);
    if (oldState == State.Starting) {
        if (newState != State.Running) {
            releaseVmCapacity(vm, false, false, oldHostId);
        }
    } else if (oldState == State.Running) {
        if (event == Event.AgentReportStopped) {
            releaseVmCapacity(vm, false, true, oldHostId);
        } else if (event == Event.AgentReportMigrated) {
            releaseVmCapacity(vm, false, false, oldHostId);
        }
    } else if (oldState == State.Migrating) {
        if (event == Event.AgentReportStopped) {
            /* Release capacity from original host */
            releaseVmCapacity(vm, false, false, vm.getLastHostId());
            releaseVmCapacity(vm, false, false, oldHostId);
        } else if (event == Event.OperationFailed) {
            /* Release from dest host */
            releaseVmCapacity(vm, false, false, oldHostId);
        } else if (event == Event.OperationSucceeded) {
            releaseVmCapacity(vm, false, false, vm.getLastHostId());
        }
    } else if (oldState == State.Stopping) {
        if (event == Event.OperationSucceeded) {
            releaseVmCapacity(vm, false, true, oldHostId);
        } else if (event == Event.AgentReportStopped) {
            releaseVmCapacity(vm, false, false, oldHostId);
        } else if (event == Event.AgentReportMigrated) {
            releaseVmCapacity(vm, false, false, oldHostId);
        }
    } else if (oldState == State.Stopped) {
        if (event == Event.DestroyRequested || event == Event.ExpungeOperation) {
            releaseVmCapacity(vm, true, false, vm.getLastHostId());
        } else if (event == Event.AgentReportMigrated) {
            releaseVmCapacity(vm, false, false, oldHostId);
        }
    }
    if ((newState == State.Starting || newState == State.Migrating || event == Event.AgentReportMigrated) && vm.getHostId() != null) {
        boolean fromLastHost = false;
        if (vm.getHostId().equals(vm.getLastHostId())) {
            s_logger.debug("VM starting again on the last host it was stopped on");
            fromLastHost = true;
        }
        allocateVmCapacity(vm, fromLastHost);
    }
    if (newState == State.Stopped) {
        if (vm.getType() == VirtualMachineType.User) {
            final UserVmVO userVM = this._userVMDao.findById(vm.getId());
            this._userVMDao.loadDetails(userVM);
            // free the message sent flag if it exists
            userVM.setDetail(MESSAGE_RESERVED_CAPACITY_FREED_FLAG, "false");
            this._userVMDao.saveDetails(userVM);
        }
    }
    return true;
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) ResourceState(com.cloud.legacymodel.resource.ResourceState) State(com.cloud.legacymodel.vm.VirtualMachine.State) CapacityState(com.cloud.model.enumeration.CapacityState) Event(com.cloud.legacymodel.vm.VirtualMachine.Event) Pair(com.cloud.legacymodel.utils.Pair)

Aggregations

Event (com.cloud.legacymodel.vm.VirtualMachine.Event)3 State (com.cloud.legacymodel.vm.VirtualMachine.State)2 EventBus (com.cloud.framework.events.EventBus)1 EventBusException (com.cloud.framework.events.EventBusException)1 ResourceState (com.cloud.legacymodel.resource.ResourceState)1 Pair (com.cloud.legacymodel.utils.Pair)1 VirtualMachine (com.cloud.legacymodel.vm.VirtualMachine)1 CapacityState (com.cloud.model.enumeration.CapacityState)1 UserVmVO (com.cloud.vm.UserVmVO)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)1