1. 优化更新,自行选择是否更新到最新

This commit is contained in:
ngfchl
2022-08-26 16:32:03 +08:00
parent cbd53223aa
commit 18a844ac4c
3 changed files with 132 additions and 40 deletions

View File

@@ -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'),
]

View File

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

View File

@@ -10,18 +10,35 @@
<div slot="header" class="clearfix">
<span>更新日志</span>
<span style="float: right;margin-top: -10px">
<el-button type="success" @click="do_update">更新</el-button>
<el-button type="primary" @click="get_update">获取更新</el-button>
<el-button type="success" @click="do_update" v-if="update">更新</el-button>
<el-button type="danger" @click="do_restart" v-if="restart">重启</el-button>
</span>
</div>
<div class="text item">
<ul v-for="item in update_note">
<li v-text="item"></li>
</ul>
{# <ul v-for="item in update_notes">#}
{# <li v-html="item"></li>#}
{# </ul>#}
<el-card class="box-card">
<div class="text item">
{{ update_notes|safe }}
</div>
{# <div class="text item">#}
{# {{ update_md|safe }}#}
{# </div>#}
<el-timeline>
<el-timeline-item
v-for="(update_note, index) in update_notes"
:key="index"
{# :icon="activity.icon"#}
{# :type="activity.type"#}
color="#0bbd87"
{# :size="activity.size"#}
:timestamp="update_note.date">
<el-card>
<p v-for="content in update_note.data" v-html="content">
</p>
</el-card>
</el-timeline-item>
</el-timeline>
</el-card>
</div>
@@ -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: '确定',