From 18a844ac4c8fe41ff5b04746fb0defcf87cee457 Mon Sep 17 00:00:00 2001 From: ngfchl Date: Fri, 26 Aug 2022 16:32:03 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E4=BC=98=E5=8C=96=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=EF=BC=8C=E8=87=AA=E8=A1=8C=E9=80=89=E6=8B=A9=E6=98=AF=E5=90=A6?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=88=B0=E6=9C=80=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- auto_pt/urls.py | 3 +- auto_pt/views.py | 111 ++++++++++++++++++++++++--------- templates/auto_pt/restart.html | 58 ++++++++++++++--- 3 files changed, 132 insertions(+), 40 deletions(-) diff --git a/auto_pt/urls.py b/auto_pt/urls.py index c9de272..12ed15e 100644 --- a/auto_pt/urls.py +++ b/auto_pt/urls.py @@ -12,5 +12,6 @@ urlpatterns = [ path(r'do_restart', views.do_restart, name='do_restart'), path(r'do_restart', views.do_restart, name='do_restart'), path(r'do_update', views.do_update, name='do_update'), - # path(r'do_sql', views.do_sql, name='do_sql'), + path(r'get_update', views.do_get_update, name='do_get_update'), + path(r'do_sql', views.do_sql, name='do_sql'), ] diff --git a/auto_pt/views.py b/auto_pt/views.py index fbec9c2..610dd7d 100644 --- a/auto_pt/views.py +++ b/auto_pt/views.py @@ -3,7 +3,6 @@ import os import subprocess from datetime import datetime -import markdown from django.db import connection from django.http import JsonResponse from django.shortcuts import render @@ -88,26 +87,73 @@ def do_sql(request): # with connection.cursor() as cursor: # for statement in contents: # res1 = cursor.execute(statement) + return JsonResponse('ok', safe=False) -def do_restart(request): - try: - print('重启') - # print(os.system('pwd')) - if os.environ.get('CONTAINER_NAME'): - subprocess.Popen('chmod +x ./restart.sh', shell=True) - subprocess.Popen('./restart.sh', shell=True) - return JsonResponse(data=CommonResponse.success( - msg='重启指令发送成功!!' - ).to_dict(), safe=False) - return JsonResponse(data=CommonResponse.error( - msg='未配置CONTAINER_NAME(容器名称)环境变量,请自行重启容器!!' - ).to_dict(), safe=False) - except Exception as e: - return JsonResponse(data=CommonResponse.error( - msg='重启指令发送失败!' + str(e) +def get_update(master='', n=10): + # 获取最新的10条更新记录 + # master='' 本地 master='remote/' 远程 + p = subprocess.Popen('git log {}origin/master -{}'.format(master, n), shell=True, stdout=subprocess.PIPE, ) + contents = p.stdout.readlines() + update_notes = [] + info = { + 'date': '', + 'data': [] + } + for i in contents: + string = i.decode('utf8') + if string == '\n' or 'commit' in string or 'Author' in string: + continue + if 'Date' in string: + update_notes.append(info) + info = {} + list1 = string.split(':', 1) + info['date'] = list1[1].strip() + info['data'] = [] + continue + info['data'].append(string.strip()) + # print(update_notes) + update_notes.pop(0) + return update_notes + + +def restart_container(request): + # scraper = pt_spider.get_scraper() + # res = scraper.get('https://gitee.com/ngfchl/ptools/raw/master/update.md') + # update_md = markdown.markdown(res.text, extensions=['tables']) + + restart = 'false' + update = 'false' + if os.environ.get('CONTAINER_NAME'): + restart = 'true' + return render(request, 'auto_pt/restart.html', + context={ + # 'update_md': update_md, + 'update_notes': get_update(), + 'restart': restart, + 'update': update, + }) + + +def do_get_update(request): + update = 'false' + p = subprocess.Popen('git log origin/master -1', shell=True, stdout=subprocess.PIPE, ) + content = p.stdout.readline() + p_remote = subprocess.Popen('git log remote/origin/master -1', shell=True, stdout=subprocess.PIPE, ) + content_remote = p_remote.stdout.readline() + if content_remote == content: + update = 'true' + return JsonResponse(data=CommonResponse.success( + msg='拉取更新日志成功!!', + data={ + 'update': update, + 'update_notes': get_update(master='remote/'), + } ).to_dict(), safe=False) + return JsonResponse(data=CommonResponse.success( + msg='已经更新到最新!', + ).to_dict(), safe=False) def do_update(request): @@ -144,17 +190,20 @@ def do_update(request): ).to_dict(), safe=False) -def restart_container(request): - scraper = pt_spider.get_scraper() - res = scraper.get('https://gitee.com/ngfchl/ptools/raw/master/update.md') - update_notes = markdown.markdown(res.text, extensions=['tables']) - print(update_notes) - restart = 'false' - if os.environ.get('CONTAINER_NAME'): - restart = 'true' - return render(request, 'auto_pt/restart.html', - context={ - 'update_notes': update_notes, - 'restart': restart - } - ) +def do_restart(request): + try: + print('重启') + # print(os.system('pwd')) + if os.environ.get('CONTAINER_NAME'): + subprocess.Popen('chmod +x ./restart.sh', shell=True) + subprocess.Popen('./restart.sh', shell=True) + return JsonResponse(data=CommonResponse.success( + msg='重启指令发送成功!!' + ).to_dict(), safe=False) + return JsonResponse(data=CommonResponse.error( + msg='未配置CONTAINER_NAME(容器名称)环境变量,请自行重启容器!!' + ).to_dict(), safe=False) + except Exception as e: + return JsonResponse(data=CommonResponse.error( + msg='重启指令发送失败!' + str(e) + ).to_dict(), safe=False) diff --git a/templates/auto_pt/restart.html b/templates/auto_pt/restart.html index 93a2b1b..ae4a4a1 100644 --- a/templates/auto_pt/restart.html +++ b/templates/auto_pt/restart.html @@ -10,18 +10,35 @@
更新日志 - 更新 + 获取更新 + 更新 重启
- + {# #} -
- {{ update_notes|safe }} -
+ {#
#} + {# {{ update_md|safe }}#} + {#
#} + + + +

+

+
+ +
+
@@ -39,8 +56,9 @@ // element: 指定用vue来管理页面中的哪个标签区域 el: '#app', data: { - update_note: ['请先拉取更新哦'], + update_notes: {{ update_notes|safe }}, restart:{{ restart }}, + update:{{ update }} }, methods: { do_restart() { @@ -81,6 +99,30 @@ }); }); }, + get_update() { + axios.get( + "{% url "do_get_update" %}" + ).then(res => { + if (res.data.code === 0) { + this.$message({ + type: 'success', + message: res.data.msg + }); + } else { + console.log(res) + this.$message({ + type: 'warning', + message: res.data.msg + }); + } + }).catch(res => { + console.log(res) + this.$message({ + type: 'info', + message: '获取更新失败!' + }); + }); + }, do_update() { this.$confirm('此操作会拉取软件更新软件,新版本不一定稳定,是否继续?', '提示', { confirmButtonText: '确定',