Example #1
0
class CloudProviderTemplate(models.Model):
    id = models.UUIDField(default=uuid.uuid4, primary_key=True)
    name = models.CharField(max_length=20, unique=True, verbose_name=_('Name'))
    meta = common_models.JsonTextField(blank=True,
                                       null=True,
                                       verbose_name=_('Meta'))
    date_created = models.DateTimeField(auto_now_add=True,
                                        verbose_name=_('Date created'))
    template_dir = os.path.join(settings.BASE_DIR, 'resource', 'clouds')

    @property
    def path(self):
        return os.path.join(self.template_dir, self.name)

    @classmethod
    def lookup(cls):
        for d in os.listdir(cls.template_dir):
            full_path = os.path.join(cls.template_dir, d)
            meta_path = os.path.join(full_path, 'meta.yml')
            if not os.path.isdir(full_path) or not os.path.isfile(meta_path):
                continue
            with open(meta_path) as f:
                metadata = yaml.load(f)
            defaults = {'name': d, 'meta': metadata}
            cls.objects.update_or_create(defaults=defaults, name=d)
Example #2
0
class AdHoc(AbstractProjectResourceModel):
    pattern = models.CharField(max_length=1024, default='all', verbose_name=_('Pattern'))
    module = models.CharField(max_length=128, default='command', verbose_name=_("Module"))
    args = common_models.JsonTextField(verbose_name=_("Args"))

    execute_times = models.IntegerField(default=0)
    created_by = models.CharField(max_length=128, blank=True, null=True, default='')

    COMMAND_MODULES = ('shell', 'command', 'raw')

    def __str__(self):
        return "{}: {}".format(self.module, self.clean_args)

    def execute(self):
        pk = None
        if current_task:
            pk = current_task.request.id
        execution = AdHocExecution(adhoc=self, pk=pk)
        execution.save()
        result = execution.start()
        return result

    @property
    def clean_args(self):
        if self.module in self.COMMAND_MODULES and isinstance(self.args, str):
            if self.args.startswith('executable='):
                _args = self.args.split(' ')
                executable, command = _args[0].split('=')[1], ' '.join(_args[1:])
                args = {'executable': executable, '_raw_params':  command}
            else:
                args = {'_raw_params':  self.args}
            return args
        else:
            return self.args

    @property
    def tasks(self):
        return [{
            "name": self.__str__(),
            "action": {
                "module": self.module,
                "args": self.clean_args,
            },
        }]

    @property
    def inventory(self):
        return self.project.inventory_obj

    @staticmethod
    def test_tasks():
        return [
            {
                "name": "Test ping",
                "actions": [{
                    "module": "ping",
                    "args": ""
                }],
            },
        ]
Example #3
0
class AdHoc(AbstractProjectResourceModel):
    pattern = models.CharField(max_length=1024, default='all', verbose_name=_('Pattern'))
    module = models.CharField(max_length=128, default='command', verbose_name=_("Module"))
    args = common_models.JsonTextField(verbose_name=_("Args"))

    execute_times = models.IntegerField(default=0)
    created_by = models.CharField(max_length=128, blank=True, null=True, default='')

    def __str__(self):
        return "{}: {}".format(self.module, self.args)

    def execute(self):
        pk = None
        if current_task:
            pk = current_task.request.id
        execution = AdHocExecution(adhoc=self, pk=pk)
        execution.save()
        result = execution.start()
        return result

    @property
    def tasks(self):
        return [{
            "name": self.__str__(),
            "action": {
                "module": self.module,
                "args": self.args,
            },
        }]

    @property
    def inventory(self):
        return self.project.inventory_obj

    @staticmethod
    def test_tasks():
        return [
            {
                "name": "Test ping",
                "actions": [{
                    "module": "ping",
                    "args": ""
                }],
            },
        ]
Example #4
0
class CeleryTask(models.Model):
    STATE_PENDING = 'PENDING'
    STATE_STARTED = 'STARTED'
    STATE_FAILURE = 'FAILURE'
    STATE_SUCCESS = 'SUCCESS'
    STATE_RETRY = 'RETRY'

    LOG_DIR = os.path.join(settings.BASE_DIR, 'data', 'celery')
    STATUS_CHOICES = (
        (STATE_PENDING, _('Pending')),
        (STATE_STARTED, _('Started')),
        (STATE_SUCCESS, _('Success')),
        (STATE_FAILURE, _('Failure')),
        (STATE_RETRY, _('Retry')),
    )
    id = models.UUIDField(primary_key=True, default=uuid.uuid4)
    root_id = models.UUIDField()
    # root_id = models.UUIDField()
    # parent_id = models.UUIDField(null=True)
    name = models.CharField(max_length=256)
    state = models.CharField(max_length=16,
                             choices=STATUS_CHOICES,
                             default=STATE_PENDING)
    result = common_models.JsonTextField(null=True)
    date_start = models.DateTimeField(auto_now_add=True)
    date_finished = models.DateTimeField(null=True)

    def __str__(self):
        return "{}: {}".format(self.name, self.id)

    @property
    def log_path(self):
        dt = self.date_start.strftime('%Y-%m-%d')
        log_dir = os.path.join(self.LOG_DIR, dt)
        if not os.path.isdir(log_dir):
            os.mkdir(log_dir)
        return os.path.join(log_dir, '{}.log'.format(self.root_id))

    @property
    def is_finished(self):
        return self.state in (self.STATE_SUCCESS, self.STATE_FAILURE)