예제 #1
0
def tables():
    form = MyForm.MyForm_input()
    if form.submit.data:
        try:
            ts = form.text.data.strip().splitlines()
            db_name = form.text2.data.strip()
            if ts and db_name:
                ts = set(ts)
                tt=('ip','port','数据库','表名','引擎','表行数','编码','版本')
                vv=[]
                Indexs = []
                INDEXS = []
                db = db_idc.idc_tableinfo
                for t_name in ts:
                    val = db.query.filter(and_(db.database_name == db_name,db.table_name == t_name)).all()
                    if val:
                        v = [str(v).encode('UTF-8').split(',') for v in val if v]
                        vv.append(v)
                        v = v[0]
                        cmds = ("show create table %s;" %v[3],"show index from %s;" %v[3])
                        for cmd in cmds:
                            Indexs.append(Mysql.Query_sql(v[0],v[1],v[2],cmd))
                    else:
                        return render_template_string('没有找到库{0}或者表{1}!'.format(db_name,t_name))
                for index in Indexs:
                    INDEX = []
                    for info in index:
                        info = [str(i).encode('UTF-8') for i in info]
                        INDEX.append(info)
                    INDEXS.append(INDEX)
                return render_template('tables_show.html',values=vv,tables=tt,INDEXS=INDEXS)
        except Exception as e:
            loging.write(e)
            return render_template_string('获取数据错误!')
    return render_template('tables.html',form=form)
예제 #2
0
def sql_kill():
    user = app.config.get('MYSQL_USER')
    password = app.config.get('MYSQL_PASSWORD')
    form = MyForm.MyForm_sql_kill()
    if form.submit.data:
        try:
            if form.ip.data and form.port.data:
                # 获取页面相关信息
                ip = form.ip.data.strip()
                port = form.port.data.strip()
                keyword = form.select_action.data
                MYSQL = Mysql.MYSQL(user, password, ip, port)
                cmd = 'show processlist;'
                i = 0
                for pid in MYSQL.Run(cmd):
                    if keyword in pid:
                        pid = pid[0]
                        cmd = 'kill %s;' % pid
                        MYSQL.Run(cmd)
                        i = i + 1
                MYSQL.Close()
                flash('清理完毕,共清除%i个sql连接......' % i)
        except Exception as e:
            flash(e)
        finally:
            return render_template('Message_static.html')
    return render_template('mysql_kill.html', form=form)
예제 #3
0
def update_java():
    produce.Async_log(g.user, request.url)
    K = '%s_%s' %(g.user,g.secret_key)
    messageKey = '%s_update_java' % K
    taskKey = 'update_java'
    form = MyForm.MyForm_updateJboss()
    if form.submit.data:
        try:
            if form.text.data:
                tags = form.text.data.strip().splitlines()
                assert len(tags)==1,'Can only execute a project at a time!'
                project = form.text.data.strip()
                warname = project.split('-')[0]
                Action = form.selectAction.data
                Gray = form.Gray.data
                Type = 1
                dbTable = db_op.java_list
                #灰度发布
                if Gray:
                    ServerList = dbTable.query.with_entities(dbTable.ip, dbTable.user).filter(db_op.DB.and_(dbTable.project == warname, dbTable.type == Type,dbTable.Gray == '1')).limit(1).all()
                    if not ServerList:
                        ServerList = dbTable.query.with_entities(dbTable.ip, dbTable.user).filter(db_op.DB.and_(dbTable.project == warname, dbTable.type == Type)).limit(1).all()
                        loging.write(ServerList[:])
                        if ServerList:
                            for ip, username in ServerList:
                                dbTable.query.filter(db_op.DB.and_(dbTable.ip == ip, dbTable.user == username)).update({dbTable.Gray:'1'})
                        else:
                            flash('%s Not found' % warname)
                            return render_template('Message.html')
                else:
                    ServerList = dbTable.query.with_entities(dbTable.ip,dbTable.user).filter(db_op.DB.and_(dbTable.project == warname,dbTable.type == Type)).all()
                if ServerList:
                    information = {}
                    information['warname'] = warname
                    information['project'] = project
                    information['ServerList'] = ServerList
                    information['Action'] = Action
                    information['key'] = messageKey
                    information['Gray'] = Gray
                    myRedis.delete(messageKey)
                    myRedis.lpush(taskKey,information)
                    mysql_operation = Mysql.mysql_op(g.user,Action,Type,project,Gray)
                    mysql_operation.op_operation()
                    Scheduler = produce.Scheduler_publish()
                    Scheduler = Scheduler.Scheduler_mem(Scheduler.job_update_java)
                    Scheduler.start()
                else:
                    flash('%s Not found' % warname)
                    return render_template('Message.html')
        except Exception as e:
                flash(e)
                return render_template('Message.html')
        return render_template('java_update_show.html')
    return render_template('java_update.html',form=form)
예제 #4
0
파일: update_php.py 프로젝트: znavy/opsweb
def update_php():
    produce.Async_log(g.user, request.url)
    K = '%s_%s' % (g.user, g.secret_key)
    Key = '%s_update_php' % K
    publish_key = '%s_update_key' % K
    form = MyForm.MyForm_php()
    if form.submit.data:
        try:
            if Redis.exists(Key):
                raise flash('项目上线操作正在执行,不能并行上线操作.请稍候......')
            Redis.lpush(Key, 'check env......')
            tm = time.strftime('%Y%m%d%H%M%S', time.localtime())
            Key_file_list = 'file_list_%s' % tm
            if form.text.data:
                List = form.text.data.strip().splitlines()
                Action = form.selectaction.data
                Gray = form.Gray.data
            else:
                return redirect(url_for('update_php.update_php'))
            lines = [line.strip() for line in List]
            lines = str(lines)
            Redis.set(Key_file_list, lines)
            Redis.expire(Key_file_list, 1800)
            Info = {}
            Type = 1
            Info['arg'] = Type
            Info['action'] = Action
            Info['Key_file_lis'] = Key_file_list
            Info['gray'] = Gray
            Redis.rpush(publish_key, str(Info))
            Redis.lpush(Key, '    --->check env pass!')
            Redis.lpush(Key, '-' * 80 + '\n')
            mysql_operation = Mysql.mysql_op(g.user, Action, Type, lines, Gray)
            mysql_operation.op_operation()
            Scheduler = produce.Scheduler_publish()
            Scheduler = Scheduler.Scheduler_mem(php_update.php_update,
                                                publish_key, Key)
            Scheduler.start()
        except Exception as e:
            Redis.lpush(Key, e)
        return render_template('php_update_show.html')
    return render_template('php_update.html', form=form)
예제 #5
0
def update_php():
    produce.Async_log(g.user, request.url)
    K = '%s_%s' %(g.user,g.secret_key)
    Key = '%s_update_php' %K
    publish_key = '%s_update_key' %K
    form = MyForm.MyForm_php()
    if form.submit.data:
        try:
            if Redis.exists(Key):
                raise flash('上线操作过于频繁,请稍等%s秒......' %Redis.ttl(Key))
            Redis.lpush(Key, 'check env......')
            tm = time.strftime('%Y%m%d%H%M%S',time.localtime())
            Key_file_list ='file_list_%s' %tm
            if form.text.data:
                List = form.text.data.strip().splitlines()
                Action = form.selectaction.data
                Gray = form.Gray.data
            else:
                return redirect(url_for('update_php.update_php'))
            lines = [line.strip() for line in List]
            lines = str(lines)
            Redis.set(Key_file_list,lines)
            Redis.expire(Key_file_list,1800)
            Info = {}
            Type = 1
            Info['arg'] = Type
            Info['action'] = Action
            Info['Key_file_lis'] = Key_file_list
            Info['gray'] = Gray
            Redis.rpush(publish_key,str(Info))
            Redis.lpush(Key, '    --->check env pass!')
            Redis.lpush(Key, '-' * 80 + '\n')
            mysql_operation = Mysql.mysql_op(g.user,Action,Type,lines,Gray)
            mysql_operation.op_operation()
            Scheduler = produce.Scheduler_publish()
            Scheduler = Scheduler.Scheduler_mem(php_update.php_update,publish_key,Key)
            Scheduler.start()
        except Exception as e:
            Redis.lpush(Key,e)
        return render_template('php_update_show.html',Main_Infos=g.main_infos)
    return render_template('php_update.html',Main_Infos=g.main_infos,form=form)
예제 #6
0
def sql_create():
    form = MyForm.MyForm_sql_create()
    if form.submit.data:
        try:
            tt = time.strftime('%Y%m', time.localtime())
            db_name = form.db_name.data.strip()
            assert db_name, '数据库库名不能为空!'
            sql_master = form.sql_master.data.strip()
            assert sql_master, '主库服务器地址不能为空'
            db_mysql = db_idc.idc_mysqldb
            master_port = db_mysql.query.with_entities(db_mysql.port).filter(
                and_(db_mysql.master == '是', db_mysql.ip == sql_master)).all()
            assert master_port, '没有找到主库端口信息!'
            sql_slave = db_mysql.query.with_entities(
                db_mysql.ip, db_mysql.port).filter(
                    and_(db_mysql.slave == '是',
                         db_mysql.Master_Host == sql_master)).all()
            assert sql_slave, '没有找到相关从库信息!'
            #创建数据库
            master_port = master_port[0][0]
            MYSQL = Mysql.MYSQL(host=sql_master, port=master_port)
            MYSQL.Run(
                "UPDATE `mysql`.`user` SET `Grant_priv` = 'Y' WHERE `Grant_priv` = 'N' AND `User` = 'mysql_op';"
            )
            MYSQL.Run("FLUSH PRIVILEGES;")
            Error_info = MYSQL.Run('create database %s;' % db_name)
            if Error_info:
                raise flash(Error_info)
            else:
                flash('%s数据库已创建!' % db_name)
            Error_info = MYSQL.Run(
                "GRANT SELECT,DELETE,UPDATE,INSERT ON {0}.* TO '{0}'@'172.16.%' IDENTIFIED BY '{0}@baihe_{1}';"
                .format(db_name, tt))
            MYSQL.Run("FLUSH PRIVILEGES;")
            if Error_info:
                raise flash(Error_info)
            else:
                flash('%s数据库权限已开通!' % db_name)
            MYSQL.Close()
            #写入dns配置库
            db = db_op.dns_innr
            #master
            c = db(domain='sql.baihe',
                   field='%s.master' % db_name.replace('_', '-'),
                   Type='A',
                   ip=sql_master,
                   stats='1',
                   system='xs')
            db_op.DB.session.add(c)
            db_op.DB.session.commit()
            flash('主库DNS解析完毕!')
            #slave
            flash('*' * 50)
            flash('用户名:%s' % db_name)
            flash('密码:%s@baihe_%s' % (db_name, tt))
            flash('主库地址:%s   端口:%s' % ('%s.master.sql.baihe' %
                                       db_name.replace('_', '-'), master_port))
            for slave_ip, slave_port in sql_slave:
                c = db(domain='sql.baihe',
                       field='%s.slave%s' %
                       (db_name.replace('_', '-'), slave_ip.split('.')[-1]),
                       Type='A',
                       ip=slave_ip,
                       stats='1',
                       system='xs')
                db_op.DB.session.add(c)
                db_op.DB.session.commit()
                flash('从库地址:%s   端口:%s' %
                      ('%s.slave%s.sql.baihe' %
                       (db_name.replace('_', '-'), slave_ip.split('.')[-1]),
                       slave_port))
            flash('*' * 50)
            flash('从库DNS解析完毕!')
        except Exception as e:
            if 'old-style' not in str(e):
                flash(e)
    return render_template('mysql_create.html',
                           Main_Infos=g.main_infos,
                           form=form)
예제 #7
0
def publish_php():
    produce.Async_log(g.user, request.url)
    K = '%s_%s' %(g.user,g.secret_key)
    Key = '%s_publish_php' %K
    form = MyForm.MyForm_php()
    qrcode_url = "https://xxx.baihe.com/xxx/{0}/{1}".format(g.user,g.grade)
    if form.submit.data:
        try:
            Redis.lpush(Key, 'check env......')
            if form.text.data and form.changelog.data:
                action = form.selectaction.data
                Type = int(form.selecttype.data)
                Way = form.selectWay.data
                if Type == 1:
                    platfrom = '线上'
                if Type == 2:
                    platfrom = '测外'
                Tags = form.text.data.strip().splitlines()
                Gray = form.Gray.data
                code = form.code.data
                work = form.selectwork.data
                grade = form.selectgrade.data
                changelog = form.changelog.data
                if Way == 'SVN':
                    if len(Tags) > 1:
                        raise flash('错误:只能同时上线一个项目!')
                    else:
                        for line in Tags:
                            if line.startswith('#') or not line.split():
                                continue
                            m = re.match(r'[0-9]', line)
                            if m:
                                raise flash('错误:不能以数字开头!')
                            if not line.startswith('/'):
                                raise flash('错误:' + line + '路径必须以"/"开头!')
                            path = line.strip()
                            path = path.replace('\\', '/')
                            App = path.split('/')
                            if len(App)<4:
                                raise flash('错误:路径错误,无法获取项目名!')
                            App = App[3]
                            if len(path.split('-')) >= 2:
                                version = path.split('-')[-1]
                            else:
                                raise flash('错误:项目名称格式错误!')
                            if version.endswith('.zip'):
                                version = version.replace('.zip', '')
                elif Way == 'GIT':
                    if len(Tags) != 2:
                        raise flash('错误:GIT上线格式不对!')
                    elif not Tags[0].strip().startswith('http://'):
                        raise flash('错误:' + Tags[0] + '路径需以"http://"开头!')
                    elif not Tags[0].strip().endswith('.git'):
                        raise flash('错误:' + Tags[0] + '应该以".git"结尾!')
                    elif 'tag' not in Tags[1]:
                        raise flash('错误:没有tag版本号!')
                    path = ';'.join(Tags)
                    path = path.replace('http://git.baihe.com/', '/')
                    App = Tags[0].strip().split('/')[-1]
                    App = App.split('.')[0]
                    version = Tags[1].split('-')[-1]
            else:
                raise flash('错误:文本框内容不能为空!')
        except Exception as e:
                pass
        else:
            try:
                #用户权限判断
                if g.grade == 2:
                    if code:
                        verify_key = "{0}_{1}".format(g.user,g.grade)
                        verify_code = Redis.get(verify_key)
                        if verify_code == str(code):
                            Redis.lpush(Key, '    --->verify code pass!')
                            Redis.delete(verify_key)
                            code_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
                            db = db_op.publish_code
                            db_op.DB.session.add(db(project=App, code=str(code), platfrom=platfrom, user=g.user, Time=code_time))
                            db_op.DB.session.commit()
                        else:
                            flash('上线码验证错误!')
                            return redirect(url_for('publish_php.publish_php'))
                    else:
                        flash('上线码不能为空!')
                        return redirect(url_for('publish_php.publish_php'))
                Redis.lpush(Key,'    --->check env pass!')
                Redis.lpush(Key,'-'*80+'\n')
                db = db_op.php_list
                if Gray:
                    if App == 'baihePhpGlobalLibrary_publish':
                        val = db.query.with_entities(db.ip).filter(and_(db.Gray == '1',db.operation == 'baihe')).limit(1).all()
                        if not val:
                            val = db.query.with_entities(db.ip).filter(and_(db.type == '%i' % Type,db.operation == 'baihe')).limit(1).all()
                    elif App == 'Baihe':
                        val = db.query.with_entities(db.ip).filter(and_(db.Gray == '1',db.operation == 'hunli')).limit(1).all()
                        if not val:
                            val = db.query.with_entities(db.ip).filter(and_(db.type == '%i' % Type,db.operation == 'hunli')).limit(1).all()
                    else:
                        val = db.query.with_entities(db.ip).filter(and_(db.Gray == '1',db.project == '%s' % App)).limit(1).all()
                        if not val:
                            val = db.query.with_entities(db.ip).filter(and_(db.project == '%s' % App, db.type == '%i' % Type)).limit(1).all()
                    if val:
                        sip = [v[0].encode('UTF-8') for v in val if v]
                        db.query.filter(and_(db.project == App, db.ip == sip[0])).update({db.Gray: '1'})
                    else:
                        raise flash('%s 没有在上线列表中找到!' % App)
                else:
                    if App == 'baihePhpGlobalLibrary_publish':
                        val = db.query.with_entities(db.ip).filter(and_(db.type == '%i' %Type,db.operation == 'baihe')).all()
                    elif App == 'Baihe':
                        val = db.query.with_entities(db.ip).filter(and_(db.type == '%i' %Type,db.operation == 'hunli')).all()
                    else:
                        val = db.query.with_entities(db.ip).filter(and_(db.project == '%s' %App,db.type == '%i' %Type)).all()
                    if val:
                        val = set(val)
                        sip = [v[0].encode('UTF-8') for v in val if v]
                    else:
                        raise flash('%s 没有在上线列表中找到!' %App)
                Redis.delete(Key)
                Info = {}
                Info['action'] = action
                Info['key'] = Key
                Info['path'] = path
                Info['app'] = App
                Info['sip'] = sip
                Info['gray'] = Gray
                Info['Type'] = Type
                Info['Way'] = Way
                Redis.lpush('publish_php',str(Info))
                mysql_operation = Mysql.mysql_op(g.user,action,Type,App,version,Gray,work,grade,changelog)
                mysql_operation.op_operation()
                Scheduler = produce.Scheduler_publish()
                Scheduler = Scheduler.Scheduler_mem(Scheduler.job_publish_php)
                Scheduler.start()
            except Exception as e:
                if 'old' not in str(e):
                    flash(e)
                Redis.lpush(Key,'End')
                return render_template('Message.html')
            return render_template('php_publish_show.html')
    return render_template('php_publish.html',form=form,qrcode_url = qrcode_url )
예제 #8
0
def publish_java():
    produce.Async_log(g.user, request.url)
    K = '%s_%s' %(g.user,g.secret_key)
    messageKey = '%s_publish_java' % K
    publish_key = '%s_publish_key' % K
    qrcode_url = None
    form = MyForm.MyForm_publishJboss()
    if form.submit.data:
        Action = form.selectAction.data
        Type = int(form.selectType.data)
        Gray = form.Gray.data
        Way = form.selectWay.data
        code = form.code.data
        work = form.selectwork.data
        changelog = form.changelog.data
        tags = form.text.data.strip().splitlines()
        if tags and changelog:
            try:
                if myRedis.exists(messageKey):
                    raise flash('上线操作过于频繁,请稍等%s秒......' %myRedis.ttl(messageKey))
                assert len(tags) == 1, '错误:只能同时上线一个项目!'
                warTagName = tags[0]
                version = '0.0.0'
                if Action == 'restart':
                    warname = warTagName
                elif Way == 'SVN':
                    assert '.war.zip' in warTagName, '错误:格式错误!格式应为: baihe-xxxx-x.x.x.war.zip'
                    warname = warTagName.split('-')[:-1]
                    version = warTagName.split('-')[-1].replace('.war.zip','')
                    warname = '-'.join(warname) + '.war'
                elif Way == 'GIT':
                    warname = "{0}.war".format(warTagName.split('/')[-1])
            except Exception as e:
                flash(e)
            else:
                try:
                    project_db = db_op.project_level
                    project_level = project_db.query.with_entities(project_db.level).filter(project_db.project == warname).all()
                    # 测外不强制项目等级
                    if Type == 2:
                        if project_level:
                            project_level = int(project_level[0][0])
                        else:
                            project_level = 1
                    else:
                        if project_level:
                            project_level = int(project_level[0][0])
                            crypto = Md5.crypto(crypto_key)
                            qrcode_url = "https://op.baihe.com/qrcode_java/{0}/{1}/{2}/{3}".format(crypto.encrypt(g.user),g.grade,crypto.encrypt(warname),project_level)
                        else:
                            raise flash('没有找到该项目的对应项目等级!')
                    publish_time = time.strftime("%H", time.localtime())
                    if code:
                        verify_key = "{0}_{1}".format(g.user,warname)
                        verify_code = myRedis.get(verify_key)
                        if verify_code == str(code):
                            myRedis.lpush(messageKey, '    --->verify code pass!')
                            myRedis.expire(messageKey,30)
                            myRedis.delete(verify_key)
                        else:
                            raise flash('%s 该上线码验证不通过!' %str(code))
                    else:
                        # 用户权限及行为判断
                        tw = int(time.strftime('%w', time.localtime(time.time())))
                        if g.grade >= 2 and Type == 1 and Action == 'publish':
                            if project_level >= 5:
                                raise flash('该项目等级为%s级,需申请上线码!' % project_level)
                            if tw in (2, 4):
                                if project_level <= 4 and (int(publish_time) >= 17 or int(publish_time) <= 9):
                                    raise flash('仅允许在10-17点时间段进行上线操作,需申请上线码!')
                            else:
                                raise flash('4级及以下常规项目只限在周二和周四上线,修复BUG需申请上线码!')
                        if g.grade >= 2 and Action == 'rollback':
                            raise flash('没有权限进行回滚操作!')
                    dbTable = db_op.java_list
                    #灰度发布
                    if Gray:
                        ServerList = dbTable.query.with_entities(dbTable.ip, dbTable.user).filter(db_op.DB.and_(dbTable.project == warname, dbTable.type == Type,dbTable.Gray == '1')).limit(1).all()
                        if not ServerList:
                            ServerList = dbTable.query.with_entities(dbTable.ip, dbTable.user).filter(db_op.DB.and_(dbTable.project == warname, dbTable.type == Type)).limit(1).all()
                            if ServerList:
                                for ip, username in ServerList:
                                    dbTable.query.filter(db_op.DB.and_(dbTable.ip == ip, dbTable.user == username)).update({dbTable.Gray:'1'})
                            else:
                                raise flash('%s 没有在上线列表中找到!' % warname)
                    else:
                        ServerList = dbTable.query.with_entities(dbTable.ip,dbTable.user).filter(db_op.DB.and_(dbTable.project == warname,dbTable.type == Type)).all()
                        if Type == 1:
                            ServerList.append(('172.16.4.188','java'))
                    if ServerList:
                        myRedis.lpush(messageKey, 'check security policy......')
                        myRedis.expire(messageKey, 30)
                        information = {}
                        information['warname'] = warname
                        information['warTagName'] = warTagName
                        information['ServerList'] = ServerList
                        information['Action'] = Action
                        information['Gray'] = Gray
                        information['Type'] = Type
                        information['Way']  = Way
                        myRedis.lpush(publish_key,information)
                        mysql_operation = Mysql.mysql_op(g.user,Action,Type,warname,version,Gray,work,project_level,changelog)
                        mysql_operation.op_operation()
                        Scheduler = produce.Scheduler_publish()
                        Scheduler = Scheduler.Scheduler_mem(java_publish.java_publish,publish_key,messageKey)
                        Scheduler.start()
                        myRedis.lpush(messageKey, '    --->check pass!')
                        myRedis.lpush(messageKey, '-' * 80 + '\n')
                    else:
                        raise flash('%s 没有在上线列表中找到!' % warname)
                except Exception as e:
                    if 'old' not in str(e):
                        flash(e)
                    if qrcode_url:
                        return render_template('java_publish.html',Main_Infos=g.main_infos, form=form, qrcode_url=qrcode_url)
                else:
                    return render_template('java_publish_show.html',Main_Infos=g.main_infos)
        else:
            flash("错误:文本框内容不能为空!")
    return render_template('java_publish.html',Main_Infos=g.main_infos,form=form,qrcode_url=qrcode_url)
예제 #9
0
def mysql_parse():
    form = MyForm.MyForm_input()
    db = db_idc.idc_mysqldb
    db_table = db_idc.idc_tableinfo
    if form.submit.data:
        if form.text.data and form.input.data:
            mysql_db = form.input.data.strip()
            try:
                if ';' in form.text.data.strip():
                    sql_lines = form.text.data.strip().replace('\r\n', ' ')
                    for Line in sql_lines.split(';'):
                        if '.' in Line:
                            raise flash('%s 语句中不能有库名!' % Line)
                        if Line:
                            Line = '%s ;' % Line.strip()
                            line = Line.lower()
                            if 'delete' in line:
                                table_name = Line.split('from')[1].split()[0]
                            elif 'update' in line:
                                table_name = Line.split()[1]
                            elif 'alter' in line:
                                table_name = Line.split()[2]
                            else:
                                flash(Line)
                                flash('------->该SQL语句不在审查范围内!')
                                continue
                            # 从sql语句中抽取表名
                            val = db_table.query.with_entities(
                                db_table.ip, db_table.port,
                                db_table.Engine_name, db_table.Rows).filter(
                                    and_(
                                        db_table.database_name == mysql_db,
                                        db_table.table_name ==
                                        table_name.replace('`', ''))).all()
                            if val:
                                mysql_ip, mysql_port, Engine_name, Rows = val[
                                    0]
                                flash('----' * 24)
                                flash('数据库IP:{0},数据库端口:{1},表引擎:{2},表行数:{3}'.
                                      format(mysql_ip, mysql_port, Engine_name,
                                             Rows))
                                flash('----' * 24)
                                MYSQL = Mysql.MYSQL(user, password, mysql_ip,
                                                    mysql_port)
                                cmd = "explain %s" % Line
                                flash(Line)
                                result = MYSQL.Run(cmd)
                                if isinstance(result, list):
                                    flash('------->语法检查通过!')
                                else:
                                    raise flash('------->语法有错误! %s' %
                                                str(result))
                                MYSQL.Close()
                                if (int(Rows) >= 10000
                                        and Engine_name == 'MyISAM') or (
                                            int(Rows) >= 50000
                                            and Engine_name == 'InnoDB'):
                                    result = 'SQL语句建议进行定时执行!'
                                else:
                                    result = 'SQL语句可以直接执行!'
                                flash('----' * 24)
                                flash('审核结果:{0}'.format(result))
                                flash('----' * 24)
                            else:
                                raise flash('没有找到%s数据库的相关服务器信息!' % mysql_db)
                else:
                    flash('SQL语句没有以分号结尾!')
                    return render_template('Message.html')
            except Exception as e:
                if 'old-style' not in str(e):
                    flash(e)
            return render_template('Message_static.html')
        else:
            flash('输入框内容不能为空!')
            return render_template('Message.html')
    return render_template('mysql_parse.html', form=form)
예제 #10
0
def publish_java():
    produce.Async_log(g.user, request.url)
    K = '%s_%s' % (g.user, g.secret_key)
    messageKey = '%s_publish_java' % K
    publish_key = '%s_publish_key' % K
    qrcode_url = "https://op.baihe.com/qrcode_java/{0}/{1}".format(
        g.user, g.grade)
    form = MyForm.MyForm_publishJboss()
    if form.submit.data:
        Action = form.selectAction.data
        Type = int(form.selectType.data)
        Gray = form.Gray.data
        Way = form.selectWay.data
        code = form.code.data
        work = form.selectwork.data
        grade = form.selectgrade.data
        changelog = form.changelog.data
        tags = form.text.data.strip().splitlines()
        if tags and changelog:
            try:
                if myRedis.exists(messageKey):
                    raise flash('项目上线操作正在执行,不能并行上线操作.请稍候......')
                myRedis.lpush(messageKey, 'check env......')
                assert len(tags) == 1, '错误:只能同时上线一个项目!'
                warTagName = tags[0]
                version = '0.0.0'
                if Action == 'restart':
                    warname = warTagName
                elif Way == 'SVN':
                    assert '.war.zip' in warTagName, '错误:格式错误!格式应为: baihe-xxxx-x.x.x.war.zip'
                    warname = warTagName.split('-')[:-1]
                    version = warTagName.split('-')[-1].replace('.war.zip', '')
                    warname = '-'.join(warname) + '.war'
                elif Way == 'GIT':
                    warname = "{0}.war".format(warTagName.split('/')[-1])
            except Exception as e:
                flash(e)
            else:
                try:
                    publish_time = time.strftime("%H", time.localtime())
                    if code:
                        verify_key = "{0}_{1}".format(g.user, g.grade)
                        verify_code = myRedis.get(verify_key)
                        if verify_code == str(code):
                            myRedis.lpush(messageKey,
                                          '    --->verify code pass!')
                            myRedis.delete(verify_key)
                        else:
                            raise flash('上线码验证错误!')
                    else:
                        # 用户权限判断
                        if g.grade >= 2 and Type == 1 and int(grade) >= 5:
                            raise flash('需申请验证码!')
                        if g.grade >= 2 and int(grade) <= 4 and Type == 1 and (
                                int(publish_time) >= 17
                                or int(publish_time) <= 9):
                            raise flash('仅允许在10-17点时间段进行自助操作,需申请验证码!')
                    dbTable = db_op.java_list
                    #灰度发布
                    if Gray:
                        ServerList = dbTable.query.with_entities(
                            dbTable.ip, dbTable.user).filter(
                                db_op.DB.and_(
                                    dbTable.project == warname,
                                    dbTable.type == Type,
                                    dbTable.Gray == '1')).limit(1).all()
                        if not ServerList:
                            ServerList = dbTable.query.with_entities(
                                dbTable.ip, dbTable.user).filter(
                                    db_op.DB.and_(
                                        dbTable.project == warname,
                                        dbTable.type == Type)).limit(1).all()
                            loging.write(ServerList[:])
                            if ServerList:
                                for ip, username in ServerList:
                                    dbTable.query.filter(
                                        db_op.DB.and_(
                                            dbTable.ip == ip,
                                            dbTable.user == username)).update(
                                                {dbTable.Gray: '1'})
                            else:
                                raise flash('%s 没有在上线列表中找到!' % warname)
                    else:
                        ServerList = dbTable.query.with_entities(
                            dbTable.ip, dbTable.user).filter(
                                db_op.DB.and_(dbTable.project == warname,
                                              dbTable.type == Type)).all()
                        if Type == 1:
                            ServerList.append(('172.16.4.188', 'java'))
                    if ServerList:
                        myRedis.lpush(messageKey, '    --->check env pass!')
                        myRedis.lpush(messageKey, '-' * 80 + '\n')
                        information = {}
                        information['warname'] = warname
                        information['warTagName'] = warTagName
                        information['ServerList'] = ServerList
                        information['Action'] = Action
                        information['Gray'] = Gray
                        information['Type'] = Type
                        information['Way'] = Way
                        myRedis.lpush(publish_key, information)
                        mysql_operation = Mysql.mysql_op(
                            g.user, Action, Type, warname, version, Gray, work,
                            grade, changelog)
                        mysql_operation.op_operation()
                        Scheduler = produce.Scheduler_publish()
                        Scheduler = Scheduler.Scheduler_mem(
                            java_publish.java_publish, publish_key, messageKey)
                        Scheduler.start()
                    else:
                        raise flash('%s 没有在上线列表中找到!' % warname)
                except Exception as e:
                    if 'old' not in str(e):
                        flash(e)
                else:
                    return render_template('java_publish_show.html')
        else:
            flash("错误:文本框内容不能为空!")
    return render_template('java_publish.html',
                           form=form,
                           qrcode_url=qrcode_url)
예제 #11
0
파일: Task2.py 프로젝트: yuhaozi2/opsweb
def get_mysqldb_info():
    try:
        db_third = db_idc.third_resource
        db_mysqldb = db_idc.idc_mysqldb
        IDC_MySql = Mysql.MYSQL(MYSQL_USER, MYSQL_PW, MYSQL_HOST, MYSQL_PORT, 'idc')
        IDC_MySql.Run("TRUNCATE TABLE tableinfo;")
        infos = IDC_MySql.Run("select ip,port from mysqldb where master = '是' or slave = '是';")
    except Exception as e:
        logging.error(e)
    else:
        for info in infos:
            ip,port = info
            try:
                INFO_MySql = Mysql.MYSQL(MYSQL_INFO_USER, MYSQL_INFO_PW, ip, port, 'mysql')
                exclude_db = ('mysql', 'test', 'information_schema', 'performance_schema')
                cmd = "show databases;"
                Lists = INFO_MySql.Run(cmd)
                Lists = [db[0] for db in Lists if db[0] not in exclude_db]
                db_lists = ','.join(Lists)
            except:
                continue
            else:
                try:
                    cmd = "SHOW SLAVE STATUS;"
                    result = INFO_MySql.Run(cmd)
                    if result:
                        result = list(result[0])
                        m_ip = result[1].strip()
                        m_user = result[2].strip()
                        m_port = result[3]
                        s_io = result[10].strip()
                        s_sql = result[11].strip()
                        #判断获取的master主机信息是否需要解析
                        if len(m_ip.split('.')) != 4:
                            ssh_port = db_third.query.with_entities(db_third.ssh_port).filter(and_(db_third.ip==ip,db_third.app_port==port)).all()
                            ssh_port = ssh_port[0][0]
                            Ssh = SSH.ssh(ip=ip, ssh_port=ssh_port)
                            ssh_cmd = "/bin/cat /etc/hosts|/bin/grep {0}".format(str(m_ip))
                            values = Ssh.Run(ssh_cmd)
                            if values['stdout']:
                                m_ip = values['stdout'][0].split()[0]
                            else:
                                m_ip = None
                        if m_ip:
                            if s_io == 'Yes' and s_sql == 'Yes':
                                #判断真实的主库服务器信息
                                values = db_mysqldb.query.with_entities(db_mysqldb.Master_Host,db_mysqldb.Master_Port).filter(
                                    and_(db_mysqldb.ip == m_ip, db_mysqldb.port == m_port,db_mysqldb.slave == '是')).all()
                                if values:
                                    m_ip,m_port = values[0]
                                cmd = "update mysqldb set slave='是',master='否',db='%s',Master_Host='%s',Master_Port='%s',Master_User='******' where ip='%s' and port=%i;" % \
                                      (db_lists, m_ip, m_port, m_user, ip, int(port))
                            else:
                                check_cmd = "show slave hosts;"
                                check_result = IDC_MySql.Run(check_cmd)
                                if check_result:
                                    cmd = "update mysqldb set slave='否',master='是',db='%s',Master_Host='',Master_Port='',Master_User='' where ip='%s' and port=%i;" % (
                                    db_lists, ip, int(port))
                    else:
                        cmd = "update mysqldb set slave='否',master='是',db='%s',Master_Host='',Master_Port='',Master_User='' where ip='%s' and port=%i;" % (
                        db_lists, ip, int(port))
                    IDC_MySql.Run(cmd)
                except Exception as e:
                    INFO_MySql.Close()
                    logging.error(e)
                try:
                    version = INFO_MySql.Run("show variables like 'version';")
                    version = version[0][-1]
                    for db in Lists:
                        cmd = "SHOW TABLE STATUS from %s;" % db
                        results = INFO_MySql.Run(cmd)
                        if results:
                            for result in results:
                                table_name = result[0]
                                table_engine = result[1]
                                table_Rows = int(result[4])
                                table_size = int(result[6]) + int(result[8])
                                if len(str(table_size)) > 9:
                                    table_size = '%sGB' % (table_size / 1000 / 1000 / 1000)
                                elif len(str(table_size)) > 6:
                                    table_size = '%sMB' % (table_size / 1000 / 1000)
                                else:
                                    table_size = '%sKB' % (table_size / 1000)
                                table_charset = result[14]
                                cmd = "insert into tableinfo (ip, port, database_name, table_name, Engine_name, Rows,size,Charset, version,update_time) VALUES ('%s',%i,'%s','%s','%s',%i,'%s','%s','%s',now());" % \
                                      (ip, int(port), db, table_name, table_engine, table_Rows, table_size, table_charset, version)
                                IDC_MySql.Run(cmd)
                except Exception as e:
                    logging.error(e)
                INFO_MySql.Close()
    finally:
        IDC_MySql.Close()
        db_idc.DB.session.remove()
예제 #12
0
파일: Task2.py 프로젝트: yuhaozi2/opsweb
def task_run():
    try:
        try:
            # 获取业务访问数据
            db_business = db_op.business
            db_project = db_op.project_list
            business = db_business.query.with_entities(db_business.id, db_business.business).all()
            business = {busi[0]: busi[1] for busi in business}
            year = time.strftime('%Y', time.localtime())
            ot = datetime.datetime.now() - datetime.timedelta(days=0)
            ot = ot.strftime('%Y-%m-%dT00:00:00Z')
            Key = 'op_business_pv_%s' % ot.split('T')[0]
            Influx_cli = InfluxDBClient(influxdb_host, influxdb_port, influxdb_user, influxdb_pw, 'analysis_logs')
            for id in business:
                business_domain = db_project.query.with_entities(distinct(db_project.domain)).filter(
                    db_project.business_id == int(id)).all()
                if business_domain:
                    hosts = [domain[0] for domain in business_domain]
                    pv_sum = []
                    for host in hosts:
                        if ',' in host:
                            hosts.extend(host.split(','))
                        else:
                            try:
                                cmd = 'select sum(mean_pv) from ' + 'analysis%s' % year + " where time >='%s' and host = '%s';" % (
                                ot, host)
                                result = Influx_cli.query(cmd)
                                if result:
                                    for infos in result.get_points():
                                        if infos:
                                            pv_sum.append(infos['sum'] * 60)
                            except Exception as e:
                                logging.error(e)
                    if pv_sum:
                        pv_sum = reduce(lambda x, y: x + y, pv_sum)
                        RC_CLUSTER.hset(Key, business[id], pv_sum)
        except Exception as e:
            logging.error(e)

        #获取数据库状态
        now_date = datetime.datetime.now()
        gd = now_date.strftime('%Y-%m-%dT%H:%M:%SZ')
        influx_fields = defaultdict()
        db_mysqldb = db_idc.idc_mysqldb
        infos = db_mysqldb.query.with_entities(db_mysqldb.ip,db_mysqldb.port).filter(or_(db_mysqldb.master == '是',db_mysqldb.slave == '是')).all()
        Influx_cli = InfluxDBClient(influxdb_host, influxdb_port, influxdb_user, influxdb_pw,'mysqld_status')
        for info in infos:
            try:
                ip, port = info
                MySql = Mysql.MYSQL(MYSQL_INFO_USER, MYSQL_INFO_PW, ip, port, 'mysql')
                vals = MySql.Run("SHOW GLOBAL STATUS;")
            except:
                continue
            else:
                if vals:
                    vals = {val[0]: val[1] for val in vals}
                    #获取QPS
                    Key = 'op_mysqld_QPS_%s_%s' %(ip,port)
                    QPS = RC.getset(Key, int(vals['Questions']))
                    RC.expire(Key,3600)
                    if not QPS:
                        QPS = int(vals['Questions'])
                    influx_fields['QPS'] = float(int(vals['Questions']) - int(QPS)) / 3000
                    #获取TPS
                    Key = 'op_mysqld_TPS_%s_%s' % (ip, port)
                    TPS = RC.getset(Key, int(vals['Com_commit']) + int(vals['Com_rollback']))
                    RC.expire(Key,3600)
                    if not TPS:
                        TPS = int(vals['Com_commit']) + int(vals['Com_rollback'])
                    influx_fields['TPS'] = float(int(vals['Com_commit']) + int(vals['Com_rollback']) - int(TPS)) / 3000
                    #获取读写比
                    influx_fields['R/W'] = (int(vals['Com_select']) + int(vals['Qcache_hits'])) / (float(int(vals['Com_insert']) +int(vals['Com_update']) + int(vals['Com_delete']) + int(vals['Com_replace']))) * 100
                    #获取慢查询占比
                    influx_fields['S/Q'] = float(vals['Slow_queries']) / int(vals['Questions']) * 100
                    #获取接受流量
                    Key = 'op_mysqld_Bytes_received_%s_%s' % (ip, port)
                    Bytes_received =RC.getset(Key, int(vals['Bytes_received']))
                    RC.expire(Key,3600)
                    if not Bytes_received:
                        Bytes_received = int(vals['Bytes_received'])
                    influx_fields['Bytes_r'] = float((int(vals['Bytes_received']) - int(float(Bytes_received))))/1000
                    #获取发送流量
                    Key = 'op_mysqld_Bytes_sent_%s_%s' % (ip, port)
                    Bytes_sent =RC.getset(Key, float(vals['Bytes_sent']))
                    RC.expire(Key,3600)
                    if not Bytes_sent:
                        Bytes_sent = int(vals['Bytes_sent'])
                    influx_fields['Bytes_s'] = (int(vals['Bytes_sent']) - int(float(Bytes_sent)))/1000
                    #获取连接数
                    influx_fields['Connections'] = len(MySql.Run("SHOW processlist;"))
                    # 写入influxdb数据库
                    if influx_fields:
                        try:
                            json_body = [{"measurement": "performance", "tags": {"ip": ip, "port":port},
                                          "fields": influx_fields, "time": gd}]
                            Influx_cli.write_points(json_body)
                        except Exception as e:
                            logging.error(e)
                MySql.Close()
    except Exception as e:
        logging.error(e)
    finally:
        db_idc.DB.session.remove()
        db_op.DB.session.remove()
    #获取k8s的hpa副本数量
    try:
        td = time.strftime('%Y-%m-%d',time.localtime())
        th = time.strftime('%H:%M',time.localtime())
        v1 = client.AutoscalingV1Api()
        ret = v1.list_horizontal_pod_autoscaler_for_all_namespaces()
        Key = 'op_hpa_chart_%s' %td
        for i in ret.items:
            RC.hset(Key,'%s_%s'%(i.spec.scale_target_ref.name,th),i.status.current_replicas)
    except Exception as e:
        logging.error(e)
예제 #13
0
def tables(db_name=None, table_name=None, host_info=None):
    table_infos = {}
    tb_index = ('Non_unique', 'Key_name', 'Seq_in_index', 'Column_name',
                'Collation', 'Cardinality', 'Sub_part', 'Packed', 'Null',
                'Index_type', 'Comment', 'Index_comment')
    tb_columns = ('Field', 'Type', 'Collation', 'Null', 'Key', 'Default',
                  'Extra', 'Comment')
    tables = ('ip', 'port', '主从', '数据库', '表名', '引擎', '行数', '大小', '编码', '版本')
    Indexs = None
    values = None
    Columns = None
    create_table = None
    #数据库表列表
    if db_name and not table_name and not host_info:
        try:
            db_tableinfo = db_idc.idc_tableinfo
            db_mysqldb = db_idc.idc_mysqldb
            values = db_tableinfo.query.with_entities(
                db_tableinfo.ip, db_tableinfo.port, db_tableinfo.database_name,
                db_tableinfo.table_name, db_tableinfo.Engine_name,
                db_tableinfo.Rows, db_tableinfo.size, db_tableinfo.Charset,
                db_tableinfo.version).filter(
                    db_tableinfo.database_name == db_name).all()
            if values:
                values = [list(v) for v in values]
                ips = [val[:2] for val in values]
                for info in ips:
                    ip, port = info
                    ms_infos = db_mysqldb.query.with_entities(
                        db_mysqldb.master, db_mysqldb.slave).filter(
                            and_(db_mysqldb.ip == ip,
                                 db_mysqldb.port == port)).all()
                    if ms_infos[0][0] == '是':
                        table_infos['%s:%s' % (ip, port)] = '主库'
                    elif ms_infos[0][1] == '是':
                        table_infos['%s:%s' % (ip, port)] = '从库'
                    else:
                        table_infos['%s:%s' % (ip, port)] = '未知'
        except Exception as e:
            logging.error(e)
    #表结构和索引详情
    if table_name and host_info and db_name:
        try:
            ip, app_port = host_info.split(':')
            INFO_MySql = Mysql.MYSQL(MYSQL_INFO_USER, MYSQL_INFO_PW, ip,
                                     app_port, 'mysql')
            Columns = INFO_MySql.Run("SHOW full COLUMNS FROM %s.%s;" %
                                     (db_name, table_name))
            Indexs = INFO_MySql.Run("SHOW INDEX FROM %s.%s;" %
                                    (db_name, table_name))
            create_table = INFO_MySql.Run("SHOW CREATE TABLE %s.%s;" %
                                          (db_name, table_name))
            create_table = create_table[0][1]
        except Exception as e:
            logging.error(e)
        finally:
            INFO_MySql.Close
    return render_template('tables_show.html',
                           values=values,
                           tables=tables,
                           db_name=db_name,
                           table_name=table_name,
                           table_infos=table_infos,
                           Columns=Columns,
                           tb_columns=tb_columns,
                           tb_index=tb_index,
                           Indexs=Indexs,
                           create_table=create_table)
예제 #14
0
파일: sql_query.py 프로젝트: zhouqiw/opsweb
def sql_query():
    form = MyForm.MyForm_sql_run()
    if form.submit.data:
        #获取页面相关信息
        try:
            db_name = form.db_name.data.strip()
            assert db_name, '数据库库名不能为空!'
            if form.sql_text.data:
                cmds = form.sql_text.data.strip()
            else:
                raise flash('没有SQL语句可执行!')
            if cmds:
                if cmds.endswith(';'):
                    cmds = cmds.split(';')
                    if len(cmds) > 6:
                        raise flash('SQL语句查询一次不能大于5条!')
                    #获取库表对应的主库服务器信息
                    db = db_idc.idc_mysqldb
                    db_table = db_idc.idc_tableinfo
                    for sql_cmd in cmds:
                        if sql_cmd:
                            sql_cmd = sql_cmd.strip()
                            if 'limit' in sql_cmd:
                                if int(sql_cmd.split()[-1]) > 5:
                                    raise flash('SQL语句中limit 的值不能大于5')
                                sql_cmd = '%s;' % sql_cmd
                            else:
                                sql_cmd = '%s limit 5;' % sql_cmd
                            if sql_cmd.lower().startswith('select'):
                                table_name = sql_cmd.split('from')[1]
                                table_name = table_name.split()[0]
                                table_name = table_name.replace('`', '')
                                val_ips = db.query.with_entities(
                                    db_table.ip).filter(
                                        and_(db_table.database_name == db_name,
                                             db_table.table_name ==
                                             table_name)).all()
                                if val_ips:
                                    val_ips = set([ip[0] for ip in val_ips])
                                else:
                                    raise flash('没有找到%s表相关信息!' % table_name)
                                val = db.query.with_entities(
                                    db.ip, db.port, db.db).filter(
                                        and_(db.master == '是',
                                             db.ip.in_(val_ips))).all()
                                if val:
                                    for info in val:
                                        #执行sql语句
                                        master_ip, master_port, master_db = info
                                        if db_name in master_db.split('|'):
                                            flash('SQL语句:{0}'.format(sql_cmd))
                                            flash('查询结果:')
                                            results = Mysql.Query_sql(
                                                master_ip, master_port,
                                                db_name, sql_cmd)
                                            if results:
                                                for result in results:
                                                    result = [
                                                        str(re)
                                                        for re in result
                                                    ]
                                                    flash('\t'.join(result))
                                                produce.Async_log(
                                                    g.user, sql_cmd)
                                    flash('--' * 40)
                                else:
                                    raise flash('没有找到%s数据库的相关服务器信息!' % db_name)
                            else:
                                raise flash("%s 不符合执行规则!" % sql_cmd)
                else:
                    raise flash("每条sql语句需要以分号结尾!")
        except Exception as e:
            logging.error(e)
        return render_template('Message_static.html', Main_Infos=g.main_infos)
    return render_template('mysql_query.html',
                           Main_Infos=g.main_infos,
                           form=form)
예제 #15
0
def update_java():
    produce.Async_log(g.user, request.url)
    K = '%s_%s' % (g.user, g.secret_key)
    messageKey = '%s_update_java' % K
    publish_key = '%s_update_key' % K
    form = MyForm.MyForm_updateJboss()
    if form.submit.data:
        try:
            if form.text.data:
                if myRedis.exists(messageKey):
                    raise flash('上线操作过于频繁,请稍等%s秒......' %
                                myRedis.ttl(messageKey))
                myRedis.lpush(messageKey, 'check env......')
                tags = form.text.data.strip().splitlines()
                assert len(tags) == 1, 'Can only execute a project at a time!'
                project = form.text.data.strip()
                warname = project.split('-')[0]
                Action = form.selectAction.data
                Gray = form.Gray.data
                Type = 1
                dbTable = db_op.java_list
                #灰度发布
                if Gray:
                    ServerList = dbTable.query.with_entities(
                        dbTable.ip, dbTable.user).filter(
                            db_op.DB.and_(dbTable.project == warname,
                                          dbTable.type == Type,
                                          dbTable.Gray == '1')).limit(1).all()
                    if not ServerList:
                        ServerList = dbTable.query.with_entities(
                            dbTable.ip, dbTable.user).filter(
                                db_op.DB.and_(
                                    dbTable.project == warname,
                                    dbTable.type == Type)).limit(1).all()
                        if ServerList:
                            for ip, username in ServerList:
                                dbTable.query.filter(
                                    db_op.DB.and_(
                                        dbTable.ip == ip,
                                        dbTable.user == username)).update(
                                            {dbTable.Gray: '1'})
                        else:
                            flash('%s Not found' % warname)
                            return render_template('Message_static.html',
                                                   Main_Infos=g.main_infos)
                else:
                    ServerList = dbTable.query.with_entities(
                        dbTable.ip, dbTable.user).filter(
                            db_op.DB.and_(dbTable.project == warname,
                                          dbTable.type == Type)).all()
                if ServerList:
                    myRedis.lpush(messageKey, '    --->check env pass!')
                    myRedis.lpush(messageKey, '-' * 80 + '\n')
                    information = {}
                    information['warname'] = warname
                    information['project'] = project
                    information['ServerList'] = ServerList
                    information['Action'] = Action
                    information['Gray'] = Gray
                    myRedis.lpush(publish_key, information)
                    mysql_operation = Mysql.mysql_op(g.user, Action, Type,
                                                     project, Gray)
                    mysql_operation.op_operation()
                    Scheduler = produce.Scheduler_publish()
                    Scheduler = Scheduler.Scheduler_mem(
                        java_update.java_update, publish_key, messageKey)
                    Scheduler.start()
                else:
                    flash('%s Not found' % warname)
                    return render_template('Message_static.html',
                                           Main_Infos=g.main_infos)
        except Exception as e:
            flash(e)
            return render_template('Message_static.html',
                                   Main_Infos=g.main_infos)
        return render_template('java_update_show.html',
                               Main_Infos=g.main_infos)
    return render_template('java_update.html',
                           Main_Infos=g.main_infos,
                           form=form)
예제 #16
0
파일: sql_run.py 프로젝트: zhouqiw/opsweb
def sql_run():
    form = MyForm.MyForm_sql_run()
    if form.submit.data:
        #获取页面相关信息
        try:
            db_name = form.db_name.data.strip()
            assert db_name, '数据库库名不能为空!'
            if form.File.data:
                File = request.files['File']
                if File:
                    if File.filename.endswith(
                            '.sql') or File.filename.endswith('.txt'):
                        File.save(os.path.join(UPLOAD_FOLDER, File.filename))
                        with open(
                                '{0}{1}'.format(UPLOAD_FOLDER, File.filename),
                                'r') as f:
                            cmds = f.read().strip()
                            chart = chardet.detect(cmds)
                            if chart['encoding'].lower() not in ('ascii',
                                                                 'utf-8'):
                                raise flash(
                                    'sql文件当前编码是{0},不是utf-8或者ascii编码!'.format(
                                        chart['encoding']))
                    else:
                        raise flash('文件名应以.sql或者.txt结尾!')
            elif form.sql_text.data:
                cmds = form.sql_text.data.strip()
            else:
                raise flash('没有SQL语句可执行!')
            if cmds:
                if cmds.endswith(';'):
                    cmds = cmds.split(';')
                    if len(cmds) > 200:
                        raise flash('SQL语句不能超过200条!')
                    #获取库表对应的主库服务器信息
                    db = db_idc.idc_mysqldb
                    db_table = db_idc.idc_tableinfo
                    for sql_cmd in cmds:
                        if sql_cmd:
                            sql_cmd = sql_cmd.strip()
                            sql_cmd = '%s;' % sql_cmd
                            if 'update' or 'insert' or 'delete' or 'alter' or 'create' in sql_cmd.lower(
                            ):
                                if sql_cmd.lower().startswith('update'):
                                    table_name = sql_cmd.split()[1]
                                else:
                                    table_name = sql_cmd.split()[2]
                                table_name = table_name.replace('`', '')
                                val_ips = db.query.with_entities(
                                    db_table.ip).filter(
                                        and_(db_table.database_name == db_name,
                                             db_table.table_name ==
                                             table_name)).all()
                                if sql_cmd.lower().startswith('create'):
                                    if val_ips:
                                        raise flash('{0}在{1}库已存在!'.format(
                                            table_name, db_name))
                                    else:
                                        val_ips = db.query.with_entities(
                                            db_table.ip).filter(
                                                and_(db_table.database_name ==
                                                     db_name)).all()
                                if val_ips:
                                    val_ips = set([ip[0] for ip in val_ips])
                                else:
                                    raise flash('没有找到%s表相关数据库信息!' % table_name)
                                val = db.query.with_entities(
                                    db.ip, db.port, db.db).filter(
                                        and_(db.master == '是',
                                             db.ip.in_(val_ips))).all()
                                if val:
                                    for info in val:
                                        #执行sql语句
                                        master_ip, master_port, master_db = info
                                        if db_name in master_db.split('|'):
                                            flash('服务器:{0}  端口:{1}  数据库:{2}'.
                                                  format(
                                                      master_ip, master_port,
                                                      db_name))
                                            result = Mysql.Query_sql(
                                                master_ip, master_port,
                                                db_name, sql_cmd)
                                            if result:
                                                flash(result)
                                            else:
                                                flash('SQL语句:{0}'.format(
                                                    sql_cmd))
                                                flash('SQL执行成功!')
                                else:
                                    raise flash('没有找到%s数据库的相关服务器信息!' % db_name)
                            else:
                                raise flash("%s 不符合执行规则!" % sql_cmd)
                else:
                    raise flash("每条sql语句需要以分号结尾!")
        except Exception as e:
            if 'old-style' not in str(e):
                flash(e)
        return render_template('Message_static.html', Main_Infos=g.main_infos)
    return render_template('mysql_run.html',
                           Main_Infos=g.main_infos,
                           form=form)
예제 #17
0
def publish_php():
    produce.Async_log(g.user, request.url)
    K = '%s_%s' %(g.user,g.secret_key)
    Key = '%s_publish_php' %K
    publish_key = '%s_publish_key' %K
    qrcode_url = None
    form = MyForm.MyForm_php()
    if form.submit.data:
        try:
            if Redis.exists(Key):
                raise flash('上线操作过于频繁,请稍等%s秒......' %Redis.ttl(Key))
            if form.text.data and form.changelog.data:
                action = form.selectaction.data
                Type = int(form.selecttype.data)
                Way = form.selectWay.data
                if Type == 1:
                    platfrom = '线上'
                if Type == 2:
                    platfrom = '测外'
                Tags = form.text.data.strip().splitlines()
                Gray = form.Gray.data
                code = form.code.data
                work = form.selectwork.data
                changelog = form.changelog.data
                if Way == 'SVN':
                    if len(Tags) > 1:
                        raise flash('错误:只能同时上线一个项目!')
                    else:
                        for line in Tags:
                            if line.startswith('#') or not line.split():
                                continue
                            m = re.match(r'[0-9]', line)
                            if m:
                                raise flash('错误:不能以数字开头!')
                            if not line.startswith('/'):
                                raise flash('错误:' + line + '路径必须以"/"开头!')
                            path = line.strip()
                            path = path.replace('\\', '/')
                            App = path.split('/')
                            if len(App)<4:
                                raise flash('错误:路径错误,无法获取项目名!')
                            App = App[3]
                            if len(path.split('-')) >= 2:
                                version = path.split('-')[-1]
                            else:
                                raise flash('错误:项目名称格式错误!')
                            if version.endswith('.zip'):
                                version = version.replace('.zip', '')
                elif Way == 'GIT':
                    if len(Tags) != 2:
                        raise flash('错误:GIT上线格式不对!')
                    elif not Tags[0].strip().startswith('http://'):
                        raise flash('错误:' + Tags[0] + '路径需以"http://"开头!')
                    elif not Tags[0].strip().endswith('.git'):
                        raise flash('错误:' + Tags[0] + '应该以".git"结尾!')
                    elif not Tags[1].strip().startswith('tag-'):
                        raise flash('错误:输入的tag版本号格式错误!')
                    path = ';'.join(Tags)
                    path = path.replace('http://git.baihe.com/', '/')
                    App = Tags[0].strip().split('/')[-1]
                    App = App.split('.')[0]
                    version = Tags[1].split('-')[-1]
            else:
                raise flash('错误:文本框内容不能为空!')
        except Exception as e:
            if 'old' not in str(e):
                flash(e)
        else:
            try:
                project_db = db_op.project_level
                project_level = project_db.query.with_entities(project_db.level).filter(project_db.project == App).all()
                #测外不强制项目等级
                if Type == 2:
                    if project_level:
                        project_level = int(project_level[0][0])
                    else:
                        project_level = 1
                else:
                    if project_level:
                        project_level = int(project_level[0][0])
                        crypto = Md5.crypto(crypto_key)
                        qrcode_url = "https://op.baihe.com/qrcode_php/{0}/{1}/{2}/{3}".format(crypto.encrypt(g.user),g.grade,crypto.encrypt(App),project_level)
                    else:
                        raise flash('没有找到该项目的对应项目等级!')
                publish_time = time.strftime("%H", time.localtime())
                if code:
                    verify_key = "{0}_{1}".format(g.user,App)
                    verify_code = Redis.get(verify_key)
                    if verify_code == str(code):
                        Redis.lpush(Key, '    --->verify code pass!')
                        Redis.expire(Key, 30)
                        Redis.delete(verify_key)
                        code_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
                        db = db_op.publish_code
                        db_op.DB.session.add(
                            db(project=App, code=str(code), platfrom=platfrom, user=g.user, Time=code_time))
                        db_op.DB.session.commit()
                    else:
                        raise flash('%s 该上线码验证不通过!' %str(code))
                else:
                    # 用户权限及行为判断
                    tw = int(time.strftime('%w',time.localtime(time.time())))
                    if g.grade >= 2 and Type == 1 and action == 'publish':
                        if project_level >= 5:
                            raise flash('该项目等级为%s级,需申请上线码!' % project_level)
                        if tw in (2,4):
                            if project_level <= 4 and (int(publish_time) >= 17 or int(publish_time) <= 9):
                                raise flash('仅允许在10-17点时间段进行上线操作,需申请上线码!')
                        else:
                            raise flash('4级及以下常规项目只限在周二和周四上线,修复BUG需申请上线码!')
                    if g.grade >= 2 and action == 'rollback':
                        raise flash('没有权限进行回滚操作!')
                db = db_op.php_list
                if Gray:
                    if App == 'baihePhpGlobalLibrary_publish':
                        val = db.query.with_entities(db.ip).filter(and_(db.Gray == '1',db.operation == 'baihe')).limit(1).all()
                        if not val:
                            val = db.query.with_entities(db.ip).filter(and_(db.type == '%i' % Type,db.operation == 'baihe')).limit(1).all()
                    elif App == 'Baihe':
                        val = db.query.with_entities(db.ip).filter(and_(db.Gray == '1',db.operation == 'hunli')).limit(1).all()
                        if not val:
                            val = db.query.with_entities(db.ip).filter(and_(db.type == '%i' % Type,db.operation == 'hunli')).limit(1).all()
                    else:
                        val = db.query.with_entities(db.ip).filter(and_(db.Gray == '1',db.project == '%s' % App)).limit(1).all()
                        if not val:
                            val = db.query.with_entities(db.ip).filter(and_(db.project == '%s' % App, db.type == '%i' % Type)).limit(1).all()
                    if val:
                        sip = [v[0].encode('UTF-8') for v in val if v]
                        db.query.filter(and_(db.project == App, db.ip == sip[0])).update({db.Gray: '1'})
                    else:
                        raise flash('%s 没有在上线列表中找到!' % App)
                else:
                    if App == 'baihePhpGlobalLibrary_publish':
                        val = db.query.with_entities(db.ip).filter(and_(db.type == '%i' %Type,db.operation == 'baihe')).all()
                    elif App == 'Baihe':
                        val = db.query.with_entities(db.ip).filter(and_(db.type == '%i' %Type,db.operation == 'hunli')).all()
                    else:
                        val = db.query.with_entities(db.ip).filter(and_(db.project == '%s' %App,db.type == '%i' %Type)).all()
                    if val:
                        val = set(val)
                        sip = [v[0].encode('UTF-8') for v in val if v]
                        if Type == 1:
                            sip.append('172.16.4.188')
                    else:
                        raise flash('%s 没有在上线列表中找到!' %App)
                Redis.lpush(Key, 'check security policy......')
                Redis.expire(Key, 30)
                Info = {}
                Info['action'] = action
                Info['path'] = path
                Info['app'] = App
                Info['sip'] = sip
                Info['gray'] = Gray
                Info['Type'] = Type
                Info['Way'] = Way
                Redis.lpush(publish_key,Info)
                mysql_operation = Mysql.mysql_op(g.user,action,Type,App,version,Gray,work,project_level,changelog)
                mysql_operation.op_operation()
                Scheduler = produce.Scheduler_publish()
                Scheduler = Scheduler.Scheduler_mem(php_publish.php_publish,publish_key,Key)
                Scheduler.start()
                Redis.lpush(Key,'    --->check pass!')
                Redis.lpush(Key,'-'*80+'\n')
            except Exception as e:
                if 'old' not in str(e):
                    flash(e)
                if qrcode_url:
                    return render_template('php_publish.html',Main_Infos=g.main_infos, form=form, qrcode_url=qrcode_url)
            else:
                return render_template('php_publish_show.html',Main_Infos=g.main_infos)
    return render_template('php_publish.html',Main_Infos=g.main_infos,form=form,qrcode_url = qrcode_url)