mirror of
https://github.com/ngfchl/ptools
synced 2023-07-10 13:41:22 +08:00
1. 重构更新页面
This commit is contained in:
@@ -8,6 +8,7 @@ urlpatterns = [
|
||||
path(r'exec_task', views.exec_task, name='exec_task'),
|
||||
path(r'test_field', views.test_field, name='test_field'),
|
||||
path(r'test_notify', views.test_notify, name='test_notify'),
|
||||
path(r'update', views.update_page, name='update_page'),
|
||||
path(r'restart', views.restart_container, name='restart_container'),
|
||||
path(r'do_restart', views.do_restart, name='do_restart'),
|
||||
path(r'do_update', views.do_update, name='do_update'),
|
||||
|
||||
@@ -160,6 +160,43 @@ def get_update_logs():
|
||||
return commits[0].hexsha == remote_commits[0].hexsha
|
||||
|
||||
|
||||
def update_page(request):
|
||||
try:
|
||||
# 获取docker对象
|
||||
client = docker.from_env()
|
||||
# 从内部获取容器id
|
||||
cid = socket.gethostname()
|
||||
client.api.info()
|
||||
started_at = client.api.inspect_container(cid).get('State').get('StartedAt')[:-4] + 'Z'
|
||||
utc_format = "%Y-%m-%dT%H:%M:%S.%fZ"
|
||||
restart = 'true'
|
||||
utc_time = datetime.strptime(started_at, utc_format)
|
||||
local_time = utc_time + timedelta(hours=8)
|
||||
delta = str((datetime.now() - local_time).seconds) + '秒'
|
||||
print(delta)
|
||||
# delta = local_time.strftime('%Y-%m-%dT%H:%M:%S.%f')
|
||||
# delta = delta.astimezone(pytz.timezone('Asia/Shanghai'))
|
||||
except Exception as e:
|
||||
# raise
|
||||
restart = 'false'
|
||||
delta = '程序未在容器中启动?'
|
||||
if get_update_logs():
|
||||
update = 'false'
|
||||
update_tips = '目前您使用的是最新版本!'
|
||||
else:
|
||||
update = 'true'
|
||||
update_tips = '已有新版本,请根据需要升级!'
|
||||
return render(request, 'auto_pt/update.html',
|
||||
context={
|
||||
'delta': delta,
|
||||
'restart': restart,
|
||||
'local_logs': get_git_logs(),
|
||||
'update_notes': get_git_logs(master='origin/master'),
|
||||
'update': update,
|
||||
'update_tips': update_tips
|
||||
})
|
||||
|
||||
|
||||
def restart_container(request):
|
||||
# scraper = pt_spider.get_scraper()
|
||||
# res = scraper.get('https://gitee.com/ngfchl/ptools/raw/master/update.md')
|
||||
@@ -175,7 +212,7 @@ def restart_container(request):
|
||||
restart = 'true'
|
||||
utc_time = datetime.strptime(started_at, utc_format)
|
||||
local_time = utc_time + timedelta(hours=8)
|
||||
delta = str((datetime.now() - local_time).seconds)+'秒'
|
||||
delta = str((datetime.now() - local_time).seconds) + '秒'
|
||||
print(delta)
|
||||
# delta = local_time.strftime('%Y-%m-%dT%H:%M:%S.%f')
|
||||
# delta = delta.astimezone(pytz.timezone('Asia/Shanghai'))
|
||||
|
||||
@@ -210,6 +210,10 @@ SIMPLEUI_CONFIG = {
|
||||
'name': '重启更新',
|
||||
'icon': 'far fa-surprise',
|
||||
'url': '/tasks/restart'
|
||||
}, {
|
||||
'name': '更新',
|
||||
'icon': 'far fa-surprise',
|
||||
'url': '/tasks/update'
|
||||
}, ]
|
||||
}]
|
||||
}
|
||||
|
||||
150
templates/auto_pt/update.html
Normal file
150
templates/auto_pt/update.html
Normal file
@@ -0,0 +1,150 @@
|
||||
{% load static %}
|
||||
|
||||
<!-- 引入样式 -->
|
||||
|
||||
<link rel="stylesheet" href="{% static 'admin/simpleui-x/elementui/theme-chalk/index.css' %}">
|
||||
<!-- 引入组件库 -->
|
||||
|
||||
<div id="app">
|
||||
<el-card class="box-card">
|
||||
<div slot="header" class="clearfix">
|
||||
<span>
|
||||
<el-button type="primary">更新提示</el-button>
|
||||
<el-popover
|
||||
placement="top-start"
|
||||
title="友情提示"
|
||||
width="300"
|
||||
trigger="hover"
|
||||
content="如果遇到无法更新的情况,您可以选择重建容器或者进入容器命令行到项目根目录执行`git pull`命令!">
|
||||
<el-link slot="reference"
|
||||
type="success">{{ update_tips }}
|
||||
<span v-if="{{ restart }}"> + 容器已运行:{{ delta }}</span>
|
||||
</el-link>
|
||||
</el-popover>
|
||||
</span>
|
||||
<span style="float: right;margin-top: -10px">
|
||||
<el-button slot="reference" type="danger" @click="do_restart" v-if="{{ restart }}">重启更新</el-button>
|
||||
</span>
|
||||
</div>
|
||||
<div class="text item">
|
||||
<el-row>
|
||||
<el-col :span="local_log_width">
|
||||
<el-card class="box-card" shadow="hover">
|
||||
<div slot="header" class="clearfix">
|
||||
<span>本地日志</span>
|
||||
</div>
|
||||
<el-timeline>
|
||||
<el-timeline-item
|
||||
v-for="(update_note, index) in local_logs"
|
||||
:key="index"
|
||||
icon="el-icon-refresh"
|
||||
type="success"
|
||||
color="#0bbd87"
|
||||
{# :size="activity.size" #}
|
||||
:timestamp="update_note.date">
|
||||
<el-card shadow="hover">
|
||||
<p v-for="content in update_note.data" v-html="content">
|
||||
</p>
|
||||
</el-card>
|
||||
|
||||
</el-timeline-item>
|
||||
</el-timeline>
|
||||
</el-card>
|
||||
</el-col>
|
||||
<el-col :span="12" v-if="update">
|
||||
<el-card class="box-card" shadow="hover">
|
||||
<div slot="header" class="clearfix">
|
||||
<span>更新日志</span>
|
||||
</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 shadow="hover">
|
||||
<p v-for="content in update_note.data" v-html="content">
|
||||
</p>
|
||||
</el-card>
|
||||
|
||||
</el-timeline-item>
|
||||
</el-timeline>
|
||||
</el-card>
|
||||
</el-col>
|
||||
</el-row>
|
||||
|
||||
</div>
|
||||
</el-card>
|
||||
</div>
|
||||
|
||||
<script src="{% static 'admin/simpleui-x/js/vue.min.js' %}"></script>
|
||||
<script src="{% static 'admin/simpleui-x/elementui/index.js' %}"></script>
|
||||
<script src="{% static 'admin/simpleui-x/js/axios.min.js' %}"></script>
|
||||
|
||||
<script type="text/javascript">
|
||||
// 配置对象 options
|
||||
const vm = new Vue({
|
||||
// 配置选项(option)
|
||||
// element: 指定用vue来管理页面中的哪个标签区域
|
||||
el: '#app',
|
||||
data: {
|
||||
update_notes: {{ update_notes | safe }},
|
||||
local_logs: {{ local_logs | safe }},
|
||||
{#local_log: [],#}
|
||||
{#update_log: [],#}
|
||||
update: {{ update }},
|
||||
local_log_width: 24,
|
||||
},
|
||||
created() {
|
||||
if (this.update) {
|
||||
this.local_log_width = 12
|
||||
}
|
||||
this.$message({
|
||||
showClose: true,
|
||||
message: "{{ update_tips }}",
|
||||
type: 'success'
|
||||
});
|
||||
}
|
||||
,
|
||||
methods: {
|
||||
do_restart() {
|
||||
this.$confirm('此操作将重启容器,并更新软件, 是否继续?', '提示', {
|
||||
confirmButtonText: '确定',
|
||||
cancelButtonText: '取消',
|
||||
type: 'warning'
|
||||
}).then(() => {
|
||||
axios.get(
|
||||
"{% url "do_restart" %}"
|
||||
).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 => {
|
||||
this.$message({
|
||||
type: 'error',
|
||||
message: "重启失败!"
|
||||
});
|
||||
})
|
||||
}).catch(res => {
|
||||
{#console.log(res)#}
|
||||
this.$message({
|
||||
type: 'info',
|
||||
message: '已取消重启'
|
||||
});
|
||||
});
|
||||
},
|
||||
}
|
||||
});
|
||||
</script>
|
||||
Reference in New Issue
Block a user