mirror of
https://github.com/jumpserver/jumpserver.git
synced 2026-01-13 19:45:40 +00:00
* [Update] 修改config * [Update] 移动存储设置到到terminal中 * [Update] 修改permission 查看 * [Update] pre merge * [Update] 录像存储 * [Update] 命令存储 * [Update] 添加存储测试可连接性 * [Update] 修改 meta 值的 key 为大写 * [Update] 修改 Terminal 相关 Storage 配置 * [Update] 删除之前获取录像/命令存储的代码 * [Update] 修改导入失败 * [Update] 迁移文件添加default存储 * [Update] 删除之前代码,添加help_text信息 * [Update] 删除之前代码 * [Update] 删除之前代码 * [Update] 抽象命令/录像存储 APIView * [Update] 抽象命令/录像存储 APIView 1 * [Update] 抽象命令/录像存储 DictField * [Update] 抽象命令/录像存储列表页面 * [Update] 修复CustomDictField的bug * [Update] RemoteApp 页面添加 hidden * [Update] 用户页面添加用户关联授权 * [Update] 修改存储测试可连接性 target * [Update] 修改配置 * [Update] 修改存储前端 Form 渲染逻辑 * [Update] 修改存储细节 * [Update] 统一存储类型到 const 文件 * [Update] 修改迁移文件及Model,创建默认存储 * [Update] 修改迁移文件及Model初始化默认数据 * [Update] 修改迁移文件 * [Update] 修改迁移文件 * [Update] 修改迁移文件 * [Update] 修改迁移文件 * [Update] 修改迁移文件 * [Update] 修改迁移文件 * [Update] 修改迁移文件 * [Update] 限制删除默认存储配置,只允许创建扩展的存储类型 * [Update] 修改ip字段长度 * [Update] 修改ip字段长度 * [Update] 修改一些css * [Update] 修改关联 * [Update] 添加操作日志定时清理 * [Update] 修改记录syslog的instance encoder * [Update] 忽略登录产生的操作日志 * [Update] 限制更新存储时不覆盖原有AK SK 等字段 * [Update] 修改迁移文件添加comment字段 * [Update] 修改迁移文件 * [Update] 添加 comment 字段 * [Update] 修改默认存储no -> null * [Update] 修改细节 * [Update] 更新翻译(存储配置 * [Update] 修改定时任务注册,修改系统用户资产、节点关系api * [Update] 添加监控磁盘任务 * [Update] 修改session * [Update] 拆分serializer * [Update] 还原setting原来的manager
157 lines
4.3 KiB
Python
157 lines
4.3 KiB
Python
# coding: utf-8
|
|
import os
|
|
import subprocess
|
|
import time
|
|
|
|
from django.conf import settings
|
|
from celery import shared_task, subtask
|
|
from celery.exceptions import SoftTimeLimitExceeded
|
|
from django.utils import timezone
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
from common.utils import get_logger, get_object_or_none, get_disk_usage
|
|
from .celery.decorator import (
|
|
register_as_period_task, after_app_shutdown_clean_periodic,
|
|
after_app_ready_start
|
|
)
|
|
from .celery.utils import create_or_update_celery_periodic_tasks
|
|
from .models import Task, CommandExecution, CeleryTask
|
|
from .utils import send_server_performance_mail
|
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
|
def rerun_task():
|
|
pass
|
|
|
|
|
|
@shared_task(queue="ansible")
|
|
def run_ansible_task(tid, callback=None, **kwargs):
|
|
"""
|
|
:param tid: is the tasks serialized data
|
|
:param callback: callback function name
|
|
:return:
|
|
"""
|
|
task = get_object_or_none(Task, id=tid)
|
|
if task:
|
|
result = task.run()
|
|
if callback is not None:
|
|
subtask(callback).delay(result, task_name=task.name)
|
|
return result
|
|
else:
|
|
logger.error("No task found")
|
|
|
|
|
|
@shared_task(soft_time_limit=60, queue="ansible")
|
|
def run_command_execution(cid, **kwargs):
|
|
execution = get_object_or_none(CommandExecution, id=cid)
|
|
if execution:
|
|
try:
|
|
os.environ.update({
|
|
"TERM_ROWS": kwargs.get("rows", ""),
|
|
"TERM_COLS": kwargs.get("cols", ""),
|
|
})
|
|
execution.run()
|
|
except SoftTimeLimitExceeded:
|
|
logger.error("Run time out")
|
|
else:
|
|
logger.error("Not found the execution id: {}".format(cid))
|
|
|
|
|
|
@shared_task
|
|
@after_app_shutdown_clean_periodic
|
|
@register_as_period_task(interval=3600*24, description=_("Clean task history period"))
|
|
def clean_tasks_adhoc_period():
|
|
logger.debug("Start clean task adhoc and run history")
|
|
tasks = Task.objects.all()
|
|
for task in tasks:
|
|
adhoc = task.adhoc.all().order_by('-date_created')[5:]
|
|
for ad in adhoc:
|
|
ad.history.all().delete()
|
|
ad.delete()
|
|
|
|
|
|
@shared_task
|
|
@after_app_shutdown_clean_periodic
|
|
@register_as_period_task(interval=3600*24, description=_("Clean celery log period"))
|
|
def clean_celery_tasks_period():
|
|
expire_days = 30
|
|
logger.debug("Start clean celery task history")
|
|
one_month_ago = timezone.now() - timezone.timedelta(days=expire_days)
|
|
tasks = CeleryTask.objects.filter(date_start__lt=one_month_ago)
|
|
for task in tasks:
|
|
if os.path.isfile(task.full_log_path):
|
|
try:
|
|
os.remove(task.full_log_path)
|
|
except (FileNotFoundError, PermissionError):
|
|
pass
|
|
task.delete()
|
|
tasks = CeleryTask.objects.filter(date_start__isnull=True)
|
|
tasks.delete()
|
|
command = "find %s -mtime +%s -name '*.log' -type f -exec rm -f {} \\;" % (
|
|
settings.CELERY_LOG_DIR, expire_days
|
|
)
|
|
subprocess.call(command, shell=True)
|
|
command = "echo > {}".format(os.path.join(settings.LOG_DIR, 'celery.log'))
|
|
subprocess.call(command, shell=True)
|
|
|
|
|
|
@shared_task
|
|
@after_app_ready_start
|
|
def create_or_update_registered_periodic_tasks():
|
|
from .celery.decorator import get_register_period_tasks
|
|
for task in get_register_period_tasks():
|
|
create_or_update_celery_periodic_tasks(task)
|
|
|
|
|
|
@shared_task
|
|
@register_as_period_task(interval=3600)
|
|
def check_server_performance_period():
|
|
usages = get_disk_usage()
|
|
usages = {path: usage for path, usage in usages.items()
|
|
if not path.startswith('/etc')}
|
|
|
|
for path, usage in usages.items():
|
|
if usage.percent > 80:
|
|
send_server_performance_mail(path, usage, usages)
|
|
return
|
|
|
|
|
|
@shared_task(queue="ansible")
|
|
def hello(name, callback=None):
|
|
import time
|
|
time.sleep(10)
|
|
print("Hello {}".format(name))
|
|
|
|
|
|
@shared_task
|
|
# @after_app_shutdown_clean_periodic
|
|
# @register_as_period_task(interval=30)
|
|
def hello123():
|
|
return None
|
|
|
|
|
|
@shared_task
|
|
def hello_callback(result):
|
|
print(result)
|
|
print("Hello callback")
|
|
|
|
|
|
@shared_task
|
|
def add(a, b):
|
|
time.sleep(5)
|
|
return a + b
|
|
|
|
|
|
@shared_task
|
|
def add_m(x):
|
|
from celery import chain
|
|
a = range(x)
|
|
b = [a[i:i + 10] for i in range(0, len(a), 10)]
|
|
s = list()
|
|
s.append(add.s(b[0], b[1]))
|
|
for i in b[1:]:
|
|
s.append(add.s(i))
|
|
res = chain(*tuple(s))()
|
|
return res
|