Search in sources :

Example 1 with Event

use of com.cloud.vm.VirtualMachine.Event in project cloudstack by apache.

the class CapacityManagerImpl method postStateTransitionEvent.

@Override
public boolean postStateTransitionEvent(StateMachine2.Transition<State, Event> transition, VirtualMachine vm, boolean status, Object opaque) {
    if (!status) {
        return false;
    }
    @SuppressWarnings("unchecked") Pair<Long, Long> hosts = (Pair<Long, Long>) opaque;
    Long oldHostId = hosts.first();
    State oldState = transition.getCurrentState();
    State newState = transition.getToState();
    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() == VirtualMachine.Type.User) {
            UserVmVO userVM = _userVMDao.findById(vm.getId());
            _userVMDao.loadDetails(userVM);
            // free the message sent flag if it exists
            userVM.setDetail(MESSAGE_RESERVED_CAPACITY_FREED_FLAG, "false");
            _userVMDao.saveDetails(userVM);
        }
    }
    return true;
}
Also used : UserVmVO(com.cloud.vm.UserVmVO) ResourceState(com.cloud.resource.ResourceState) State(com.cloud.vm.VirtualMachine.State) Event(com.cloud.vm.VirtualMachine.Event) Pair(com.cloud.utils.Pair)

Example 2 with Event

use of com.cloud.vm.VirtualMachine.Event in project cloudstack by apache.

the class UserVmStateListener method pubishOnEventBus.

private void pubishOnEventBus(String event, String status, VirtualMachine vo, VirtualMachine.State oldState, VirtualMachine.State newState) {
    String configKey = Config.PublishResourceStateEvent.key();
    String value = _configDao.getValue(configKey);
    boolean configValue = Boolean.parseBoolean(value);
    if (!configValue)
        return;
    try {
        s_eventBus = ComponentContext.getComponent(EventBus.class);
    } catch (NoSuchBeanDefinitionException nbe) {
        // no provider is configured to provide events bus, so just return
        return;
    }
    String resourceName = getEntityFromClassName(VirtualMachine.class.getName());
    org.apache.cloudstack.framework.events.Event eventMsg = new org.apache.cloudstack.framework.events.Event(ManagementService.Name, EventCategory.RESOURCE_STATE_CHANGE_EVENT.getName(), event, resourceName, vo.getUuid());
    Map<String, String> eventDescription = new HashMap<String, String>();
    eventDescription.put("resource", resourceName);
    eventDescription.put("id", vo.getUuid());
    eventDescription.put("old-state", oldState.name());
    eventDescription.put("new-state", newState.name());
    eventDescription.put("status", status);
    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 (org.apache.cloudstack.framework.events.EventBusException e) {
        s_logger.warn("Failed to publish state change event on the the event bus.");
    }
}
Also used : HashMap(java.util.HashMap) EventBus(org.apache.cloudstack.framework.events.EventBus) Date(java.util.Date) Event(com.cloud.vm.VirtualMachine.Event) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 3 with Event

use of com.cloud.vm.VirtualMachine.Event in project cloudstack by apache.

the class UserVmStateListener method postStateTransitionEvent.

@Override
public boolean postStateTransitionEvent(StateMachine2.Transition<State, Event> transition, VirtualMachine vo, boolean status, Object opaque) {
    if (!status) {
        return false;
    }
    Event event = transition.getEvent();
    State oldState = transition.getCurrentState();
    State newState = transition.getToState();
    pubishOnEventBus(event.name(), "postStateTransitionEvent", vo, oldState, newState);
    if (vo.getType() != VirtualMachine.Type.User) {
        return true;
    }
    if (transition.isImpacted(StateMachine2.Transition.Impact.USAGE)) {
        if (oldState == State.Destroyed && newState == State.Stopped) {
            generateUsageEvent(vo.getServiceOfferingId(), vo, EventTypes.EVENT_VM_CREATE);
        } else if (newState == State.Running) {
            generateUsageEvent(vo.getServiceOfferingId(), vo, EventTypes.EVENT_VM_START);
        } else if (newState == State.Stopped) {
            generateUsageEvent(vo.getServiceOfferingId(), vo, EventTypes.EVENT_VM_STOP);
            List<NicVO> nics = _nicDao.listByVmId(vo.getId());
            for (NicVO nic : nics) {
                NetworkVO network = _networkDao.findById(nic.getNetworkId());
                UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NETWORK_OFFERING_REMOVE, vo.getAccountId(), vo.getDataCenterId(), vo.getId(), Long.toString(nic.getId()), network.getNetworkOfferingId(), null, 0L, vo.getClass().getName(), vo.getUuid(), vo.isDisplay());
            }
        } else if (newState == State.Destroyed || newState == State.Error || newState == State.Expunging) {
            generateUsageEvent(vo.getServiceOfferingId(), vo, EventTypes.EVENT_VM_DESTROY);
        }
    }
    return true;
}
Also used : NetworkVO(com.cloud.network.dao.NetworkVO) State(com.cloud.vm.VirtualMachine.State) Event(com.cloud.vm.VirtualMachine.Event)

Example 4 with Event

use of com.cloud.vm.VirtualMachine.Event in project cloudstack by apache.

the class SecurityGroupManagerImpl method postStateTransitionEvent.

@Override
public boolean postStateTransitionEvent(StateMachine2.Transition<State, Event> transition, VirtualMachine vm, boolean status, Object opaque) {
    if (!status) {
        return false;
    }
    State oldState = transition.getCurrentState();
    State newState = transition.getToState();
    Event event = transition.getEvent();
    if (VirtualMachine.State.isVmStarted(oldState, event, newState)) {
        if (s_logger.isTraceEnabled()) {
            s_logger.trace("Security Group Mgr: handling start of vm id" + vm.getId());
        }
        handleVmStarted((VMInstanceVO) vm);
    } else if (VirtualMachine.State.isVmStopped(oldState, event, newState)) {
        if (s_logger.isTraceEnabled()) {
            s_logger.trace("Security Group Mgr: handling stop of vm id" + vm.getId());
        }
        handleVmStopped((VMInstanceVO) vm);
    } else if (VirtualMachine.State.isVmMigrated(oldState, event, newState)) {
        if (s_logger.isTraceEnabled()) {
            s_logger.trace("Security Group Mgr: handling migration of vm id" + vm.getId());
        }
        handleVmMigrated((VMInstanceVO) vm);
    }
    return true;
}
Also used : State(com.cloud.vm.VirtualMachine.State) Event(com.cloud.vm.VirtualMachine.Event) ActionEvent(com.cloud.event.ActionEvent) VMInstanceVO(com.cloud.vm.VMInstanceVO)

Aggregations

Event (com.cloud.vm.VirtualMachine.Event)4 State (com.cloud.vm.VirtualMachine.State)3 ActionEvent (com.cloud.event.ActionEvent)1 NetworkVO (com.cloud.network.dao.NetworkVO)1 ResourceState (com.cloud.resource.ResourceState)1 Pair (com.cloud.utils.Pair)1 UserVmVO (com.cloud.vm.UserVmVO)1 VMInstanceVO (com.cloud.vm.VMInstanceVO)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 EventBus (org.apache.cloudstack.framework.events.EventBus)1 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)1