mirror of https://github.com/jumpserver/jumpserver
parent
8ed823d587
commit
d182d14e26
|
@ -1,22 +1,17 @@
|
||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
from openpyxl import Workbook
|
|
||||||
from collections import defaultdict, OrderedDict
|
from collections import defaultdict, OrderedDict
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.db.models import F
|
from openpyxl import Workbook
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
|
|
||||||
from accounts.models import Account
|
|
||||||
from assets.const import AllTypes
|
|
||||||
from accounts.serializers import AccountSecretSerializer
|
|
||||||
from accounts.notifications import AccountBackupExecutionTaskMsg
|
from accounts.notifications import AccountBackupExecutionTaskMsg
|
||||||
from users.models import User
|
from accounts.serializers import AccountSecretSerializer
|
||||||
from common.utils import get_logger
|
from assets.const import AllTypes
|
||||||
from common.utils.timezone import local_now_display
|
|
||||||
from common.utils.file import encrypt_and_compress_zip_file
|
from common.utils.file import encrypt_and_compress_zip_file
|
||||||
|
from common.utils.timezone import local_now_display
|
||||||
logger = get_logger(__file__)
|
from users.models import User
|
||||||
|
|
||||||
PATH = os.path.join(os.path.dirname(settings.BASE_DIR), 'tmp')
|
PATH = os.path.join(os.path.dirname(settings.BASE_DIR), 'tmp')
|
||||||
|
|
||||||
|
@ -99,7 +94,7 @@ class AssetAccountHandler(BaseAccountHandler):
|
||||||
data = AccountSecretSerializer(_accounts, many=True).data
|
data = AccountSecretSerializer(_accounts, many=True).data
|
||||||
data_map.update(cls.add_rows(data, header_fields, sheet_name))
|
data_map.update(cls.add_rows(data, header_fields, sheet_name))
|
||||||
|
|
||||||
logger.info('\n\033[33m- 共备份 {} 条账号\033[0m'.format(accounts.count()))
|
print('\n\033[33m- 共备份 {} 条账号\033[0m'.format(accounts.count()))
|
||||||
return data_map
|
return data_map
|
||||||
|
|
||||||
|
|
||||||
|
@ -110,7 +105,7 @@ class AccountBackupHandler:
|
||||||
self.is_frozen = False # 任务状态冻结标志
|
self.is_frozen = False # 任务状态冻结标志
|
||||||
|
|
||||||
def create_excel(self):
|
def create_excel(self):
|
||||||
logger.info(
|
print(
|
||||||
'\n'
|
'\n'
|
||||||
'\033[32m>>> 正在生成资产或应用相关备份信息文件\033[0m'
|
'\033[32m>>> 正在生成资产或应用相关备份信息文件\033[0m'
|
||||||
''
|
''
|
||||||
|
@ -133,14 +128,14 @@ class AccountBackupHandler:
|
||||||
wb.save(filename)
|
wb.save(filename)
|
||||||
files.append(filename)
|
files.append(filename)
|
||||||
timedelta = round((time.time() - time_start), 2)
|
timedelta = round((time.time() - time_start), 2)
|
||||||
logger.info('步骤完成: 用时 {}s'.format(timedelta))
|
print('步骤完成: 用时 {}s'.format(timedelta))
|
||||||
return files
|
return files
|
||||||
|
|
||||||
def send_backup_mail(self, files, recipients):
|
def send_backup_mail(self, files, recipients):
|
||||||
if not files:
|
if not files:
|
||||||
return
|
return
|
||||||
recipients = User.objects.filter(id__in=list(recipients))
|
recipients = User.objects.filter(id__in=list(recipients))
|
||||||
logger.info(
|
print(
|
||||||
'\n'
|
'\n'
|
||||||
'\033[32m>>> 发送备份邮件\033[0m'
|
'\033[32m>>> 发送备份邮件\033[0m'
|
||||||
''
|
''
|
||||||
|
@ -155,7 +150,7 @@ class AccountBackupHandler:
|
||||||
encrypt_and_compress_zip_file(attachment, password, files)
|
encrypt_and_compress_zip_file(attachment, password, files)
|
||||||
attachment_list = [attachment, ]
|
attachment_list = [attachment, ]
|
||||||
AccountBackupExecutionTaskMsg(plan_name, user).publish(attachment_list)
|
AccountBackupExecutionTaskMsg(plan_name, user).publish(attachment_list)
|
||||||
logger.info('邮件已发送至{}({})'.format(user, user.email))
|
print('邮件已发送至{}({})'.format(user, user.email))
|
||||||
for file in files:
|
for file in files:
|
||||||
os.remove(file)
|
os.remove(file)
|
||||||
|
|
||||||
|
@ -163,13 +158,13 @@ class AccountBackupHandler:
|
||||||
self.execution.reason = reason[:1024]
|
self.execution.reason = reason[:1024]
|
||||||
self.execution.is_success = is_success
|
self.execution.is_success = is_success
|
||||||
self.execution.save()
|
self.execution.save()
|
||||||
logger.info('已完成对任务状态的更新')
|
print('已完成对任务状态的更新')
|
||||||
|
|
||||||
def step_finished(self, is_success):
|
def step_finished(self, is_success):
|
||||||
if is_success:
|
if is_success:
|
||||||
logger.info('任务执行成功')
|
print('任务执行成功')
|
||||||
else:
|
else:
|
||||||
logger.error('任务执行失败')
|
print('任务执行失败')
|
||||||
|
|
||||||
def _run(self):
|
def _run(self):
|
||||||
is_success = False
|
is_success = False
|
||||||
|
@ -177,7 +172,7 @@ class AccountBackupHandler:
|
||||||
try:
|
try:
|
||||||
recipients = self.execution.plan_snapshot.get('recipients')
|
recipients = self.execution.plan_snapshot.get('recipients')
|
||||||
if not recipients:
|
if not recipients:
|
||||||
logger.info(
|
print(
|
||||||
'\n'
|
'\n'
|
||||||
'\033[32m>>> 该备份任务未分配收件人\033[0m'
|
'\033[32m>>> 该备份任务未分配收件人\033[0m'
|
||||||
''
|
''
|
||||||
|
@ -187,9 +182,9 @@ class AccountBackupHandler:
|
||||||
self.send_backup_mail(files, recipients)
|
self.send_backup_mail(files, recipients)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.is_frozen = True
|
self.is_frozen = True
|
||||||
logger.error('任务执行被异常中断')
|
print('任务执行被异常中断')
|
||||||
logger.info('下面打印发生异常的 Traceback 信息 : ')
|
print('下面打印发生异常的 Traceback 信息 : ')
|
||||||
logger.error(e, exc_info=True)
|
print(e)
|
||||||
error = str(e)
|
error = str(e)
|
||||||
else:
|
else:
|
||||||
is_success = True
|
is_success = True
|
||||||
|
@ -199,15 +194,15 @@ class AccountBackupHandler:
|
||||||
self.step_finished(is_success)
|
self.step_finished(is_success)
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
logger.info('任务开始: {}'.format(local_now_display()))
|
print('任务开始: {}'.format(local_now_display()))
|
||||||
time_start = time.time()
|
time_start = time.time()
|
||||||
try:
|
try:
|
||||||
self._run()
|
self._run()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error('任务运行出现异常')
|
print('任务运行出现异常')
|
||||||
logger.error('下面显示异常 Traceback 信息: ')
|
print('下面显示异常 Traceback 信息: ')
|
||||||
logger.error(e, exc_info=True)
|
print(e)
|
||||||
finally:
|
finally:
|
||||||
logger.info('\n任务结束: {}'.format(local_now_display()))
|
print('\n任务结束: {}'.format(local_now_display()))
|
||||||
timedelta = round((time.time() - time_start), 2)
|
timedelta = round((time.time() - time_start), 2)
|
||||||
logger.info('用时: {}'.format(timedelta))
|
print('用时: {}'.format(timedelta))
|
||||||
|
|
|
@ -4,13 +4,9 @@ import time
|
||||||
|
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
|
||||||
from common.utils import get_logger
|
|
||||||
from common.utils.timezone import local_now_display
|
from common.utils.timezone import local_now_display
|
||||||
|
|
||||||
from .handlers import AccountBackupHandler
|
from .handlers import AccountBackupHandler
|
||||||
|
|
||||||
logger = get_logger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
class AccountBackupManager:
|
class AccountBackupManager:
|
||||||
def __init__(self, execution):
|
def __init__(self, execution):
|
||||||
|
@ -23,7 +19,7 @@ class AccountBackupManager:
|
||||||
|
|
||||||
def do_run(self):
|
def do_run(self):
|
||||||
execution = self.execution
|
execution = self.execution
|
||||||
logger.info('\n\033[33m# 账号备份计划正在执行\033[0m')
|
print('\n\033[33m# 账号备份计划正在执行\033[0m')
|
||||||
handler = AccountBackupHandler(execution)
|
handler = AccountBackupHandler(execution)
|
||||||
handler.run()
|
handler.run()
|
||||||
|
|
||||||
|
@ -35,10 +31,10 @@ class AccountBackupManager:
|
||||||
self.time_end = time.time()
|
self.time_end = time.time()
|
||||||
self.date_end = timezone.now()
|
self.date_end = timezone.now()
|
||||||
|
|
||||||
logger.info('\n\n' + '-' * 80)
|
print('\n\n' + '-' * 80)
|
||||||
logger.info('计划执行结束 {}\n'.format(local_now_display()))
|
print('计划执行结束 {}\n'.format(local_now_display()))
|
||||||
self.timedelta = self.time_end - self.time_start
|
self.timedelta = self.time_end - self.time_start
|
||||||
logger.info('用时: {}s'.format(self.timedelta))
|
print('用时: {}s'.format(self.timedelta))
|
||||||
self.execution.timedelta = self.timedelta
|
self.execution.timedelta = self.timedelta
|
||||||
self.execution.save()
|
self.execution.save()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue