Example #1
0
    def isReady(self, userService: UserService) -> bool:
        userService.refresh_from_db()
        logger.debug('Checking ready of %s', userService)

        if userService.state != State.USABLE or userService.os_state != State.USABLE:
            logger.debug('State is not usable for %s', userService.name)
            return False

        logger.debug('Service %s is usable, checking it via setReady',
                     userService)
        userServiceInstance = userService.getInstance()
        try:
            state = userServiceInstance.setReady()
        except Exception as e:
            logger.warn('Could not check readyness of %s: %s', userService, e)
            return False

        logger.debug('State: %s', state)

        if state == State.FINISHED:
            userService.updateData(userServiceInstance)
            return True

        userService.setState(State.PREPARING)
        UserServiceOpChecker.makeUnique(userService, userServiceInstance,
                                        state)

        return False
Example #2
0
    def cancel(self, userService: UserService) -> UserService:
        """
        Cancels an user service creation
        @return: the Uservice canceling
        """
        userService.refresh_from_db()
        logger.debug('Canceling userService %s creation', userService)

        if userService.isPreparing() is False:
            logger.info(
                'Cancel requested for a non running operation, performing removal instead'
            )
            return self.remove(userService)

        userServiceInstance = userService.getInstance()

        if not userServiceInstance.supportsCancel(
        ):  # Does not supports cancel, but destroy, so mark it for "later" destroy
            # State is kept, just mark it for destroy after finished preparing
            userService.setProperty('destroy_after', 'y')
        else:
            userService.setState(State.CANCELING)
            # We simply notify service that it should cancel operation
            state = userServiceInstance.cancel()

            # Data will be serialized on makeUnique process
            # If cancel is not supported, base cancel always returns "FINISHED", and
            # opchecker will set state to "removable"
            UserServiceOpChecker.makeUnique(userService, userServiceInstance,
                                            state)

        return userService
Example #3
0
    def remove(self, userService: UserService) -> UserService:
        """
        Removes a uService element
        """
        with transaction.atomic():
            userService = UserService.objects.select_for_update().get(
                id=userService.id)
            logger.debug('Removing userService %a', userService)
            if userService.isUsable() is False and State.isRemovable(
                    userService.state) is False:
                raise OperationException(
                    _('Can\'t remove a non active element'))
            userService.setState(State.REMOVING)
            logger.debug("***** The state now is %s *****",
                         State.toString(userService.state))
            userService.setInUse(
                False
            )  # For accounting, ensure that it is not in use right now
            userService.save()

        userServiceInstance = userService.getInstance()
        state = userServiceInstance.destroy()

        # Data will be serialized on makeUnique process
        UserServiceOpChecker.makeUnique(userService, userServiceInstance,
                                        state)

        return userService
Example #4
0
    def checkAndUpdateState(userService: UserService,
                            userServiceInstance: UserDeployment, state: str):
        """
        Checks the value returned from invocation to publish or checkPublishingState, updating the servicePoolPub database object
        Return True if it has to continue checking, False if finished
        """
        try:
            # Fills up basic data
            userService.unique_id = userServiceInstance.getUniqueId(
            )  # Updates uniqueId
            userService.friendly_name = userServiceInstance.getName(
            )  # And name, both methods can modify serviceInstance, so we save it later
            userService.save(update_fields=['unique_id', 'friendly_name'])

            updater = {
                State.PREPARING: UpdateFromPreparing,
                State.REMOVING: UpdateFromRemoving,
                State.CANCELING: UpdateFromCanceling
            }.get(userService.state, UpdateFromOther)

            logger.debug('Updating %s from %s with updater %s and state %s',
                         userService.friendly_name,
                         State.toString(userService.state), updater, state)

            updater(userService, userServiceInstance).run(state)

        except Exception as e:
            logger.exception('Checking service state')
            log.doLog(userService, log.ERROR, 'Exception: {}'.format(e),
                      log.INTERNAL)
            userService.setState(State.ERROR)
            userService.save(update_fields=['data'])
 def notifyReadyFromOsManager(self, uService, data):
     try:
         ui = uService.getInstance()
         logger.debug('Notifying user service ready state')
         state = ui.notifyReadyFromOsManager(data)
         logger.debug('State: {0}'.format(state))
         uService.updateData(ui)
         if state == State.FINISHED:
             logger.debug('Service is now ready')
             uService.save()
         elif uService.state in (State.USABLE, State.PREPARING):  # We don't want to get active deleting or deleted machines...
             uService.setState(State.PREPARING)
             UserServiceOpChecker.makeUnique(uService, ui, state)
     except Exception as e:
         logger.exception('Unhandled exception on notyfyReady: {}'.format(e))
         UserService.setState(State.ERROR)
         return
Example #6
0
 def notifyReadyFromOsManager(self, userService: UserService, data: typing.Any) -> None:
     try:
         userServiceInstance = userService.getInstance()
         logger.debug('Notifying user service ready state')
         state = userServiceInstance.notifyReadyFromOsManager(data)
         logger.debug('State: %s', state)
         if state == State.FINISHED:
             userService.updateData(userServiceInstance)
             logger.debug('Service is now ready')
         elif userService.state in (State.USABLE, State.PREPARING):  # We don't want to get active deleting or deleted machines...
             userService.setState(State.PREPARING)
             UserServiceOpChecker.makeUnique(userService, userServiceInstance, state)
         userService.save(update_fields=['os_state'])
     except Exception as e:
         logger.exception('Unhandled exception on notyfyReady: %s', e)
         userService.setState(State.ERROR)
         return
Example #7
0
    def moveToLevel(self, cache: UserService, cacheLevel: int) -> None:
        """
        Moves a cache element from one level to another
        @return: cache element
        """
        cache.refresh_from_db()
        logger.debug('Moving cache %s to level %s', cache, cacheLevel)
        cacheInstance = cache.getInstance()
        state = cacheInstance.moveToCache(cacheLevel)
        cache.cache_level = cacheLevel
        cache.save(update_fields=['cache_level'])
        logger.debug('Service State: %a %s %s', State.toString(state), State.toString(cache.state), State.toString(cache.os_state))
        if State.isRuning(state) and cache.isUsable():
            cache.setState(State.PREPARING)

        # Data will be serialized on makeUnique process
        UserServiceOpChecker.makeUnique(cache, cacheInstance, state)
Example #8
0
 def notifyReadyFromOsManager(self, uService, data):
     try:
         ui = uService.getInstance()
         logger.debug('Notifying user service ready state')
         state = ui.notifyReadyFromOsManager(data)
         logger.debug('State: {0}'.format(state))
         uService.updateData(ui)
         if state == State.FINISHED:
             logger.debug('Service is now ready')
             uService.save()
         elif uService.state in (
                 State.USABLE, State.PREPARING
         ):  # We don't want to get active deleting or deleted machines...
             uService.setState(State.PREPARING)
             UserServiceOpChecker.makeUnique(uService, ui, state)
     except Exception as e:
         logger.exception(
             'Unhandled exception on notyfyReady: {}'.format(e))
         UserService.setState(State.ERROR)
         return