diff --git a/auto_pt/views.py b/auto_pt/views.py index 0d71602..68edf9c 100644 --- a/auto_pt/views.py +++ b/auto_pt/views.py @@ -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) diff --git a/templates/auto_pt/update.html b/templates/auto_pt/update.html index 8df1acf..810ed57 100644 --- a/templates/auto_pt/update.html +++ b/templates/auto_pt/update.html @@ -23,7 +23,7 @@ - 重启更新 + 重启更新
@@ -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: '已取消更新' }); }); - }, + } } }); diff --git a/update.sh b/update.sh index 0ddd66b..7f7d604 100755 --- a/update.sh +++ b/update.sh @@ -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