Skip to content

Commit

Permalink
submitsql
Browse files Browse the repository at this point in the history
  • Loading branch information
jly8866 committed Feb 7, 2017
1 parent 1f927ec commit 029954f
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 60 deletions.
3 changes: 3 additions & 0 deletions archer/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,6 @@
# https://docs.djangoproject.com/en/1.8/howto/static-files/

STATIC_URL = '/static/'

INCEPTION_HOST = '172.16.5.7'
INCEPTION_PORT = '6100'
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Django==1.8.17
python==3.4.1
MySQLdb
24 changes: 24 additions & 0 deletions sql/dao.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# coding = utf-8

import MySQLdb

class Dao(object):
#连进指定的mysql实例里,读取所有databases并返回
def getAlldbByCluster(self, masterHost, masterPort, masterUser, masterPassword):
listDb = []
try:
conn=MySQLdb.connect(host=masterHost, port=masterPort, user=masterUser, passwd=masterPassword)
cursor = conn.cursor()
sql = "show databases"
n = cursor.execute(sql)
listDb = [row[0] for row in cursor.fetchall()
if row[0] not in ('information_schema', 'performance_schema', 'mysql', 'test')]
except MySQLdb.Warning as w:
print(str(w))
except MySQLdb.Error as e:
print(str(e))
finally:
cursor.close()
conn.commit()
conn.close()
return listDb
36 changes: 36 additions & 0 deletions sql/inception.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#-*-coding: utf-8-*-

import MySQLdb
from django.conf import settings

class InceptionDao(object):
def __init__(self):
try:
self.inception_host = getattr(settings, 'INCEPTION_HOST')
self.inception_port = getattr(settings, 'INCEPTION_PORT')
except AttributeError as a:
print("Error:%s" % a)
quit(1)


def sqlautoReview(self, sql_content, cluster_name):
sql='/*--user=username;--password=password;--host=127.0.0.1;--execute=1;--port=3306;*/\
inception_magic_start;\
use mysql;\
CREATE TABLE adaptive_office(id int);\
inception_magic_commit;'
try:
conn=MySQLdb.connect(host='127.0.0.1',user='',passwd='',db='',port=9998)
cur=conn.cursor()
ret=cur.execute(sql)
result=cur.fetchall()
num_fields = len(cur.description)
field_names = [i[0] for i in cur.description]
print field_names
for row in result:
print row[0], "|",row[1],"|",row[2],"|",row[3],"|",row[4],"|",
row[5],"|",row[6],"|",row[7],"|",row[8],"|",row[9],"|",row[10]
cur.close()
conn.close()
except MySQLdb.Error,e:
print "Mysql Error %d: %s" % (e.args[0], e.args[1])
48 changes: 20 additions & 28 deletions sql/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,44 +11,36 @@ class Migration(migrations.Migration):

operations = [
migrations.CreateModel(
name='engineers',
fields=[
('id', models.AutoField(serialize=False, verbose_name='ID', auto_created=True, primary_key=True)),
('username', models.CharField(max_length=50)),
('password', models.CharField(max_length=50)),
('display', models.CharField(max_length=50)),
],
),
migrations.CreateModel(
name='managers',
name='master_config',
fields=[
('id', models.AutoField(serialize=False, verbose_name='ID', auto_created=True, primary_key=True)),
('username', models.CharField(max_length=50)),
('password', models.CharField(max_length=50)),
('display', models.CharField(max_length=50)),
('id', models.AutoField(serialize=False, verbose_name='ID', primary_key=True, auto_created=True)),
('cluster_name', models.CharField(verbose_name='集群名称', max_length=50)),
('master_host', models.CharField(verbose_name='主库地址', max_length=200)),
('master_port', models.IntegerField(default=3306, verbose_name='主库端口')),
('master_user', models.CharField(verbose_name='登录主库的用户名', max_length=50)),
('master_password', models.CharField(verbose_name='登录主库的密码', max_length=50)),
('create_time', models.DateTimeField(verbose_name='创建时间', auto_now_add=True)),
('update_time', models.DateTimeField(verbose_name='更新时间', auto_now=True)),
],
),
migrations.CreateModel(
name='master_config',
name='users',
fields=[
('id', models.AutoField(serialize=False, verbose_name='ID', auto_created=True, primary_key=True)),
('cluster_name', models.CharField(max_length=50)),
('master_host', models.CharField(max_length=200)),
('master_port', models.IntegerField(default=3306)),
('master_user', models.CharField(max_length=50)),
('master_password', models.CharField(max_length=50)),
('create_time', models.DateTimeField(auto_now_add=True)),
('update_time', models.DateTimeField(auto_now=True)),
('id', models.AutoField(serialize=False, verbose_name='ID', primary_key=True, auto_created=True)),
('username', models.CharField(verbose_name='用户名', max_length=50)),
('password', models.CharField(verbose_name='密码', max_length=50)),
('display', models.CharField(verbose_name='显示的中文名', max_length=50)),
('role', models.CharField(default='工程师', verbose_name='角色', choices=[('工程师', '工程师'), ('审核人', '审核人')], max_length=20)),
],
),
migrations.CreateModel(
name='workflow',
fields=[
('id', models.AutoField(serialize=False, verbose_name='ID', auto_created=True, primary_key=True)),
('workflow_name', models.CharField(max_length=50)),
('engineer', models.CharField(max_length=50)),
('manager', models.CharField(max_length=50)),
('create_time', models.DateTimeField(auto_now_add=True)),
('id', models.AutoField(serialize=False, verbose_name='ID', primary_key=True, auto_created=True)),
('workflow_name', models.CharField(verbose_name='工单内容', max_length=50)),
('engineer', models.CharField(verbose_name='发起人', max_length=50)),
('review_man', models.CharField(verbose_name='审核人', max_length=50)),
('create_time', models.DateTimeField(verbose_name='创建时间', auto_now_add=True)),
('finish_time', models.DateTimeField()),
('status', models.CharField(choices=[('已正常结束', '已正常结束'), ('人工终止流程', '人工终止流程'), ('等待审核人审核', '等待审核人审核'), ('执行中', '执行中'), ('自动审核不通过', '自动审核不通过'), ('执行有异常', '执行有异常')], max_length=50)),
('is_backup', models.IntegerField(choices=[(0, 0), (1, 1)])),
Expand Down
30 changes: 15 additions & 15 deletions sql/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,33 @@
#1.工程师:可以提交SQL上线单的工程师们,username字段为登录用户名,display字段为展示的中文名。
#2.审核人:可以审核并执行SQL上线单的管理者、高级工程师、系统管理员们。
class users(models.Model):
username = models.CharField(max_length=50)
password = models.CharField(max_length=50)
display = models.CharField(max_length=50)
role = models.CharField(max_length=20, choices=(('工程师','工程师'),('审核人','审核人')), default='工程师')
username = models.CharField('用户名', max_length=50)
password = models.CharField('密码', max_length=50)
display = models.CharField('显示的中文名', max_length=50)
role = models.CharField('角色', max_length=20, choices=(('工程师','工程师'),('审核人','审核人')), default='工程师')

def __str__(self):
return self.username

#各个线上主库地址。
class master_config(models.Model):
cluster_name = models.CharField(max_length=50)
master_host = models.CharField(max_length=200)
master_port = models.IntegerField(default=3306)
master_user = models.CharField(max_length=50)
master_password = models.CharField(max_length=50)
create_time = models.DateTimeField(auto_now_add=True)
update_time = models.DateTimeField(auto_now=True)
cluster_name = models.CharField('集群名称', max_length=50)
master_host = models.CharField('主库地址', max_length=200)
master_port = models.IntegerField('主库端口', default=3306)
master_user = models.CharField('登录主库的用户名', max_length=50)
master_password = models.CharField('登录主库的密码', max_length=50)
create_time = models.DateTimeField('创建时间', auto_now_add=True)
update_time = models.DateTimeField('更新时间', auto_now=True)

def __str__(self):
return self.cluster_name

#存放各个SQL上线工单的详细内容,可定期归档或清理历史数据,也可通过alter table workflow row_format=compressed; 来进行压缩
class workflow(models.Model):
workflow_name = models.CharField(max_length=50)
engineer = models.CharField(max_length=50)
review_man = models.CharField(max_length=50)
create_time = models.DateTimeField(auto_now_add=True)
workflow_name = models.CharField('工单内容', max_length=50)
engineer = models.CharField('发起人', max_length=50)
review_man = models.CharField('审核人', max_length=50)
create_time = models.DateTimeField('创建时间', auto_now_add=True)
finish_time = models.DateTimeField()
status = models.CharField(max_length=50, choices=(('已正常结束','已正常结束'),('人工终止流程','人工终止流程'),('等待审核人审核','等待审核人审核'),('执行中','执行中'),('自动审核不通过','自动审核不通过'),('执行有异常','执行有异常')))
is_backup = models.IntegerField(choices=((0,0),(1,1)))
Expand Down
8 changes: 4 additions & 4 deletions sql/processor.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# coding: utf-8

leftMenuBtns = (
{'key':'allworkflow', 'name':'查看历史工单', 'url':'/allworkflow/', 'class':'glyphicon glyphicon-home'},
{'key':'submitsql', 'name':'发起SQL上线', 'url':'/submitsql/', 'class':'glyphicon glyphicon-asterisk'},
{'key':'masterconfig', 'name':'主库地址配置', 'url':'/masterconfig/', 'class':'glyphicon glyphicon-user'},
{'key':'userconfig', 'name':'用户权限配置', 'url':'/userconfig/', 'class':'glyphicon glyphicon-th-large'},
{'key':'allworkflow', 'name':'查看历史工单', 'url':'/allworkflow/', 'class':'glyphicon glyphicon-home'},
{'key':'submitsql', 'name':'发起SQL上线', 'url':'/submitsql/', 'class':'glyphicon glyphicon-asterisk'},
{'key':'masterconfig', 'name':'主库地址配置', 'url':'/admin/sql/master_config/', 'class':'glyphicon glyphicon-user'},
{'key':'userconfig', 'name':'用户权限配置', 'url':'/admin/sql/users/', 'class':'glyphicon glyphicon-th-large'},
)

def global_info(request):
Expand Down
30 changes: 18 additions & 12 deletions sql/static/submitSql.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,29 @@
{% block content %}
<div class="container-fluid">
<div class="row clearfix">

<form action="/autoreview/" method="post" class="form-horizontal" role="form">
{% csrf_token %}
<div class="col-md-8 column">
<textarea name="sql_content" class="form-control" placeholder="请在此提交SQL..." rows=20></textarea>
<textarea name="sql_content" class="form-control" placeholder="请在此提交SQL..." rows=20 required></textarea>
</div>

<div class="col-md-4 column">
<form class="form-horizontal" role="form">
<div class="form-group">
<input id="workflow_name" type="text" name="workflow_name" class="form-control" data-name="上线单名称" placeholder="请输入上线单名称,如:XX项目会员功能建表" required>
</div>
<div class="form-group">
<select id="cluster_name" name="cluster_name" class="selectpicker show-tick form-control bs-select-hidden" data-name="要上线的集群" data-placeholder="请选择要上线的集群:" required>
<option disabled="" selected="selected">请选择要上线的集群:</option>
<option value="prod">prod集群 (包含db: activity,ad,card,cdb,cdb_ext,cms,comment,db_bbs,db_bbs_dede,db_external,db_sem,db_statistics,jifen,marketing,pay,storage,wenda,yunying)</option>
<option value="hermes">hermes集群 (包含db: api,forum,hermes,news_db,tts,zhixue_db)</option>
<option value="statistics">statistics集群 (包含db: baijiatools,data_cms,db_galaxy,media,pycron,zhanqun,zhixue_spider)</option>
<option value="finance">finance集群 (包含db: accounting,fenqi,shoukuan,zhifu)</option>
<option value="baijiabao">baijiabao集群 (包含db: baijiacloud,super_class,super_wish,wesambo,wesambo_new,xxtt,yiqixiu)</option>
<option value="tianxiao">tianxiao集群 (包含db: business_school,libra,parallel,tx_bbs,tx_connect,virgo)</option>
<option value="service">service集群 (包含db: azkaban,cas,jigou,service,yunying,yunying_statistics)</option>
<option value="chuizhichanpin">chuizhichanpin集群 (包含db: jinyou)</option>
{% for cluster_name,dbs in dictAllClusterDb.items %}
<option value="{{cluster_name}}">{{cluster_name}}集群 (包含db:
{% for db in dbs %}
{{db}}{% if not forloop.last %},{% endif%}
{% endfor %}
)
</option>
{% endfor %}

</select>
</div>
<div class="form-group">
Expand All @@ -34,14 +38,16 @@
<div class="form-group">
<select id="basic" name="review_man" class="selectpicker show-tick form-control bs-select-hidden" data-name="审核人" data-placeholder="请选择RD审核人:" required>
<option disabled="" selected="selected">请选择审核人:</option>
<option value="zhouwei">周伟(总部-基础平台部)(zhouwei)</option><option value="zouyang">邹洋(zouyang)</option><option value="caiyili">蔡宜利(caiyili)</option><option value="changming">常明(changming)</option><option value="lichangchun">黎长春(lichangchun)</option><option value="minyifei">闵益飞(minyifei)</option><option value="gaoyanpeng">高延鹏(gaoyanpeng)</option>
{% for man in reviewMen %}
<option value="{{man}}">{{man}}</option>
{% endfor %}
</select>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">提交SQL</button>
<button type="reset" class="btn btn-default">清空重填</button>
</div>
</form>
</form>
</div>
</div>
</div>
Expand Down
6 changes: 6 additions & 0 deletions sql/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@
from . import views

urlpatterns = [
url(r'^$', views.allworkflow, name='allworkflow'),
url(r'^index/$', views.allworkflow, name='allworkflow'),
url(r'^login/$', views.login, name='login'),
url(r'^logout/$', views.logout, name='logout'),
url(r'^authenticate/$', views.authenticate, name='authenticate'),
url(r'^submitsql/$', views.submitSql, name='submitSql'),
url(r'^allworkflow/$', views.allworkflow, name='allworkflow'),

url(r'^autoreview/$', views.autoreview, name='autoreview'),
url(r'^detail/$', views.detail, name='detail'),
url(r'^rollback/$', views.rollback, name='rollback'),
]
50 changes: 49 additions & 1 deletion sql/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
from django.contrib.auth.decorators import login_required

from .models import users, master_config, workflow
from .dao import Dao

dao = Dao()

# Create your views here.
def login(request):
Expand Down Expand Up @@ -42,11 +45,56 @@ def authenticate(request):
result = {'status':1, 'msg':'用户名或密码错误,请重新输入!', 'data':''}
return HttpResponse(json.dumps(result), content_type='application/json')

#首页,也是查看所有SQL工单页面
def allworkflow(request):
context = {'currentMenu':'allworkflow'}
return render(request, 'allWorkflow.html', context)

#提交SQL的页面
def submitSql(request):
context = {'currentMenu':'submitsql'}
masters = master_config.objects.all()
if len(masters) == 0:
context = {'errMsg': '集群数为0,可能后端数据没有配置集群'}
return render(request, 'error.html', context)

#获取所有集群名称
listAllClusterName = [master.cluster_name for master in masters]

dictAllClusterDb = {}
#每一个都首先获取主库地址在哪里
for clusterName in listAllClusterName:
listMasters = master_config.objects.filter(cluster_name=clusterName)
if len(listMasters) != 1:
context = {'errMsg': '存在两个集群名称一样的集群,请修改数据库'}
return render(request, 'error.html', context)
#取出该集群的名称以及连接方式,为了后面连进去获取所有databases
masterHost = listMasters[0].master_host
masterPort = listMasters[0].master_port
masterUser = listMasters[0].master_user
masterPassword = listMasters[0].master_password

listDb = dao.getAlldbByCluster(masterHost, masterPort, masterUser, masterPassword)
dictAllClusterDb[clusterName] = listDb

#获取所有审核人
reviewMen = users.objects.filter(role='审核人')
if len(reviewMen) == 0:
context = {'errMsg': '审核人为0,请配置审核人'}
return render(request, 'error.html', context)
listAllReviewMen = [user.username for user in reviewMen]

context = {'currentMenu':'submitsql', 'dictAllClusterDb':dictAllClusterDb, 'reviewMen':reviewMen}
return render(request, 'submitSql.html', context)

#提交SQL给inception进行解析
def autoreview(request):
return HttpResponse("aaa")

#展示SQL工单详细内容,以及可以人工审核,审核通过即可执行
def detail(request):
return HttpResponse("bbb")

#展示回滚的SQL
def rollback(request):
return HttpResponse("rollback")

0 comments on commit 029954f

Please sign in to comment.