jumpserver/apps/ops/models/playbook.py

27 lines
1.0 KiB
Python
Raw Normal View History

2022-11-11 11:20:17 +00:00
import os.path
import uuid
from django.conf import settings
2022-09-29 12:44:45 +00:00
from django.db import models
from django.utils.translation import gettext_lazy as _
2022-12-02 04:21:56 +00:00
from ops.exception import PlaybookNoValidEntry
2022-12-15 09:25:21 +00:00
from orgs.mixins.models import JMSOrgBaseModel
2022-10-08 08:55:14 +00:00
2022-12-15 09:25:21 +00:00
class Playbook(JMSOrgBaseModel):
2022-11-11 11:20:17 +00:00
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
name = models.CharField(max_length=128, verbose_name=_('Name'), null=True)
path = models.FileField(upload_to='playbooks/')
creator = models.ForeignKey('users.User', verbose_name=_("Creator"), on_delete=models.SET_NULL, null=True)
2022-11-29 11:52:48 +00:00
comment = models.CharField(max_length=1024, default='', verbose_name=_('Comment'), null=True, blank=True)
2022-10-08 08:55:14 +00:00
2022-11-11 11:20:17 +00:00
@property
2022-12-02 04:21:56 +00:00
def entry(self):
work_dir = os.path.join(settings.DATA_DIR, "ops", "playbook", self.id.__str__())
valid_entry = ('main.yml', 'main.yaml', 'main')
for f in os.listdir(work_dir):
if f in valid_entry:
return os.path.join(work_dir, f)
raise PlaybookNoValidEntry