From 8fb7db8c2b89d7b2805a7514210d0ef98e599143 Mon Sep 17 00:00:00 2001 From: ngfchl Date: Sun, 28 Aug 2022 19:59:19 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E4=BC=98=E5=8C=96=E9=87=8D=E5=90=AF?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=EF=BC=8C=E4=BB=A5=E5=90=8E=E6=97=A0=E9=9C=80?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E7=8E=AF=E5=A2=83=E5=8F=98=E9=87=8F=E5=8D=B3?= =?UTF-8?q?=E5=8F=AF=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- auto_pt/urls.py | 1 - auto_pt/views.py | 131 +++++++++++++++------------------ requirements.txt | 15 ++++ templates/auto_pt/restart.html | 3 +- 4 files changed, 77 insertions(+), 73 deletions(-) diff --git a/auto_pt/urls.py b/auto_pt/urls.py index ecc8325..69ab7a5 100644 --- a/auto_pt/urls.py +++ b/auto_pt/urls.py @@ -10,7 +10,6 @@ urlpatterns = [ path(r'test_notify', views.test_notify, name='test_notify'), path(r'restart', views.restart_container, name='restart_container'), 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'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 344bb70..3a975ec 100644 --- a/auto_pt/views.py +++ b/auto_pt/views.py @@ -1,8 +1,10 @@ import json -import os +import socket import subprocess from datetime import datetime +import docker as docker +import git from django.db import connection from django.http import JsonResponse from django.shortcuts import render @@ -56,7 +58,7 @@ def get_tasks(request): def exec_task(request): # res = AutoPt.auto_sign_in() # print(res) - tasks.auto_sign_in.delay() + # tasks.auto_sign_in return JsonResponse('ok!', safe=False) @@ -68,12 +70,6 @@ def test_field(request): def test_notify(request): - """ - app_id:28987 - uid: UID_jkMs0DaVVwOcBuFPQGzymjCwYVgH - 应用名称:pt_helper - appToken:AT_ShUnRu2CJRcsqbbW540voVkjMZ1PKjGy - """ # res = NotifyDispatch().send_text(text='66666') res = pt_spider.send_text('666') @@ -87,6 +83,14 @@ def do_sql(request): # with connection.cursor() as cursor: # for statement in contents: # res1 = cursor.execute(statement) + # print(threading.main_thread().getName()) + try: + print(0) + except Exception as e: + + print(e) + # autoreload.start_django(au) + # django_main_thread return JsonResponse('ok', safe=False) @@ -120,76 +124,62 @@ def get_git_logs(master='', n=10): return update_notes +def get_git_log(master='master', n=10): + repo = git.Repo(path='.') + # 拉取仓库更新记录元数据 + repo.remote().update() + # 获取本地仓库commits更新记录 + commits = list(repo.iter_commits(master, max_count=n)) + # 获取远程仓库commits记录 + # remote_commits = list(repo.iter_commits("origin/master", max_count=10)) + + +def get_update_logs(): + repo = git.Repo(path='.') + # 拉取仓库更新记录元数据 + repo.remote().update() + # 获取本地仓库commits更新记录 + commits = list(repo.iter_commits('master', max_count=10)) + # 获取远程仓库commits记录 + remote_commits = list(repo.iter_commits("origin/master", max_count=10)) + """ + # commits = [str(commit) for commit in commits] + # remote_commits = [str(commit) for commit in remote_commits] + # # for commit in commits: + # # print(commit.hexsha) + # # print(commit.committed_datetime) + # # print(commit.message) + # return render(request, 'auto_pt/restart.html', + # context={ + # # 'update_md': update_md, + # 'local_logs': commits, + # 'update_notes': remote_commits, + # 'update': update, + # 'update_tips': update_tips, + # }) + """ + return commits[0].hexsha == remote_commits[0].hexsha + + 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']) - - # 拉取更新元数据 - update_log = subprocess.Popen('git remote update', shell=True) - update_log.wait() - # 获取本地更新日志第一条 - p_local = subprocess.Popen('git log --oneline -1', shell=True, stdout=subprocess.PIPE, ) - commit_local = p_local.stdout.readline().decode('utf8').strip() - # 获取远端仓库更新日志第一条 - p_remote = subprocess.Popen('git log origin/master --oneline -1', shell=True, stdout=subprocess.PIPE, ) - commit_remote = p_remote.stdout.readline().decode('utf8').strip() - print(commit_local, commit_remote) - # if 'HEAD' in commit and 'origin' in commit: - print(commit_remote == commit_local) - # 如果日志相同则更新到最新,否则显示远端更新日志 - if commit_remote == commit_local: + if get_update_logs(): update = 'false' update_tips = '目前您使用的是最新版本!' else: update = 'true' update_tips = '已有新版本,请根据需要升级!' - restart = 'false' - if os.environ.get('CONTAINER_NAME'): - restart = 'true' return render(request, 'auto_pt/restart.html', context={ # 'update_md': update_md, 'local_logs': get_git_logs(), 'update_notes': get_git_logs(master='origin/master'), - 'restart': restart, 'update': update, - 'update_tips': update_tips, + 'update_tips': update_tips }) -# -# def do_get_update(request): -# update = 'false' -# # 拉取更新元数据 -# update_log = subprocess.Popen('git remote update', shell=True) -# update_log.wait() -# # 获取本地更新日志第一条 -# p_local = subprocess.Popen('git log --oneline -1', shell=True, stdout=subprocess.PIPE, ) -# commit_local = p_local.stdout.readline().decode('utf8').strip() -# # 获取远端仓库更新日志第一条 -# p_remote = subprocess.Popen('git log origin/master --oneline -1', shell=True, stdout=subprocess.PIPE, ) -# commit_remote = p_remote.stdout.readline().decode('utf8').strip() -# print(commit_local, commit_remote) -# # if 'HEAD' in commit and 'origin' in commit: -# print(commit_remote == commit_local) -# # 如果日志相同则更新到最新,否则显示远端更新日志 -# if commit_remote == commit_local: -# return JsonResponse(data=CommonResponse.success( -# msg='已经更新到最新!', -# data={ -# 'update_notes': get_git_logs(master='origin/master'), -# } -# ).to_dict(), safe=False) -# else: -# update = 'true' -# return JsonResponse(data=CommonResponse.success( -# msg='拉取更新日志成功!!', -# data={ -# 'update': update, -# 'update_notes': get_git_logs(master='origin/master'), -# } -# ).to_dict(), safe=False) - def do_update(request): try: @@ -226,16 +216,17 @@ def do_update(request): 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) + # 获取docker对象 + client = docker.from_env() + # 从内部获取容器id + cid = socket.gethostname() + # 获取容器对象 + # container = client.containers.get(cid) + # 重启容器 + client.api.restart(cid) + print('重启中') return JsonResponse(data=CommonResponse.error( - msg='未配置CONTAINER_NAME(容器名称)环境变量,请自行重启容器!!' + msg='重启指令发送成功,容器重启中 ...' ).to_dict(), safe=False) except Exception as e: return JsonResponse(data=CommonResponse.error( diff --git a/requirements.txt b/requirements.txt index 31b7296..693d736 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,6 +2,7 @@ APScheduler==3.9.1 asgiref==3.5.2 async-timeout==4.0.2 baidu-aip==4.16.7 +borax==3.5.6 certifi==2022.6.15 chardet==5.0.0 charset-normalizer==2.1.1 @@ -15,18 +16,28 @@ django-apscheduler==0.6.2 django-import-export==2.8.0 django-redis==5.2.0 django-simpleui==2022.7.29 +docker==6.0.0 et-xmlfile==1.1.0 +gitdb==4.0.9 +GitPython==3.1.27 +htmlgenerator==1.2.18 idna==3.3 importlib-metadata==4.12.0 +Jinja2==3.1.2 lxml==4.9.1 Markdown==3.4.1 MarkupPy==1.14 +MarkupSafe==2.1.1 +numpy==1.23.2 odfpy==1.4.1 OpenCC==1.1.4 openpyxl==3.0.10 packaging==21.3 +pandas==1.4.3 +prettytable==3.3.0 pyparsing==3.0.9 pypushdeer==0.0.3 +python-dateutil==2.8.2 pytz==2022.2.1 pytz-deprecation-shim==0.1.0.post0 PyYAML==6.0 @@ -34,7 +45,9 @@ qbittorrent-api==2022.8.36 redis==4.3.4 requests==2.28.1 requests-toolbelt==0.9.1 +simplejson==3.17.6 six==1.16.0 +smmap==5.0.0 sqlparse==0.4.2 tablib==3.2.1 transmission-rpc==3.3.2 @@ -42,6 +55,8 @@ typing_extensions==4.3.0 tzdata==2022.2 tzlocal==4.2 urllib3==1.26.11 +wcwidth==0.2.5 +websocket-client==1.4.0 wechat-push==1.0.1 wrapt==1.14.1 wxpusher==2.2.0 diff --git a/templates/auto_pt/restart.html b/templates/auto_pt/restart.html index b7f6f58..e5fa3c0 100644 --- a/templates/auto_pt/restart.html +++ b/templates/auto_pt/restart.html @@ -22,7 +22,7 @@ {# 获取更新#} 更新 - 重启 + 重启
@@ -92,7 +92,6 @@ data: { update_notes: {{ update_notes|safe }}, local_logs: {{ local_logs|safe }}, - restart:{{ restart }}, update: false, local_log_width: 24 },