Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

支持redis集群模式 #1392

Merged
merged 5 commits into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions common/static/dist/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,21 @@ var onLoadErrorCallback = function (status, jqXHR) {
alert("未知错误,请联系管理员处理!");
}
};

// 实例配置页面根据db_type选择显示或隐藏mode字段,mode字段只适用于redis实例
(function($) {
$(function() {
let db_type = $('#id_db_type');
let mode = $('#id_mode').parent().parent();

function toggleMode(value) {
value === 'redis' ? mode.show() : mode.hide();
}

toggleMode(db_type.val());

db_type.change(function() {
toggleMode($(this).val());
});
});
})(django.jQuery);
3 changes: 2 additions & 1 deletion sql/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
WorkflowAudit, WorkflowLog, ParamTemplate, ParamHistory, InstanceTag, \
Tunnel, AuditEntry

from sql.form import TunnelForm
from sql.form import TunnelForm, InstanceForm


# 用户管理
Expand Down Expand Up @@ -62,6 +62,7 @@ def get_readonly_fields(self, request, obj=None):
# 实例管理
@admin.register(Instance)
class InstanceAdmin(admin.ModelAdmin):
form = InstanceForm
list_display = ('id', 'instance_name', 'db_type', 'type', 'host', 'port', 'user', 'create_time')
search_fields = ['instance_name', 'host', 'port', 'user']
list_filter = ('db_type', 'type', 'instance_tag')
Expand Down
1 change: 1 addition & 0 deletions sql/engines/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def __init__(self, instance=None):
self.user = instance.user
self.password = instance.password
self.db_name = instance.db_name
self.mode = instance.mode

# 判断如果配置了隧道则连接隧道,只测试了MySQL
if self.instance.tunnel:
Expand Down
8 changes: 6 additions & 2 deletions sql/engines/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@
class RedisEngine(EngineBase):
def get_connection(self, db_name=None):
db_name = db_name or self.db_name
return redis.Redis(host=self.host, port=self.port, db=db_name, password=self.password,
encoding_errors='ignore', decode_responses=True, socket_connect_timeout=10)
if self.mode == 'cluster':
return redis.cluster.RedisCluster(host=self.host, port=self.port, password=self.password,
encoding_errors='ignore', decode_responses=True, socket_connect_timeout=10)
else:
return redis.Redis(host=self.host, port=self.port, db=db_name, password=self.password,
encoding_errors='ignore', decode_responses=True, socket_connect_timeout=10)

@property
def name(self):
Expand Down
4 changes: 2 additions & 2 deletions sql/engines/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,8 +539,8 @@ def test_seconds_behind_master(self, _query):
class TestRedis(TestCase):
@classmethod
def setUpClass(cls):
cls.ins = Instance(instance_name='some_ins', type='slave', db_type='redis', host='some_host',
port=1366, user='ins_user', password='some_str')
cls.ins = Instance(instance_name='some_ins', type='slave', db_type='redis', mode='standalone',
host='some_host', port=1366, user='ins_user', password='some_str')
cls.ins.save()

@classmethod
Expand Down
8 changes: 7 additions & 1 deletion sql/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
---------------------------------------------------------
"""
from django.forms import ModelForm, Textarea
from sql.models import Tunnel
from sql.models import Tunnel, Instance
from django.core.exceptions import ValidationError


Expand All @@ -30,3 +30,9 @@ def clean(self):
cleaned_data['pkey'] = str(pkey_path, 'utf-8').replace(r'\r', '').replace(r'\n', '')
except IOError:
raise ValidationError("秘钥文件不存在, 请勾选秘钥路径的清除选项再进行保存")


class InstanceForm(ModelForm):
class Media:
model = Instance
js = ('jquery/jquery.min.js', 'dist/js/utils.js', )
1 change: 1 addition & 0 deletions sql/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ class Instance(models.Model):
instance_name = models.CharField('实例名称', max_length=50, unique=True)
type = models.CharField('实例类型', max_length=6, choices=(('master', '主库'), ('slave', '从库')))
db_type = models.CharField('数据库类型', max_length=20, choices=DB_TYPE_CHOICES)
mode = models.CharField('运行模式', max_length=10, default='', choices=(('standalone', '单机'), ('cluster', '集群')))
host = models.CharField('实例连接', max_length=200)
port = models.IntegerField('端口', default=0)
user = fields.EncryptedCharField(verbose_name='用户名', max_length=200, default='', blank=True)
Expand Down
5 changes: 4 additions & 1 deletion src/init_sql/v1.8.3.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ insert IGNORE INTO auth_permission (name, content_type_id, codename) VALUES
-- 在线查询下载权限
set @content_type_id=(select id from django_content_type where app_label='sql' and model='permission');
insert IGNORE INTO auth_permission (name, content_type_id, codename) VALUES
('在线查询下载权限', @content_type_id, 'query_download');
('在线查询下载权限', @content_type_id, 'query_download');

-- 实例配置表新增mode字段,用于redis实例
alter table sql_instance add column `mode` varchar(10) DEFAULT '' after `db_type`;
nick2wang marked this conversation as resolved.
Show resolved Hide resolved