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)
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 )
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)