1. 优化重启功能,以后无需设置环境变量即可使用

This commit is contained in:
ngfchl
2022-08-28 19:59:19 +08:00
parent 1554e8a417
commit 8fb7db8c2b
4 changed files with 77 additions and 73 deletions

View File

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

View File

@@ -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_id28987
uid: UID_jkMs0DaVVwOcBuFPQGzymjCwYVgH
应用名称pt_helper
appTokenAT_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(

View File

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

View File

@@ -22,7 +22,7 @@
<span style="float: right;margin-top: -10px">
{# <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>
<el-button type="danger" @click="do_restart">重启</el-button>
</span>
</div>
<div class="text item">
@@ -92,7 +92,6 @@
data: {
update_notes: {{ update_notes|safe }},
local_logs: {{ local_logs|safe }},
restart:{{ restart }},
update: false,
local_log_width: 24
},