1
0
mirror of https://github.com/dkmstr/openuds.git synced 2024-12-24 21:34:41 +03:00

upgrading service

This commit is contained in:
Adolfo Gómez García 2022-01-29 21:44:14 +01:00
commit a4390f7918
4 changed files with 96 additions and 21 deletions

View File

@ -154,10 +154,8 @@ class MetaAssignedService(DetailHandler):
return UserService.objects.filter(
uuid=processUuid(userServiceId),
cache_level=0,
deployed_service__meta=metaPool,
)[
0 # type: ignore # Slicing is not supported by pylance right now
]
deployed_service__in=[i.pool for i in metaPool.members.all()],
)[0]
except Exception:
pass
raise self.invalidItemException()

View File

@ -710,23 +710,27 @@ class UserServiceManager(metaclass=singleton.Singleton):
logger.debug('Kind of service: %s, idService: %s', kind, uuidService)
userService: typing.Optional[UserService] = None
if kind == 'A': # This is an assigned service
if kind in 'A': # This is an assigned service
logger.debug('Getting A service %s', uuidService)
userService = UserService.objects.get(uuid=uuidService, user=user)
typing.cast(UserService, userService).deployed_service.validateUser(user)
else:
servicePool: ServicePool = ServicePool.objects.get(uuid=uuidService)
# We first do a sanity check for this, if the user has access to this service
# If it fails, will raise an exception
servicePool.validateUser(user)
try:
servicePool: ServicePool = ServicePool.objects.get(uuid=uuidService)
# We first do a sanity check for this, if the user has access to this service
# If it fails, will raise an exception
servicePool.validateUser(user)
# Now we have to locate an instance of the service, so we can assign it to user.
if (
create
): # getAssignation, if no assignation is found, tries to create one
userService = self.getAssignationForUser(servicePool, user)
else: # Sometimes maybe we only need to locate the existint user service
userService = self.getExistingAssignationForUser(servicePool, user)
# Now we have to locate an instance of the service, so we can assign it to user.
if (
create
): # getAssignation, if no assignation is found, tries to create one
userService = self.getAssignationForUser(servicePool, user)
else: # Sometimes maybe we only need to locate the existint user service
userService = self.getExistingAssignationForUser(servicePool, user)
except ServicePool.DoesNotExist:
logger.debug('Service pool does not exist')
return None
logger.debug('Found service: %s', userService)
@ -908,6 +912,33 @@ class UserServiceManager(metaclass=singleton.Singleton):
code=serviceNotReadyCode, userService=userService, transport=transport
)
def isMetaService(self, metaId: str) -> bool:
return metaId[0] == 'M'
def locateMetaService(
self, user: User, idService: str, create: bool = False
) -> typing.Optional[UserService]:
kind, uuidMetapool = idService[0], idService[1:]
if kind != 'M':
return None
meta: MetaPool = MetaPool.objects.get(uuid=uuidMetapool)
# Get pool members. Just pools "visible" and "usable"
pools = [
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
try:
return UserService.objects.filter(
deployed_service__in=pools,
state__in=State.VALID_STATES,
user=user,
cache_level=0,
).order_by('deployed_service__name')[0]
except IndexError:
return None
def getMeta(
self,
user: User,

View File

@ -59,10 +59,50 @@ from uds.core.auths.auth import webPassword
# Not imported at runtime, just for type checking
if typing.TYPE_CHECKING:
from uds.core.util.request import ExtendedHttpRequestWithUser
from uds.models import Image
logger = logging.getLogger(__name__)
def _serviceInfo(
uuid: str,
is_meta: bool,
name: str,
visual_name: str,
description: str,
group: typing.Mapping[str, typing.Any],
transports: typing.List[typing.Mapping[str, typing.Any]],
image: typing.Optional['Image'],
show_transports: bool,
allow_users_remove: bool,
allow_users_reset: bool,
maintenance: bool,
not_accessible: bool,
in_use: bool,
to_be_replaced: typing.Optional[str],
to_be_replaced_text: str,
custom_calendar_text: str,
):
return {
'id': ('M' if is_meta else 'F') + uuid,
'is_meta': is_meta,
'name': name,
'visual_name': visual_name,
'description': description,
'group': group,
'transports': transports,
'imageId': image and image.uuid or 'x',
'show_transports': show_transports,
'allow_users_remove': allow_users_remove,
'allow_users_reset': allow_users_reset,
'maintenance': maintenance,
'not_accesible': not_accessible,
'in_use': in_use,
'to_be_replaced': to_be_replaced,
'to_be_replaced_text': to_be_replaced_text,
'custom_calendar_text': custom_calendar_text,
}
def getServicesData(
request: 'ExtendedHttpRequestWithUser',

View File

@ -51,6 +51,7 @@ from uds.web.util import services
# Not imported at runtime, just for type checking
if typing.TYPE_CHECKING:
from uds.core.util.request import ExtendedHttpRequest, ExtendedHttpRequestWithUser
from uds.models import UserService
logger = logging.getLogger(__name__)
@ -157,12 +158,17 @@ def userServiceStatus(
Note:
'''
ip: typing.Union[str, None, bool]
userService = None
userService: typing.Optional['UserService'] = None
status = 'running'
# If service exists
userService = userServiceManager().locateUserService(
user=request.user, idService=idService, create=False
)
# If service exists (meta or not)
if userServiceManager().isMetaService(idService):
userService = userServiceManager().locateMetaService(
user=request.user, idService=idService
)
else:
userService = userServiceManager().locateUserService(
user=request.user, idService=idService, create=False
)
if userService:
# Service exists...
try: