jumpserver/apps/assets/models/platform.py

137 lines
5.7 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-04-20 02:15:20 +00:00
2022-08-18 09:58:59 +00:00
__all__ = ['Platform', 'PlatformProtocol']
class PlatformProtocol(models.Model):
name = models.CharField(max_length=32, verbose_name=_('Name'))
port = models.IntegerField(verbose_name=_('Port'))
setting = models.JSONField(verbose_name=_('Setting'), default=dict)
2022-04-02 10:35:46 +00:00
class Platform(models.Model):
2022-08-10 11:27:08 +00:00
"""
对资产提供 约束和默认值
对资产进行抽象
"""
2022-04-02 10:35:46 +00:00
CHARSET_CHOICES = (
('utf8', 'UTF-8'),
('gbk', 'GBK'),
)
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
# 资产有关的
charset = models.CharField(default='utf8', choices=CHARSET_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-08-18 09:58:59 +00:00
protocols = models.ManyToManyField(PlatformProtocol, blank=True, verbose_name=_("Protocols"))
2022-09-01 09:42:48 +00:00
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-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-01 09:42:48 +00:00
create_account_enabled = models.BooleanField(default=False, verbose_name=_("Create account enabled"))
create_account_method = models.TextField(max_length=32, blank=True, null=True, verbose_name=_("Create account method"))
2022-08-10 11:27:08 +00:00
change_password_enabled = models.BooleanField(default=False, verbose_name=_("Change password enabled"))
change_password_method = models.TextField(max_length=32, blank=True, null=True, verbose_name=_("Change password method"))
2022-09-01 09:42:48 +00:00
verify_account_enabled = models.BooleanField(default=False, verbose_name=_("Verify account enabled"))
verify_account_method = models.TextField(max_length=32, blank=True, null=True, verbose_name=_("Verify account method"))
gather_accounts_enabled = models.BooleanField(default=False, verbose_name=_("Gather facts enabled"))
gather_accounts_method = models.TextField(max_length=32, blank=True, null=True, verbose_name=_("Gather facts method"))
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
@staticmethod
def set_default_platforms_ops(platform_model):
default_ok = {
'su_enabled': True,
'su_method': 'sudo',
'domain_enabled': True,
'change_password_enabled': True,
'change_password_method': 'change_password_linux',
'verify_account_enabled': True,
'verify_account_method': 'ansible_posix_ping',
}
db_default = {
'su_enabled': False,
'domain_enabled': True,
'change_password_enabled': True,
'verify_account_enabled': True,
}
platform_ops_map = {
('host', 'linux'): {
**default_ok,
'change_password_method': 'change_password_linux',
'verify_account_method': 'ansible_posix_ping'
},
('host', 'windows'): {
**default_ok,
'su_enabled': False,
'change_password_method': 'change_password_windows',
'verify_account_method': 'ansible_win_ping'
},
('host', 'unix'): {
**default_ok,
'verify_account_method': 'ansible_posix_ping',
'change_password_method': 'change_password_aix'
},
('database', 'mysql'): {
**db_default,
'verify_account_method': 'mysql_ping',
'change_password_method': 'change_password_mysql'
},
('database', 'postgresql'): {
**db_default,
'verify_account_method': 'postgresql_ping',
'change_password_method': 'change_password_postgresql'
},
('database', 'oracle'): {
**db_default,
'verify_account_method': 'oracle_ping',
'change_password_method': 'change_password_oracle'
},
('database', 'sqlserver'): {
**db_default,
'verify_account_method': 'mysql_ping',
'change_password_method': 'change_password_sqlserver'
},
}
platforms = platform_model.objects.all()
updated = []
for p in platforms:
attrs = platform_ops_map.get((p.category, p.type), {})
if not attrs:
continue
for k, v in attrs.items():
setattr(p, k, v)
updated.append(p)
platform_model.objects.bulk_update(updated, list(default_ok.keys()))
2022-04-02 10:35:46 +00:00
def __str__(self):
return self.name
class Meta:
verbose_name = _("Platform")
# ordering = ('name',)