1
0
mirror of https://github.com/dkmstr/openuds.git synced 2025-01-10 01:17:59 +03:00

upgraded settings for black

This commit is contained in:
Adolfo Gómez García 2023-04-23 02:34:15 +02:00
parent 713e793f2d
commit c4703a5e91
No known key found for this signature in database
GPG Key ID: DD1ABF20724CDA23
3 changed files with 46 additions and 145 deletions

View File

@ -51,21 +51,12 @@ class NotificationsManager(metaclass=singleton.Singleton):
@staticmethod @staticmethod
def manager() -> 'NotificationsManager': def manager() -> 'NotificationsManager':
return ( return NotificationsManager() # Singleton pattern will return always the same instance
NotificationsManager()
) # Singleton pattern will return always the same instance
def notify( def notify(self, group: str, identificator: str, level: LogLevel, message: str, *args) -> None:
self, # logger.debug(
group: str,
identificator: str,
level: LogLevel,
message: str,
*args
) -> None:
#logger.debug(
# 'Notify: %s, %s, %s, %s, [%s]', group, identificator, level, message, args # 'Notify: %s, %s, %s, %s, [%s]', group, identificator, level, message, args
#) # )
# Format the string # Format the string
try: try:
message = message % args message = message % args
@ -74,9 +65,7 @@ class NotificationsManager(metaclass=singleton.Singleton):
# Store the notification on local persistent storage # Store the notification on local persistent storage
# Will be processed by UDS backend # Will be processed by UDS backend
with Notification.atomicPersistent(): with Notification.atomicPersistent():
notify = Notification( notify = Notification(group=group, identificator=identificator, level=level, message=message)
group=group, identificator=identificator, level=level, message=message
)
Notification.savePersistent(notify) Notification.savePersistent(notify)
def registerGroup(self, group: str) -> None: def registerGroup(self, group: str) -> None:

View File

@ -79,18 +79,14 @@ class UserServiceManager(metaclass=singleton.Singleton):
@staticmethod @staticmethod
def manager() -> 'UserServiceManager': def manager() -> 'UserServiceManager':
return ( return UserServiceManager() # Singleton pattern will return always the same instance
UserServiceManager()
) # Singleton pattern will return always the same instance
def getCacheStateFilter(self, servicePool: ServicePool, level: int) -> Q: def getCacheStateFilter(self, servicePool: ServicePool, level: int) -> Q:
return Q(cache_level=level) & self.getStateFilter(servicePool.service) return Q(cache_level=level) & self.getStateFilter(servicePool.service)
@staticmethod @staticmethod
def getStateFilter(service: 'models.Service') -> Q: def getStateFilter(service: 'models.Service') -> Q:
if ( if service.oldMaxAccountingMethod: # If no limits and accounting method is not old one
service.oldMaxAccountingMethod
): # If no limits and accounting method is not old one
# Valid states are: PREPARING, USABLE # Valid states are: PREPARING, USABLE
states = [State.PREPARING, State.USABLE] states = [State.PREPARING, State.USABLE]
else: # New accounting method selected else: # New accounting method selected
@ -104,9 +100,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
""" """
if self.maximumUserServicesDeployed(servicePool.service): if self.maximumUserServicesDeployed(servicePool.service):
raise MaxServicesReachedError( raise MaxServicesReachedError(
_('Maximum number of user services reached for this {}').format( _('Maximum number of user services reached for this {}').format(servicePool)
servicePool
)
) )
def getExistingUserServices(self, service: 'models.Service') -> int: def getExistingUserServices(self, service: 'models.Service') -> int:
@ -131,9 +125,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
return False return False
def _createCacheAtDb( def _createCacheAtDb(self, publication: ServicePoolPublication, cacheLevel: int) -> UserService:
self, publication: ServicePoolPublication, cacheLevel: int
) -> UserService:
""" """
Private method to instatiate a cache element at database with default states Private method to instatiate a cache element at database with default states
""" """
@ -152,9 +144,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
in_use=False, in_use=False,
) )
def _createAssignedAtDb( def _createAssignedAtDb(self, publication: ServicePoolPublication, user: User) -> UserService:
self, publication: ServicePoolPublication, user: User
) -> UserService:
""" """
Private method to instatiate an assigned element at database with default state Private method to instatiate an assigned element at database with default state
""" """
@ -172,9 +162,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
in_use=False, in_use=False,
) )
def _createAssignedAtDbForNoPublication( def _createAssignedAtDbForNoPublication(self, servicePool: ServicePool, user: User) -> UserService:
self, servicePool: ServicePool, user: User
) -> UserService:
""" """
__createCacheAtDb and __createAssignedAtDb uses a publication for create the UserService. __createCacheAtDb and __createAssignedAtDb uses a publication for create the UserService.
There is cases where deployed services do not have publications (do not need them), so we need this method to create There is cases where deployed services do not have publications (do not need them), so we need this method to create
@ -194,9 +182,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
in_use=False, in_use=False,
) )
def createCacheFor( def createCacheFor(self, publication: ServicePoolPublication, cacheLevel: int) -> UserService:
self, publication: ServicePoolPublication, cacheLevel: int
) -> UserService:
""" """
Creates a new cache for the deployed service publication at level indicated Creates a new cache for the deployed service publication at level indicated
""" """
@ -253,9 +239,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
return assigned return assigned
def createFromAssignable( def createFromAssignable(self, servicePool: ServicePool, user: User, assignableId: str) -> UserService:
self, servicePool: ServicePool, user: User, assignableId: str
) -> UserService:
""" """
Creates an assigned service from an "assignable" id Creates an assigned service from an "assignable" id
""" """
@ -289,9 +273,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
# Now, get from serviceInstance the data # Now, get from serviceInstance the data
assignedInstance = assigned.getInstance() assignedInstance = assigned.getInstance()
state = serviceInstance.assignFromAssignables( state = serviceInstance.assignFromAssignables(assignableId, user, assignedInstance)
assignableId, user, assignedInstance
)
# assigned.updateData(assignedInstance) # assigned.updateData(assignedInstance)
UserServiceOpChecker.makeUnique(assigned, assignedInstance, state) UserServiceOpChecker.makeUnique(assigned, assignedInstance, state)
@ -329,9 +311,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
userService.refresh_from_db() userService.refresh_from_db()
if userService.isPreparing() is False: if userService.isPreparing() is False:
logger.debug( logger.debug('Cancel requested for a non running operation, performing removal instead')
'Cancel requested for a non running operation, performing removal instead'
)
return self.remove(userService) return self.remove(userService)
operationsLogger.info('Canceling userService %s', userService.name) operationsLogger.info('Canceling userService %s', userService.name)
@ -361,18 +341,11 @@ class UserServiceManager(metaclass=singleton.Singleton):
with transaction.atomic(): with transaction.atomic():
userService = UserService.objects.select_for_update().get(id=userService.id) userService = UserService.objects.select_for_update().get(id=userService.id)
operationsLogger.info('Removing userService %a', userService.name) operationsLogger.info('Removing userService %a', userService.name)
if ( if userService.isUsable() is False and State.isRemovable(userService.state) is False:
userService.isUsable() is False
and State.isRemovable(userService.state) is False
):
raise OperationException(_('Can\'t remove a non active element')) raise OperationException(_('Can\'t remove a non active element'))
userService.setState(State.REMOVING) userService.setState(State.REMOVING)
logger.debug( logger.debug("***** The state now is %s *****", State.toString(userService.state))
"***** The state now is %s *****", State.toString(userService.state) userService.setInUse(False) # For accounting, ensure that it is not in use right now
)
userService.setInUse(
False
) # For accounting, ensure that it is not in use right now
userService.save() userService.save()
userServiceInstance = userService.getInstance() userServiceInstance = userService.getInstance()
@ -391,9 +364,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
return self.cancel(userService) return self.cancel(userService)
raise OperationException( raise OperationException(
_('Can\'t remove nor cancel {} cause its state don\'t allow it').format( _('Can\'t remove nor cancel {} cause its state don\'t allow it').format(userService.name)
userService.name
)
) )
def getExistingAssignationForUser( def getExistingAssignationForUser(
@ -403,9 +374,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
user=user, state__in=State.VALID_STATES user=user, state__in=State.VALID_STATES
) # , deployed_service__visible=True ) # , deployed_service__visible=True
if existing.exists(): if existing.exists():
logger.debug( logger.debug('Found assigned service from %s to user %s', servicePool, user.name)
'Found assigned service from %s to user %s', servicePool, user.name
)
return existing.first() return existing.first()
return None return None
@ -460,9 +429,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
typing.List[UserService], typing.List[UserService],
servicePool.cachedUserServices() servicePool.cachedUserServices()
.select_for_update() .select_for_update()
.filter( .filter(cache_level=services.UserDeployment.L1_CACHE, state=State.USABLE)[
cache_level=services.UserDeployment.L1_CACHE, state=State.USABLE
)[
:1 # type: ignore # Slicing is not supported by pylance right now :1 # type: ignore # Slicing is not supported by pylance right now
], ],
) )
@ -494,9 +461,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
servicePool, servicePool,
events.ET_CACHE_HIT, events.ET_CACHE_HIT,
fld1=servicePool.cachedUserServices() fld1=servicePool.cachedUserServices()
.filter( .filter(cache_level=services.UserDeployment.L1_CACHE, state=State.USABLE)
cache_level=services.UserDeployment.L1_CACHE, state=State.USABLE
)
.count(), .count(),
) )
return cache return cache
@ -508,9 +473,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
caches = ( caches = (
servicePool.cachedUserServices() servicePool.cachedUserServices()
.select_for_update() .select_for_update()
.filter( .filter(cache_level=services.UserDeployment.L1_CACHE, state=State.PREPARING)[
cache_level=services.UserDeployment.L1_CACHE, state=State.PREPARING
)[
:1 # type: ignore # Slicing is not supported by pylance right now :1 # type: ignore # Slicing is not supported by pylance right now
] ]
) )
@ -541,9 +504,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
servicePool, servicePool,
events.ET_CACHE_MISS, events.ET_CACHE_MISS,
fld1=servicePool.cachedUserServices() fld1=servicePool.cachedUserServices()
.filter( .filter(cache_level=services.UserDeployment.L1_CACHE, state=State.PREPARING)
cache_level=services.UserDeployment.L1_CACHE, state=State.PREPARING
)
.count(), .count(),
) )
return cache return cache
@ -552,9 +513,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
serviceType = servicePool.service.getType() serviceType = servicePool.service.getType()
if serviceType.usesCache: if serviceType.usesCache:
inAssigned = ( inAssigned = (
servicePool.assignedUserServices() servicePool.assignedUserServices().filter(self.getStateFilter(servicePool.service)).count()
.filter(self.getStateFilter(servicePool.service))
.count()
) )
if ( if (
inAssigned >= servicePool.max_srvs inAssigned >= servicePool.max_srvs
@ -571,9 +530,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
events.addEvent(servicePool, events.ET_CACHE_MISS, fld1=0) events.addEvent(servicePool, events.ET_CACHE_MISS, fld1=0)
return self.createAssignedFor(servicePool, user) return self.createAssignedFor(servicePool, user)
def getUserServicesInStatesForProvider( def getUserServicesInStatesForProvider(self, provider: 'models.Provider', states: typing.List[str]) -> int:
self, provider: 'models.Provider', states: typing.List[str]
) -> int:
""" """
Returns the number of services of a service provider in the state indicated Returns the number of services of a service provider in the state indicated
""" """
@ -586,9 +543,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
checks if we can do a "remove" from a deployed service checks if we can do a "remove" from a deployed service
serviceIsntance is just a helper, so if we already have deserialized deployedService serviceIsntance is just a helper, so if we already have deserialized deployedService
""" """
removing = self.getUserServicesInStatesForProvider( removing = self.getUserServicesInStatesForProvider(servicePool.service.provider, [State.REMOVING])
servicePool.service.provider, [State.REMOVING]
)
serviceInstance = servicePool.service.getInstance() serviceInstance = servicePool.service.getInstance()
if ( if (
serviceInstance.isAvailable() serviceInstance.isAvailable()
@ -654,9 +609,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
except Exception: except Exception:
logger.exception('Reseting service') logger.exception('Reseting service')
def notifyPreconnect( def notifyPreconnect(self, userService: UserService, userName: str, protocol: str) -> None:
self, userService: UserService, userName: str, protocol: str
) -> None:
comms.notifyPreconnect(userService, userName, protocol) comms.notifyPreconnect(userService, userName, protocol)
def checkUuid(self, userService: UserService) -> bool: def checkUuid(self, userService: UserService) -> bool:
@ -665,9 +618,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
def requestScreenshot(self, userService: UserService) -> bytes: def requestScreenshot(self, userService: UserService) -> bytes:
return comms.requestScreenshot(userService) return comms.requestScreenshot(userService)
def sendScript( def sendScript(self, userService: UserService, script: str, forUser: bool = False) -> None:
self, userService: UserService, script: str, forUser: bool = False
) -> None:
comms.sendScript(userService, script, forUser) comms.sendScript(userService, script, forUser)
def requestLogoff(self, userService: UserService) -> None: def requestLogoff(self, userService: UserService) -> None:
@ -683,16 +634,12 @@ class UserServiceManager(metaclass=singleton.Singleton):
""" """
osManager = userService.deployed_service.osmanager osManager = userService.deployed_service.osmanager
# If os manager says "machine is persistent", do not try to delete "previous version" assigned machines # If os manager says "machine is persistent", do not try to delete "previous version" assigned machines
doPublicationCleanup = ( doPublicationCleanup = True if not osManager else not osManager.getInstance().isPersistent()
True if not osManager else not osManager.getInstance().isPersistent()
)
if doPublicationCleanup: if doPublicationCleanup:
remove = False remove = False
with transaction.atomic(): with transaction.atomic():
userService = UserService.objects.select_for_update().get( userService = UserService.objects.select_for_update().get(id=userService.id)
id=userService.id
)
activePublication = userService.deployed_service.activePublication() activePublication = userService.deployed_service.activePublication()
if ( if (
userService.publication userService.publication
@ -708,9 +655,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
if remove: if remove:
userService.remove() userService.remove()
def notifyReadyFromOsManager( def notifyReadyFromOsManager(self, userService: UserService, data: typing.Any) -> None:
self, userService: UserService, data: typing.Any
) -> None:
try: try:
userServiceInstance = userService.getInstance() userServiceInstance = userService.getInstance()
logger.debug('Notifying user service ready state') logger.debug('Notifying user service ready state')
@ -751,9 +696,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
servicePool.validateUser(user) servicePool.validateUser(user)
# Now we have to locate an instance of the service, so we can assign it to user. # Now we have to locate an instance of the service, so we can assign it to user.
if ( if create: # getAssignation, if no assignation is found, tries to create one
create
): # getAssignation, if no assignation is found, tries to create one
userService = self.getAssignationForUser(servicePool, user) userService = self.getAssignationForUser(servicePool, user)
else: # Sometimes maybe we only need to locate the existint user service else: # Sometimes maybe we only need to locate the existint user service
userService = self.getExistingAssignationForUser(servicePool, user) userService = self.getExistingAssignationForUser(servicePool, user)
@ -794,9 +737,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
if not userService: if not userService:
raise InvalidServiceException( raise InvalidServiceException(
_( _('Invalid service. The service is not available at this moment. Please, try later')
'Invalid service. The service is not available at this moment. Please, try later'
)
) )
# Early log of "access try" so we can imagine what is going on # Early log of "access try" so we can imagine what is going on
@ -812,12 +753,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
t: Transport t: Transport
for t in userService.deployed_service.transports.order_by('priority'): for t in userService.deployed_service.transports.order_by('priority'):
typeTrans = t.getType() typeTrans = t.getType()
if ( if typeTrans and t.validForIp(srcIp) and typeTrans.supportsOs(os.os) and t.validForOs(os.os):
typeTrans
and t.validForIp(srcIp)
and typeTrans.supportsOs(os.os)
and t.validForOs(os.os)
):
idTransport = t.uuid idTransport = t.uuid
break break
@ -832,9 +768,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
# If transport is not available for the request IP... # If transport is not available for the request IP...
if not transport.validForIp(srcIp): if not transport.validForIp(srcIp):
msg = _('The requested transport {} is not valid for {}').format( msg = _('The requested transport {} is not valid for {}').format(transport.name, srcIp)
transport.name, srcIp
)
logger.error(msg) logger.error(msg)
raise InvalidServiceException(msg) raise InvalidServiceException(msg)
@ -861,9 +795,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
userService.logIP(ip) # Update known ip userService.logIP(ip) # Update known ip
logger.debug('IP: %s', ip) logger.debug('IP: %s', ip)
if ( if self.checkUuid(userService) is False: # The service is not the expected one
self.checkUuid(userService) is False
): # The service is not the expected one
serviceNotReadyCode = 0x0004 serviceNotReadyCode = 0x0004
log.doLog( log.doLog(
userService, userService,
@ -907,9 +839,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
transportInstance, transportInstance,
) )
message = transportInstance.getCustomAvailableErrorMsg( message = transportInstance.getCustomAvailableErrorMsg(userService, ip)
userService, ip
)
log.doLog(userService, log.LogLevel.WARNING, message, log.LogSource.TRANSPORT) log.doLog(userService, log.LogLevel.WARNING, message, log.LogSource.TRANSPORT)
logger.debug( logger.debug(
'Transport is not ready for user service %s: %s', 'Transport is not ready for user service %s: %s',
@ -934,27 +864,19 @@ class UserServiceManager(metaclass=singleton.Singleton):
transport.name, transport.name,
ip, ip,
) )
raise ServiceNotReadyError( raise ServiceNotReadyError(code=serviceNotReadyCode, userService=userService, transport=transport)
code=serviceNotReadyCode, userService=userService, transport=transport
)
def isMetaService(self, metaId: str) -> bool: def isMetaService(self, metaId: str) -> bool:
return metaId[0] == 'M' return metaId[0] == 'M'
def locateMetaService( def locateMetaService(self, user: User, idService: str) -> typing.Optional[UserService]:
self, user: User, idService: str
) -> typing.Optional[UserService]:
kind, uuidMetapool = idService[0], idService[1:] kind, uuidMetapool = idService[0], idService[1:]
if kind != 'M': if kind != 'M':
return None return None
meta: MetaPool = MetaPool.objects.get(uuid=uuidMetapool) meta: MetaPool = MetaPool.objects.get(uuid=uuidMetapool)
# Get pool members. Just pools "visible" and "usable" # Get pool members. Just pools "visible" and "usable"
pools = [ pools = [p.pool for p in meta.members.all() if p.pool.isVisible() and p.pool.isUsable()]
p.pool
for p in meta.members.all()
if p.pool.isVisible() and p.pool.isUsable()
]
# look for an existing user service in the pool # look for an existing user service in the pool
try: try:
return UserService.objects.filter( return UserService.objects.filter(
@ -991,9 +913,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
raise ServiceAccessDeniedByCalendar() raise ServiceAccessDeniedByCalendar()
# Get pool members. Just pools "visible" and "usable" # Get pool members. Just pools "visible" and "usable"
poolMembers = [ poolMembers = [p for p in meta.members.all() if p.pool.isVisible() and p.pool.isUsable()]
p for p in meta.members.all() if p.pool.isVisible() and p.pool.isUsable()
]
# Sort pools array. List of tuples with (priority, pool) # Sort pools array. List of tuples with (priority, pool)
sortPools: typing.List[typing.Tuple[int, ServicePool]] sortPools: typing.List[typing.Tuple[int, ServicePool]]
# Sort pools based on meta selection # Sort pools based on meta selection
@ -1015,9 +935,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
# Sort pools related to policy now, and xtract only pools, not sort keys # Sort pools related to policy now, and xtract only pools, not sort keys
# split resuult in two lists, 100% full and not 100% full # split resuult in two lists, 100% full and not 100% full
# Remove "full" pools (100%) from result and pools in maintenance mode, not ready pools, etc... # Remove "full" pools (100%) from result and pools in maintenance mode, not ready pools, etc...
sortedPools = sorted( sortedPools = sorted(sortPools, key=operator.itemgetter(0)) # sort by priority (first element)
sortPools, key=operator.itemgetter(0)
) # sort by priority (first element)
pools: typing.List[ServicePool] = [] pools: typing.List[ServicePool] = []
poolsFull: typing.List[ServicePool] = [] poolsFull: typing.List[ServicePool] = []
for p in sortedPools: for p in sortedPools:
@ -1092,9 +1010,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
) )
except Exception: # No service already assigned, lets find a suitable one except Exception: # No service already assigned, lets find a suitable one
for ( for pool in pools: # Pools are already sorted, and "full" pools are filtered out
pool
) in pools: # Pools are already sorted, and "full" pools are filtered out
if meta.ha_policy == MetaPool.HA_POLICY_ENABLED: if meta.ha_policy == MetaPool.HA_POLICY_ENABLED:
# If not available, skip it # If not available, skip it
if pool.service.getInstance().isAvailable() is False: if pool.service.getInstance().isAvailable() is False:
@ -1130,6 +1046,4 @@ class UserServiceManager(metaclass=singleton.Singleton):
f'No user service accessible from device (ip {srcIp}, os: {os.os.name})', f'No user service accessible from device (ip {srcIp}, os: {os.os.name})',
log.LogSource.SERVICE, log.LogSource.SERVICE,
) )
raise InvalidServiceException( raise InvalidServiceException(_('The service is not accessible from this device'))
_('The service is not accessible from this device')
)

View File

@ -91,9 +91,7 @@ class Notifier(Module):
Default implementation does nothing Default implementation does nothing
""" """
def notify( def notify(self, group: str, identificator: str, level: LogLevel, message: str) -> None:
self, group: str, identificator: str, level: LogLevel, message: str
) -> None:
""" """
This method will be invoked from UDS to notify an event to this notifier. This method will be invoked from UDS to notify an event to this notifier.
This method will be invoked in real time, so ensure this method does not block or This method will be invoked in real time, so ensure this method does not block or