From bb2f6bac680a97ba68593eb1b603c09c28511885 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=8E=89=E5=9D=A1?= Date: Sun, 28 Jul 2019 16:33:33 +0800 Subject: [PATCH] =?UTF-8?q?U=20-=20=E4=BC=98=E5=8C=96=E9=92=89=E9=92=89?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E9=80=9A=E7=9F=A5=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spug_api/apps/deploy/publish.py | 22 +++++++++++++++++----- spug_api/apps/system/notify.py | 13 +++++++++++++ spug_api/libs/utils.py | 17 +++++------------ spug_web/src/components/system/Notify.vue | 12 +++++++++++- 4 files changed, 46 insertions(+), 18 deletions(-) diff --git a/spug_api/apps/deploy/publish.py b/spug_api/apps/deploy/publish.py index 3cc45da..fae6287 100644 --- a/spug_api/apps/deploy/publish.py +++ b/spug_api/apps/deploy/publish.py @@ -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 = '发布成功' + elif status == 'failed': + publish_status = '发布失败' + else: + publish_status = '开始发布' + + msg = f'# 运维平台通知 # \n
\n ' \ + f'**发布信息:** {message} \n \n ' \ + f'**平台地址:** http://spug.qbangmang.com \n \n ' \ + f'**发布状态:** {publish_status}
\n \n ' \ + + send_ding_ding(token_url=notice_value.value, contacts=[], msg=msg) diff --git a/spug_api/apps/system/notify.py b/spug_api/apps/system/notify.py index 77e06ff..c10cee1 100644 --- a/spug_api/apps/system/notify.py +++ b/spug_api/apps/system/notify.py @@ -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/', methods=['POST']) +def send_ding_test(d_id): + if d_id: + notice_value = NotifyWay.query.filter_by(id=d_id).first() + message = f'# 运维平台通知 # \n ' \ + '**平台地址:** http://spug.qbangmang.com \n \n ' \ + '**测试状态:** 测试成功
\n \n ' + + send_ding_ding(token_url=notice_value.value, contacts=[], msg=message) + return json_response() diff --git a/spug_api/libs/utils.py b/spug_api/libs/utils.py index 7fcd29c..79bb084 100644 --- a/spug_api/libs/utils.py +++ b/spug_api/libs/utils.py @@ -225,18 +225,11 @@ class Git(object): return '' % 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: diff --git a/spug_web/src/components/system/Notify.vue b/spug_web/src/components/system/Notify.vue index 1661f17..47efb91 100644 --- a/spug_web/src/components/system/Notify.vue +++ b/spug_web/src/components/system/Notify.vue @@ -28,6 +28,8 @@ @@ -49,7 +51,9 @@ - + @@ -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) => {