Skip to content

Commit

Permalink
实例参数管理支持管理inception的参数
Browse files Browse the repository at this point in the history
  • Loading branch information
hhyo committed May 26, 2019
1 parent 954e9a3 commit e076271
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 11 deletions.
6 changes: 6 additions & 0 deletions sql/engines/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,9 @@ def get_engine(instance=None):
elif instance.db_type == 'oracle':
from .oracle import OracleEngine
return OracleEngine(instance=instance)
elif instance.db_type == 'inception':
from .inception import InceptionEngine
return InceptionEngine(instance=instance)
elif instance.db_type == 'goinception':
from .goinception import GoInceptionEngine
return GoInceptionEngine(instance=instance)
23 changes: 22 additions & 1 deletion sql/engines/goinception.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: UTF-8 -*-
import logging
import re
import traceback
import pymysql

from common.config import SysConfig
Expand All @@ -15,6 +16,9 @@ class GoInceptionEngine(EngineBase):
def get_connection(self, db_name=None):
if self.conn:
return self.conn
if hasattr(self, 'instance'):
self.conn = pymysql.connect(host=self.host, port=self.port, charset='utf8mb4')
return self.conn
archer_config = SysConfig()
go_inception_host = archer_config.get('go_inception_host')
go_inception_port = int(archer_config.get('go_inception_port', 4000))
Expand Down Expand Up @@ -78,7 +82,8 @@ def query(self, db_name=None, sql='', limit_num=0, close_conn=True):
"""返回 ResultSet """
result_set = ResultSet(full_sql=sql)
conn = self.get_connection()
with conn.cursor() as cursor:
try:
cursor = conn.cursor()
effect_row = cursor.execute(sql)
if int(limit_num) > 0:
rows = cursor.fetchmany(size=int(limit_num))
Expand All @@ -89,10 +94,26 @@ def query(self, db_name=None, sql='', limit_num=0, close_conn=True):
result_set.column_list = [i[0] for i in fields] if fields else []
result_set.rows = rows
result_set.affected_rows = effect_row
except Exception as e:
logger.error(f'goInception语句执行报错,语句:{sql},错误信息{traceback.format_exc()}')
result_set.error = str(e)
if close_conn:
self.close()
return result_set

def get_variables(self, variables=None):
"""获取实例参数"""
if variables:
sql = f"inception get variables like '{variables[0]}';"
else:
sql = "inception get variables;"
return self.query(sql=sql)

def set_variable(self, variable_name, variable_value):
"""修改实例参数值"""
sql = f"""inception set {variable_name}={variable_value};"""
return self.query(sql=sql)

def osc_control(self, **kwargs):
"""控制osc执行,获取进度、终止、暂停、恢复等"""
sqlsha1 = kwargs.get('sqlsha1')
Expand Down
22 changes: 21 additions & 1 deletion sql/engines/inception.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class InceptionEngine(EngineBase):
def get_connection(self, db_name=None):
if self.conn:
return self.conn
if hasattr(self, 'instance'):
self.conn = MySQLdb.connect(host=self.host, port=self.port, charset='utf8mb4')
return self.conn
archer_config = SysConfig()
inception_host = archer_config.get('inception_host')
inception_port = int(archer_config.get('inception_port', 6669))
Expand Down Expand Up @@ -126,7 +129,8 @@ def query(self, db_name=None, sql='', limit_num=0, close_conn=True):
"""返回 ResultSet """
result_set = ResultSet(full_sql=sql)
conn = self.get_connection()
with conn.cursor() as cursor:
try:
cursor = conn.cursor()
effect_row = cursor.execute(sql)
if int(limit_num) > 0:
rows = cursor.fetchmany(size=int(limit_num))
Expand All @@ -137,6 +141,9 @@ def query(self, db_name=None, sql='', limit_num=0, close_conn=True):
result_set.column_list = [i[0] for i in fields] if fields else []
result_set.rows = rows
result_set.affected_rows = effect_row
except Exception as e:
logger.error(f"Inception语句执行报错,语句:{sql},错误信息{traceback.format_exc()}")
result_set.error = str(e)
if close_conn:
self.close()
return result_set
Expand Down Expand Up @@ -210,6 +217,19 @@ def get_rollback(self, workflow):
raise Exception(e)
return list_backup_sql

def get_variables(self, variables=None):
"""获取实例参数"""
if variables:
sql = f"inception get variables '{variables[0]}';"
else:
sql = "inception get variables;"
return self.query(sql=sql)

def set_variable(self, variable_name, variable_value):
"""修改实例参数值"""
sql = f"""inception set {variable_name}={variable_value};"""
return self.query(sql=sql)

def osc_control(self, **kwargs):
"""控制osc执行,获取进度、终止、暂停、恢复等"""
sqlsha1 = kwargs.get('sqlsha1')
Expand Down
46 changes: 46 additions & 0 deletions sql/engines/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,8 @@ class TestInception(TestCase):
def setUp(self):
self.ins = Instance.objects.create(instance_name='some_ins', type='slave', db_type='mysql', host='some_host',
port=3306, user='ins_user', password='some_pass')
self.ins_inc = Instance.objects.create(instance_name='some_ins_inc', type='slave', db_type='inception',
host='some_host', port=6669)
self.wf = SqlWorkflow.objects.create(
workflow_name='some_name',
group_id=1,
Expand All @@ -682,6 +684,7 @@ def setUp(self):

def tearDown(self):
self.ins.delete()
self.ins_inc.delete()
SqlWorkflow.objects.all().delete()
SqlWorkflowContent.objects.all().delete()

Expand Down Expand Up @@ -833,12 +836,34 @@ def test_osc_not_support(self, _query):
with self.assertRaisesMessage(ValueError, 'pt-osc不支持暂停和恢复,需要停止执行请使用终止按钮!'):
new_engine.osc_control(sqlsha1=sqlsha1, command=command)

@patch('sql.engines.inception.InceptionEngine.query')
def test_get_variables(self, _query):
new_engine = InceptionEngine(instance=self.ins_inc)
new_engine.get_variables()
sql = f"inception get variables;"
_query.assert_called_once_with(sql=sql)

@patch('sql.engines.inception.InceptionEngine.query')
def test_get_variables_filter(self, _query):
new_engine = InceptionEngine(instance=self.ins_inc)
new_engine.get_variables(variables=['inception_osc_on'])
sql = f"inception get variables 'inception_osc_on';"
_query.assert_called_once_with(sql=sql)

@patch('sql.engines.inception.InceptionEngine.query')
def test_set_variable(self, _query):
new_engine = InceptionEngine(instance=self.ins)
new_engine.set_variable('inception_osc_on', 'on')
_query.assert_called_once_with(sql="inception set inception_osc_on=on;")


class TestGoInception(TestCase):
def setUp(self):
self.ins = Instance.objects.create(instance_name='some_ins', type='slave', db_type='mysql',
host='some_host',
port=3306, user='ins_user', password='some_pass')
self.ins_inc = Instance.objects.create(instance_name='some_ins_inc', type='slave', db_type='goinception',
host='some_host', port=4000)
self.wf = SqlWorkflow.objects.create(
workflow_name='some_name',
group_id=1,
Expand All @@ -856,6 +881,7 @@ def setUp(self):

def tearDown(self):
self.ins.delete()
self.ins_inc.delete()
SqlWorkflow.objects.all().delete()
SqlWorkflowContent.objects.all().delete()

Expand Down Expand Up @@ -954,6 +980,26 @@ def test_osc_kill(self, _query):
new_engine.osc_control(sqlsha1=sqlsha1, command=command)
_query.assert_called_once_with(sql=sql)

@patch('sql.engines.goinception.GoInceptionEngine.query')
def test_get_variables(self, _query):
new_engine = GoInceptionEngine(instance=self.ins_inc)
new_engine.get_variables()
sql = f"inception get variables;"
_query.assert_called_once_with(sql=sql)

@patch('sql.engines.goinception.GoInceptionEngine.query')
def test_get_variables_filter(self, _query):
new_engine = GoInceptionEngine(instance=self.ins_inc)
new_engine.get_variables(variables=['inception_osc_on'])
sql = f"inception get variables like 'inception_osc_on';"
_query.assert_called_once_with(sql=sql)

@patch('sql.engines.goinception.GoInceptionEngine.query')
def test_set_variable(self, _query):
new_engine = GoInceptionEngine(instance=self.ins)
new_engine.set_variable('inception_osc_on', 'on')
_query.assert_called_once_with(sql="inception set inception_osc_on=on;")


class TestOracle(TestCase):
"""Oracle 测试"""
Expand Down
2 changes: 1 addition & 1 deletion sql/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def param_list(request):
return HttpResponse(json.dumps(result), content_type='application/json')
# 获取已配置参数列表
cnf_params = dict()
for param in ParamTemplate.objects.filter(variable_name__contains=search).values(
for param in ParamTemplate.objects.filter(db_type=ins.db_type, variable_name__contains=search).values(
'variable_name', 'default_value', 'valid_values', 'description', 'editable'):
param['variable_name'] = param['variable_name'].lower()
cnf_params[param['variable_name']] = param
Expand Down
8 changes: 5 additions & 3 deletions sql/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ class Meta:
('mssql', 'MsSQL'),
('redis', 'Redis'),
('pgsql', 'PgSQL'),
('oracle', 'Oracle'),)
('oracle', 'Oracle'),
('inception', 'Inception'),
('goinception', 'goInception'))


class Instance(models.Model):
Expand All @@ -83,7 +85,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=10, choices=DB_TYPE_CHOICES)
db_type = models.CharField('数据库类型', max_length=20, choices=DB_TYPE_CHOICES)
host = models.CharField('实例连接', max_length=200)
port = models.IntegerField('端口', default=0)
user = models.CharField('用户名', max_length=100, default='', blank=True)
Expand Down Expand Up @@ -447,7 +449,7 @@ class ParamTemplate(models.Model):
"""
实例参数模板配置
"""
db_type = models.CharField('数据库类型', max_length=10, choices=DB_TYPE_CHOICES)
db_type = models.CharField('数据库类型', max_length=20, choices=DB_TYPE_CHOICES)
variable_name = models.CharField('参数名', max_length=64)
default_value = models.CharField('默认参数值', max_length=1024)
editable = models.BooleanField('是否支持修改', default=False)
Expand Down
2 changes: 1 addition & 1 deletion sql/templates/group.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<div id="toolbar" class="form-inline">
<div class="form-group ">
<button id="btn_add" type="button" class="btn btn-default"
onclick="window.location.href='/admin/sql/resourcegroup/add/'">
onclick="window.open('/admin/sql/resourcegroup/add/')">
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
添加组
</button>
Expand Down
2 changes: 1 addition & 1 deletion sql/templates/instance.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
</div>
<div class="form-group ">
<button id="btn_add_master" type="button" class="btn btn-default"
onclick="window.location.href='/admin/sql/instance/add/'">
onclick="window.open('/admin/sql/instance/add/')">
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
添加实例
</button>
Expand Down
2 changes: 1 addition & 1 deletion sql/templates/param.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<div class="form-inline pull-right">
<div class="form-group ">
<button id="btn_add_param" type="button" class="btn btn-default"
onclick="window.location.href='/admin/sql/paramtemplate/add/'">
onclick="window.open('/admin/sql/paramtemplate/add/')">
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
添加参数
</button>
Expand Down
4 changes: 3 additions & 1 deletion sql/utils/resource_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ def user_instances(user, type='all', db_type='all', tags=None):

# 过滤db_type
if db_type != 'all':
instances = instances.filter(db_type=db_type)
if isinstance(db_type, str):
db_type = [db_type]
instances = instances.filter(db_type__in=db_type)

# 过滤tag
if tags:
Expand Down
2 changes: 1 addition & 1 deletion sql/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ def instanceuser(request, instance_id):
@permission_required('sql.menu_param', raise_exception=True)
def instance_param(request):
# 获取用户关联实例列表
instances = user_instances(request.user, type='all', db_type='mysql')
instances = user_instances(request.user, type='all', db_type=['mysql', 'inception', 'goinception'])
context = {'tab': 'param_tab', 'instances': instances}
return render(request, 'param.html', context)

Expand Down
2 changes: 2 additions & 0 deletions src/init_sql/v1.6.0_v1.6.1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- 扩充数据库类型字段
alter table sql_instance modify db_type varchar(20) not null default '' comment '数据库类型';

0 comments on commit e076271

Please sign in to comment.