From beb09115c20774770a379f3dfcdc1afb33c9f818 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20G=C3=B3mez=20Garc=C3=ADa?= Date: Fri, 16 Aug 2019 11:26:48 +0200 Subject: [PATCH] Refactoring manager filenames & some fixes for python 3.7 conversion --- server/src/uds/REST/methods/actor.py | 7 +++---- server/src/uds/core/managers/__init__.py | 8 ++++---- .../managers/{StatsManager.py => stats.py} | 0 .../core/managers/{TaskManager.py => task.py} | 0 server/src/uds/core/osmanagers/osmanager.py | 7 ------- server/src/uds/core/workers/__init__.py | 4 ++-- .../src/uds/management/commands/taskManager.py | 4 ++-- .../WindowsOsManager/WindowsOsManager.py | 18 ++++++++++++------ .../src/uds/services/OpenNebula/on/__init__.py | 18 ++++-------------- .../src/uds/services/OpenNebula/on/common.py | 8 ++++++-- 10 files changed, 33 insertions(+), 41 deletions(-) rename server/src/uds/core/managers/{StatsManager.py => stats.py} (100%) rename server/src/uds/core/managers/{TaskManager.py => task.py} (100%) diff --git a/server/src/uds/REST/methods/actor.py b/server/src/uds/REST/methods/actor.py index 42d3f395..3a80794b 100644 --- a/server/src/uds/REST/methods/actor.py +++ b/server/src/uds/REST/methods/actor.py @@ -209,11 +209,11 @@ class Actor(Handler): logger.debug('Setting comms url to %s', data) service.setCommsUrl(data) return Actor.result('ok') - elif message == 'ssoAvailable': + if message == 'ssoAvailable': logger.debug('Setting that SSO is available') service.setProperty('sso_available', 1) return Actor.result('ok') - elif message == 'version': + if message == 'version': version = self._params.get('version', 'unknown') logger.debug('Got notified version %s', version) service.setProperty('actor_version', version) @@ -237,8 +237,7 @@ class Actor(Handler): service.release() # Release for removal return 'ok' raise Exception('Unknown message {} for an user service without os manager'.format(message)) - else: - res = osmanager.process(service, message, data, options={'scramble': False}) + res = osmanager.process(service, message, data, options={'scramble': False}) except Exception as e: logger.exception("Exception processing from OS Manager") return Actor.result(six.text_type(e), ERR_OSMANAGER_ERROR) diff --git a/server/src/uds/core/managers/__init__.py b/server/src/uds/core/managers/__init__.py index 71b0ffcd..83057846 100644 --- a/server/src/uds/core/managers/__init__.py +++ b/server/src/uds/core/managers/__init__.py @@ -37,10 +37,10 @@ import typing # Imports for type checking only (not on runtime), we have later to get rid of false "redefined outer names" for pylint if typing.TYPE_CHECKING: from .crypto import CryptoManager - from .TaskManager import TaskManager + from .task import TaskManager from .downloads import DownloadsManager from .log import LogManager - from .StatsManager import StatsManager + from .stats import StatsManager from .UserServiceManager import UserServiceManager from .publication import PublicationManager @@ -50,7 +50,7 @@ def cryptoManager() -> 'CryptoManager': def taskManager() -> typing.Type['TaskManager']: - from .TaskManager import TaskManager # pylint: disable=redefined-outer-name + from .task import TaskManager # pylint: disable=redefined-outer-name return TaskManager @@ -65,7 +65,7 @@ def logManager() -> 'LogManager': def statsManager() -> 'StatsManager': - from .StatsManager import StatsManager # pylint: disable=redefined-outer-name + from .stats import StatsManager # pylint: disable=redefined-outer-name return StatsManager.manager() diff --git a/server/src/uds/core/managers/StatsManager.py b/server/src/uds/core/managers/stats.py similarity index 100% rename from server/src/uds/core/managers/StatsManager.py rename to server/src/uds/core/managers/stats.py diff --git a/server/src/uds/core/managers/TaskManager.py b/server/src/uds/core/managers/task.py similarity index 100% rename from server/src/uds/core/managers/TaskManager.py rename to server/src/uds/core/managers/task.py diff --git a/server/src/uds/core/osmanagers/osmanager.py b/server/src/uds/core/osmanagers/osmanager.py index b125dcfc..81c34a88 100644 --- a/server/src/uds/core/osmanagers/osmanager.py +++ b/server/src/uds/core/osmanagers/osmanager.py @@ -131,13 +131,6 @@ class OSManager(Module): """ return None - 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 - """ - return None - @classmethod def transformsUserOrPasswordForService(cls: typing.Type['OSManager']) -> bool: """ diff --git a/server/src/uds/core/workers/__init__.py b/server/src/uds/core/workers/__init__.py index 936f9c0e..a22aab17 100644 --- a/server/src/uds/core/workers/__init__.py +++ b/server/src/uds/core/workers/__init__.py @@ -44,7 +44,7 @@ def initialize(): import pkgutil import sys from uds.core import jobs - from uds.core.managers.TaskManager import TaskManager + from uds.core.managers import taskManager # Dinamycally import children of this package. pkgpath = os.path.dirname(sys.modules[__name__].__file__) @@ -59,4 +59,4 @@ def initialize(): # Limit to autoregister just workers jobs inside this module if cls.__module__[0:16] == 'uds.core.workers': logger.debug('Added worker %s to list', cls.__module__) - TaskManager.registerJob(cls) + taskManager().registerJob(cls) diff --git a/server/src/uds/management/commands/taskManager.py b/server/src/uds/management/commands/taskManager.py index 51cec13c..dd09bfec 100644 --- a/server/src/uds/management/commands/taskManager.py +++ b/server/src/uds/management/commands/taskManager.py @@ -40,7 +40,7 @@ import time from django.core.management.base import BaseCommand # , CommandError from django.conf import settings -from uds.core.managers.TaskManager import TaskManager +from uds.core.managers import taskManager from uds.core.util.Config import GlobalConfig logger = logging.getLogger(__name__) @@ -137,7 +137,7 @@ class Command(BaseCommand): open(getPidFile(), 'w+').write('{}\n'.format(pid)) - manager = TaskManager() + manager = taskManager()() manager.run() if start is False and stop is False: diff --git a/server/src/uds/osmanagers/WindowsOsManager/WindowsOsManager.py b/server/src/uds/osmanagers/WindowsOsManager/WindowsOsManager.py index 78ac4ab5..8762c9d1 100644 --- a/server/src/uds/osmanagers/WindowsOsManager/WindowsOsManager.py +++ b/server/src/uds/osmanagers/WindowsOsManager/WindowsOsManager.py @@ -8,16 +8,22 @@ """ @author: Adolfo Gómez, dkmaster at dkmon dot com """ +import logging +import typing + from django.utils.translation import ugettext_noop as _, ugettext_lazy from uds.core.services import types as serviceTypes from uds.core.ui import gui from uds.core import osmanagers -from uds.core.managers.UserServiceManager import UserServiceManager +from uds.core.managers import userServiceManager from uds.core.util.State import State from uds.core.util import log from uds.models import TicketStore -import logging +# Not imported in runtime, just for type checking +if typing.TYPE_CHECKING: + from uds.models import UserService + logger = logging.getLogger(__name__) @@ -148,7 +154,7 @@ class WindowsOsManager(osmanagers.OSManager): def readyReceived(self, userService, data): pass - def process(self, userService, msg, data, options=None): + def process(self, userService: 'UserService', msg, data, options=None): """ We understand this messages: * msg = info, data = None. Get information about name of machine (or domain, in derived WinDomainOsManager class) (old method) @@ -186,11 +192,11 @@ class WindowsOsManager(osmanagers.OSManager): # We get the userService logged hostname & ip and returns this ip, hostname = userService.getConnectionSource() deadLine = userService.deployed_service.getDeadline() - if userService.getProperty('actor_version', '0.0.0') >= '2.0.0': + if typing.cast(str, userService.getProperty('actor_version', '0.0.0')) >= '2.0.0': ret = "{0}\t{1}\t{2}".format(ip, hostname, 0 if deadLine is None else deadLine) else: ret = "{0}\t{1}".format(ip, hostname) - elif msg == "logoff" or msg == 'logout': + elif msg in ('logoff', 'logout'): self.loggedOut(userService, data) doRemove = self.isRemovableOnLogout(userService) elif msg == "ip": @@ -214,7 +220,7 @@ class WindowsOsManager(osmanagers.OSManager): userService.save(update_fields=['in_use', 'in_use_date', 'os_state', 'state', 'data']) else: logger.debug('Notifying ready') - UserServiceManager.manager().notifyReadyFromOsManager(userService, '') + userServiceManager().notifyReadyFromOsManager(userService, '') logger.debug('Returning {} to {} message'.format(ret, msg)) if options is not None and options.get('scramble', True) is False: return ret diff --git a/server/src/uds/services/OpenNebula/on/__init__.py b/server/src/uds/services/OpenNebula/on/__init__.py index baeaca45..8115538a 100644 --- a/server/src/uds/services/OpenNebula/on/__init__.py +++ b/server/src/uds/services/OpenNebula/on/__init__.py @@ -34,29 +34,19 @@ # pylint: disable=maybe-no-member import types import xmlrpc.client +import logging from uds.core.util import xml2dict from . import storage from . import template from . import vm -# Import submodules -from .common import * -__updated__ = '2017-03-28' +# Import submodules +from .common import VmState, ImageState, sanitizeName logger = logging.getLogger(__name__) -module = sys.modules[__name__] -VmState = types.ModuleType('VmState') -ImageState = types.ModuleType('ImageState') - -for i in enumerate(['INIT', 'PENDING', 'HOLD', 'ACTIVE', 'STOPPED', 'SUSPENDED', 'DONE', 'FAILED', 'POWEROFF', 'UNDEPLOYED']): - setattr(VmState, i[1], i[0]) - -for i in enumerate(['INIT', 'READY', 'USED', 'DISABLED', 'LOCKED', 'ERROR', 'CLONE', 'DELETE', 'USED_PERS', 'LOCKED_USED', 'LOCKED_USED_PERS']): - setattr(ImageState, i[1], i[0]) - # Decorator def ensureConnected(fnc): @@ -83,7 +73,7 @@ def asList(element): # noinspection PyShadowingNames -class OpenNebulaClient(object): +class OpenNebulaClient: def __init__(self, username, password, endpoint): self.username = username self.password = password diff --git a/server/src/uds/services/OpenNebula/on/common.py b/server/src/uds/services/OpenNebula/on/common.py index 52697687..7fcb6707 100644 --- a/server/src/uds/services/OpenNebula/on/common.py +++ b/server/src/uds/services/OpenNebula/on/common.py @@ -31,7 +31,7 @@ .. moduleauthor:: Adolfo Gómez, dkmaster at dkmon dot com """ -import sys +# import sys import types import re @@ -39,12 +39,16 @@ import logging logger = logging.getLogger(__name__) -module = sys.modules[__name__] +# module = sys.modules[__name__] VmState = types.ModuleType('VmState') +ImageState = types.ModuleType('ImageState') for i in enumerate(['INIT', 'PENDING', 'HOLD', 'ACTIVE', 'STOPPED', 'SUSPENDED', 'DONE', 'FAILED', 'POWEROFF', 'UNDEPLOYED', 'UNKNOWN']): setattr(VmState, i[1], i[0]) +for i in enumerate(['INIT', 'READY', 'USED', 'DISABLED', 'LOCKED', 'ERROR', 'CLONE', 'DELETE', 'USED_PERS', 'LOCKED_USED', 'LOCKED_USED_PERS']): + setattr(ImageState, i[1], i[0]) + def sanitizeName(name): """