2017-03-09 06:55:33 +00:00
|
|
|
# ~*~ coding: utf-8 ~*~
|
2022-10-08 11:12:04 +00:00
|
|
|
import os.path
|
2022-11-11 11:20:17 +00:00
|
|
|
import uuid
|
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-11-11 11:20:17 +00:00
|
|
|
from common.db.models import BaseCreateUpdateModel
|
2022-10-08 08:55:14 +00:00
|
|
|
from common.utils import get_logger
|
2022-11-01 03:52:51 +00:00
|
|
|
from .base import BaseAnsibleJob, BaseAnsibleExecution
|
2022-10-08 08:55:14 +00:00
|
|
|
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-11-11 11:20:17 +00:00
|
|
|
class AdHoc(BaseCreateUpdateModel):
|
|
|
|
class Modules(models.TextChoices):
|
|
|
|
shell = 'shell', _('Shell')
|
|
|
|
winshell = 'win_shell', _('Powershell')
|
|
|
|
|
|
|
|
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
|
|
|
|
name = models.CharField(max_length=128, verbose_name=_('Name'))
|
2022-10-08 08:55:14 +00:00
|
|
|
pattern = models.CharField(max_length=1024, verbose_name=_("Pattern"), default='all')
|
2022-11-11 11:20:17 +00:00
|
|
|
module = models.CharField(max_length=128, choices=Modules.choices, default=Modules.shell,
|
|
|
|
verbose_name=_('Module'))
|
2022-10-08 08:55:14 +00:00
|
|
|
args = models.CharField(max_length=1024, default='', verbose_name=_('Args'))
|
2022-11-11 11:20:17 +00:00
|
|
|
owner = models.ForeignKey('users.User', verbose_name=_("Creator"), on_delete=models.SET_NULL, null=True)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def row_count(self):
|
|
|
|
if len(self.args) == 0:
|
|
|
|
return 0
|
|
|
|
count = str(self.args).count('\n')
|
|
|
|
return count + 1
|
2022-10-08 11:12:04 +00:00
|
|
|
|
2022-11-11 11:20:17 +00:00
|
|
|
@property
|
|
|
|
def size(self):
|
|
|
|
return len(self.args)
|
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")
|