1
0
mirror of https://github.com/dkmstr/openuds.git synced 2024-12-22 13:34:04 +03:00

Refactoring uds.core, removing nonsense imports that creates circular dependencies problems

This commit is contained in:
Adolfo Gómez García 2023-04-05 14:44:41 +02:00
parent 79b350486a
commit 831db0acf3
No known key found for this signature in database
GPG Key ID: DD1ABF20724CDA23
45 changed files with 119 additions and 74 deletions

View File

@ -234,6 +234,7 @@ WORKERSFILE = 'workers.log'
AUTHFILE = 'auth.log'
USEFILE = 'use.log'
TRACEFILE = 'trace.log'
OPERATIONSFILE = 'operations.log'
LOGLEVEL = DEBUG and 'DEBUG' or 'INFO'
ROTATINGSIZE = 32 * 1024 * 1024 # 32 Megabytes before rotating files
@ -246,11 +247,20 @@ LOGGING = {
}
},
'formatters': {
'verbose': {
'database': {
'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
},
'simple': {
'format': '%(levelname)s %(asctime)s %(module)s %(funcName)s %(lineno)d %(message)s'
'format': '%(levelname)s %(asctime)s %(name)s:%(funcName)s %(lineno)d %(message)s'
},
'uds': {
'format': 'uds[%(process)-5s]: %(levelname)s %(asctime)s %(name)s:%(funcName)s %(lineno)d %(message)s'
},
'services': {
'format': 'uds-s[%(process)-5s]: %(levelname)s %(asctime)s %(name)s:%(funcName)s %(lineno)d %(message)s'
},
'workers': {
'format': 'uds-w[%(process)-5s]: %(levelname)s %(asctime)s %(name)s:%(funcName)s %(lineno)d %(message)s'
},
'database': {'format': '%(levelname)s %(asctime)s Database %(message)s'},
'auth': {'format': '%(asctime)s %(message)s'},
@ -262,6 +272,14 @@ LOGGING = {
'level': 'DEBUG',
'class': 'logging.NullHandler',
},
# Sample logging to syslog
#'file': {
# 'level': 'DEBUG',
# 'class': 'logging.handlers.SysLogHandler',
# 'formatter': 'uds',
# 'facility': 'local0',
# 'address': '/dev/log',
#},
'file': {
'level': 'DEBUG',
'class': 'logging.handlers.RotatingFileHandler',
@ -275,7 +293,7 @@ LOGGING = {
'database': {
'level': 'DEBUG',
'class': 'logging.handlers.RotatingFileHandler',
'formatter': 'verbose',
'formatter': 'database',
'filename': LOGDIR + '/' + 'sql.log',
'mode': 'a',
'maxBytes': ROTATINGSIZE,
@ -332,6 +350,16 @@ LOGGING = {
'backupCount': 3,
'encoding': 'utf-8',
},
'operationsFile': {
'level': 'DEBUG',
'class': 'logging.handlers.RotatingFileHandler',
'formatter': 'trace',
'filename': LOGDIR + '/' + OPERATIONSFILE,
'mode': 'a',
'maxBytes': ROTATINGSIZE,
'backupCount': 3,
'encoding': 'utf-8',
},
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
@ -374,6 +402,12 @@ LOGGING = {
'propagate': True,
'level': 'ERROR',
},
# Disable matplotlib (used by reports) logging (too verbose)
'matplotlib': {
'handlers': ['null'],
'propagate': True,
'level': 'ERROR',
},
'uds': {
'handlers': ['file'],
'level': LOGLEVEL,
@ -412,5 +446,11 @@ LOGGING = {
'level': 'INFO',
'propagate': False,
},
# Custom operations
'operationsLog': {
'handlers': ['operationsFile'],
'level': 'INFO',
'propagate': False,
},
},
}

View File

@ -50,7 +50,7 @@ from .users_groups import Users, Groups
# Not imported at runtime, just for type checking
if typing.TYPE_CHECKING:
from django.db import models
from uds.core import Module
from uds.core.module import Module
logger = logging.getLogger(__name__)

View File

@ -45,7 +45,8 @@ from uds.core.ui import gui as uiGui
from uds.core.util import log
from uds.core.util import permissions
from uds.core.util.model import processUuid
from uds.core import Module, exceptions as g_exceptions
from uds.core.module import Module
from uds.core import exceptions as g_exceptions
from uds.models import Tag, TaggingMixin, ManagedObjectModel, Network

View File

@ -34,11 +34,6 @@ This package contains all core-related code for UDS
"""
import time
# Core needs tasks manager to register scheduled jobs, so we ensure of that here
from .environment import Environmentable
from .serializable import Serializable
from .module import Module
VERSION = '4.x.x-DEVEL'
VERSION_STAMP = '{}-DEVEL'.format(time.strftime("%Y%m%d"))

View File

@ -39,7 +39,7 @@ import typing
from django.utils.translation import gettext_noop as _
from django.urls import reverse
from uds.core import Module
from uds.core.module import Module
# Not imported at runtime, just for type checking
if typing.TYPE_CHECKING:

View File

@ -31,7 +31,7 @@
import logging
import typing
from uds.core import Environmentable
from uds.core.environment import Environmentable
from uds.core.util.config import Config

View File

@ -72,7 +72,7 @@ if typing.TYPE_CHECKING:
logger = logging.getLogger(__name__)
traceLogger = logging.getLogger('traceLog')
operationsLogger = logging.getLogger('operationsLog')
class UserServiceManager(metaclass=singleton.Singleton):
def __init__(self):
@ -126,7 +126,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
return False
if self.getExistingUserServices(service) >= (serviceInstance.maxDeployed or 1):
logger.debug('Maximum number of user services reached for this service: {}'.format(service.name))
operationsLogger.info('Maximum number of user services reached for service: {}'.format(service.name))
return True
return False
@ -200,7 +200,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
"""
Creates a new cache for the deployed service publication at level indicated
"""
logger.info(
operationsLogger.info(
'Creating a new cache element at level %s for publication %s',
cacheLevel,
publication,
@ -219,20 +219,21 @@ class UserServiceManager(metaclass=singleton.Singleton):
# First, honor maxPreparingServices
if self.canGrowServicePool(servicePool) is False:
# Cannot create new
logger.info(
operationsLogger.info(
'Too many preparing services. Creation of assigned service denied by max preparing services parameter. (login storm with insufficient cache?).'
)
raise ServiceNotReadyError()
if servicePool.service.getType().publicationType is not None:
publication = servicePool.activePublication()
logger.info(
'Creating a new assigned element for user %s por publication %s',
user,
publication,
)
if publication:
assigned = self._createAssignedAtDb(publication, user)
operationsLogger.info(
'Creating a new assigned element for user %s for publication %s on pool %s',
user.pretty_name,
publication.revision,
servicePool.name,
)
else:
raise Exception(
'Invalid publication creating service assignation: {} {}'.format(
@ -240,7 +241,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
)
)
else:
logger.debug('Creating a new assigned element for user %s', user)
operationsLogger.info('Creating a new assigned element for user %s on pool %s', user.pretty_name, servicePool.name)
assigned = self._createAssignedAtDbForNoPublication(servicePool, user)
assignedInstance = assigned.getInstance()
@ -262,14 +263,15 @@ class UserServiceManager(metaclass=singleton.Singleton):
if servicePool.service.getType().publicationType is not None:
publication = servicePool.activePublication()
logger.debug(
'Creating an assigned element from assignable %s for user %s por publication %s',
user,
assignableId,
publication,
)
if publication:
assigned = self._createAssignedAtDb(publication, user)
operationsLogger.info(
'Creating an assigned element from assignable %s for user %s for publication %s on pool %s',
assignableId,
user.pretty_name,
publication.revision,
servicePool.name,
)
else:
raise Exception(
'Invalid publication creating service assignation: {} {}'.format(
@ -277,10 +279,11 @@ class UserServiceManager(metaclass=singleton.Singleton):
)
)
else:
logger.debug(
'Creating an assigned element from assignable %s for user %s',
operationsLogger.info(
'Creating an assigned element from assignable %s for user %s on pool %s',
assignableId,
user,
user.pretty_name,
servicePool.name,
)
assigned = self._createAssignedAtDbForNoPublication(servicePool, user)
@ -324,14 +327,14 @@ class UserServiceManager(metaclass=singleton.Singleton):
@return: the Uservice canceling
"""
userService.refresh_from_db()
logger.info('Canceling userService %s creation', userService)
if userService.isPreparing() is False:
logger.info(
logger.debug(
'Cancel requested for a non running operation, performing removal instead'
)
return self.remove(userService)
operationsLogger.info('Canceling userService %s', userService.name)
userServiceInstance = userService.getInstance()
if (
@ -357,7 +360,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
"""
with transaction.atomic():
userService = UserService.objects.select_for_update().get(id=userService.id)
logger.info('Removing userService %a', userService)
operationsLogger.info('Removing userService %a', userService.name)
if (
userService.isUsable() is False
and State.isRemovable(userService.state) is False
@ -643,7 +646,7 @@ class UserServiceManager(metaclass=singleton.Singleton):
if not userService.deployed_service.service.getType().canReset:
return
logger.debug('Reseting %s', userService)
operationsLogger.info('Reseting %s', userService)
userServiceInstance = userService.getInstance()
try:

View File

@ -33,7 +33,7 @@ import enum
from django.utils.translation import gettext_noop as _
from uds.core import Module
from uds.core.module import Module
if typing.TYPE_CHECKING:
from uds.core.environment import Environment

View File

@ -38,7 +38,7 @@ import logging
import typing
from django.utils.translation import gettext_noop as _
from uds.core import Module
from uds.core.module import Module
from uds.models.util import getSqlDatetime
from uds.core.auths import exceptions

View File

@ -38,7 +38,7 @@ from uds.core.util.state import State
from uds.core.util.stats.events import addEvent, ET_LOGIN, ET_LOGOUT
from uds.core.util import log
from uds.core.util.config import GlobalConfig
from uds.core import Module
from uds.core.module import Module
STORAGE_KEY = 'osmk'

View File

@ -33,7 +33,7 @@
import logging
import typing
from uds.core import Module
from uds.core.module import Module
from uds.core.environment import Environment
from uds.core.util import log

View File

@ -32,8 +32,8 @@
"""
import typing
from uds.core import Environmentable
from uds.core import Serializable
from uds.core.environment import Environmentable
from uds.core.serializable import Serializable
if typing.TYPE_CHECKING:
from uds.core import services

View File

@ -35,7 +35,7 @@ import typing
import logging
from django.utils.translation import gettext_noop as _
from uds.core import Module
from uds.core.module import Module
from uds.core.transports import protocols
from uds.core.util.state import State
from uds.core.util import log

View File

@ -32,8 +32,8 @@
"""
import typing
from uds.core import Environmentable
from uds.core import Serializable
from uds.core.environment import Environmentable
from uds.core.serializable import Serializable
from uds.core.util.state import State
from uds.core.util import log

View File

@ -40,7 +40,7 @@ import typing
from django.utils.translation import gettext_noop as _
from uds.core.util import os_detector as OsDetector
from uds.core import Module
from uds.core.module import Module
from uds.core.transports import protocols
from uds.core.util import net

View File

@ -31,6 +31,7 @@
@author: Adolfo Gómez, dkmaster at dkmon dot com
"""
import logging
import logging.handlers
import typing
from uds.core.managers import logManager
@ -171,3 +172,8 @@ def clearLogs(wichObject: 'Model') -> None:
Clears the logs associated with the object using the logManager
"""
return logManager().clearLogs(wichObject)
class UDSLogHandler(logging.handlers.RotatingFileHandler):
def emit(self, record: logging.LogRecord) -> None:
# Currently, simply call to parent
return super().emit(record)

View File

@ -36,7 +36,7 @@ import typing
from django.db import models
from uds.core.environment import Environment
from uds.core import Module
from uds.core.module import Module
from .uuid_model import UUIDModel

View File

@ -46,7 +46,7 @@ from uds.core.util import validators
# Not imported at runtime, just for type checking
if typing.TYPE_CHECKING:
from uds.core import Module
from uds.core.module import Module
logger = logging.getLogger(__name__)

View File

@ -49,7 +49,7 @@ from .windows import WindowsOsManager
# Not imported at runtime, just for type checking
if typing.TYPE_CHECKING:
from uds.core import Module
from uds.core.module import Module
from uds.core.environment import Environment
from uds.models import UserService

View File

@ -47,7 +47,7 @@ from .windows import WindowsOsManager
# Not imported at runtime, just for type checking
if typing.TYPE_CHECKING:
from uds.core import Module
from uds.core.module import Module
from uds.core.environment import Environment
from uds.models import UserService

View File

@ -44,7 +44,7 @@ from .service import OVirtLinkedService
# Not imported at runtime, just for type checking
if typing.TYPE_CHECKING:
from uds.core import Module
from uds.core.module import Module
from uds.core.environment import Environment
logger = logging.getLogger(__name__)

View File

@ -48,7 +48,7 @@ from . import helpers
# Not imported at runtime, just for type checking
if typing.TYPE_CHECKING:
from .provider import OVirtProvider
from uds.core import Module
from uds.core.module import Module
logger = logging.getLogger(__name__)

View File

@ -44,7 +44,7 @@ from .service import OGService
# Not imported at runtime, just for type checking
if typing.TYPE_CHECKING:
from uds.core import Module
from uds.core.module import Module
logger = logging.getLogger(__name__)

View File

@ -47,7 +47,7 @@ from . import og
# Not imported at runtime, just for type checking
if typing.TYPE_CHECKING:
from uds.core import Module
from uds.core.module import Module
from uds.core.environment import Environment
logger = logging.getLogger(__name__)

View File

@ -45,7 +45,7 @@ from .publication import OGPublication
# Not imported at runtime, just for type checking
if typing.TYPE_CHECKING:
from .provider import OGProvider
from uds.core import Module
from uds.core.module import Module
logger = logging.getLogger(__name__)

View File

@ -45,7 +45,7 @@ from .service import LiveService
# Not imported at runtime, just for type checking
if typing.TYPE_CHECKING:
from uds.core import Module
from uds.core.module import Module
from uds.core.environment import Environment
logger = logging.getLogger(__name__)

View File

@ -46,7 +46,7 @@ from .deployment import LiveDeployment
if typing.TYPE_CHECKING:
from . import on
from .provider import OpenNebulaProvider
from uds.core import Module
from uds.core.module import Module
logger = logging.getLogger(__name__)

View File

@ -45,7 +45,7 @@ from .service import LiveService
# Not imported at runtime, just for type checking
if typing.TYPE_CHECKING:
from uds.core import Module
from uds.core.module import Module
logger = logging.getLogger(__name__)

View File

@ -47,7 +47,7 @@ from .service import LiveService
# Not imported at runtime, just for type checking
if typing.TYPE_CHECKING:
from uds.core import Module
from uds.core.module import Module
logger = logging.getLogger(__name__)

View File

@ -49,7 +49,7 @@ from .service_base import IPServiceBase
# Not imported at runtime, just for type checking
if typing.TYPE_CHECKING:
from uds import models
from uds.core import Module
from uds.core.module import Module
logger = logging.getLogger(__name__)

View File

@ -44,7 +44,7 @@ from .service_base import IPServiceBase
# Not imported at runtime, just for type checking
if typing.TYPE_CHECKING:
from uds.core import Module
from uds.core.module import Module
logger = logging.getLogger(__name__)

View File

@ -46,7 +46,7 @@ from . import client
# Not imported at runtime, just for type checking
if typing.TYPE_CHECKING:
from uds.core import Module
from uds.core.module import Module
from uds.core.environment import Environment
logger = logging.getLogger(__name__)

View File

@ -47,7 +47,7 @@ from .publication import ProxmoxPublication
# Not imported at runtime, just for type checking
if typing.TYPE_CHECKING:
from uds.core import Module
from uds.core.module import Module
from . import client
from .provider import ProxmoxProvider

View File

@ -43,7 +43,7 @@ from .service import ServiceOne, ServiceTwo
# Not imported at runtime, just for type checking
if typing.TYPE_CHECKING:
from uds.core import Module
from uds.core.module import Module
from uds.core.environment import Environment
logger = logging.getLogger(__name__)

View File

@ -44,7 +44,7 @@ from .deployment_two import SampleUserDeploymentTwo
# Not imported at runtime, just for type checking
if typing.TYPE_CHECKING:
from uds.core import Module
from uds.core.module import Module
from uds.core.environment import Environment
logger = logging.getLogger(__name__)

View File

@ -49,7 +49,7 @@ logger = logging.getLogger(__name__)
# Not imported at runtime, just for type checking
if typing.TYPE_CHECKING:
from uds.core import Module
from uds.core.module import Module
from uds.core.environment import Environment
CACHE_TIME_FOR_SERVER = 1800

View File

@ -43,7 +43,7 @@ from .deployment import XenLinkedDeployment
# Not imported at runtime, just for type checking
if typing.TYPE_CHECKING:
from .provider import XenProvider
from uds.core import Module
from uds.core.module import Module
logger = logging.getLogger(__name__)

View File

@ -44,7 +44,7 @@ from uds import models
# Not imported at runtime, just for type checking
if typing.TYPE_CHECKING:
from uds.core import Module
from uds.core.module import Module
from uds.core.util.request import ExtendedHttpRequestWithUser
from uds.core.util.os_detector import DetectedOsInfo

View File

@ -45,7 +45,7 @@ from uds import models
# Not imported at runtime, just for type checking
if typing.TYPE_CHECKING:
from uds.core import Module
from uds.core.module import Module
from uds.core.util.request import ExtendedHttpRequestWithUser
from uds.core.util.os_detector import DetectedOsInfo

View File

@ -43,7 +43,7 @@ from uds import models
# Not imported at runtime, just for type checking
if typing.TYPE_CHECKING:
from uds.core import Module
from uds.core.module import Module
from uds.core.util.request import ExtendedHttpRequestWithUser
from uds.core.util.os_detector import DetectedOsInfo

View File

@ -46,7 +46,7 @@ from .rdp_file import RDPFile
# Not imported at runtime, just for type checking
if typing.TYPE_CHECKING:
from uds import models
from uds.core import Module
from uds.core.module import Module
from uds.core.util.request import ExtendedHttpRequestWithUser
logger = logging.getLogger(__name__)

View File

@ -45,7 +45,7 @@ from .remote_viewer_file import RemoteViewerFile
# Not imported at runtime, just for type checking
if typing.TYPE_CHECKING:
from uds import models
from uds.core import Module
from uds.core.module import Module
from uds.core.util.request import ExtendedHttpRequestWithUser
from uds.core.util import os_detector

View File

@ -42,7 +42,7 @@ from uds import models
# Not imported at runtime, just for type checking
if typing.TYPE_CHECKING:
from uds.core import Module
from uds.core.module import Module
from uds.core.util.request import ExtendedHttpRequestWithUser
from uds.core.util.os_detector import DetectedOsInfo

View File

@ -42,7 +42,7 @@ from uds import models
# Not imported at runtime, just for type checking
if typing.TYPE_CHECKING:
from uds.core import Module
from uds.core.module import Module
from uds.core.util.request import ExtendedHttpRequestWithUser
from uds.core.util.os_detector import DetectedOsInfo

View File

@ -44,7 +44,7 @@ from . import x2go_file
# Not imported at runtime, just for type checking
if typing.TYPE_CHECKING:
from uds import models
from uds.core import Module
from uds.core.module import Module
from uds.core.util.request import ExtendedHttpRequestWithUser
from uds.core.util.os_detector import DetectedOsInfo