Search in sources :

Example 11 with State

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

the class AffinityGroupServiceImpl 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 State newState = transition.getToState();
    if ((newState == State.Expunging) || (newState == State.Error)) {
        // cleanup all affinity groups associations of the Expunged VM
        final SearchCriteria<AffinityGroupVMMapVO> sc = _affinityGroupVMMapDao.createSearchCriteria();
        sc.addAnd("instanceId", SearchCriteria.Op.EQ, vo.getId());
        _affinityGroupVMMapDao.expunge(sc);
    }
    return true;
}
Also used : State(com.cloud.legacymodel.vm.VirtualMachine.State)

Example 12 with State

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

the class TemplateManagerImpl method detachIso.

@Override
@ActionEvent(eventType = EventTypes.EVENT_ISO_DETACH, eventDescription = "detaching ISO", async = true)
public boolean detachIso(final long vmId) {
    final Account caller = CallContext.current().getCallingAccount();
    final Long userId = CallContext.current().getCallingUserId();
    // Verify input parameters
    final UserVmVO vmInstanceCheck = this._userVmDao.findById(vmId);
    if (vmInstanceCheck == null) {
        throw new InvalidParameterValueException("Unable to find a virtual machine with id " + vmId);
    }
    final UserVm userVM = this._userVmDao.findById(vmId);
    if (userVM == null) {
        throw new InvalidParameterValueException("Please specify a valid VM.");
    }
    this._accountMgr.checkAccess(caller, null, true, userVM);
    final Long isoId = userVM.getIsoId();
    if (isoId == null) {
        throw new InvalidParameterValueException("The specified VM has no ISO attached to it.");
    }
    CallContext.current().setEventDetails("Vm Id: " + vmId + " ISO Id: " + isoId);
    final State vmState = userVM.getState();
    if (vmState != State.Running && vmState != State.Stopped) {
        throw new InvalidParameterValueException("Please specify a VM that is either Stopped or Running.");
    }
    // attach=false
    final boolean result = attachISOToVM(vmId, userId, isoId, false);
    // => detach
    if (result) {
        return result;
    } else {
        throw new CloudRuntimeException("Failed to detach iso");
    }
}
Also used : Account(com.cloud.legacymodel.user.Account) UserVmVO(com.cloud.vm.UserVmVO) UserVm(com.cloud.uservm.UserVm) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) State(com.cloud.legacymodel.vm.VirtualMachine.State) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) ActionEvent(com.cloud.event.ActionEvent)

Example 13 with State

use of com.cloud.legacymodel.vm.VirtualMachine.State 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

State (com.cloud.legacymodel.vm.VirtualMachine.State)13 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)5 InvalidParameterValueException (com.cloud.legacymodel.exceptions.InvalidParameterValueException)5 PowerState (com.cloud.legacymodel.vm.VirtualMachine.PowerState)4 CloudException (com.cloud.legacymodel.exceptions.CloudException)3 ConcurrentOperationException (com.cloud.legacymodel.exceptions.ConcurrentOperationException)3 NoTransitionException (com.cloud.legacymodel.exceptions.NoTransitionException)3 Account (com.cloud.legacymodel.user.Account)3 UserVmVO (com.cloud.vm.UserVmVO)3 ActionEvent (com.cloud.event.ActionEvent)2 AgentUnavailableException (com.cloud.legacymodel.exceptions.AgentUnavailableException)2 ConnectionException (com.cloud.legacymodel.exceptions.ConnectionException)2 ExecutionException (com.cloud.legacymodel.exceptions.ExecutionException)2 InsufficientAddressCapacityException (com.cloud.legacymodel.exceptions.InsufficientAddressCapacityException)2 InsufficientCapacityException (com.cloud.legacymodel.exceptions.InsufficientCapacityException)2 InsufficientServerCapacityException (com.cloud.legacymodel.exceptions.InsufficientServerCapacityException)2 InsufficientVirtualNetworkCapacityException (com.cloud.legacymodel.exceptions.InsufficientVirtualNetworkCapacityException)2 OperationTimedoutException (com.cloud.legacymodel.exceptions.OperationTimedoutException)2 ResourceUnavailableException (com.cloud.legacymodel.exceptions.ResourceUnavailableException)2 StorageUnavailableException (com.cloud.legacymodel.exceptions.StorageUnavailableException)2