mirror of https://github.com/jumpserver/jumpserver
feat: 修改 DBPortManager 处理逻辑
parent
57e12256e7
commit
497a52a509
|
@ -6,7 +6,9 @@ from common.signals import django_ready
|
|||
from django.dispatch import receiver
|
||||
from common.utils import get_logger
|
||||
from .models import Application
|
||||
from .utils import db_port_manager
|
||||
from .utils import db_port_manager, DBPortManager
|
||||
|
||||
db_port_manager: DBPortManager
|
||||
|
||||
|
||||
logger = get_logger(__file__)
|
||||
|
|
|
@ -5,6 +5,7 @@ from applications.const import AppCategory
|
|||
from applications.models import Application
|
||||
from common.utils import get_logger
|
||||
from common.utils import get_object_or_none
|
||||
from orgs.utils import tmp_to_root_org
|
||||
|
||||
|
||||
logger = get_logger(__file__)
|
||||
|
@ -19,9 +20,13 @@ class DBPortManager(object):
|
|||
def __init__(self):
|
||||
self.port_start = settings.MAGNUS_DB_PORTS_START
|
||||
self.port_limit = settings.MAGNUS_DB_PORTS_LIMIT_COUNT
|
||||
self.port_end = self.port_start + self.port_limit + 1
|
||||
self.port_end = self.port_start + self.port_limit
|
||||
# 可以使用的端口列表
|
||||
self.all_usable_ports = [i for i in range(self.port_start, self.port_end)]
|
||||
self.all_usable_ports = [i for i in range(self.port_start, self.port_end+1)]
|
||||
|
||||
@property
|
||||
def magnus_listen_port_range(self):
|
||||
return f'{self.port_start}-{self.port_end}'
|
||||
|
||||
def init(self):
|
||||
db_ids = Application.objects.filter(category=AppCategory.db).values_list('id', flat=True)
|
||||
|
@ -57,6 +62,7 @@ class DBPortManager(object):
|
|||
mapper = self.get_mapper()
|
||||
db_id = mapper.get(port, None)
|
||||
if db_id:
|
||||
with tmp_to_root_org():
|
||||
db = get_object_or_none(Application, id=db_id)
|
||||
if not db:
|
||||
msg = 'Database not exists, database id: {}'.format(db_id)
|
||||
|
@ -69,11 +75,12 @@ class DBPortManager(object):
|
|||
|
||||
def get_next_usable_port(self):
|
||||
already_use_ports = self.get_already_use_ports()
|
||||
usable_ports = list(set(self.all_usable_ports) - set(already_use_ports))
|
||||
usable_ports = sorted(list(set(self.all_usable_ports) - set(already_use_ports)))
|
||||
if len(usable_ports) > 1:
|
||||
return usable_ports[0]
|
||||
port = usable_ports[0]
|
||||
logger.debug('Get next usable port: {}'.format(port))
|
||||
return port
|
||||
|
||||
already_use_ports = self.get_already_use_ports()
|
||||
msg = 'No port is usable, All usable port count: {}, Already use port count: {}'.format(
|
||||
len(self.all_usable_ports), len(already_use_ports)
|
||||
)
|
||||
|
@ -81,7 +88,7 @@ class DBPortManager(object):
|
|||
|
||||
def get_already_use_ports(self):
|
||||
mapper = self.get_mapper()
|
||||
return list(mapper.keys())
|
||||
return sorted(list(mapper.keys()))
|
||||
|
||||
def get_mapper(self):
|
||||
return cache.get(self.CACHE_KEY, {})
|
||||
|
|
|
@ -2,7 +2,7 @@ from rest_framework import serializers
|
|||
from django.utils.translation import ugettext_lazy as _
|
||||
from common.drf.serializers import BulkModelSerializer
|
||||
from acls.serializers.rules import ip_group_child_validator, ip_group_help_text
|
||||
from django.conf import settings
|
||||
from applications.utils import db_port_manager
|
||||
from ..models import Endpoint, EndpointRule
|
||||
|
||||
__all__ = ['EndpointSerializer', 'EndpointRuleSerializer']
|
||||
|
@ -11,7 +11,13 @@ __all__ = ['EndpointSerializer', 'EndpointRuleSerializer']
|
|||
class EndpointSerializer(BulkModelSerializer):
|
||||
# 解决 luna 处理繁琐的问题, 返回 magnus 监听的当前 db 的 port
|
||||
magnus_listen_db_port = serializers.SerializerMethodField(label=_('Magnus listen db port'))
|
||||
magnus_listen_port_range = serializers.SerializerMethodField(label=_('Magnus Listen port range'))
|
||||
magnus_listen_port_range = serializers.CharField(
|
||||
max_length=128, default=db_port_manager.magnus_listen_port_range, read_only=True,
|
||||
label=_('Magnus Listen port range'),
|
||||
help_text=_(
|
||||
'The range of ports that Magnus listens on is modified in the configuration file'
|
||||
)
|
||||
)
|
||||
|
||||
class Meta:
|
||||
model = Endpoint
|
||||
|
@ -19,6 +25,7 @@ class EndpointSerializer(BulkModelSerializer):
|
|||
fields_small = [
|
||||
'host',
|
||||
'https_port', 'http_port', 'ssh_port', 'rdp_port',
|
||||
'magnus_listen_db_port', 'magnus_listen_port_range',
|
||||
]
|
||||
fields = fields_mini + fields_small + [
|
||||
'comment', 'date_created', 'date_updated', 'created_by'
|
||||
|
@ -36,13 +43,6 @@ class EndpointSerializer(BulkModelSerializer):
|
|||
return 0
|
||||
return obj.get_port(view.target_instance, view.target_protocol)
|
||||
|
||||
@staticmethod
|
||||
def get_magnus_listen_port_range(obj: Endpoint):
|
||||
port_start = settings.MAGNUS_DB_PORTS_START
|
||||
port_limit = settings.MAGNUS_DB_PORTS_LIMIT_COUNT
|
||||
port_end = port_start + port_limit + 1
|
||||
return f'{port_start} - {port_end}'
|
||||
|
||||
|
||||
class EndpointRuleSerializer(BulkModelSerializer):
|
||||
_ip_group_help_text = '{} <br> {}'.format(
|
||||
|
|
Loading…
Reference in New Issue