U - 优化钉钉消息通知。

pull/15/head
张玉坡 2019-07-28 16:33:33 +08:00
parent e26650befd
commit bb2f6bac68
4 changed files with 46 additions and 18 deletions

View File

@ -16,7 +16,7 @@ import uuid
import time
import os
from apps.system.models import NotifyWay
from libs.utils import send_ding_msg
from libs.utils import send_ding_ding
blueprint = Blueprint(__name__, __name__)
@ -121,7 +121,7 @@ def do_update(q, form, host_id):
with_exit_code=True)
if exec_code != 0:
send_message('执行应用更新失败,退出状态码:{0}'.format(exec_code), level='error')
send_publish_message(pro.notify_way_id, pro.name + ' 发布失败!')
send_publish_message(pro.notify_way_id, pro.name + ' 发布失败!', status='failed')
send_message(exec_output, level='console')
return
else:
@ -132,7 +132,7 @@ def do_update(q, form, host_id):
ctr.restart(timeout=3)
send_message('重启容器成功!', update=True)
# 整个流程正常结束
send_publish_message(pro.notify_way_id, pro.name + ' 发布成功')
send_publish_message(pro.notify_way_id, pro.name + ' 发布成功', status='success')
send_message('完成发布!', level='success')
deploy_success = True
except Exception as e:
@ -162,7 +162,19 @@ class PublishMessage(object):
q.put(data)
def send_publish_message(notify_way_id, message):
def send_publish_message(notify_way_id, message, status='info'):
if notify_way_id:
notice_value = NotifyWay.query.filter_by(id=notify_way_id).first()
send_ding_msg(token=notice_value.value, contacts=[], msg=message)
if status == 'success':
publish_status = '<font color=\"#85CE60\">发布成功</font>'
elif status == 'failed':
publish_status = '<font color=\"#f90202\">发布失败</font>'
else:
publish_status = '<font color=\"#A6A9AD\">开始发布</font>'
msg = f'# <font face=\"微软雅黑\">运维平台通知</font> # \n <br> \n ' \
f'**发布信息:** {message} \n \n ' \
f'**平台地址:** http://spug.qbangmang.com \n \n ' \
f'**发布状态:** {publish_status}<br /> \n \n ' \
send_ding_ding(token_url=notice_value.value, contacts=[], msg=msg)

View File

@ -2,6 +2,7 @@ from flask import Blueprint, request
from libs.tools import json_response, JsonParser, Argument
from .models import NotifyWay
from libs.decorators import require_permission
from libs.utils import send_ding_ding
blueprint = Blueprint(__name__, __name__)
@ -58,3 +59,15 @@ def put(n_id):
notify_info.save()
return json_response(notify_info)
return json_response(message=error)
@blueprint.route('/test/<int:d_id>', methods=['POST'])
def send_ding_test(d_id):
if d_id:
notice_value = NotifyWay.query.filter_by(id=d_id).first()
message = f'# <font face=\"微软雅黑\">运维平台通知</font> # \n ' \
'**平台地址:** http://spug.qbangmang.com \n \n ' \
'**测试状态:** <font color=\"#85CE60\">测试成功</font><br /> \n \n '
send_ding_ding(token_url=notice_value.value, contacts=[], msg=message)
return json_response()

View File

@ -225,18 +225,11 @@ class Git(object):
return '<Git %r>' % self.work_tree
def send_ding_msg(token_url='', contacts=[], msg=''):
payload = {
"msgtype": "text",
"text": {
"content": msg,
"isAtAll": False
},
"at": {
"atMobiles": contacts
}
}
req = requests.post(token_url, json=payload)
def send_ding_ding(token_url='', contacts='', msg=''):
payload = {"msgtype": "markdown", "markdown": {"title": "运维平台", "text": msg}}
binary_data = json.dumps(payload).encode(encoding='UTF8')
headers = {"Content-Type": "application/json"}
req = requests.post(token_url, data=binary_data, headers=headers)
if req.status_code == 200:
return True
else:

View File

@ -28,6 +28,8 @@
<el-table-column v-if="has_permission('system_notify_view|system_notify_add|system_notify_edit')" label="操作">
<template slot-scope="scope">
<el-button v-if="has_permission('system_notify_edit')" size="small" @click="handleEdit(scope.$index, scope.row)"></el-button>
<el-button v-if="has_permission('system_notify_add')" type="primary" size="small"
@click="handleDingTest(scope.row)">测试</el-button>
</template>
</el-table-column>
</el-table>
@ -49,7 +51,9 @@
<el-input v-model="editForm.name" ></el-input>
</el-form-item>
<el-form-item prop="value" label="通知URL" >
<el-input placeholder="输入钉钉webhook" type="textarea" :rows="3" v-model="editForm.value" auto-complete="off"></el-input>
<el-input
placeholder="钉钉机器人完整URL例如https://oapi.dingtalk.com/robot/send?access_token=858124219d02d5bf412aab28a0b26"
type="textarea" :rows="3" v-model="editForm.value" auto-complete="off"></el-input>
</el-form-item>
<el-form-item prop="desc" label="备注信息">
@ -155,6 +159,12 @@
this.getUsers(this.currentPage);
}, response => this.$layer_message(response.result)).finally(() => this.btnDelLoading = {});
},
handleDingTest: function (row){
this.$http.post(`/api/system/notify/test/${row.id}`, row).then( res=> {
this.$layer_message('测试成功', 'success');
this.getNotify(this.currentPage);
})
},
editSubmit: function () {
this.$refs.editForm.validate((valid) => {