1
0
mirror of https://github.com/dkmstr/openuds.git synced 2025-01-08 21:18:00 +03:00

3.7 advances

This commit is contained in:
Adolfo Gómez García 2019-08-02 11:49:49 +02:00
parent 999a23fb6d
commit 3588ea20f4
5 changed files with 65 additions and 74 deletions

View File

@ -117,12 +117,8 @@ class Actor(Handler):
services = UserService.objects.filter(unique_id__in=clientIds, state__in=[State.USABLE, State.PREPARING])
if not services:
return None
return services[0]
return services[0] if services else None
def getTicket(self):
"""
@ -174,17 +170,19 @@ class Actor(Handler):
if service is None:
logger.info('Unmanaged host request: %s', self._args)
return Actor.result(_('Unmanaged host'), error=ERR_HOST_NOT_MANAGED)
else:
# Set last seen actor version
service.setProperty('actor_version', actorVersion)
maxIdle = None
if service.deployed_service.osmanager is not None:
maxIdle = service.deployed_service.osmanager.getInstance().maxIdle()
logger.debug('Max idle: %s', maxIdle)
return Actor.result((service.uuid,
service.unique_id,
0 if maxIdle is None else maxIdle)
)
# Set last seen actor version
service.setProperty('actor_version', actorVersion)
maxIdle = None
if service.deployed_service.osmanager is not None:
maxIdle = service.deployed_service.osmanager.getInstance().maxIdle()
logger.debug('Max idle: %s', maxIdle)
return Actor.result(
(
service.uuid,
service.unique_id,
0 if maxIdle is None else maxIdle
)
)
raise RequestError('Invalid request')
# Must be invoked as '/rest/actor/UUID/[message], with message data in post body

View File

@ -67,7 +67,7 @@ class UserServiceManager:
pass
@staticmethod
def manager():
def manager() -> 'UserServiceManager':
if not UserServiceManager._manager:
UserServiceManager._manager = UserServiceManager()
return UserServiceManager._manager

View File

@ -44,7 +44,7 @@ STORAGE_KEY = 'osmk'
if typing.TYPE_CHECKING:
from uds.models import UserService
from uds.core.environmentable import Environment
from uds.core.environment import Environment
class OSManager(Module):
"""
@ -96,7 +96,7 @@ class OSManager(Module):
"""
# These methods must be overriden
def process(self, userService: 'UserService', message: str, data: str, options=None):
def process(self, userService: 'UserService', message: str, data: str, options: typing.Optional[typing.Dict[str, typing.Any]] = None) -> None:
"""
This method must be overriden so your so manager can manage requests and responses from agent.
@param userService: Service that sends the request (virtual machine or whatever)
@ -104,7 +104,7 @@ class OSManager(Module):
@param data: Data for this message
"""
def checkState(self, service):
def checkState(self, userService: 'UserService') -> str:
"""
This method must be overriden so your os manager can respond to requests from system to the current state of the service
This method will be invoked when:
@ -118,21 +118,20 @@ class OSManager(Module):
"""
return State.FINISHED
def processUnused(self, userService):
def processUnused(self, userService: 'UserService') -> None:
"""
This will be invoked for every assigned and unused user service that has been in this state at least 1/2 of Globalconfig.CHECK_UNUSED_TIME
This function can update userService values. Normal operation will be remove machines if this state is not valid
"""
pass
def maxIdle(self):
def maxIdle(self) -> typing.Optional[int]:
"""
If os manager request "max idle", this method will return a value different to None so actors will get informed on Connection
@return Must return None (default if not override), or a "max idle" in seconds
"""
return None
def maxSession(self):
def maxSession(self) -> typing.Optional[int]:
"""
If os manager requests "max session duration", this methos will return a value distinct of None so actors will get informed on Connection
@return Must return None (default if not override), or a "max session duration" in seconds
@ -140,14 +139,14 @@ class OSManager(Module):
return None
@classmethod
def transformsUserOrPasswordForService(cls):
def transformsUserOrPasswordForService(cls: typing.Type['OSManager']):
"""
Helper method that informs if the os manager transforms the username and/or the password.
This is used from DeployedService
"""
return cls.processUserPassword != OSManager.processUserPassword
def processUserPassword(self, service, username, password):
def processUserPassword(self, userService: 'UserService', username: str, password: str) -> typing.Tuple[str, str]:
"""
This will be invoked prior to passsing username/password to Transport.
@ -162,21 +161,23 @@ class OSManager(Module):
Note: This method is, right now, invoked by Transports directly. So if you implement a Transport, remember to invoke this
"""
return [username, password]
return username, password
def destroy(self):
def destroy(self) -> None:
"""
Invoked when OS Manager is deleted
"""
pass
def logKnownIp(self, userService, ip):
def logKnownIp(self, userService: 'UserService', ip: str) -> None:
userService.logIP(ip)
def toReady(self, userService):
def toReady(self, userService: 'UserService') -> None:
'''
Resets login counter to 0
'''
userService.setProperty('loginsCounter', '0')
def loggedIn(self, userService, userName=None):
def loggedIn(self, userService: 'UserService', userName: typing.Optional[str] = None) -> None:
"""
This method:
- Add log in event to stats
@ -185,21 +186,18 @@ class OSManager(Module):
"""
uniqueId = userService.unique_id
userService.setInUse(True)
si = userService.getInstance()
si.userLoggedIn(userName)
userService.updateData(si)
userServiceInstance = userService.getInstance()
userServiceInstance.userLoggedIn(userName)
userService.updateData(userServiceInstance)
serviceIp = si.getIp()
serviceIp = userServiceInstance.getIp()
fullUserName = 'unknown'
if userService.user is not None:
fullUserName = userService.user.manager.name + '\\' + userService.user.name
fullUserName = userService.user.manager.name + '\\' + userService.user.name if userService.user else 'unknown'
knownUserIP = userService.src_ip + ':' + userService.src_hostname
knownUserIP = knownUserIP if knownUserIP != ':' else 'unknown'
if userName is None:
userName = 'unknown'
userName = userName or 'unknown'
addEvent(userService.deployed_service, ET_LOGIN, fld1=userName, fld2=knownUserIP, fld3=serviceIp, fld4=fullUserName)
@ -208,10 +206,10 @@ class OSManager(Module):
log.useLog('login', uniqueId, serviceIp, userName, knownUserIP, fullUserName, userService.friendly_name, userService.deployed_service.name)
counter = int(userService.getProperty('loginsCounter', '0')) + 1
userService.setProperty('loginsCounter', six.text_type(counter))
userService.setProperty('loginsCounter', str(counter))
def loggedOut(self, userService, userName=None):
def loggedOut(self, userService: 'UserService', userName: typing.Optional[str] = None) -> None:
"""
This method:
- Add log in event to stats
@ -221,19 +219,18 @@ class OSManager(Module):
counter = int(userService.getProperty('loginsCounter', '0'))
if counter > 0:
counter -= 1
userService.setProperty('loginsCounter', six.text_type(counter))
userService.setProperty('loginsCounter', str(counter))
if GlobalConfig.EXCLUSIVE_LOGOUT.getBool(True) is True:
if counter > 0:
return
if GlobalConfig.EXCLUSIVE_LOGOUT.getBool(True) and counter > 0:
return
uniqueId = userService.unique_id
userService.setInUse(False)
si = userService.getInstance()
si.userLoggedOut(userName)
userService.updateData(si)
userServiceInstance = userService.getInstance()
userServiceInstance.userLoggedOut(userName)
userService.updateData(userServiceInstance)
serviceIp = si.getIp()
serviceIp = userServiceInstance.getIp()
fullUserName = 'unknown'
if userService.user is not None:
@ -242,8 +239,7 @@ class OSManager(Module):
knownUserIP = userService.src_ip + ':' + userService.src_hostname
knownUserIP = knownUserIP if knownUserIP != ':' else 'unknown'
if userName is None:
userName = 'unknown'
userName = userName or 'unknown'
addEvent(userService.deployed_service, ET_LOGOUT, fld1=userName, fld2=knownUserIP, fld3=serviceIp, fld4=fullUserName)
@ -251,7 +247,7 @@ class OSManager(Module):
log.useLog('logout', uniqueId, serviceIp, userName, knownUserIP, fullUserName, userService.friendly_name, userService.deployed_service.name)
def isPersistent(self):
def isPersistent(self) -> bool:
"""
When a publication if finished, old assigned machines will be removed if this value is True.
Defaults to False
@ -260,6 +256,3 @@ class OSManager(Module):
def __str__(self):
return "Base OS Manager"
def __unicode__(self):
return self.__str__()

View File

@ -30,17 +30,17 @@
"""
@author: Adolfo Gómez, dkmaster at dkmon dot com
"""
from __future__ import unicode_literals
import logging
__updated__ = '2014-11-12'
import typing
logger = logging.getLogger(__name__)
if typing.TYPE_CHECKING:
from .BaseOsManager import OSManager
class OSManagersFactory(object):
_factory = None
class OSManagersFactory:
_factory: typing.Optional['OSManagersFactory'] = None
_osManagers: typing.Dict[str, typing.Type['OSManager']]
def __init__(self):
self._osManagers = {}
@ -54,8 +54,8 @@ class OSManagersFactory(object):
def providers(self):
return self._osManagers
def insert(self, type_):
logger.debug('Adding OS Manager {0} as {1}'.format(type_.type(), type_))
def insert(self, type_: typing.Type['OSManager']):
logger.debug('Adding OS Manager %s as %s', type_.type(), type_)
typeName = type_.type().lower()
self._osManagers[typeName] = type_

View File

@ -342,7 +342,7 @@ class UserService(UUIDModel): # pylint: disable=too-many-public-methods
self.user = user
self.save(update_fields=['cache_level', 'state_date', 'user'])
def setInUse(self, state):
def setInUse(self, inUse: bool) -> None:
"""
Set the "in_use" flag for this user deployed service
@ -352,17 +352,17 @@ class UserService(UUIDModel): # pylint: disable=too-many-public-methods
:note: If the state is Fase (set to not in use), a check for removal of this deployed service is launched.
"""
from uds.core.managers.UserServiceManager import UserServiceManager
self.in_use = state
self.in_use = inUse
self.in_use_date = getSqlDatetime()
self.save(update_fields=['in_use', 'in_use_date'])
# Start/stop accounting
if state is True:
if inUse:
self.startUsageAccounting()
else:
self.stopUsageAccounting()
if state is False: # Service released, check y we should mark it for removal
if not inUse: # Service released, check y we should mark it for removal
# If our publication is not current, mark this for removal
UserServiceManager.manager().checkForRemoval(self)
@ -445,10 +445,10 @@ class UserService(UUIDModel): # pylint: disable=too-many-public-methods
from uds.core.managers.UserServiceManager import UserServiceManager
UserServiceManager.manager().moveToLevel(self, cacheLevel)
def getProperty(self, propName, default=None):
def getProperty(self, propName: str, default: typing.Optional[str] = None):
try:
val = self.properties.get(name=propName).value
return val if val is not '' else default # Empty string is null
return val or default # Empty string is null
except Exception:
return default
@ -462,9 +462,9 @@ class UserService(UUIDModel): # pylint: disable=too-many-public-methods
dct[v.name] = v.value
return dct
def setProperty(self, propName, propValue):
def setProperty(self, propName: str, propValue: typing.Optional[str]):
prop, _ = self.properties.get_or_create(name=propName)
prop.value = propValue if propValue is not None else ''
prop.value = propValue or ''
prop.save()
def setCommsUrl(self, commsUrl=None):
@ -473,7 +473,7 @@ class UserService(UUIDModel): # pylint: disable=too-many-public-methods
def getCommsUrl(self) -> typing.Optional[str]:
return self.getProperty('comms_url', None)
def logIP(self, ip: str = None) -> None:
def logIP(self, ip: typing.Optional[str] = None) -> None:
self.setProperty('ip', ip)
def getLoggedIP(self):