67 lines
2.0 KiB
Python
Executable File
67 lines
2.0 KiB
Python
Executable File
from __future__ import absolute_import, unicode_literals
|
|
from celery import shared_task
|
|
import subprocess
|
|
from apps.utils.tasks import CustomTask
|
|
from server.settings import DATABASES, BACKUP_PATH, SH_PATH, SD_PWD
|
|
import requests
|
|
import json
|
|
from django.conf import settings
|
|
from django.core.cache import cache
|
|
|
|
|
|
@shared_task
|
|
def backup_database():
|
|
"""
|
|
备份数据库
|
|
"""
|
|
import datetime
|
|
name = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
|
|
command = 'echo "{}" | sudo -S pg_dump "user={} password={} dbname={}" > {}/bak_{}.sql'.format(
|
|
SD_PWD,
|
|
DATABASES['default']['USER'],
|
|
DATABASES['default']['PASSWORD'],
|
|
DATABASES['default']['NAME'],
|
|
BACKUP_PATH + '/database',
|
|
name)
|
|
completed = subprocess.run(command, shell=True, capture_output=True, text=True)
|
|
return completed
|
|
|
|
|
|
@shared_task
|
|
def reload_server_git():
|
|
command = 'bash {}/git_server.sh'.format(SH_PATH)
|
|
completed = subprocess.run(command, shell=True, capture_output=True, text=True)
|
|
if completed.returncode != 0:
|
|
return completed.stderr
|
|
|
|
|
|
@shared_task
|
|
def reload_web_git():
|
|
command = 'bash {}/git_web.sh'.format(SH_PATH)
|
|
completed = subprocess.run(command, shell=True, capture_output=True, text=True)
|
|
if completed.returncode != 0:
|
|
return completed.stderr
|
|
|
|
|
|
@shared_task
|
|
def reload_server_only():
|
|
command = 'echo "{}" | sudo -S supervisorctl reload'.format(SD_PWD)
|
|
completed = subprocess.run(command, shell=True, capture_output=True, text=True)
|
|
return completed
|
|
|
|
|
|
@shared_task
|
|
def backup_media():
|
|
command = 'bash {}/backup_media.sh'.format(SH_PATH)
|
|
completed = subprocess.run(command, shell=True, capture_output=True, text=True)
|
|
return completed
|
|
|
|
|
|
@shared_task(base=CustomTask)
|
|
def get_wx_token():
|
|
r = requests.get('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}'.format(
|
|
settings.WX_APPID, settings.WX_APPSECRET)).text
|
|
token = json.loads(r)['access_token']
|
|
cache.set('wx_token', token)
|
|
return token
|