Skip to content

Commit

Permalink
mysql binlog解析在线操作
Browse files Browse the repository at this point in the history
  • Loading branch information
qingduyu committed Nov 7, 2018
1 parent 0afa986 commit 544277b
Show file tree
Hide file tree
Showing 51 changed files with 1,985 additions and 11,733 deletions.
876 changes: 501 additions & 375 deletions .idea/workspace.xml

Large diffs are not rendered by default.

3 changes: 0 additions & 3 deletions api/views/assets_api.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
#!/usr/bin/env python
# _#_ coding:utf-8 _*_
from rest_framework import viewsets,permissions
from api import serializers
from CodeOps.models import *
from CMDB.models import *

from rest_framework import status
from django.http import Http404
from django.contrib.auth.models import Group
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.decorators import api_view
from tasks.assets import recordAssets
Expand Down
8 changes: 1 addition & 7 deletions api/views/db_api.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
#!/usr/bin/env python
# _#_ coding:utf-8 _*_
from rest_framework import viewsets,permissions
from api import serializers
from MysqlOps.models import *
from rest_framework import status
from django.http import Http404
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.decorators import api_view
from django.contrib.auth.decorators import permission_required
from tasks.sql import sendOrderNotice
from Orders.models import Order_System
from utils.data.base import MySQLPool
from django.http import JsonResponse
from utils.logger import logger
from utils import mysql as MySQL


@api_view(['POST' ])
@permission_required('OpsManage.can_add_database_server_config',raise_exception=True)
Expand Down
11 changes: 3 additions & 8 deletions api/views/deploy_api.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
#!/usr/bin/env python
# _#_ coding:utf-8 _*_
from rest_framework import viewsets,permissions
from api import serializers
from OpsControl.models import *
from CodeOps.models import *
from rest_framework import status
from django.http import Http404
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.decorators import api_view
from tasks.deploy import recordProject
# from tasks.assets import recordProject
from django.contrib.auth.decorators import permission_required
from rest_framework import generics
from django.db.models import Q


@api_view(['GET', 'POST' ])
@permission_required('OpsManage.can_add_project_config',raise_exception=True)
Expand Down Expand Up @@ -44,7 +39,7 @@ def deploy_detail(request, id,format=None):
elif request.method == 'DELETE':
if not request.user.has_perm('OpsManage.delete_project_config'):
return Response(status=status.HTTP_403_FORBIDDEN)
recordProject.delay(project_user=str(request.user),project_id=id,project_name=snippet.project.project_name,project_content="删除项目")
# recordProject.delay(project_user=str(request.user),project_id=id,project_name=snippet.project.project_name,project_content="删除项目")
snippet.delete()
return Response(status=status.HTTP_204_NO_CONTENT)

Expand Down
1 change: 0 additions & 1 deletion api/views/orders_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env python
# _#_ coding:utf-8 _*_
from rest_framework import viewsets,permissions
from api import serializers

from rest_framework import status
Expand Down
Binary file modified apps/CMDB/model/mysql_modles.pyc
Binary file not shown.
Binary file modified apps/CMDB/model/server_models.pyc
Binary file not shown.
2 changes: 1 addition & 1 deletion apps/CMDB/views/assets.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
import os, xlrd, time
import os, xlrd
from django.http import JsonResponse, StreamingHttpResponse
from django.shortcuts import render, HttpResponseRedirect
from django.contrib.auth.decorators import login_required
Expand Down
14 changes: 7 additions & 7 deletions apps/CodeOps/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from django.contrib.auth.models import User,Group
from CMDB.views.assets import getBaseAssets
from Orders.models import Order_System
from tasks.deploy import recordProject
# from tasks import recordProject
from django.contrib.auth.decorators import permission_required
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from utils.logger import logger
Expand Down Expand Up @@ -61,7 +61,7 @@ def deploy_add(request):
project_repo_passwd = request.POST.get('project_repo_passwd'),
project_audit_group = request.POST.get('project_audit_group',None),
)
recordProject.delay(project_user=str(request.user),project_id=project.id,project_name=project.project_name,project_content="添加项目")
# recordProject.delay(project_user=str(request.user),project_id=project.id,project_name=project.project_name,project_content="添加项目")
except Exception, ex:
logger.error(msg="部署项目添加失败: {ex}".format(ex=ex))
return JsonResponse({'msg':"部署项目添加失败: {ex}".format(ex=ex),"code":500,'data':[]})
Expand Down Expand Up @@ -118,7 +118,7 @@ def deploy_modf(request,pid):
project_repo_passwd = request.POST.get('project_repo_passwd'),
project_model = request.POST.get('project_model'),
)
recordProject.delay(project_user=str(request.user),project_id=pid,project_name=project.project_name,project_content="修改项目")
# recordProject.delay(project_user=str(request.user),project_id=pid,project_name=project.project_name,project_content="修改项目")
except Exception,ex:
logger.error(msg="部署项目修改失败: {ex}".format(ex=ex))
return JsonResponse({'msg':"部署项目修改失败: {ex}".format(ex=ex),"code":500,'data':[]})
Expand Down Expand Up @@ -182,7 +182,7 @@ def deploy_init(request,pid):
if result[0] > 0:return JsonResponse({'msg':result[1],"code":500,'data':[]})
else:
Project_Config.objects.filter(id=pid).update(project_status = 1)
recordProject.delay(project_user=str(request.user),project_id=project.id,project_name=project.project_name,project_content="初始化项目")
# recordProject.delay(project_user=str(request.user),project_id=project.id,project_name=project.project_name,project_content="初始化项目")
return JsonResponse({'msg':"初始化成功","code":200,'data':[]})

@login_required()
Expand Down Expand Up @@ -395,9 +395,9 @@ def deploy_run(request,pid):
DsRedis.OpsProject.delete(redisKey=project.project_uuid+"-locked")
#异步记入操作日志
# if request.POST.get('project_version'):bName = request.POST.get('project_version')
recordProject.delay(project_user=str(request.user),project_id=project.id,
project_name=project.project_name,project_content=project_content,
project_branch=verName)
# recordProject.delay(project_user=str(request.user),project_id=project.id,
# project_name=project.project_name,project_content=project_content,
# project_branch=verName)
return JsonResponse({'msg':"项目部署成功","code":200,'data':tmpServer})
else:
return JsonResponse({'msg':"项目部署失败:{user}正在部署改项目,请稍后再提交部署。".format(user=DsRedis.OpsProject.get(redisKey=project.project_uuid+"-locked")),"code":500,'data':[]})
Expand Down
31 changes: 31 additions & 0 deletions apps/MysqlOps/migrations/0002_binlogparseredo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.15 on 2018-11-06 11:50
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('MysqlOps', '0001_initial'),
]

operations = [
migrations.CreateModel(
name='BinlogParseRedo',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('sql', models.CharField(blank=True, max_length=1900, null=True, verbose_name=b'\xe8\xa7\xa3\xe6\x9e\x90\xe5\x90\x8e\xe7\x9a\x84sql')),
('start_pos', models.IntegerField(blank=True, null=True, verbose_name=b'\xe8\xb5\xb7\xe5\xa7\x8b\xe4\xbd\x8d\xe7\xbd\xae')),
('end_pos', models.IntegerField(blank=True, null=True, verbose_name=b'\xe7\xbb\x93\xe6\x9d\x9f\xe4\xbd\x8d\xe7\xbd\xae')),
('date', models.CharField(blank=True, max_length=50, null=True, verbose_name=b'\xe6\x89\xa7\xe8\xa1\x8c\xe6\x97\xa5\xe6\x9c\x9f')),
('time', models.CharField(blank=True, max_length=50, null=True, verbose_name=b'\xe6\x89\xa7\xe8\xa1\x8c\xe6\x97\xb6\xe9\x97\xb4')),
],
options={
'db_table': 'binlogParseRedo',
'verbose_name': 'bin\u65e5\u5fd7\u89e3\u6790',
'verbose_name_plural': 'bin\u65e5\u5fd7\u89e3\u6790',
},
),
]
31 changes: 31 additions & 0 deletions apps/MysqlOps/migrations/0003_binlogparseundo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.15 on 2018-11-06 11:52
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('MysqlOps', '0002_binlogparseredo'),
]

operations = [
migrations.CreateModel(
name='BinlogParseUndo',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('sql', models.CharField(blank=True, max_length=1900, null=True, verbose_name=b'\xe9\x80\x86\xe5\x90\x91\xe8\xa7\xa3\xe6\x9e\x90\xe5\x90\x8e\xe7\x9a\x84sql')),
('start_pos', models.IntegerField(blank=True, null=True, verbose_name=b'\xe8\xb5\xb7\xe5\xa7\x8b\xe4\xbd\x8d\xe7\xbd\xae')),
('end_pos', models.IntegerField(blank=True, null=True, verbose_name=b'\xe7\xbb\x93\xe6\x9d\x9f\xe4\xbd\x8d\xe7\xbd\xae')),
('date', models.CharField(blank=True, max_length=50, null=True, verbose_name=b'\xe6\x89\xa7\xe8\xa1\x8c\xe6\x97\xa5\xe6\x9c\x9f')),
('time', models.CharField(blank=True, max_length=50, null=True, verbose_name=b'\xe6\x89\xa7\xe8\xa1\x8c\xe6\x97\xb6\xe9\x97\xb4')),
],
options={
'db_table': 'binlogParseUndo',
'verbose_name': 'bin\u65e5\u5fd7\u9006\u5411\u89e3\u6790',
'verbose_name_plural': 'bin\u65e5\u5fd7\u9006\u5411\u89e3\u6790',
},
),
]
25 changes: 25 additions & 0 deletions apps/MysqlOps/migrations/0004_auto_20181107_1022.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.15 on 2018-11-07 02:22
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('MysqlOps', '0003_binlogparseundo'),
]

operations = [
migrations.AlterField(
model_name='binlogparseundo',
name='date',
field=models.DateField(blank=True, max_length=50, null=True, verbose_name=b'\xe6\x89\xa7\xe8\xa1\x8c\xe6\x97\xa5\xe6\x9c\x9f'),
),
migrations.AlterField(
model_name='binlogparseundo',
name='time',
field=models.TimeField(blank=True, max_length=50, null=True, verbose_name=b'\xe6\x89\xa7\xe8\xa1\x8c\xe6\x97\xb6\xe9\x97\xb4'),
),
]
28 changes: 28 additions & 0 deletions apps/MysqlOps/migrations/0005_auto_20181107_1030.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.15 on 2018-11-07 02:30
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('MysqlOps', '0004_auto_20181107_1022'),
]

operations = [
migrations.RemoveField(
model_name='binlogparseundo',
name='date',
),
migrations.RemoveField(
model_name='binlogparseundo',
name='time',
),
migrations.AddField(
model_name='binlogparseundo',
name='datetime',
field=models.DateTimeField(blank=True, max_length=50, null=True, verbose_name=b'\xe6\x89\xa7\xe8\xa1\x8c\xe6\x97\xb6\xe9\x97\xb4'),
),
]
20 changes: 20 additions & 0 deletions apps/MysqlOps/migrations/0006_auto_20181107_1031.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.15 on 2018-11-07 02:31
from __future__ import unicode_literals

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('MysqlOps', '0005_auto_20181107_1030'),
]

operations = [
migrations.RenameField(
model_name='binlogparseundo',
old_name='datetime',
new_name='date_time',
),
]
29 changes: 29 additions & 0 deletions apps/MysqlOps/migrations/0007_auto_20181107_1051.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.15 on 2018-11-07 02:51
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('MysqlOps', '0006_auto_20181107_1031'),
]

operations = [
migrations.RemoveField(
model_name='binlogparseundo',
name='date_time',
),
migrations.AddField(
model_name='binlogparseundo',
name='date',
field=models.CharField(blank=True, max_length=50, null=True, verbose_name=b'\xe6\x89\xa7\xe8\xa1\x8c\xe6\x97\xa5\xe6\x9c\x9f'),
),
migrations.AddField(
model_name='binlogparseundo',
name='time',
field=models.CharField(blank=True, max_length=50, null=True, verbose_name=b'\xe6\x89\xa7\xe8\xa1\x8c\xe6\x97\xb6\xe9\x97\xb4'),
),
]
25 changes: 25 additions & 0 deletions apps/MysqlOps/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,31 @@
reload(sys)
sys.setdefaultencoding("utf-8")


class BinlogParseRedo(models.Model):
sql = models.CharField(max_length=1900,blank=True, null=True, verbose_name='解析后的sql')
start_pos = models.IntegerField(blank=True, null=True, verbose_name='起始位置')
end_pos = models.IntegerField(blank=True, null=True, verbose_name='结束位置')
date = models.CharField(max_length=50,blank=True, null=True, verbose_name='执行日期')
time = models.CharField(max_length=50, blank=True, null=True, verbose_name='执行时间')

class Meta:
db_table = 'binlogParseRedo'
verbose_name = 'bin日志解析'
verbose_name_plural = 'bin日志解析'

class BinlogParseUndo(models.Model):
sql = models.CharField(max_length=1900,blank=True, null=True, verbose_name='逆向解析后的sql')
start_pos = models.IntegerField(blank=True, null=True, verbose_name='起始位置')
end_pos = models.IntegerField(blank=True, null=True, verbose_name='结束位置')
date = models.CharField(max_length=50,blank=True, null=True, verbose_name='执行日期')
time = models.CharField(max_length=50, blank=True, null=True, verbose_name='执行时间')

class Meta:
db_table = 'binlogParseUndo'
verbose_name = 'bin日志逆向解析'
verbose_name_plural = 'bin日志逆向解析'

class Inception_Server_Config(models.Model):
db_name = models.CharField(max_length=100, verbose_name='数据库名', blank=True, null=True)
db_host = models.CharField(max_length=100, verbose_name='数据库地址')
Expand Down
Binary file modified apps/MysqlOps/models.pyc
Binary file not shown.
36 changes: 30 additions & 6 deletions apps/MysqlOps/mysql_ops.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#!/bin/env python
#-*-coding:utf-8-*-
import MySQLdb,sys,string,time,datetime
from mysqlask.include import function as func
# from mysqlask.models import Db_name,Db_account,Db_instance,Oper_log,Task,Incep_error_log
from mysqlask.include.encrypt import prpcrypt
import MySQLdb,sys
# from mysqlask.include import function as func
from utils.mysql import MySQL
from celery import task
from dbcmdb.models import MySQL_Instance,MySQLDB_name,MySQLDB_user
from CMDB.model.mysql_modles import MySQLCluster

public_user = func.public_user
# public_user = func.public_user

#mysql
def mysql_query(sql,user,passwd,host,port,dbname):
try:
conn=MySQLdb.connect(host=host,user=user,passwd=passwd,port=int(port),connect_timeout=5,charset='utf8')
Expand All @@ -35,6 +35,30 @@ def mysql_query(sql,user,passwd,host,port,dbname):
except Exception,e:
return([str(e)],''),['error']


class MySQL_OPS(MySQL):
def __init__(self, clusterobj,dbname=''):
self.ip = clusterobj.foreign_ip
self.user = clusterobj.plat_user
self.passwd = clusterobj.plat_user_pass
self.db = dbname
self.port = int(clusterobj.foreign_port)
self._sql=MySQL(self.ip,self.port,self.db,self.user,self.passwd)
# self._conn = self.connect(self.ip, self.port, self.db, self.user, self.passwd)
# self._cursor = self._conn.cursor()
def getBinaryLog(self):
result,col = self._sql.getBinaryLog()
return result,col

def getTables(self):
if self.db:
sql='show tables;'
rc,rs=self._sql.queryAll(sql=sql)
return rc,rs
else:
rc=0
rs=None
return rc,rs
#??????????????? ???db id
def get_metadata(hosttag,flag,tbname=''):
dbname = MySQLDB_name.objects.get(dbtag=hosttag).dbname
Expand Down
Loading

0 comments on commit 544277b

Please sign in to comment.