2018-01-11 12:10:27 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2019-01-10 03:50:08 +00:00
|
|
|
import json
|
2021-01-26 09:54:12 +00:00
|
|
|
import threading
|
2019-01-10 03:50:08 +00:00
|
|
|
|
2018-01-11 12:10:27 +00:00
|
|
|
from django.dispatch import receiver
|
2019-03-18 03:34:13 +00:00
|
|
|
from django.db.models.signals import post_save, pre_save
|
2021-01-26 09:54:12 +00:00
|
|
|
from django.utils.functional import LazyObject
|
2021-04-13 10:33:00 +00:00
|
|
|
from django.db import close_old_connections
|
2018-01-11 12:10:27 +00:00
|
|
|
|
2018-09-03 03:24:25 +00:00
|
|
|
from jumpserver.utils import current_request
|
2021-01-26 09:54:12 +00:00
|
|
|
from common.decorator import on_transaction_commit
|
2019-02-26 04:38:20 +00:00
|
|
|
from common.utils import get_logger, ssh_key_gen
|
2021-01-26 09:54:12 +00:00
|
|
|
from common.utils.connection import RedisPubSub
|
2019-02-26 04:38:20 +00:00
|
|
|
from common.signals import django_ready
|
2018-01-11 12:10:27 +00:00
|
|
|
from .models import Setting
|
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
|
|
|
|
2021-01-26 09:54:12 +00:00
|
|
|
def get_settings_pub_sub():
|
|
|
|
return RedisPubSub('settings')
|
|
|
|
|
|
|
|
|
|
|
|
class SettingSubPub(LazyObject):
|
|
|
|
def _setup(self):
|
|
|
|
self._wrapped = get_settings_pub_sub()
|
|
|
|
|
|
|
|
|
|
|
|
setting_pub_sub = SettingSubPub()
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_save, sender=Setting)
|
|
|
|
@on_transaction_commit
|
2018-01-11 12:10:27 +00:00
|
|
|
def refresh_settings_on_changed(sender, instance=None, **kwargs):
|
|
|
|
if instance:
|
2021-01-26 09:54:12 +00:00
|
|
|
setting_pub_sub.publish(instance.name)
|
2018-01-11 12:10:27 +00:00
|
|
|
|
|
|
|
|
2019-09-17 04:34:47 +00:00
|
|
|
@receiver(django_ready)
|
2019-12-05 07:09:25 +00:00
|
|
|
def on_django_ready_add_db_config(sender, **kwargs):
|
2021-01-26 09:54:12 +00:00
|
|
|
Setting.refresh_all_settings()
|
2018-01-12 07:43:26 +00:00
|
|
|
|
|
|
|
|
2019-01-10 03:50:08 +00:00
|
|
|
@receiver(django_ready)
|
|
|
|
def auto_generate_terminal_host_key(sender, **kwargs):
|
2019-01-15 02:23:30 +00:00
|
|
|
try:
|
|
|
|
if Setting.objects.filter(name='TERMINAL_HOST_KEY').exists():
|
|
|
|
return
|
2019-01-29 06:07:46 +00:00
|
|
|
private_key, public_key = ssh_key_gen()
|
|
|
|
value = json.dumps(private_key)
|
|
|
|
Setting.objects.create(name='TERMINAL_HOST_KEY', value=value)
|
2019-01-29 04:27:51 +00:00
|
|
|
except:
|
2019-01-29 06:07:46 +00:00
|
|
|
pass
|
2019-01-10 03:50:08 +00:00
|
|
|
|
|
|
|
|
2018-09-03 03:24:25 +00:00
|
|
|
@receiver(pre_save, dispatch_uid="my_unique_identifier")
|
|
|
|
def on_create_set_created_by(sender, instance=None, **kwargs):
|
2018-12-10 02:11:54 +00:00
|
|
|
if getattr(instance, '_ignore_auto_created_by', False) is True:
|
|
|
|
return
|
2021-01-26 09:54:12 +00:00
|
|
|
if not hasattr(instance, 'created_by') or instance.created_by:
|
|
|
|
return
|
|
|
|
if current_request and current_request.user.is_authenticated:
|
|
|
|
user_name = current_request.user.name
|
|
|
|
if isinstance(user_name, str):
|
|
|
|
user_name = user_name[:30]
|
|
|
|
instance.created_by = user_name
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(django_ready)
|
|
|
|
def subscribe_settings_change(sender, **kwargs):
|
|
|
|
logger.debug("Start subscribe setting change")
|
|
|
|
|
|
|
|
def keep_subscribe():
|
2021-04-12 03:30:09 +00:00
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
sub = setting_pub_sub.subscribe()
|
|
|
|
for msg in sub.listen():
|
2021-04-13 10:33:00 +00:00
|
|
|
close_old_connections()
|
2021-04-12 03:30:09 +00:00
|
|
|
if msg["type"] != "message":
|
|
|
|
continue
|
|
|
|
item = msg['data'].decode()
|
|
|
|
logger.debug("Found setting change: {}".format(str(item)))
|
|
|
|
Setting.refresh_item(item)
|
|
|
|
except Exception as e:
|
|
|
|
logger.exception(f'subscribe_settings_change: {e}')
|
2021-04-13 10:33:00 +00:00
|
|
|
close_old_connections()
|
2021-04-12 03:30:09 +00:00
|
|
|
Setting.refresh_all_settings()
|
|
|
|
|
2021-01-26 09:54:12 +00:00
|
|
|
t = threading.Thread(target=keep_subscribe)
|
|
|
|
t.daemon = True
|
|
|
|
t.start()
|