1. 重构更新页面2

This commit is contained in:
ngfchl
2022-09-05 21:33:30 +08:00
parent 3579477ad1
commit f7259f7538
3 changed files with 42 additions and 24 deletions

View File

@@ -166,7 +166,6 @@ def update_page(request):
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'
@@ -239,25 +238,39 @@ def restart_container(request):
def do_update(request):
try:
print('更新')
print('开始拉取更新')
# print(os.system('cat ./update.sh'))
subprocess.Popen('chmod +x ./update.sh', shell=True)
p = subprocess.Popen('./update.sh', shell=True, stdout=subprocess.PIPE, bufsize=1)
p.wait()
out = p.stdout.readlines()
result = []
for i in out:
result.append(i.decode('utf8'))
print(result)
return JsonResponse(data=CommonResponse.success(
msg='更新成功!!',
data={
'result': result,
# 'xpath_update': xpath_update
}).to_dict(), safe=False)
print(i.decode('utf8'))
# 更新Xpath规则
print('拉取更新完毕开始更新Xpath规则')
with open('./main_pt_site_site.json', 'r') as f:
# print(f.readlines())
data = json.load(f)
# print(data[2])
# print(data[0].get('url'))
# xpath_update = []
print('更新规则中返回结果为True为新建为False为更新其他是错误了')
for site_rules in data:
if site_rules.get('pk'):
del site_rules['pk']
site_obj = Site.objects.update_or_create(defaults=site_rules, url=site_rules.get('url'))
print(site_obj[0].name + (' 规则新增成功!' if site_obj[1] else '规则更新成功!'))
print('更新完毕,开始重启')
cid = socket.gethostname()
subprocess.Popen('docker restart {}'.format(cid), shell=True)
# client.api.inspect_container(cid)
# StartedAt = client.api.inspect_container(cid).get('State').get('StartedAt')
return JsonResponse(data=CommonResponse.error(
msg='更新成功,重启指令发送成功,容器重启中 ...'
).to_dict(), safe=False)
except Exception as e:
return JsonResponse(data=CommonResponse.error(
msg='更新指令发送失败!' + str(e)
msg='更新失败!' + str(e)
).to_dict(), safe=False)

View File

@@ -23,7 +23,7 @@
</el-popover>
</span>
<span style="float: right;margin-top: -10px">
<el-button slot="reference" type="danger" @click="do_restart" v-if="{{ restart }}">重启更新</el-button>
<el-button slot="reference" type="danger" @click="do_update" v-if="{{ restart }}">重启更新</el-button>
</span>
</div>
<div class="text item">
@@ -110,41 +110,46 @@
}
,
methods: {
do_restart() {
this.$confirm('此操作将重启容器,并更新软件, 是否继续?', '提示', {
do_update() {
this.$confirm('此操作会拉取软件更新软件,并重启容器,是否继续?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
axios.get(
"{% url "do_restart" %}"
"{% url "do_update" %}"
).then(res => {
if (res.data.code === 0) {
console.log(res, 1)
this.update_log = res.data.data.result
{#console.log('规则更新记录', res.data.data.xpath_update)#}
this.$message({
type: 'success',
message: res.data.msg
});
} else {
console.log(res)
console.log(res, 2)
this.$message({
type: 'warning',
message: res.data.msg
});
}
}).catch(res => {
console.log(res, 3)
this.$message({
type: 'error',
message: "重启失败!"
message: "更新失败!"
});
})
}).catch(res => {
{#console.log(res)#}
console.log(res)
this.$message({
type: 'info',
message: '已取消重启'
type: 'warning',
message: '已取消更新'
});
});
},
}
}
});
</script>

View File

@@ -1,6 +1,6 @@
# 先备份数据库文件再拉取更新 $(date "+%Y%m%d%H%M%S")当前时间年月日时分秒
#cp /var/www/html/ptools/db/db.sqlite3 /var/www/html/ptools/db/db.sqlite3-$(date "+%Y%m%d%H%M%S") &&
git pull &&
cp /var/www/html/ptools/db/db.sqlite3 /var/www/html/ptools/db/db.sqlite3-$(date "+%Y%m%d%H%M%S") &&
git pull &&
mv -f /var/www/html/ptools/start.sh /var/www/html/start.sh &&
python manage.py makemigrations &&
python manage.py migrate