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
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
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
def reset(self, userService: UserService) -> None: userService.refresh_from_db() if not userService.deployed_service.service.getType().canReset: return logger.debug('Reseting %s', userService) userServiceInstance = userService.getInstance() try: userServiceInstance.reset() except Exception: logger.exception('Reseting service')
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
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)
def __init__(self, userService: UserService, userServiceInstance: typing.Optional[UserDeployment] = None): self.userService = userService self.userServiceInstance = userServiceInstance if userServiceInstance is not None else userService.getInstance( )