From 8cf6815e8eb5d26ead681cad0c84e74f3560b5d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20G=C3=B3mez=20Garc=C3=ADa?= Date: Tue, 2 Dec 2014 19:39:26 +0100 Subject: [PATCH] * Added comm url as property * Moved values from storage() to properties table * Added support for idle times to Windows & Linux OS Managers * Fixed configurations objects, so if get() is invoked before DB is accesible, returns default value and, later, will be executed "in fact" --- server/src/uds/REST/methods/actor.py | 4 +-- server/src/uds/REST/methods/osmanagers.py | 3 ++- server/src/uds/core/util/Config.py | 19 +++++++++++--- .../migrations/0005_userservice_comms_url.py | 20 -------------- .../migrations/0006_add_user_parent_uuid.py | 3 +-- server/src/uds/models/UserService.py | 26 ++++++++++++------- .../LinuxOsManager/LinuxOsManager.py | 4 +-- .../WindowsOsManager/WindowsOsManager.py | 6 ++--- .../osmanagers/WindowsOsManager/__init__.py | 6 ++--- 9 files changed, 45 insertions(+), 46 deletions(-) delete mode 100644 server/src/uds/migrations/0005_userservice_comms_url.py diff --git a/server/src/uds/REST/methods/actor.py b/server/src/uds/REST/methods/actor.py index 4adc3f8c9..a10b176c5 100644 --- a/server/src/uds/REST/methods/actor.py +++ b/server/src/uds/REST/methods/actor.py @@ -141,6 +141,7 @@ class Actor(Handler): maxIdle = None if service.deployed_service.osmanager is not None: maxIdle = service.deployed_service.osmanager.getInstance().maxIdle() + logger.debug('Max idle: {}'.format(maxIdle)) return Actor.result((service.uuid, service.unique_id, 0 if maxIdle is None else maxIdle)) raise RequestError('Invalid request') @@ -169,8 +170,7 @@ class Actor(Handler): username = '' if message == 'notifyComms': - service.comms_url = data - service.save() + service.setProperty('comms_url', data) return Actor.result('ok') # Preprocess some messages, common to all clients, such as "log" diff --git a/server/src/uds/REST/methods/osmanagers.py b/server/src/uds/REST/methods/osmanagers.py index be801864a..1b95a22f9 100644 --- a/server/src/uds/REST/methods/osmanagers.py +++ b/server/src/uds/REST/methods/osmanagers.py @@ -33,6 +33,7 @@ from __future__ import unicode_literals from django.utils.translation import ugettext, ugettext_lazy as _ +from django.conf import settings from uds.models import OSManager from uds.REST import NotFound, RequestError @@ -77,7 +78,7 @@ class OsManagers(ModelHandler): raise RequestError(ugettext('Can\'t delete an OSManager with deployed services associated')) def checkSave(self, item): - if item.deployedServices.count() > 0: + if item.deployedServices.count() > 0 and settings.DEBUG is False: raise RequestError(ugettext('Can\'t modify an OSManager with deployed services associated')) # Types related diff --git a/server/src/uds/core/util/Config.py b/server/src/uds/core/util/Config.py index 568b6967d..22704a719 100644 --- a/server/src/uds/core/util/Config.py +++ b/server/src/uds/core/util/Config.py @@ -45,6 +45,7 @@ CLUSTER_SECTION = 'Cluster' # For save when initialized saveLater = [] +getLater = [] class Config(object): @@ -74,9 +75,13 @@ class Config(object): def get(self, force=False): # Ensures DB contains configuration values # From Django 1.7, DB can only be accessed AFTER all apps are initialized, curious at least.. :) - if apps.ready is True and GlobalConfig.initDone is False: - logger.debug('Initializing configuration & updating db values') - GlobalConfig.initialize() + if apps.ready is True: + if GlobalConfig.initDone is False: + logger.debug('Initializing configuration & updating db values') + GlobalConfig.initialize() + else: + getLater.append(self) + return self._default try: if force or self._data is None: @@ -108,7 +113,7 @@ class Config(object): logger.error('Value for {0}.{1} is invalid (integer expected)'.format(self._section, self._key)) try: return int(self._default) - except: + except Exception: logger.error('Default value for {0}.{1} is also invalid (integer expected)'.format(self._section, self._key)) return -1 @@ -303,6 +308,12 @@ class GlobalConfig(object): if type(v) is Config._Value: v.get() + for c in getLater: + logger.debug('Get later: {}'.format(c)) + c.get() + + getLater[:] = [] + for c, v in saveLater: logger.debug('Saving delayed value: {}'.format(c)) c.set(v) diff --git a/server/src/uds/migrations/0005_userservice_comms_url.py b/server/src/uds/migrations/0005_userservice_comms_url.py deleted file mode 100644 index c04d576e1..000000000 --- a/server/src/uds/migrations/0005_userservice_comms_url.py +++ /dev/null @@ -1,20 +0,0 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - -from django.db import models, migrations - - -class Migration(migrations.Migration): - - dependencies = [ - ('uds', '0004_auto_20140916_1217'), - ] - - operations = [ - migrations.AddField( - model_name='userservice', - name='comms_url', - field=models.CharField(default=None, max_length=256, null=True, blank=True), - preserve_default=True, - ), - ] diff --git a/server/src/uds/migrations/0006_add_user_parent_uuid.py b/server/src/uds/migrations/0006_add_user_parent_uuid.py index 0581ce225..ab5a9767a 100644 --- a/server/src/uds/migrations/0006_add_user_parent_uuid.py +++ b/server/src/uds/migrations/0006_add_user_parent_uuid.py @@ -10,7 +10,6 @@ def add_parent_uuids(apps, schema_editor): ''' model = apps.get_model("uds", 'User') for m in model.objects.all(): - print m parent = int(m.parent) if parent != -1: try: @@ -44,7 +43,7 @@ def remove_parent_uuids(apps, schema_editor): class Migration(migrations.Migration): dependencies = [ - ('uds', '0005_userservice_comms_url'), + ('uds', '0004_auto_20140916_1217'), ] operations = [ diff --git a/server/src/uds/models/UserService.py b/server/src/uds/models/UserService.py index 6ff8e41b0..5bc342803 100644 --- a/server/src/uds/models/UserService.py +++ b/server/src/uds/models/UserService.py @@ -35,7 +35,7 @@ from __future__ import unicode_literals -__updated__ = '2014-11-24' +__updated__ = '2014-12-02' from django.db import models from django.db.models import signals @@ -92,7 +92,7 @@ class UserService(UUIDModel): # "Secret" url used to communicate (send message) to services # if This is None, communication is not possible # The communication is done using POST via REST & Json - comms_url = models.CharField(max_length=256, default=None, null=True, blank=True) + # comms_url = models.CharField(max_length=256, default=None, null=True, blank=True) # objects = LockingManager() This model is on an innoDb table, so we do not need the locking manager anymore @@ -199,7 +199,8 @@ class UserService(UUIDModel): name: Name of the value to store value: Value of the value to store ''' - self.getEnvironment().storage().put(name, value) + # Store value as a property + self.setProperty(name, value) def recoverValue(self, name): ''' @@ -211,7 +212,13 @@ class UserService(UUIDModel): Returns: Stored value, None if no value was stored ''' - return self.getEnvironment().storage().get(name) + val = self.getProperty(name) + + # To transition between old stor at storage table and new properties table + # If value is found on property, use it, else, try to recover it from storage + if val is None: + val = self.getEnvironment().storage().get(name) + return val def setConnectionSource(self, ip, hostname=''): ''' @@ -403,13 +410,10 @@ class UserService(UUIDModel): res.append({'id': us.id, 'name': usi.getName(), 'transports': us.deployed_service.transports, 'service': us}) return res - def __str__(self): - return "User service {0}, cache_level {1}, user {2}, name {3}, state {4}:{5}".format(self.id, self.cache_level, self.user, self.friendly_name, - State.toString(self.state), State.toString(self.os_state)) - def getProperty(self, propName, default=None): try: - return self.properties.get(name=propName).value + val = self.properties.get(name=propName).value + return val if val is not '' else default # Empty string is null except Exception: return default @@ -418,6 +422,10 @@ class UserService(UUIDModel): prop.value = propValue prop.save() + def __str__(self): + return "User service {0}, cache_level {1}, user {2}, name {3}, state {4}:{5}".format(self.id, self.cache_level, self.user, self.friendly_name, + State.toString(self.state), State.toString(self.os_state)) + @staticmethod def beforeDelete(sender, **kwargs): ''' diff --git a/server/src/uds/osmanagers/LinuxOsManager/LinuxOsManager.py b/server/src/uds/osmanagers/LinuxOsManager/LinuxOsManager.py index 5c2f68338..2938427bc 100644 --- a/server/src/uds/osmanagers/LinuxOsManager/LinuxOsManager.py +++ b/server/src/uds/osmanagers/LinuxOsManager/LinuxOsManager.py @@ -63,7 +63,7 @@ class LinuxOsManager(osmanagers.OSManager): defvalue='keep') idle = gui.NumericField(label=_("Max.Idle time"), length=4, defvalue=-1, rdonly=False, order=11, - tooltip=_('Maximum idle time (in seconds) before session is automaticatlly closed to the user (<= 0 means no max idle time). Note that this value only applies to "removable" services'), required=True) + tooltip=_('Maximum idle time (in seconds) before session is automaticatlly closed to the user (<= 0 means no max idle time).'), required=True) def __setProcessUnusedMachines(self): self.processUnusedMachines = self._onLogout == 'remove' @@ -188,7 +188,7 @@ class LinuxOsManager(osmanagers.OSManager): return State.RUNNING def maxIdle(self): - if self._onLogout == 'remove' or self._idle <= 0: + if self._idle <= 0: return None return self._idle diff --git a/server/src/uds/osmanagers/WindowsOsManager/WindowsOsManager.py b/server/src/uds/osmanagers/WindowsOsManager/WindowsOsManager.py index 99f8498a7..485242896 100644 --- a/server/src/uds/osmanagers/WindowsOsManager/WindowsOsManager.py +++ b/server/src/uds/osmanagers/WindowsOsManager/WindowsOsManager.py @@ -56,7 +56,7 @@ class WindowsOsManager(osmanagers.OSManager): ) idle = gui.NumericField(label=_("Max.Idle time"), length=4, defvalue=-1, rdonly=False, order=11, - tooltip=_('Maximum idle time (in seconds) before session is automaticatlly closed to the user (<= 0 means no max idle time). Note that this value only applies to "removable" services'), required=True) + tooltip=_('Maximum idle time (in seconds) before session is automaticatlly closed to the user (<= 0 means no max idle time).'), required=True) @staticmethod def validateLen(length): @@ -112,7 +112,7 @@ class WindowsOsManager(osmanagers.OSManager): msg, level = data.split('\t') log.doLog(service, int(level), msg, origin) - except: + except Exception: log.doLog(service, log.ERROR, "do not understand {0}".format(data), origin) def process(self, service, msg, data): @@ -195,7 +195,7 @@ class WindowsOsManager(osmanagers.OSManager): return State.RUNNING def maxIdle(self): - if self._onLogout == 'remove' or self._idle <= 0: + if self._idle <= 0: return None return self._idle diff --git a/server/src/uds/osmanagers/WindowsOsManager/__init__.py b/server/src/uds/osmanagers/WindowsOsManager/__init__.py index e8671928a..51a8cc2fd 100644 --- a/server/src/uds/osmanagers/WindowsOsManager/__init__.py +++ b/server/src/uds/osmanagers/WindowsOsManager/__init__.py @@ -12,9 +12,9 @@ from django.utils.translation import ugettext_noop as _ from uds.core.osmanagers.OSManagersFactory import OSManagersFactory from uds.core.managers.DownloadsManager import DownloadsManager -from WindowsOsManager import WindowsOsManager -from WinDomainOsManager import WinDomainOsManager -from WinRandomPassOsManager import WinRandomPassManager +from .WindowsOsManager import WindowsOsManager +from .WinDomainOsManager import WinDomainOsManager +from .WinRandomPassOsManager import WinRandomPassManager import os.path import sys