2017-03-15 16:43:43 +00:00
|
|
|
# ~*~ coding: utf-8 ~*~
|
2020-12-29 13:05:18 +00:00
|
|
|
import os
|
2021-01-19 06:36:41 +00:00
|
|
|
import uuid
|
2020-12-29 13:05:18 +00:00
|
|
|
|
2018-12-10 02:11:54 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2019-12-05 07:09:25 +00:00
|
|
|
|
2022-12-06 09:30:54 +00:00
|
|
|
from common.utils import get_logger, get_object_or_none, make_dirs
|
2020-12-16 09:31:30 +00:00
|
|
|
from orgs.utils import org_aware_func
|
2021-01-19 06:36:41 +00:00
|
|
|
from jumpserver.const import PROJECT_DIR
|
2020-03-12 08:24:38 +00:00
|
|
|
|
2022-12-05 07:03:21 +00:00
|
|
|
from .models import AdHoc, CeleryTask
|
2022-09-07 11:49:42 +00:00
|
|
|
from .const import DEFAULT_PASSWORD_RULES
|
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,
|
2021-07-14 08:49:28 +00:00
|
|
|
run_as_admin=False, run_as=None, system_user=None, become_info=None,
|
2022-09-07 11:49:42 +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,
|
2021-07-14 08:49:28 +00:00
|
|
|
run_as=run_as, run_system_user=system_user)
|
2017-12-15 07:50:15 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
2020-12-29 13:05:18 +00:00
|
|
|
def get_task_log_path(base_path, task_id, level=2):
|
|
|
|
task_id = str(task_id)
|
2021-01-19 06:36:41 +00:00
|
|
|
try:
|
|
|
|
uuid.UUID(task_id)
|
|
|
|
except:
|
|
|
|
return os.path.join(PROJECT_DIR, 'data', 'caution.txt')
|
|
|
|
|
2020-12-29 13:05:18 +00:00
|
|
|
rel_path = os.path.join(*task_id[:level], task_id + '.log')
|
|
|
|
path = os.path.join(base_path, rel_path)
|
2022-10-18 09:47:13 +00:00
|
|
|
make_dirs(os.path.dirname(path), exist_ok=True)
|
2020-12-29 13:05:18 +00:00
|
|
|
return path
|
2022-09-07 11:49:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
def generate_random_password(**kwargs):
|
|
|
|
import random
|
|
|
|
import string
|
|
|
|
length = int(kwargs.get('length', DEFAULT_PASSWORD_RULES['length']))
|
|
|
|
symbol_set = kwargs.get('symbol_set')
|
|
|
|
if symbol_set is None:
|
|
|
|
symbol_set = DEFAULT_PASSWORD_RULES['symbol_set']
|
|
|
|
chars = string.ascii_letters + string.digits + symbol_set
|
|
|
|
password = ''.join([random.choice(chars) for _ in range(length)])
|
|
|
|
return password
|