jumpserver/apps/assets/models/platform.py

112 lines
4.9 KiB
Python
Raw Normal View History

2022-04-02 10:35:46 +00:00
from django.db import models
from django.utils.translation import gettext_lazy as _
2022-09-01 06:46:31 +00:00
from assets.const import AllTypes
2022-07-17 06:28:55 +00:00
from common.db.fields import JsonDictTextField
2022-04-02 10:35:46 +00:00
2022-09-23 10:59:19 +00:00
from assets.const import Protocol
2022-09-15 08:22:01 +00:00
__all__ = ['Platform', 'PlatformProtocol', 'PlatformAutomation']
2022-08-18 09:58:59 +00:00
class PlatformProtocol(models.Model):
2022-09-14 12:55:14 +00:00
SETTING_ATTRS = {
'console': True,
'security': 'any,tls,rdp',
'sftp_enabled': True,
'sftp_home': '/tmp'
}
2022-10-18 12:37:17 +00:00
default = models.BooleanField(default=False, verbose_name=_('Default'))
required = models.BooleanField(default=False, verbose_name=_('Required'))
2022-08-18 09:58:59 +00:00
name = models.CharField(max_length=32, verbose_name=_('Name'))
port = models.IntegerField(verbose_name=_('Port'))
setting = models.JSONField(verbose_name=_('Setting'), default=dict)
2022-09-22 07:24:32 +00:00
platform = models.ForeignKey('Platform', on_delete=models.CASCADE, related_name='protocols')
2022-04-02 10:35:46 +00:00
2022-09-23 10:59:19 +00:00
def __str__(self):
return '{}/{}'.format(self.name, self.port)
2022-10-18 12:37:17 +00:00
@property
def primary(self):
primary_protocol_name = AllTypes.get_primary_protocol_name(
self.platform.category, self.platform.type
)
return self.name == primary_protocol_name
2022-09-23 10:59:19 +00:00
@property
def secret_types(self):
return Protocol.settings().get(self.name, {}).get('secret_types')
2022-04-02 10:35:46 +00:00
2022-09-15 08:22:01 +00:00
class PlatformAutomation(models.Model):
ansible_enabled = models.BooleanField(default=False, verbose_name=_("Enabled"))
ansible_config = models.JSONField(default=dict, verbose_name=_("Ansible config"))
2022-09-15 08:22:01 +00:00
ping_enabled = models.BooleanField(default=False, verbose_name=_("Ping enabled"))
ping_method = models.CharField(max_length=32, blank=True, null=True, verbose_name=_("Ping method"))
gather_facts_enabled = models.BooleanField(default=False, verbose_name=_("Gather facts enabled"))
gather_facts_method = models.TextField(max_length=32, blank=True, null=True, verbose_name=_("Gather facts method"))
2022-10-19 03:39:11 +00:00
push_account_enabled = models.BooleanField(default=False, verbose_name=_("Push account enabled"))
push_account_method = models.TextField(max_length=32, blank=True, null=True, verbose_name=_("Push account method"))
2022-10-13 09:47:29 +00:00
change_secret_enabled = models.BooleanField(default=False, verbose_name=_("Change password enabled"))
2022-11-07 11:17:02 +00:00
change_secret_method = models.TextField(
max_length=32, blank=True, null=True, verbose_name=_("Change password method"))
2022-09-15 08:22:01 +00:00
verify_account_enabled = models.BooleanField(default=False, verbose_name=_("Verify account enabled"))
2022-11-07 11:17:02 +00:00
verify_account_method = models.TextField(
max_length=32, blank=True, null=True, verbose_name=_("Verify account method"))
2022-09-15 08:22:01 +00:00
gather_accounts_enabled = models.BooleanField(default=False, verbose_name=_("Gather facts enabled"))
2022-11-07 11:17:02 +00:00
gather_accounts_method = models.TextField(
max_length=32, blank=True, null=True, verbose_name=_("Gather facts method")
)
2022-09-15 08:22:01 +00:00
2022-04-02 10:35:46 +00:00
class Platform(models.Model):
2022-08-10 11:27:08 +00:00
"""
对资产提供 约束和默认值
对资产进行抽象
"""
2022-11-07 11:17:02 +00:00
class CharsetChoices(models.TextChoices):
utf8 = 'utf8', 'UTF-8'
gbk = 'gbk', 'GBK'
2022-04-02 10:35:46 +00:00
name = models.SlugField(verbose_name=_("Name"), unique=True, allow_unicode=True)
2022-08-24 08:14:32 +00:00
category = models.CharField(default='host', max_length=32, verbose_name=_("Category"))
type = models.CharField(max_length=32, default='linux', verbose_name=_("Type"))
2022-04-02 10:35:46 +00:00
meta = JsonDictTextField(blank=True, null=True, verbose_name=_("Meta"))
internal = models.BooleanField(default=False, verbose_name=_("Internal"))
comment = models.TextField(blank=True, null=True, verbose_name=_("Comment"))
2022-09-01 09:42:48 +00:00
# 资产有关的
2022-11-07 11:17:02 +00:00
charset = models.CharField(
default=CharsetChoices.utf8, choices=CharsetChoices.choices, max_length=8, verbose_name=_("Charset")
)
2022-09-09 03:00:09 +00:00
domain_enabled = models.BooleanField(default=True, verbose_name=_("Domain enabled"))
2022-04-30 15:19:43 +00:00
protocols_enabled = models.BooleanField(default=True, verbose_name=_("Protocols enabled"))
2022-09-01 09:42:48 +00:00
# 账号有关的
2022-08-15 10:31:57 +00:00
su_enabled = models.BooleanField(default=False, verbose_name=_("Su enabled"))
su_method = models.CharField(max_length=32, blank=True, null=True, verbose_name=_("SU method"))
2022-09-22 07:24:32 +00:00
automation = models.OneToOneField(PlatformAutomation, on_delete=models.CASCADE, related_name='platform',
blank=True, null=True, verbose_name=_("Automation"))
2022-04-02 10:35:46 +00:00
2022-05-05 08:18:05 +00:00
@property
2022-08-10 11:27:08 +00:00
def type_constraints(self):
return AllTypes.get_constraints(self.category, self.type)
2022-05-02 13:37:42 +00:00
2022-04-02 10:35:46 +00:00
@classmethod
def default(cls):
linux, created = cls.objects.get_or_create(
defaults={'name': 'Linux'}, name='Linux'
)
return linux.id
2022-10-18 12:37:17 +00:00
@property
def primary_protocol(self):
primary_protocol_name = AllTypes.get_primary_protocol_name(self.category, self.type)
return self.protocols.filter(name=primary_protocol_name).first()
2022-04-02 10:35:46 +00:00
def __str__(self):
return self.name
class Meta:
verbose_name = _("Platform")
# ordering = ('name',)