Search in sources :

Example 1 with State

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

the class VirtualMachineManagerImpl method cleanup.

protected boolean cleanup(final VirtualMachineGuru guru, final VirtualMachineProfile profile, final ItWorkVO work, final Event event, final boolean cleanUpEvenIfUnableToStop) {
    final VirtualMachine vm = profile.getVirtualMachine();
    final State state = vm.getState();
    s_logger.debug("Cleaning up resources for the vm " + vm + " in " + state + " state");
    try {
        if (state == State.Starting) {
            if (work != null) {
                final Step step = work.getStep();
                if (step == Step.Starting && !cleanUpEvenIfUnableToStop) {
                    s_logger.warn("Unable to cleanup vm " + vm + "; work state is incorrect: " + step);
                    return false;
                }
                if (step == Step.Started || step == Step.Starting || step == Step.Release) {
                    if (vm.getHostId() != null) {
                        if (!sendStop(guru, profile, cleanUpEvenIfUnableToStop, false)) {
                            s_logger.warn("Failed to stop vm " + vm + " in " + State.Starting + " state as a part of cleanup process");
                            return false;
                        }
                    }
                }
                if (step != Step.Release && step != Step.Prepare && step != Step.Started && step != Step.Starting) {
                    s_logger.debug("Cleanup is not needed for vm " + vm + "; work state is incorrect: " + step);
                    return true;
                }
            } else {
                if (vm.getHostId() != null) {
                    if (!sendStop(guru, profile, cleanUpEvenIfUnableToStop, false)) {
                        s_logger.warn("Failed to stop vm " + vm + " in " + State.Starting + " state as a part of cleanup process");
                        return false;
                    }
                }
            }
        } else if (state == State.Stopping) {
            if (vm.getHostId() != null) {
                if (!sendStop(guru, profile, cleanUpEvenIfUnableToStop, false)) {
                    s_logger.warn("Failed to stop vm " + vm + " in " + State.Stopping + " state as a part of cleanup process");
                    return false;
                }
            }
        } else if (state == State.Migrating) {
            if (vm.getHostId() != null) {
                if (!sendStop(guru, profile, cleanUpEvenIfUnableToStop, false)) {
                    s_logger.warn("Failed to stop vm " + vm + " in " + State.Migrating + " state as a part of cleanup process");
                    return false;
                }
            }
            if (vm.getLastHostId() != null) {
                if (!sendStop(guru, profile, cleanUpEvenIfUnableToStop, false)) {
                    s_logger.warn("Failed to stop vm " + vm + " in " + State.Migrating + " state as a part of cleanup process");
                    return false;
                }
            }
        } else if (state == State.Running) {
            if (!sendStop(guru, profile, cleanUpEvenIfUnableToStop, false)) {
                s_logger.warn("Failed to stop vm " + vm + " in " + State.Running + " state as a part of cleanup process");
                return false;
            }
        }
    } finally {
        try {
            _networkMgr.release(profile, cleanUpEvenIfUnableToStop);
            s_logger.debug("Successfully released network resources for the vm " + vm);
        } catch (final Exception e) {
            s_logger.warn("Unable to release some network resources.", e);
        }
        volumeMgr.release(profile);
        s_logger.debug("Successfully cleanued up resources for the vm " + vm + " in " + state + " state");
    }
    return true;
}
Also used : PowerState(com.cloud.legacymodel.vm.VirtualMachine.PowerState) State(com.cloud.legacymodel.vm.VirtualMachine.State) Step(com.cloud.vm.ItWorkVO.Step) ConnectionException(com.cloud.legacymodel.exceptions.ConnectionException) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) ConcurrentOperationException(com.cloud.legacymodel.exceptions.ConcurrentOperationException) OperationTimedoutException(com.cloud.legacymodel.exceptions.OperationTimedoutException) InsufficientAddressCapacityException(com.cloud.legacymodel.exceptions.InsufficientAddressCapacityException) VirtualMachineMigrationException(com.cloud.legacymodel.exceptions.VirtualMachineMigrationException) InsufficientServerCapacityException(com.cloud.legacymodel.exceptions.InsufficientServerCapacityException) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) ExecutionException(com.cloud.legacymodel.exceptions.ExecutionException) CloudException(com.cloud.legacymodel.exceptions.CloudException) SQLException(java.sql.SQLException) InsufficientVirtualNetworkCapacityException(com.cloud.legacymodel.exceptions.InsufficientVirtualNetworkCapacityException) NoTransitionException(com.cloud.legacymodel.exceptions.NoTransitionException) InsufficientCapacityException(com.cloud.legacymodel.exceptions.InsufficientCapacityException) AgentUnavailableException(com.cloud.legacymodel.exceptions.AgentUnavailableException) ConfigurationException(javax.naming.ConfigurationException) StorageUnavailableException(com.cloud.legacymodel.exceptions.StorageUnavailableException) ResourceUnavailableException(com.cloud.legacymodel.exceptions.ResourceUnavailableException) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) VirtualMachine(com.cloud.legacymodel.vm.VirtualMachine)

Example 2 with State

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

the class DeploymentPlanningManagerImpl 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 oldState = transition.getCurrentState();
    final State newState = transition.getToState();
    if ((oldState == State.Starting) && (newState != State.Starting)) {
        // cleanup all VM reservation entries
        final SearchCriteria<VMReservationVO> sc = _reservationDao.createSearchCriteria();
        sc.addAnd("vmId", SearchCriteria.Op.EQ, vo.getId());
        _reservationDao.expunge(sc);
    }
    return true;
}
Also used : VMReservationVO(com.cloud.engine.cloud.entity.api.db.VMReservationVO) AllocationState(com.cloud.model.enumeration.AllocationState) State(com.cloud.legacymodel.vm.VirtualMachine.State)

Example 3 with State

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

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

the class TemplateManagerImpl method attachIso.

@Override
@ActionEvent(eventType = EventTypes.EVENT_ISO_ATTACH, eventDescription = "attaching ISO", async = true)
public boolean attachIso(final long isoId, final long vmId) {
    final Account caller = CallContext.current().getCallingAccount();
    final Long userId = CallContext.current().getCallingUserId();
    // Verify input parameters
    final UserVmVO vm = this._userVmDao.findById(vmId);
    if (vm == null) {
        throw new InvalidParameterValueException("Unable to find a virtual machine with id " + vmId);
    }
    final VMTemplateVO iso = this._tmpltDao.findById(isoId);
    if (iso == null || iso.getRemoved() != null) {
        throw new InvalidParameterValueException("Unable to find an ISO with id " + isoId);
    }
    // check permissions
    // check if caller has access to VM and ISO
    // and also check if the VM's owner has access to the ISO.
    this._accountMgr.checkAccess(caller, null, false, iso, vm);
    final Account vmOwner = this._accountDao.findById(vm.getAccountId());
    this._accountMgr.checkAccess(vmOwner, null, false, iso, vm);
    final State vmState = vm.getState();
    if (vmState != State.Running && vmState != State.Stopped) {
        throw new InvalidParameterValueException("Please specify a VM that is either Stopped or Running.");
    }
    if ("xen-pv-drv-iso".equals(iso.getDisplayText()) && vm.getHypervisorType() != HypervisorType.XenServer) {
        throw new InvalidParameterValueException("Cannot attach Xenserver PV drivers to incompatible hypervisor " + vm.getHypervisorType());
    }
    final boolean result = attachISOToVM(vmId, userId, isoId, true);
    if (result) {
        return result;
    } else {
        throw new CloudRuntimeException("Failed to attach iso");
    }
}
Also used : Account(com.cloud.legacymodel.user.Account) UserVmVO(com.cloud.vm.UserVmVO) InvalidParameterValueException(com.cloud.legacymodel.exceptions.InvalidParameterValueException) State(com.cloud.legacymodel.vm.VirtualMachine.State) CloudRuntimeException(com.cloud.legacymodel.exceptions.CloudRuntimeException) VMTemplateVO(com.cloud.storage.VMTemplateVO) ActionEvent(com.cloud.event.ActionEvent)

Example 5 with State

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

the class VirtualMachineManagerImpl method changeToStartState.

@DB
private Ternary<VMInstanceVO, ReservationContext, ItWorkVO> changeToStartState(final VMInstanceVO vm, final User caller, final Account account) throws ConcurrentOperationException {
    final long vmId = vm.getId();
    ItWorkVO work = new ItWorkVO(UUID.randomUUID().toString(), _nodeId, State.Starting, vm.getType(), vm.getId());
    int retry = VmOpLockStateRetry.value();
    while (retry-- != 0) {
        try {
            final ItWorkVO workFinal = work;
            final Ternary<VMInstanceVO, ReservationContext, ItWorkVO> result = Transaction.execute(new TransactionCallbackWithException<Ternary<VMInstanceVO, ReservationContext, ItWorkVO>, NoTransitionException>() {

                @Override
                public Ternary<VMInstanceVO, ReservationContext, ItWorkVO> doInTransaction(final TransactionStatus status) throws NoTransitionException {
                    final Journal journal = new Journal.LogJournal("Creating " + vm, s_logger);
                    final ItWorkVO work = _workDao.persist(workFinal);
                    final ReservationContextImpl context = new ReservationContextImpl(work.getId(), journal, caller, account);
                    if (stateTransitTo(vm, Event.StartRequested, null, work.getId())) {
                        s_logger.debug("Successfully transitioned to start state for " + vm + " reservation id = " + work.getId());
                        return new Ternary<>(vm, context, work);
                    }
                    return new Ternary<>(null, null, work);
                }
            });
            work = result.third();
            if (result.first() != null) {
                return result;
            }
        } catch (final NoTransitionException e) {
            s_logger.debug("Unable to transition into Starting state due to " + e.getMessage());
        }
        final VMInstanceVO instance = _vmDao.findById(vmId);
        if (instance == null) {
            throw new ConcurrentOperationException("Unable to acquire lock on " + vm);
        }
        s_logger.debug("Determining why we're unable to update the state to Starting for " + instance + ".  Retry=" + retry);
        final State state = instance.getState();
        if (state == State.Running) {
            s_logger.debug("VM is already started: " + vm);
            return null;
        }
        if (state.isTransitional()) {
            if (!checkWorkItems(vm, state)) {
                throw new ConcurrentOperationException("There are concurrent operations on " + vm);
            } else {
                continue;
            }
        }
        if (state != State.Stopped) {
            s_logger.debug("VM " + vm + " is not in a state to be started: " + state);
            return null;
        }
    }
    throw new ConcurrentOperationException("Unable to change the state of " + vm);
}
Also used : Ternary(com.cloud.legacymodel.utils.Ternary) TransactionStatus(com.cloud.utils.db.TransactionStatus) Journal(com.cloud.utils.Journal) ConcurrentOperationException(com.cloud.legacymodel.exceptions.ConcurrentOperationException) PowerState(com.cloud.legacymodel.vm.VirtualMachine.PowerState) State(com.cloud.legacymodel.vm.VirtualMachine.State) NoTransitionException(com.cloud.legacymodel.exceptions.NoTransitionException) DB(com.cloud.utils.db.DB)

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