-
Notifications
You must be signed in to change notification settings - Fork 641
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from hhyo/github
优化报错体验
- Loading branch information
Showing
31 changed files
with
4,135 additions
and
424 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,11 +15,11 @@ | |
# Build paths inside the project like this: os.path.join(BASE_DIR, ...) | ||
import os | ||
import pymysql | ||
|
||
pymysql.install_as_MySQLdb() | ||
|
||
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | ||
|
||
|
||
# Quick-start development settings - unsuitable for production | ||
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/ | ||
|
||
|
@@ -44,6 +44,7 @@ | |
'django.contrib.sessions', | ||
'django.contrib.messages', | ||
'django.contrib.staticfiles', | ||
'django_apscheduler', | ||
'sql', | ||
) | ||
|
||
|
@@ -103,15 +104,15 @@ | |
STATIC_URL = '/static/' | ||
STATIC_ROOT = os.path.join(BASE_DIR, 'static') | ||
|
||
#扩展django admin里users字段用到,指定了sql/models.py里的class users | ||
AUTH_USER_MODEL="sql.users" | ||
# 扩展django admin里users字段用到,指定了sql/models.py里的class users | ||
AUTH_USER_MODEL = "sql.users" | ||
|
||
###############以下部分需要用户根据自己环境自行修改################### | ||
|
||
# Database | ||
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases | ||
|
||
#该项目本身的mysql数据库地址 | ||
# 该项目本身的mysql数据库地址 | ||
DATABASES = { | ||
'default': { | ||
'ENGINE': 'django.db.backends.mysql', | ||
|
@@ -123,16 +124,16 @@ | |
} | ||
} | ||
|
||
#inception组件所在的地址 | ||
# inception组件所在的地址 | ||
INCEPTION_HOST = '192.168.1.11' | ||
INCEPTION_PORT = '6100' | ||
|
||
#查看回滚SQL时候会用到,这里要告诉archer去哪个mysql里读取inception备份的回滚信息和SQL. | ||
#注意这里要和inception组件的inception.conf里的inception_remote_XX部分保持一致. | ||
INCEPTION_REMOTE_BACKUP_HOST='192.168.1.12' | ||
INCEPTION_REMOTE_BACKUP_PORT=5621 | ||
INCEPTION_REMOTE_BACKUP_USER='inception' | ||
INCEPTION_REMOTE_BACKUP_PASSWORD='inception' | ||
# 查看回滚SQL时候会用到,这里要告诉archer去哪个mysql里读取inception备份的回滚信息和SQL. | ||
# 注意这里要和inception组件的inception.conf里的inception_remote_XX部分保持一致. | ||
INCEPTION_REMOTE_BACKUP_HOST = '192.168.1.12' | ||
INCEPTION_REMOTE_BACKUP_PORT = 5621 | ||
INCEPTION_REMOTE_BACKUP_USER = 'inception' | ||
INCEPTION_REMOTE_BACKUP_PASSWORD = 'inception' | ||
|
||
# 账户登录失败锁定时间(秒) | ||
LOCK_TIME_THRESHOLD = 300 | ||
|
@@ -145,18 +146,19 @@ | |
import ldap | ||
# from django_auth_ldap.config import LDAPSearch, GroupOfNamesType | ||
from django_auth_ldap.config import LDAPSearch, GroupOfUniqueNamesType | ||
|
||
# if use self signed certificate, Remove AUTH_LDAP_GLOBAL_OPTIONS annotations | ||
#AUTH_LDAP_GLOBAL_OPTIONS={ | ||
# AUTH_LDAP_GLOBAL_OPTIONS={ | ||
# ldap.OPT_X_TLS_REQUIRE_CERT: ldap.OPT_X_TLS_NEVER | ||
#} | ||
# } | ||
AUTH_LDAP_BIND_DN = "cn=ro,dc=xxx,dc=cn" | ||
AUTH_LDAP_BIND_PASSWORD = "xxxxxx" | ||
AUTH_LDAP_SERVER_URI = "ldap://auth.xxx.com" | ||
AUTH_LDAP_BASEDN = "ou=users,dc=xxx,dc=cn" | ||
AUTH_LDAP_USER_DN_TEMPLATE = "cn=%(user)s,ou=users,dc=xxx,dc=cn" | ||
AUTH_LDAP_GROUP_SEARCH = LDAPSearch("ou=groups,dc=xxx,dc=cn", | ||
ldap.SCOPE_SUBTREE, "(objectClass=groupOfUniqueNames)" | ||
) | ||
ldap.SCOPE_SUBTREE, "(objectClass=groupOfUniqueNames)" | ||
) | ||
AUTH_LDAP_GROUP_TYPE = GroupOfUniqueNamesType() | ||
AUTH_LDAP_USER_ATTRLIST = ["cn", "sn", "mail"] | ||
AUTH_LDAP_USER_ATTR_MAP = { | ||
|
@@ -170,7 +172,7 @@ | |
# AUTH_LDAP_CACHE_GROUPS = True # 如打开FIND_GROUP_PERMS后,此配置生效,对组关系进行缓存,不用每次请求都调用ldap | ||
# AUTH_LDAP_GROUP_CACHE_TIMEOUT = 600 # 缓存时间 | ||
|
||
#开启以下配置注释,可以帮助调试ldap集成 | ||
# 开启以下配置注释,可以帮助调试ldap集成 | ||
LDAP_LOGS = '/tmp/ldap.log' | ||
DEFAULT_LOGS = '/tmp/default.log' | ||
stamdard_format = '[%(asctime)s][%(threadName)s:%(thread)d]' + \ | ||
|
@@ -229,18 +231,18 @@ | |
} | ||
} | ||
|
||
#是否开启邮件提醒功能:发起SQL上线后会发送邮件提醒审核人审核,执行完毕会发送给DBA. on是开,off是关,配置为其他值均会被archer认为不开启邮件功能 | ||
MAIL_ON_OFF='on' | ||
|
||
MAIL_REVIEW_SMTP_SERVER='mail.xxx.com' | ||
MAIL_REVIEW_SMTP_PORT=25 | ||
MAIL_REVIEW_FROM_ADDR='[email protected]' #发件人,也是登录SMTP server需要提供的用户名 | ||
MAIL_REVIEW_FROM_PASSWORD='' #发件人邮箱密码,如果为空则不需要login SMTP server | ||
MAIL_REVIEW_DBA_ADDR=['[email protected]', '[email protected]'] #DBA地址,执行完毕会发邮件给DBA,以list形式保存 | ||
MAIL_REVIEW_SECURE_ADDR=['[email protected]', '[email protected]'] #登录失败,等安全相关发送地址 | ||
#是否过滤【DROP DATABASE】|【DROP TABLE】|【TRUNCATE PARTITION】|【TRUNCATE TABLE】等高危DDL操作: | ||
#on是开,会首先用正则表达式匹配sqlContent,如果匹配到高危DDL操作,则判断为“自动审核不通过”;off是关,直接将所有的SQL语句提交给inception,对于上述高危DDL操作,只备份元数据 | ||
CRITICAL_DDL_ON_OFF='off' | ||
# 是否开启邮件提醒功能:发起SQL上线后会发送邮件提醒审核人审核,执行完毕会发送给DBA. on是开,off是关,配置为其他值均会被archer认为不开启邮件功能 | ||
MAIL_ON_OFF = 'on' | ||
|
||
MAIL_REVIEW_SMTP_SERVER = 'mail.xxx.com' | ||
MAIL_REVIEW_SMTP_PORT = 25 | ||
MAIL_REVIEW_FROM_ADDR = '[email protected]' # 发件人,也是登录SMTP server需要提供的用户名 | ||
MAIL_REVIEW_FROM_PASSWORD = '' # 发件人邮箱密码,如果为空则不需要login SMTP server | ||
MAIL_REVIEW_DBA_ADDR = ['[email protected]', '[email protected]'] # DBA地址,执行完毕会发邮件给DBA,以list形式保存 | ||
MAIL_REVIEW_SECURE_ADDR = ['[email protected]', '[email protected]'] # 登录失败,等安全相关发送地址 | ||
# 是否过滤【DROP DATABASE】|【DROP TABLE】|【TRUNCATE PARTITION】|【TRUNCATE TABLE】等高危DDL操作: | ||
# on是开,会首先用正则表达式匹配sqlContent,如果匹配到高危DDL操作,则判断为“自动审核不通过”;off是关,直接将所有的SQL语句提交给inception,对于上述高危DDL操作,只备份元数据 | ||
CRITICAL_DDL_ON_OFF = 'off' | ||
|
||
# 在线查询当inception语法树打印失败时的控制,on是开启校验,失败不允许继续执行并返回错误,off是关闭校验,继续执行,允许执行会导致解析失败的查询表权限验证和脱敏功能失效 | ||
CHECK_QUERY_ON_OFF = True | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# -*- coding:utf-8 -*- | ||
import datetime | ||
import time | ||
|
||
from apscheduler.schedulers.background import BackgroundScheduler | ||
from apscheduler.schedulers import SchedulerAlreadyRunningError, SchedulerNotRunningError | ||
from django.core.urlresolvers import reverse | ||
from django.http import HttpResponseRedirect | ||
from django.shortcuts import render | ||
from django_apscheduler.jobstores import DjangoJobStore, register_events, register_job | ||
|
||
from sql.const import Const | ||
from sql.models import workflow | ||
from .sqlreview import execute_job, getDetailUrl | ||
|
||
import logging | ||
|
||
logging.basicConfig() | ||
logging.getLogger('apscheduler').setLevel(logging.DEBUG) | ||
|
||
logger = logging.getLogger('default') | ||
|
||
# 初始化scheduler | ||
scheduler = BackgroundScheduler() | ||
scheduler.add_jobstore(DjangoJobStore(), "default") | ||
register_events(scheduler) | ||
try: | ||
scheduler.start() | ||
logger.debug("Scheduler started!") | ||
except SchedulerAlreadyRunningError: | ||
logger.debug("Scheduler is already running!") | ||
|
||
|
||
# 添加/修改sql执行任务 | ||
def add_sqlcronjob(request): | ||
workflowId = request.POST.get('workflowid') | ||
run_date = request.POST.get('run_date') | ||
if run_date is None or workflowId is None: | ||
context = {'errMsg': '时间不能为空'} | ||
return render(request, 'error.html', context) | ||
elif run_date < datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'): | ||
context = {'errMsg': '时间不能小于当前时间'} | ||
return render(request, 'error.html', context) | ||
workflowDetail = workflow.objects.get(id=workflowId) | ||
if workflowDetail.status not in ['审核通过', '定时执行']: | ||
context = {'errMsg': '必须为审核通过或者定时执行状态'} | ||
return render(request, 'error.html', context) | ||
|
||
run_date = datetime.datetime.strptime(run_date, "%Y-%m-%d %H:%M:%S") | ||
url = getDetailUrl(request) + str(workflowId) + '/' | ||
job_id = Const.workflowJobprefix['sqlreview'] + '-' + str(workflowId) | ||
|
||
try: | ||
scheduler = BackgroundScheduler() | ||
scheduler.add_jobstore(DjangoJobStore(), "default") | ||
scheduler.add_job(execute_job, 'date', run_date=run_date, args=[workflowId, url], id=job_id, | ||
replace_existing=True) | ||
register_events(scheduler) | ||
try: | ||
scheduler.start() | ||
logger.debug("Scheduler started!") | ||
except SchedulerAlreadyRunningError: | ||
logger.debug("Scheduler is already running!") | ||
workflowDetail.status = Const.workflowStatus['tasktiming'] | ||
workflowDetail.save() | ||
except Exception as e: | ||
context = {'errMsg': '任务添加失败,错误信息:' + str(e)} | ||
return render(request, 'error.html', context) | ||
else: | ||
logger.debug('add_sqlcronjob:' + job_id + "run_date:" + run_date.strftime('%Y-%m-%d %H:%M:%S')) | ||
|
||
return HttpResponseRedirect(reverse('sql:detail', args=(workflowId,))) | ||
|
||
|
||
# 删除sql执行任务 | ||
def del_sqlcronjob(job_id): | ||
logger.debug('del_sqlcronjob:' + job_id) | ||
return scheduler.remove_job(job_id) | ||
|
||
|
||
# 获取任务详情 | ||
def job_info(job_id): | ||
return scheduler.get_job(job_id) |
Oops, something went wrong.