2017-03-15 16:43:43 +00:00
|
|
|
# ~*~ coding: utf-8 ~*~
|
2020-12-29 13:05:18 +00:00
|
|
|
import os
|
|
|
|
|
2018-12-10 02:11:54 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2019-12-05 07:09:25 +00:00
|
|
|
|
2017-12-22 13:42:12 +00:00
|
|
|
from common.utils import get_logger, get_object_or_none
|
2019-12-05 07:09:25 +00:00
|
|
|
from common.tasks import send_mail_async
|
2020-12-16 09:31:30 +00:00
|
|
|
from orgs.utils import org_aware_func
|
2020-03-12 08:24:38 +00:00
|
|
|
|
2017-12-22 13:42:12 +00:00
|
|
|
from .models import Task, AdHoc
|
2017-03-15 16:43:43 +00:00
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
2017-12-06 10:31:51 +00:00
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
DEFAULT_TASK_OPTIONS = {
|
|
|
|
'timeout': 10,
|
|
|
|
'forks': 10,
|
|
|
|
}
|
|
|
|
|
2017-12-07 05:01:33 +00:00
|
|
|
|
2017-12-22 13:42:12 +00:00
|
|
|
def get_task_by_id(task_id):
|
|
|
|
return get_object_or_none(Task, id=task_id)
|
2017-12-10 16:29:25 +00:00
|
|
|
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
@org_aware_func("hosts")
|
2017-12-24 10:53:07 +00:00
|
|
|
def update_or_create_ansible_task(
|
2020-03-12 08:24:38 +00:00
|
|
|
task_name, hosts, tasks,
|
2017-12-24 10:53:07 +00:00
|
|
|
interval=None, crontab=None, is_periodic=False,
|
|
|
|
callback=None, pattern='all', options=None,
|
2018-12-10 02:11:54 +00:00
|
|
|
run_as_admin=False, run_as=None, become_info=None,
|
2017-12-15 07:50:15 +00:00
|
|
|
):
|
2018-01-16 03:40:18 +00:00
|
|
|
if not hosts or not tasks or not task_name:
|
2019-06-13 10:58:43 +00:00
|
|
|
return None, None
|
2020-03-12 08:24:38 +00:00
|
|
|
if options is None:
|
|
|
|
options = DEFAULT_TASK_OPTIONS
|
2017-12-24 10:53:07 +00:00
|
|
|
defaults = {
|
|
|
|
'name': task_name,
|
|
|
|
'interval': interval,
|
|
|
|
'crontab': crontab,
|
|
|
|
'is_periodic': is_periodic,
|
|
|
|
'callback': callback,
|
|
|
|
}
|
2017-12-22 13:42:12 +00:00
|
|
|
|
2017-12-24 10:53:07 +00:00
|
|
|
created = False
|
2018-12-10 02:11:54 +00:00
|
|
|
task, ok = Task.objects.update_or_create(
|
2020-03-12 08:24:38 +00:00
|
|
|
defaults=defaults, name=task_name
|
2017-12-24 10:53:07 +00:00
|
|
|
)
|
2018-12-10 02:11:54 +00:00
|
|
|
adhoc = task.get_latest_adhoc()
|
2017-12-15 07:50:15 +00:00
|
|
|
new_adhoc = AdHoc(task=task, pattern=pattern,
|
|
|
|
run_as_admin=run_as_admin,
|
|
|
|
run_as=run_as)
|
|
|
|
new_adhoc.tasks = tasks
|
|
|
|
new_adhoc.options = options
|
|
|
|
new_adhoc.become = become_info
|
2017-12-24 10:53:07 +00:00
|
|
|
|
2018-12-10 02:11:54 +00:00
|
|
|
hosts_same = True
|
|
|
|
if adhoc:
|
|
|
|
old_hosts = set([str(asset.id) for asset in adhoc.hosts.all()])
|
|
|
|
new_hosts = set([str(asset.id) for asset in hosts])
|
|
|
|
hosts_same = old_hosts == new_hosts
|
|
|
|
|
2020-03-16 06:18:29 +00:00
|
|
|
if not adhoc or not adhoc.same_with(new_adhoc) or not hosts_same:
|
2019-01-21 09:43:08 +00:00
|
|
|
logger.debug(_("Update task content: {}").format(task_name))
|
2017-12-15 07:50:15 +00:00
|
|
|
new_adhoc.save()
|
2018-12-10 02:11:54 +00:00
|
|
|
new_adhoc.hosts.set(hosts)
|
2017-12-15 07:50:15 +00:00
|
|
|
task.latest_adhoc = new_adhoc
|
2017-12-24 10:53:07 +00:00
|
|
|
created = True
|
|
|
|
return task, created
|
2017-12-22 13:42:12 +00:00
|
|
|
|
|
|
|
|
2019-12-05 07:09:25 +00:00
|
|
|
def send_server_performance_mail(path, usage, usages):
|
|
|
|
from users.models import User
|
|
|
|
subject = _("Disk used more than 80%: {} => {}").format(path, usage.percent)
|
|
|
|
message = subject
|
2020-07-28 09:53:01 +00:00
|
|
|
admins = User.objects.filter(role=User.ROLE.ADMIN)
|
2019-12-05 07:09:25 +00:00
|
|
|
recipient_list = [u.email for u in admins if u.email]
|
|
|
|
logger.info(subject)
|
|
|
|
send_mail_async(subject, message, recipient_list, html_message=message)
|
|
|
|
|
|
|
|
|
2020-12-29 13:05:18 +00:00
|
|
|
def get_task_log_path(base_path, task_id, level=2):
|
|
|
|
task_id = str(task_id)
|
|
|
|
rel_path = os.path.join(*task_id[:level], task_id + '.log')
|
|
|
|
path = os.path.join(base_path, rel_path)
|
|
|
|
os.makedirs(os.path.dirname(path), exist_ok=True)
|
|
|
|
return path
|
|
|
|
|