2017-03-09 06:55:33 +00:00
|
|
|
# ~*~ coding: utf-8 ~*~
|
2022-10-08 11:12:04 +00:00
|
|
|
import os.path
|
2018-03-30 14:03:43 +00:00
|
|
|
|
2017-03-09 06:55:33 +00:00
|
|
|
from django.db import models
|
2022-05-07 08:20:12 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2017-12-10 16:29:25 +00:00
|
|
|
|
2022-10-08 08:55:14 +00:00
|
|
|
from common.utils import get_logger
|
|
|
|
from .base import BaseAnsibleTask, BaseAnsibleExecution
|
|
|
|
from ..ansible import AdHocRunner
|
2017-03-09 06:55:33 +00:00
|
|
|
|
2022-10-08 08:55:14 +00:00
|
|
|
__all__ = ["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
|
|
|
|
|
|
|
|
2022-10-08 08:55:14 +00:00
|
|
|
class AdHoc(BaseAnsibleTask):
|
|
|
|
pattern = models.CharField(max_length=1024, verbose_name=_("Pattern"), default='all')
|
|
|
|
module = models.CharField(max_length=128, default='shell', verbose_name=_('Module'))
|
|
|
|
args = models.CharField(max_length=1024, default='', verbose_name=_('Args'))
|
2022-10-08 11:12:04 +00:00
|
|
|
last_execution = models.ForeignKey('AdHocExecution', verbose_name=_("Last execution"),
|
|
|
|
on_delete=models.SET_NULL, null=True, blank=True)
|
|
|
|
|
|
|
|
def get_register_task(self):
|
|
|
|
from ops.tasks import run_adhoc
|
|
|
|
return "run_adhoc_{}".format(self.id), run_adhoc, (str(self.id),), {}
|
2017-12-21 18:08:29 +00:00
|
|
|
|
2017-12-15 07:50:15 +00:00
|
|
|
def __str__(self):
|
2022-10-08 08:55:14 +00:00
|
|
|
return "{}: {}".format(self.module, self.args)
|
2017-12-10 16:29:25 +00:00
|
|
|
|
|
|
|
|
2022-10-08 08:55:14 +00:00
|
|
|
class AdHocExecution(BaseAnsibleExecution):
|
2017-12-10 16:29:25 +00:00
|
|
|
"""
|
|
|
|
AdHoc running history.
|
|
|
|
"""
|
2022-10-08 08:55:14 +00:00
|
|
|
task = models.ForeignKey('AdHoc', verbose_name=_("Adhoc"), related_name='executions', on_delete=models.CASCADE)
|
2017-12-10 16:29:25 +00:00
|
|
|
|
2022-10-08 08:55:14 +00:00
|
|
|
def get_runner(self):
|
2022-10-08 11:12:04 +00:00
|
|
|
inv = self.task.inventory
|
|
|
|
inv.write_to_file(self.inventory_path)
|
|
|
|
|
|
|
|
runner = AdHocRunner(
|
|
|
|
self.inventory_path, self.task.module, module_args=self.task.args,
|
2022-10-08 08:55:14 +00:00
|
|
|
pattern=self.task.pattern, project_dir=self.private_dir
|
2020-03-12 08:24:38 +00:00
|
|
|
)
|
2022-10-08 11:12:04 +00:00
|
|
|
return runner
|
2020-03-12 08:24:38 +00:00
|
|
|
|
2022-10-18 02:43:51 +00:00
|
|
|
def task_display(self):
|
|
|
|
return str(self.task)
|
|
|
|
|
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'
|
2022-02-17 12:13:31 +00:00
|
|
|
verbose_name = _("AdHoc execution")
|