2022-04-02 10:35:46 +00:00
|
|
|
from django.db import models
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
2024-04-22 03:27:48 +00:00
|
|
|
from assets.const import AllTypes, Category, Protocol
|
2022-12-07 09:24:30 +00:00
|
|
|
from common.db.fields import JsonDictTextField
|
2023-02-20 05:31:56 +00:00
|
|
|
from common.db.models import JMSBaseModel
|
2022-09-23 10:59:19 +00:00
|
|
|
|
2022-09-15 08:22:01 +00:00
|
|
|
__all__ = ['Platform', 'PlatformProtocol', 'PlatformAutomation']
|
2022-08-18 09:58:59 +00:00
|
|
|
|
2023-07-04 02:29:27 +00:00
|
|
|
from common.utils import lazyproperty
|
2023-12-05 03:16:34 +00:00
|
|
|
from labels.mixins import LabeledMixin
|
2023-07-04 02:29:27 +00:00
|
|
|
|
2022-08-18 09:58:59 +00:00
|
|
|
|
|
|
|
class PlatformProtocol(models.Model):
|
|
|
|
name = models.CharField(max_length=32, verbose_name=_('Name'))
|
|
|
|
port = models.IntegerField(verbose_name=_('Port'))
|
2023-03-22 06:15:25 +00:00
|
|
|
primary = models.BooleanField(default=False, verbose_name=_('Primary'))
|
|
|
|
required = models.BooleanField(default=False, verbose_name=_('Required'))
|
|
|
|
default = models.BooleanField(default=False, verbose_name=_('Default'))
|
2023-04-03 10:18:31 +00:00
|
|
|
public = models.BooleanField(default=True, verbose_name=_('Public'))
|
2022-08-18 09:58:59 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def secret_types(self):
|
2023-04-10 02:57:44 +00:00
|
|
|
return Protocol.settings().get(self.name, {}).get('secret_types', ['password'])
|
2022-09-23 10:59:19 +00:00
|
|
|
|
2023-07-04 02:29:27 +00:00
|
|
|
@lazyproperty
|
|
|
|
def port_from_addr(self):
|
|
|
|
from assets.const.protocol import Protocol as ProtocolConst
|
|
|
|
return ProtocolConst.settings().get(self.name, {}).get('port_from_addr', False)
|
|
|
|
|
2022-04-02 10:35:46 +00:00
|
|
|
|
2022-09-15 08:22:01 +00:00
|
|
|
class PlatformAutomation(models.Model):
|
2022-09-26 10:03:48 +00:00
|
|
|
ansible_enabled = models.BooleanField(default=False, verbose_name=_("Enabled"))
|
|
|
|
ansible_config = models.JSONField(default=dict, verbose_name=_("Ansible config"))
|
2023-04-13 11:02:04 +00:00
|
|
|
|
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"))
|
2023-04-13 11:02:04 +00:00
|
|
|
ping_params = models.JSONField(default=dict, verbose_name=_("Ping params"))
|
|
|
|
|
2022-09-15 08:22:01 +00:00
|
|
|
gather_facts_enabled = models.BooleanField(default=False, verbose_name=_("Gather facts enabled"))
|
2023-04-13 11:02:04 +00:00
|
|
|
gather_facts_method = models.TextField(
|
|
|
|
max_length=32, blank=True, null=True, verbose_name=_("Gather facts method")
|
|
|
|
)
|
|
|
|
gather_facts_params = models.JSONField(default=dict, verbose_name=_("Gather facts params"))
|
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
change_secret_enabled = models.BooleanField(default=False, verbose_name=_("Change secret enabled"))
|
2022-11-07 11:17:02 +00:00
|
|
|
change_secret_method = models.TextField(
|
2023-01-16 11:02:09 +00:00
|
|
|
max_length=32, blank=True, null=True, verbose_name=_("Change secret method")
|
|
|
|
)
|
2023-04-13 11:02:04 +00:00
|
|
|
change_secret_params = models.JSONField(default=dict, verbose_name=_("Change secret params"))
|
|
|
|
|
2023-01-16 11:02:09 +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")
|
|
|
|
)
|
2023-04-13 11:02:04 +00:00
|
|
|
push_account_params = models.JSONField(default=dict, verbose_name=_("Push account params"))
|
|
|
|
|
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(
|
2023-04-13 11:02:04 +00:00
|
|
|
max_length=32, blank=True, null=True, verbose_name=_("Verify account method")
|
|
|
|
)
|
|
|
|
verify_account_params = models.JSONField(default=dict, verbose_name=_("Verify account params"))
|
|
|
|
|
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")
|
|
|
|
)
|
2023-04-13 11:02:04 +00:00
|
|
|
gather_accounts_params = models.JSONField(default=dict, verbose_name=_("Gather facts params"))
|
2023-12-06 10:48:35 +00:00
|
|
|
|
|
|
|
remove_account_enabled = models.BooleanField(default=False, verbose_name=_("Remove account enabled"))
|
|
|
|
remove_account_method = models.TextField(
|
|
|
|
max_length=32, blank=True, null=True, verbose_name=_("Remove account method")
|
|
|
|
)
|
|
|
|
remove_account_params = models.JSONField(default=dict, verbose_name=_("Remove account params"))
|
2023-04-17 07:24:10 +00:00
|
|
|
platform = models.OneToOneField('Platform', on_delete=models.CASCADE, related_name='automation', null=True)
|
2022-09-15 08:22:01 +00:00
|
|
|
|
|
|
|
|
2023-12-05 03:16:34 +00:00
|
|
|
class Platform(LabeledMixin, JMSBaseModel):
|
2022-08-10 11:27:08 +00:00
|
|
|
"""
|
|
|
|
对资产提供 约束和默认值
|
|
|
|
对资产进行抽象
|
|
|
|
"""
|
2022-11-07 11:17:02 +00:00
|
|
|
|
|
|
|
class CharsetChoices(models.TextChoices):
|
2023-02-06 07:05:38 +00:00
|
|
|
utf8 = 'utf-8', 'UTF-8'
|
2022-11-07 11:17:02 +00:00
|
|
|
gbk = 'gbk', 'GBK'
|
|
|
|
|
2023-02-20 05:31:56 +00:00
|
|
|
id = models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')
|
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"))
|
2022-09-01 09:42:48 +00:00
|
|
|
# 资产有关的
|
2022-11-07 11:17:02 +00:00
|
|
|
charset = models.CharField(
|
2023-04-10 02:57:44 +00:00
|
|
|
default=CharsetChoices.utf8, choices=CharsetChoices.choices,
|
|
|
|
max_length=8, verbose_name=_("Charset")
|
2022-11-07 11:17:02 +00:00
|
|
|
)
|
2024-05-13 07:28:11 +00:00
|
|
|
domain_enabled = models.BooleanField(default=True, verbose_name=_("Gateway 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"))
|
2022-12-07 09:24:30 +00:00
|
|
|
su_method = models.CharField(max_length=32, blank=True, null=True, verbose_name=_("Su method"))
|
2023-04-10 02:57:44 +00:00
|
|
|
custom_fields = models.JSONField(null=True, default=list, verbose_name=_("Custom fields"))
|
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
|
|
|
|
|
2024-04-22 03:27:48 +00:00
|
|
|
def is_huawei(self):
|
|
|
|
if self.category != Category.DEVICE:
|
|
|
|
return False
|
|
|
|
if 'huawei' in self.name.lower():
|
|
|
|
return True
|
|
|
|
if '华为' in self.name:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2022-04-02 10:35:46 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name = _("Platform")
|
|
|
|
# ordering = ('name',)
|