2017-03-09 06:55:33 +00:00
|
|
|
# ~*~ coding: utf-8 ~*~
|
|
|
|
|
2017-11-23 06:08:01 +00:00
|
|
|
import uuid
|
2018-03-30 14:03:43 +00:00
|
|
|
import os
|
2017-12-21 18:08:29 +00:00
|
|
|
import time
|
2018-03-30 14:03:43 +00:00
|
|
|
import datetime
|
|
|
|
|
2018-04-01 15:45:37 +00:00
|
|
|
from celery import current_task
|
2017-03-09 06:55:33 +00:00
|
|
|
from django.db import models
|
2018-03-30 14:03:43 +00:00
|
|
|
from django.conf import settings
|
2017-12-21 18:08:29 +00:00
|
|
|
from django.utils import timezone
|
2017-03-09 06:55:33 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2017-12-10 16:29:25 +00:00
|
|
|
|
2019-12-18 07:37:53 +00:00
|
|
|
from common.utils import get_logger, lazyproperty
|
|
|
|
from common.fields.model import (
|
|
|
|
JsonListTextField, JsonDictCharField, EncryptJsonDictCharField,
|
|
|
|
JsonDictTextField,
|
|
|
|
)
|
2020-03-12 08:24:38 +00:00
|
|
|
from orgs.mixins.models import OrgModelMixin
|
2018-04-02 07:54:49 +00:00
|
|
|
from ..ansible import AdHocRunner, AnsibleError
|
|
|
|
from ..inventory import JMSInventory
|
2020-03-12 08:24:38 +00:00
|
|
|
from ..mixin import PeriodTaskModelMixin
|
2017-03-09 06:55:33 +00:00
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
__all__ = ["Task", "AdHoc", "AdHocExecution"]
|
2017-03-09 06:55:33 +00:00
|
|
|
|
|
|
|
|
2017-12-22 13:42:12 +00:00
|
|
|
logger = get_logger(__file__)
|
2017-03-09 06:55:33 +00:00
|
|
|
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
class Task(PeriodTaskModelMixin, OrgModelMixin):
|
2017-12-10 16:29:25 +00:00
|
|
|
"""
|
|
|
|
This task is different ansible task, Task like 'push system user', 'get asset info' ..
|
|
|
|
One task can have some versions of adhoc, run a task only run the latest version adhoc
|
|
|
|
"""
|
2017-12-22 13:42:12 +00:00
|
|
|
callback = models.CharField(max_length=128, blank=True, null=True, verbose_name=_("Callback")) # Callback must be a registered celery task
|
2017-12-07 08:25:50 +00:00
|
|
|
is_deleted = models.BooleanField(default=False)
|
2017-12-21 18:08:29 +00:00
|
|
|
comment = models.TextField(blank=True, verbose_name=_("Comment"))
|
2019-09-19 13:21:05 +00:00
|
|
|
date_created = models.DateTimeField(auto_now_add=True, db_index=True, verbose_name=_("Date created"))
|
|
|
|
date_updated = models.DateTimeField(auto_now=True, verbose_name=_("Date updated"))
|
2019-12-18 07:37:53 +00:00
|
|
|
latest_adhoc = models.ForeignKey('ops.AdHoc', on_delete=models.SET_NULL, null=True, related_name='task_latest')
|
2020-03-12 08:24:38 +00:00
|
|
|
latest_execution = models.ForeignKey('ops.AdHocExecution', on_delete=models.SET_NULL, null=True, related_name='task_latest')
|
2019-12-18 07:37:53 +00:00
|
|
|
total_run_amount = models.IntegerField(default=0)
|
|
|
|
success_run_amount = models.IntegerField(default=0)
|
2018-12-10 02:11:54 +00:00
|
|
|
_ignore_auto_created_by = True
|
2017-12-07 05:01:33 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def short_id(self):
|
|
|
|
return str(self.id).split('-')[-1]
|
2017-03-09 06:55:33 +00:00
|
|
|
|
2019-12-18 07:37:53 +00:00
|
|
|
@lazyproperty
|
2019-09-19 13:21:05 +00:00
|
|
|
def versions(self):
|
|
|
|
return self.adhoc.all().count()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_success(self):
|
2020-03-12 08:24:38 +00:00
|
|
|
if self.latest_execution:
|
|
|
|
return self.latest_execution.is_success
|
2019-09-19 13:21:05 +00:00
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def timedelta(self):
|
2020-03-12 08:24:38 +00:00
|
|
|
if self.latest_execution:
|
|
|
|
return self.latest_execution.timedelta
|
2019-09-19 13:21:05 +00:00
|
|
|
else:
|
|
|
|
return 0
|
2017-12-07 05:01:33 +00:00
|
|
|
|
2019-09-19 13:21:05 +00:00
|
|
|
@property
|
|
|
|
def date_start(self):
|
2020-03-12 08:24:38 +00:00
|
|
|
if self.latest_execution:
|
|
|
|
return self.latest_execution.date_start
|
2019-09-19 13:21:05 +00:00
|
|
|
else:
|
|
|
|
return None
|
2017-12-10 16:29:25 +00:00
|
|
|
|
2017-12-15 07:50:15 +00:00
|
|
|
@property
|
2019-09-19 13:21:05 +00:00
|
|
|
def assets_amount(self):
|
2020-03-12 08:24:38 +00:00
|
|
|
if self.latest_execution:
|
|
|
|
return self.latest_execution.hosts_amount
|
2019-12-18 07:37:53 +00:00
|
|
|
return 0
|
2017-12-10 16:29:25 +00:00
|
|
|
|
2017-12-15 07:50:15 +00:00
|
|
|
def get_latest_adhoc(self):
|
2019-12-18 07:37:53 +00:00
|
|
|
if self.latest_adhoc:
|
|
|
|
return self.latest_adhoc
|
2017-12-15 07:50:15 +00:00
|
|
|
try:
|
2019-12-18 07:37:53 +00:00
|
|
|
adhoc = self.adhoc.all().latest()
|
|
|
|
self.latest_adhoc = adhoc
|
|
|
|
self.save()
|
|
|
|
return adhoc
|
2017-12-15 07:50:15 +00:00
|
|
|
except AdHoc.DoesNotExist:
|
|
|
|
return None
|
2017-12-10 16:29:25 +00:00
|
|
|
|
2017-12-15 07:50:15 +00:00
|
|
|
@property
|
|
|
|
def history_summary(self):
|
2019-12-18 07:37:53 +00:00
|
|
|
total = self.total_run_amount
|
|
|
|
success = self.success_run_amount
|
|
|
|
failed = total - success
|
2017-12-10 16:29:25 +00:00
|
|
|
return {'total': total, 'success': success, 'failed': failed}
|
2017-03-09 06:55:33 +00:00
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
def get_run_execution(self):
|
|
|
|
return self.execution.all()
|
2017-12-15 07:50:15 +00:00
|
|
|
|
2019-12-18 07:37:53 +00:00
|
|
|
def run(self):
|
|
|
|
latest_adhoc = self.get_latest_adhoc()
|
|
|
|
if latest_adhoc:
|
|
|
|
return latest_adhoc.run()
|
2017-12-15 07:50:15 +00:00
|
|
|
else:
|
|
|
|
return {'error': 'No adhoc'}
|
|
|
|
|
2017-12-22 13:42:12 +00:00
|
|
|
@property
|
2020-03-12 08:24:38 +00:00
|
|
|
def period_key(self):
|
|
|
|
return self.__str__()
|
|
|
|
|
|
|
|
def get_register_task(self):
|
|
|
|
from ..tasks import run_ansible_task
|
|
|
|
name = self.__str__()
|
|
|
|
task = run_ansible_task.name
|
|
|
|
args = (str(self.id),)
|
|
|
|
kwargs = {"callback": self.callback}
|
|
|
|
return name, task, args, kwargs
|
2017-12-21 18:08:29 +00:00
|
|
|
|
2017-12-15 07:50:15 +00:00
|
|
|
def __str__(self):
|
2020-03-12 08:24:38 +00:00
|
|
|
return self.name + '@' + str(self.org_id)
|
2017-12-15 07:50:15 +00:00
|
|
|
|
2017-12-10 16:29:25 +00:00
|
|
|
class Meta:
|
|
|
|
db_table = 'ops_task'
|
2020-03-12 08:24:38 +00:00
|
|
|
unique_together = ('name', 'org_id')
|
2019-09-19 13:21:05 +00:00
|
|
|
ordering = ('-date_updated',)
|
2017-12-15 07:50:15 +00:00
|
|
|
get_latest_by = 'date_created'
|
2017-12-10 16:29:25 +00:00
|
|
|
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
class AdHoc(OrgModelMixin):
|
2017-12-10 16:29:25 +00:00
|
|
|
"""
|
|
|
|
task: A task reference
|
|
|
|
_tasks: [{'name': 'task_name', 'action': {'module': '', 'args': ''}, 'other..': ''}, ]
|
|
|
|
_options: ansible options, more see ops.ansible.runner.Options
|
|
|
|
run_as_admin: if true, then need get every host admin user run it, because every host may be have different admin user, so we choise host level
|
2019-03-18 02:15:33 +00:00
|
|
|
run_as: username(Add the uniform AssetUserManager <AssetUserManager> and change it to username)
|
2017-12-10 16:29:25 +00:00
|
|
|
_become: May be using become [sudo, su] options. {method: "sudo", user: "user", pass: "pass"]
|
|
|
|
pattern: Even if we set _hosts, We only use that to make inventory, We also can set `patter` to run task on match hosts
|
|
|
|
"""
|
|
|
|
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
|
|
|
|
task = models.ForeignKey(Task, related_name='adhoc', on_delete=models.CASCADE)
|
2019-12-18 07:37:53 +00:00
|
|
|
tasks = JsonListTextField(verbose_name=_('Tasks'))
|
2017-12-15 09:07:52 +00:00
|
|
|
pattern = models.CharField(max_length=64, default='{}', verbose_name=_('Pattern'))
|
2019-12-18 07:37:53 +00:00
|
|
|
options = JsonDictCharField(max_length=1024, default='', verbose_name=_('Options'))
|
2018-12-10 02:11:54 +00:00
|
|
|
hosts = models.ManyToManyField('assets.Asset', verbose_name=_("Host"))
|
2017-12-07 05:01:33 +00:00
|
|
|
run_as_admin = models.BooleanField(default=False, verbose_name=_('Run as admin'))
|
2019-08-21 12:27:21 +00:00
|
|
|
run_as = models.CharField(max_length=64, default='', blank=True, null=True, verbose_name=_('Username'))
|
2020-03-12 08:24:38 +00:00
|
|
|
become = EncryptJsonDictCharField(max_length=1024, default='', blank=True, null=True, verbose_name=_("Become"))
|
2019-08-21 12:27:21 +00:00
|
|
|
created_by = models.CharField(max_length=64, default='', blank=True, null=True, verbose_name=_('Create by'))
|
2018-12-20 02:40:52 +00:00
|
|
|
date_created = models.DateTimeField(auto_now_add=True, db_index=True)
|
2017-03-09 06:55:33 +00:00
|
|
|
|
2017-12-22 13:42:12 +00:00
|
|
|
@property
|
|
|
|
def inventory(self):
|
|
|
|
if self.become:
|
|
|
|
become_info = {
|
|
|
|
'become': {
|
|
|
|
self.become
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
become_info = None
|
|
|
|
|
|
|
|
inventory = JMSInventory(
|
2018-12-10 02:11:54 +00:00
|
|
|
self.hosts.all(), run_as_admin=self.run_as_admin,
|
2017-12-22 13:42:12 +00:00
|
|
|
run_as=self.run_as, become_info=become_info
|
|
|
|
)
|
|
|
|
return inventory
|
|
|
|
|
2017-12-07 08:25:50 +00:00
|
|
|
@property
|
2019-12-18 07:37:53 +00:00
|
|
|
def become_display(self):
|
|
|
|
if self.become:
|
|
|
|
return self.become.get("user", "")
|
|
|
|
return ""
|
2017-12-21 18:08:29 +00:00
|
|
|
|
2019-12-18 07:37:53 +00:00
|
|
|
def run(self):
|
2018-04-01 15:45:37 +00:00
|
|
|
try:
|
|
|
|
hid = current_task.request.id
|
|
|
|
except AttributeError:
|
|
|
|
hid = str(uuid.uuid4())
|
2020-03-12 08:24:38 +00:00
|
|
|
execution = AdHocExecution(
|
2019-12-18 07:37:53 +00:00
|
|
|
id=hid, adhoc=self, task=self.task,
|
2020-03-12 08:24:38 +00:00
|
|
|
task_display=str(self.task),
|
|
|
|
date_start=timezone.now(),
|
2020-03-18 11:58:08 +00:00
|
|
|
hosts_amount=self.hosts.count(),
|
2019-12-18 07:37:53 +00:00
|
|
|
)
|
2020-03-12 08:24:38 +00:00
|
|
|
execution.save()
|
|
|
|
return execution.start()
|
2017-12-07 08:25:50 +00:00
|
|
|
|
2017-12-07 05:01:33 +00:00
|
|
|
@property
|
2017-12-10 16:29:25 +00:00
|
|
|
def short_id(self):
|
|
|
|
return str(self.id).split('-')[-1]
|
2017-03-13 16:58:25 +00:00
|
|
|
|
2017-12-15 07:50:15 +00:00
|
|
|
@property
|
2020-03-12 08:24:38 +00:00
|
|
|
def latest_execution(self):
|
2017-12-15 07:50:15 +00:00
|
|
|
try:
|
2020-03-12 08:24:38 +00:00
|
|
|
return self.execution.all().latest()
|
|
|
|
except AdHocExecution.DoesNotExist:
|
2017-12-15 07:50:15 +00:00
|
|
|
return None
|
2017-12-07 08:25:50 +00:00
|
|
|
|
2019-11-14 03:46:42 +00:00
|
|
|
def save(self, **kwargs):
|
|
|
|
instance = super().save(**kwargs)
|
2019-12-18 07:37:53 +00:00
|
|
|
self.task.latest_adhoc = instance
|
|
|
|
self.task.save()
|
2019-11-14 03:46:42 +00:00
|
|
|
return instance
|
2017-12-24 10:53:07 +00:00
|
|
|
|
2017-12-07 05:01:33 +00:00
|
|
|
def __str__(self):
|
2017-12-10 16:29:25 +00:00
|
|
|
return "{} of {}".format(self.task.name, self.short_id)
|
2017-12-07 05:01:33 +00:00
|
|
|
|
2020-03-16 06:18:29 +00:00
|
|
|
def same_with(self, other):
|
2017-12-15 07:50:15 +00:00
|
|
|
if not isinstance(other, self.__class__):
|
|
|
|
return False
|
|
|
|
fields_check = []
|
|
|
|
for field in self.__class__._meta.fields:
|
|
|
|
if field.name not in ['id', 'date_created']:
|
|
|
|
fields_check.append(field)
|
|
|
|
for field in fields_check:
|
|
|
|
if getattr(self, field.name) != getattr(other, field.name):
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2017-12-07 08:25:50 +00:00
|
|
|
class Meta:
|
2017-12-10 16:29:25 +00:00
|
|
|
db_table = "ops_adhoc"
|
2017-12-15 07:50:15 +00:00
|
|
|
get_latest_by = 'date_created'
|
2017-12-07 08:25:50 +00:00
|
|
|
|
2017-12-07 05:01:33 +00:00
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
class AdHocExecution(OrgModelMixin):
|
2017-12-10 16:29:25 +00:00
|
|
|
"""
|
|
|
|
AdHoc running history.
|
|
|
|
"""
|
|
|
|
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
|
2020-03-12 08:24:38 +00:00
|
|
|
task = models.ForeignKey(Task, related_name='execution', on_delete=models.SET_NULL, null=True)
|
2019-12-18 07:37:53 +00:00
|
|
|
task_display = models.CharField(max_length=128, blank=True, default='', verbose_name=_("Task display"))
|
|
|
|
hosts_amount = models.IntegerField(default=0, verbose_name=_("Host amount"))
|
2020-03-12 08:24:38 +00:00
|
|
|
adhoc = models.ForeignKey(AdHoc, related_name='execution', on_delete=models.SET_NULL, null=True)
|
2017-12-06 10:31:51 +00:00
|
|
|
date_start = models.DateTimeField(auto_now_add=True, verbose_name=_('Start time'))
|
|
|
|
date_finished = models.DateTimeField(blank=True, null=True, verbose_name=_('End time'))
|
|
|
|
timedelta = models.FloatField(default=0.0, verbose_name=_('Time'), null=True)
|
|
|
|
is_finished = models.BooleanField(default=False, verbose_name=_('Is finished'))
|
|
|
|
is_success = models.BooleanField(default=False, verbose_name=_('Is success'))
|
2019-12-18 07:37:53 +00:00
|
|
|
result = JsonDictTextField(blank=True, null=True, verbose_name=_('Adhoc raw result'))
|
|
|
|
summary = JsonDictTextField(blank=True, null=True, verbose_name=_('Adhoc result summary'))
|
2017-12-07 05:01:33 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def short_id(self):
|
|
|
|
return str(self.id).split('-')[-1]
|
|
|
|
|
2019-12-18 07:37:53 +00:00
|
|
|
@property
|
|
|
|
def adhoc_short_id(self):
|
|
|
|
return str(self.adhoc_id).split('-')[-1]
|
|
|
|
|
2018-03-30 14:03:43 +00:00
|
|
|
@property
|
|
|
|
def log_path(self):
|
|
|
|
dt = datetime.datetime.now().strftime('%Y-%m-%d')
|
|
|
|
log_dir = os.path.join(settings.PROJECT_DIR, 'data', 'ansible', dt)
|
|
|
|
if not os.path.exists(log_dir):
|
|
|
|
os.makedirs(log_dir)
|
|
|
|
return os.path.join(log_dir, str(self.id) + '.log')
|
|
|
|
|
2019-12-18 07:37:53 +00:00
|
|
|
def start_runner(self):
|
|
|
|
runner = AdHocRunner(self.adhoc.inventory, options=self.adhoc.options)
|
|
|
|
try:
|
|
|
|
result = runner.run(
|
|
|
|
self.adhoc.tasks,
|
|
|
|
self.adhoc.pattern,
|
|
|
|
self.task.name,
|
|
|
|
)
|
|
|
|
return result.results_raw, result.results_summary
|
|
|
|
except AnsibleError as e:
|
|
|
|
logger.warn("Failed run adhoc {}, {}".format(self.task.name, e))
|
|
|
|
return {}, {}
|
2017-12-10 16:29:25 +00:00
|
|
|
|
2019-12-18 07:37:53 +00:00
|
|
|
def start(self):
|
2020-03-12 08:24:38 +00:00
|
|
|
self.task.latest_execution = self
|
2019-12-18 07:37:53 +00:00
|
|
|
self.task.save()
|
|
|
|
time_start = time.time()
|
|
|
|
summary = {}
|
|
|
|
raw = ''
|
2017-12-10 16:29:25 +00:00
|
|
|
|
2019-11-14 03:46:42 +00:00
|
|
|
try:
|
2019-12-18 07:37:53 +00:00
|
|
|
date_start_s = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
|
|
|
print(_("{} Start task: {}").format(date_start_s, self.task.name))
|
|
|
|
raw, summary = self.start_runner()
|
|
|
|
except Exception as e:
|
|
|
|
logger.error(e, exc_info=True)
|
|
|
|
raw = {"dark": {"all": str(e)}, "contacted": []}
|
|
|
|
finally:
|
2020-03-12 08:24:38 +00:00
|
|
|
self.clean_up(summary, time_start)
|
2019-12-18 07:37:53 +00:00
|
|
|
date_end = timezone.now()
|
|
|
|
date_end_s = date_end.strftime('%Y-%m-%d %H:%M:%S')
|
|
|
|
print(_("{} Task finish").format(date_end_s))
|
|
|
|
print('.\n\n.')
|
|
|
|
return raw, summary
|
2017-12-10 16:29:25 +00:00
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
def clean_up(self, summary, time_start):
|
|
|
|
is_success = summary.get('success', False)
|
|
|
|
task = Task.objects.get(id=self.task_id)
|
|
|
|
task.total_run_amount = models.F('total_run_amount') + 1
|
|
|
|
if is_success:
|
|
|
|
task.success_run_amount = models.F('success_run_amount') + 1
|
|
|
|
task.save()
|
|
|
|
AdHocExecution.objects.filter(id=self.id).update(
|
|
|
|
is_finished=True,
|
|
|
|
is_success=is_success,
|
|
|
|
date_finished=timezone.now(),
|
|
|
|
timedelta=time.time() - time_start,
|
|
|
|
summary=summary
|
|
|
|
)
|
|
|
|
|
2017-12-20 03:30:15 +00:00
|
|
|
@property
|
|
|
|
def success_hosts(self):
|
|
|
|
return self.summary.get('contacted', [])
|
|
|
|
|
|
|
|
@property
|
|
|
|
def failed_hosts(self):
|
|
|
|
return self.summary.get('dark', {})
|
|
|
|
|
2017-12-07 05:01:33 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.short_id
|
2017-12-07 08:25:50 +00:00
|
|
|
|
|
|
|
class Meta:
|
2020-03-12 08:24:38 +00:00
|
|
|
db_table = "ops_adhoc_execution"
|
2017-12-15 07:50:15 +00:00
|
|
|
get_latest_by = 'date_start'
|