From 3d6d478aedba7b56dc6468bd2703c3b2c1b66ca4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20G=C3=B3mez?= Date: Wed, 27 Feb 2013 18:16:16 +0000 Subject: [PATCH] * Activated worker so we can automatically remove certain services (those assigned but not in use). To do this, the os Manager must inform if it can "process" unused machines (assigned but not in use). If the os manager wants this, there is a background worker, that works every 10 minutes by default,that will pass every service in that state since more than the last execution of the background worker. With this, for example, linux and windows os managers, will set this if we mark the action on logout as "remove", and every machine assigned and not in use, for a os manager marked as "remove", will be removed automatically. Has to test this extensively!!! :-) --- .../src/uds/core/workers/AssignedAndUnused.py | 4 ++-- .../LinuxOsManager/LinuxOsManager.py | 20 +++++++++++++++++-- .../WindowsOsManager/WindowsOsManager.py | 15 ++++++++++++++ 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/server/src/uds/core/workers/AssignedAndUnused.py b/server/src/uds/core/workers/AssignedAndUnused.py index 64635789..05696cd1 100644 --- a/server/src/uds/core/workers/AssignedAndUnused.py +++ b/server/src/uds/core/workers/AssignedAndUnused.py @@ -40,7 +40,7 @@ import logging logger = logging.getLogger(__name__) -class AssignedAndUnused(object): # When derived from Job, it will be auto-registered +class AssignedAndUnused(Job): frecuency = GlobalConfig.CHECK_UNUSED_TIME.getInt() friendly_name = 'Unused services checker' @@ -52,7 +52,7 @@ class AssignedAndUnused(object): # When derived from Job, it will be auto-regist osm = ds.osmanager.getInstance() if osm.processUnusedMachines is True: logger.debug('Processing unused services for {0}'.format(osm)) - since_state = getSqlDatetime() - timedelta( seconds = GlobalConfig.CHECK_UNUSED_TIME.getInt() / 2 ) + since_state = getSqlDatetime() - timedelta( seconds = GlobalConfig.CHECK_UNUSED_TIME.getInt() ) for us in ds.assignedUserServices().select_for_update().filter(in_use=False,since_state__lt=since_state): logger.debug('Found unused assigned service {0}'.format(us)) osm.processUnused(us) \ No newline at end of file diff --git a/server/src/uds/osmanagers/LinuxOsManager/LinuxOsManager.py b/server/src/uds/osmanagers/LinuxOsManager/LinuxOsManager.py index d914a02f..d14d2f15 100644 --- a/server/src/uds/osmanagers/LinuxOsManager/LinuxOsManager.py +++ b/server/src/uds/osmanagers/LinuxOsManager/LinuxOsManager.py @@ -45,20 +45,26 @@ class LinuxOsManager(osmanagers.OSManager): typeName = _('Linux OS Manager') typeType = 'LinuxManager' typeDescription = _('Os Manager to control linux virtual machines (basically renames machine and notify state)') - iconFile = 'losmanager.png' + iconFile = 'losmanager.png' + + onLogout = gui.ChoiceField( label = _('On Logout'), order = 10, rdonly = False, tooltip = _('What to do when user logout from service'), values = [ {'id' : 'keep', 'text' : _('Keep service assigned') }, {'id' : 'remove', 'text' : _('Remove service') } ], defvalue = 'keep' ) + def __setProcessUnusedMachines(self): + self.processUnusedMachines = self._onLogout == 'remove' + def __init__(self,environment, values): super(LinuxOsManager, self).__init__(environment, values) if values is not None: self._onLogout = values['onLogout'] else: self._onLogout = '' - + + self.__setProcessUnusedMachines() def release(self, service): pass @@ -144,6 +150,15 @@ class LinuxOsManager(osmanagers.OSManager): logger.debug('Returning {0}'.format(ret)) return ret + def processUnused(self, userService): + ''' + 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 + ''' + if self._onLogout == 'remove': + userService.remove() + + def checkState(self,service): logger.debug('Checking state for service {0}'.format(service)) return State.RUNNING @@ -158,6 +173,7 @@ class LinuxOsManager(osmanagers.OSManager): data = s.split('\t') if data[0] == 'v1': self._onLogout = data[1] + self.__setProcessUnusedMachines() def valuesDict(self): return { 'onLogout' : self._onLogout } diff --git a/server/src/uds/osmanagers/WindowsOsManager/WindowsOsManager.py b/server/src/uds/osmanagers/WindowsOsManager/WindowsOsManager.py index 6ef26692..6f415e88 100644 --- a/server/src/uds/osmanagers/WindowsOsManager/WindowsOsManager.py +++ b/server/src/uds/osmanagers/WindowsOsManager/WindowsOsManager.py @@ -55,12 +55,17 @@ class WindowsOsManager(osmanagers.OSManager): raise osmanagers.OSManager.ValidationException(_('Length must be betwen 1 and six')) return len + def __setProcessUnusedMachines(self): + self.processUnusedMachines = self._onLogout == 'remove' + def __init__(self,environment, values): super(WindowsOsManager, self).__init__(environment, values) if values is not None: self._onLogout = values['onLogout'] else: self._onLogout = '' + + self.__setProcessUnusedMachines() def release(self, service): pass @@ -161,6 +166,14 @@ class WindowsOsManager(osmanagers.OSManager): logger.debug('Returning {0}'.format(ret)) return scrambleMsg(ret) + def processUnused(self, userService): + ''' + 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 + ''' + if self._onLogout == 'remove': + userService.remove() + def checkState(self,service): logger.debug('Checking state for service {0}'.format(service)) return State.RUNNING @@ -175,6 +188,8 @@ class WindowsOsManager(osmanagers.OSManager): data = s.split('\t') if data[0] == 'v1': self._onLogout = data[1] + + self.__setProcessUnusedMachines() def valuesDict(self): return { 'onLogout' : self._onLogout }