2016-12-20 16:43:52 +00:00
|
|
|
|
#!/usr/bin/env python
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
#
|
|
|
|
|
|
2017-11-23 06:08:01 +00:00
|
|
|
|
import uuid
|
2017-12-11 09:08:43 +00:00
|
|
|
|
import logging
|
2018-05-31 11:47:57 +00:00
|
|
|
|
from functools import reduce
|
2019-08-23 10:23:07 +00:00
|
|
|
|
from collections import OrderedDict
|
2017-11-23 06:08:01 +00:00
|
|
|
|
|
2016-12-20 16:43:52 +00:00
|
|
|
|
from django.db import models
|
2021-07-08 06:23:18 +00:00
|
|
|
|
from common.db.models import TextChoices
|
2016-12-20 16:43:52 +00:00
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2021-07-08 06:23:18 +00:00
|
|
|
|
from rest_framework.exceptions import ValidationError
|
2016-12-20 16:43:52 +00:00
|
|
|
|
|
2019-12-16 08:53:29 +00:00
|
|
|
|
from common.fields.model import JsonDictTextField
|
|
|
|
|
from common.utils import lazyproperty
|
2019-08-21 12:27:21 +00:00
|
|
|
|
from orgs.mixins.models import OrgModelMixin, OrgManager
|
2021-07-08 06:23:18 +00:00
|
|
|
|
|
|
|
|
|
from .base import AbsConnectivity
|
2016-12-20 16:43:52 +00:00
|
|
|
|
|
2021-02-05 05:29:29 +00:00
|
|
|
|
__all__ = ['Asset', 'ProtocolsMixin', 'Platform', 'AssetQuerySet']
|
2016-12-20 16:43:52 +00:00
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
2018-01-09 15:33:14 +00:00
|
|
|
|
def default_cluster():
|
|
|
|
|
from .cluster import Cluster
|
|
|
|
|
name = "Default"
|
|
|
|
|
defaults = {"name": name}
|
|
|
|
|
cluster, created = Cluster.objects.get_or_create(
|
|
|
|
|
defaults=defaults, name=name
|
|
|
|
|
)
|
2018-01-10 04:03:35 +00:00
|
|
|
|
return cluster.id
|
2018-01-09 15:33:14 +00:00
|
|
|
|
|
|
|
|
|
|
2018-02-08 03:34:21 +00:00
|
|
|
|
def default_node():
|
2018-02-09 03:12:40 +00:00
|
|
|
|
try:
|
2018-02-09 07:24:44 +00:00
|
|
|
|
from .node import Node
|
2019-08-23 10:23:07 +00:00
|
|
|
|
root = Node.org_root()
|
2021-04-28 07:26:40 +00:00
|
|
|
|
return Node.objects.filter(id=root.id)
|
2018-02-09 03:12:40 +00:00
|
|
|
|
except:
|
|
|
|
|
return None
|
2018-02-08 03:34:21 +00:00
|
|
|
|
|
|
|
|
|
|
2019-12-16 08:53:29 +00:00
|
|
|
|
class AssetManager(OrgManager):
|
2020-09-27 08:02:44 +00:00
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
2018-07-13 07:05:46 +00:00
|
|
|
|
class AssetQuerySet(models.QuerySet):
|
2018-04-27 03:41:47 +00:00
|
|
|
|
def active(self):
|
|
|
|
|
return self.filter(is_active=True)
|
|
|
|
|
|
|
|
|
|
def valid(self):
|
|
|
|
|
return self.active()
|
|
|
|
|
|
2019-07-05 10:07:10 +00:00
|
|
|
|
def has_protocol(self, name):
|
|
|
|
|
return self.filter(protocols__contains=name)
|
|
|
|
|
|
2018-04-27 03:41:47 +00:00
|
|
|
|
|
2019-07-05 10:07:10 +00:00
|
|
|
|
class ProtocolsMixin:
|
|
|
|
|
protocols = ''
|
2021-07-08 06:23:18 +00:00
|
|
|
|
|
|
|
|
|
class Protocol(TextChoices):
|
|
|
|
|
ssh = 'ssh', 'SSH'
|
|
|
|
|
rdp = 'rdp', 'RDP'
|
|
|
|
|
telnet = 'telnet', 'Telnet'
|
|
|
|
|
vnc = 'vnc', 'VNC'
|
2019-06-13 10:58:43 +00:00
|
|
|
|
|
2019-07-05 10:07:10 +00:00
|
|
|
|
@property
|
|
|
|
|
def protocols_as_list(self):
|
|
|
|
|
if not self.protocols:
|
|
|
|
|
return []
|
|
|
|
|
return self.protocols.split(' ')
|
2019-06-13 10:58:43 +00:00
|
|
|
|
|
2019-07-05 10:07:10 +00:00
|
|
|
|
@property
|
|
|
|
|
def protocols_as_dict(self):
|
|
|
|
|
d = OrderedDict()
|
|
|
|
|
protocols = self.protocols_as_list
|
|
|
|
|
for i in protocols:
|
|
|
|
|
if '/' not in i:
|
|
|
|
|
continue
|
|
|
|
|
name, port = i.split('/')[:2]
|
|
|
|
|
if not all([name, port]):
|
|
|
|
|
continue
|
|
|
|
|
d[name] = int(port)
|
|
|
|
|
return d
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def protocols_as_json(self):
|
|
|
|
|
return [
|
|
|
|
|
{"name": name, "port": port}
|
|
|
|
|
for name, port in self.protocols_as_dict.items()
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
def has_protocol(self, name):
|
|
|
|
|
return name in self.protocols_as_dict
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def ssh_port(self):
|
|
|
|
|
return self.protocols_as_dict.get("ssh", 22)
|
2019-06-13 10:58:43 +00:00
|
|
|
|
|
|
|
|
|
|
2019-07-11 10:12:14 +00:00
|
|
|
|
class NodesRelationMixin:
|
|
|
|
|
NODES_CACHE_KEY = 'ASSET_NODES_{}'
|
|
|
|
|
ALL_ASSET_NODES_CACHE_KEY = 'ALL_ASSETS_NODES'
|
|
|
|
|
CACHE_TIME = 3600 * 24 * 7
|
|
|
|
|
id = ""
|
|
|
|
|
_all_nodes_keys = None
|
|
|
|
|
|
|
|
|
|
def get_nodes(self):
|
|
|
|
|
from .node import Node
|
2019-08-23 10:23:07 +00:00
|
|
|
|
nodes = self.nodes.all()
|
|
|
|
|
if not nodes:
|
|
|
|
|
nodes = Node.objects.filter(id=Node.org_root().id)
|
2019-07-11 10:12:14 +00:00
|
|
|
|
return nodes
|
|
|
|
|
|
|
|
|
|
def get_all_nodes(self, flat=False):
|
|
|
|
|
nodes = []
|
|
|
|
|
for node in self.get_nodes():
|
2019-09-24 07:00:32 +00:00
|
|
|
|
_nodes = node.get_ancestors(with_self=True)
|
2019-07-11 10:12:14 +00:00
|
|
|
|
nodes.append(_nodes)
|
|
|
|
|
if flat:
|
|
|
|
|
nodes = list(reduce(lambda x, y: set(x) | set(y), nodes))
|
|
|
|
|
return nodes
|
|
|
|
|
|
|
|
|
|
|
2019-12-16 08:53:29 +00:00
|
|
|
|
class Platform(models.Model):
|
|
|
|
|
CHARSET_CHOICES = (
|
|
|
|
|
('utf8', 'UTF-8'),
|
|
|
|
|
('gbk', 'GBK'),
|
|
|
|
|
)
|
|
|
|
|
BASE_CHOICES = (
|
|
|
|
|
('Linux', 'Linux'),
|
|
|
|
|
('Unix', 'Unix'),
|
|
|
|
|
('MacOS', 'MacOS'),
|
|
|
|
|
('BSD', 'BSD'),
|
|
|
|
|
('Windows', 'Windows'),
|
|
|
|
|
('Other', 'Other'),
|
|
|
|
|
)
|
|
|
|
|
name = models.SlugField(verbose_name=_("Name"), unique=True, allow_unicode=True)
|
|
|
|
|
base = models.CharField(choices=BASE_CHOICES, max_length=16, default='Linux', verbose_name=_("Base"))
|
|
|
|
|
charset = models.CharField(default='utf8', choices=CHARSET_CHOICES, max_length=8, verbose_name=_("Charset"))
|
|
|
|
|
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"))
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def default(cls):
|
|
|
|
|
linux, created = cls.objects.get_or_create(
|
|
|
|
|
defaults={'name': 'Linux'}, name='Linux'
|
|
|
|
|
)
|
|
|
|
|
return linux.id
|
|
|
|
|
|
2019-12-21 11:44:25 +00:00
|
|
|
|
def is_windows(self):
|
|
|
|
|
return self.base.lower() in ('windows',)
|
|
|
|
|
|
|
|
|
|
def is_unixlike(self):
|
|
|
|
|
return self.base.lower() in ("linux", "unix", "macos", "bsd")
|
|
|
|
|
|
2019-12-16 08:53:29 +00:00
|
|
|
|
def __str__(self):
|
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
verbose_name = _("Platform")
|
|
|
|
|
# ordering = ('name',)
|
|
|
|
|
|
|
|
|
|
|
2021-11-11 03:56:30 +00:00
|
|
|
|
class AbsHardwareInfo(models.Model):
|
|
|
|
|
# Collect
|
|
|
|
|
vendor = models.CharField(max_length=64, null=True, blank=True, verbose_name=_('Vendor'))
|
|
|
|
|
model = models.CharField(max_length=54, null=True, blank=True, verbose_name=_('Model'))
|
|
|
|
|
sn = models.CharField(max_length=128, null=True, blank=True, verbose_name=_('Serial number'))
|
|
|
|
|
|
|
|
|
|
cpu_model = models.CharField(max_length=64, null=True, blank=True, verbose_name=_('CPU model'))
|
|
|
|
|
cpu_count = models.IntegerField(null=True, verbose_name=_('CPU count'))
|
|
|
|
|
cpu_cores = models.IntegerField(null=True, verbose_name=_('CPU cores'))
|
|
|
|
|
cpu_vcpus = models.IntegerField(null=True, verbose_name=_('CPU vcpus'))
|
|
|
|
|
memory = models.CharField(max_length=64, null=True, blank=True, verbose_name=_('Memory'))
|
|
|
|
|
disk_total = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('Disk total'))
|
|
|
|
|
disk_info = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('Disk info'))
|
|
|
|
|
|
|
|
|
|
os = models.CharField(max_length=128, null=True, blank=True, verbose_name=_('OS'))
|
|
|
|
|
os_version = models.CharField(max_length=16, null=True, blank=True, verbose_name=_('OS version'))
|
|
|
|
|
os_arch = models.CharField(max_length=16, blank=True, null=True, verbose_name=_('OS arch'))
|
|
|
|
|
hostname_raw = models.CharField(max_length=128, blank=True, null=True, verbose_name=_('Hostname raw'))
|
2018-06-29 06:59:43 +00:00
|
|
|
|
|
2021-11-11 03:56:30 +00:00
|
|
|
|
class Meta:
|
|
|
|
|
abstract = True
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def cpu_info(self):
|
|
|
|
|
info = ""
|
|
|
|
|
if self.cpu_model:
|
|
|
|
|
info += self.cpu_model
|
|
|
|
|
if self.cpu_count and self.cpu_cores:
|
|
|
|
|
info += "{}*{}".format(self.cpu_count, self.cpu_cores)
|
|
|
|
|
return info
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def hardware_info(self):
|
|
|
|
|
if self.cpu_count:
|
|
|
|
|
return '{} Core {} {}'.format(
|
|
|
|
|
self.cpu_vcpus or self.cpu_count * self.cpu_cores,
|
|
|
|
|
self.memory, self.disk_total
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
return ''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Asset(AbsConnectivity, AbsHardwareInfo, ProtocolsMixin, NodesRelationMixin, OrgModelMixin):
|
2017-11-23 06:08:01 +00:00
|
|
|
|
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
|
2019-05-21 09:07:47 +00:00
|
|
|
|
ip = models.CharField(max_length=128, verbose_name=_('IP'), db_index=True)
|
2018-07-15 10:39:11 +00:00
|
|
|
|
hostname = models.CharField(max_length=128, verbose_name=_('Hostname'))
|
2021-07-08 06:23:18 +00:00
|
|
|
|
protocol = models.CharField(max_length=128, default=ProtocolsMixin.Protocol.ssh,
|
2021-11-11 03:56:30 +00:00
|
|
|
|
choices=ProtocolsMixin.Protocol.choices, verbose_name=_('Protocol'))
|
2016-12-20 16:43:52 +00:00
|
|
|
|
port = models.IntegerField(default=22, verbose_name=_('Port'))
|
2019-07-05 10:28:56 +00:00
|
|
|
|
protocols = models.CharField(max_length=128, default='ssh/22', blank=True, verbose_name=_("Protocols"))
|
2019-12-16 08:53:29 +00:00
|
|
|
|
platform = models.ForeignKey(Platform, default=Platform.default, on_delete=models.PROTECT, verbose_name=_("Platform"), related_name='assets')
|
2018-12-18 09:28:45 +00:00
|
|
|
|
domain = models.ForeignKey("assets.Domain", null=True, blank=True, related_name='assets', verbose_name=_("Domain"), on_delete=models.SET_NULL)
|
|
|
|
|
nodes = models.ManyToManyField('assets.Node', default=default_node, related_name='assets', verbose_name=_("Nodes"))
|
2017-03-15 16:19:47 +00:00
|
|
|
|
is_active = models.BooleanField(default=True, verbose_name=_('Is active'))
|
|
|
|
|
|
2017-12-11 09:08:43 +00:00
|
|
|
|
# Auth
|
2021-07-15 09:51:22 +00:00
|
|
|
|
admin_user = models.ForeignKey('assets.SystemUser', on_delete=models.SET_NULL, null=True, verbose_name=_("Admin user"), related_name='admin_assets')
|
2017-12-11 09:08:43 +00:00
|
|
|
|
|
2017-03-15 16:19:47 +00:00
|
|
|
|
# Some information
|
2019-06-13 10:58:43 +00:00
|
|
|
|
public_ip = models.CharField(max_length=128, blank=True, null=True, verbose_name=_('Public IP'))
|
2018-07-15 10:39:11 +00:00
|
|
|
|
number = models.CharField(max_length=32, null=True, blank=True, verbose_name=_('Asset number'))
|
2017-03-15 16:19:47 +00:00
|
|
|
|
|
2018-12-18 09:28:45 +00:00
|
|
|
|
labels = models.ManyToManyField('assets.Label', blank=True, related_name='assets', verbose_name=_("Labels"))
|
2020-07-13 04:00:44 +00:00
|
|
|
|
created_by = models.CharField(max_length=128, null=True, blank=True, verbose_name=_('Created by'))
|
2018-12-18 09:28:45 +00:00
|
|
|
|
date_created = models.DateTimeField(auto_now_add=True, null=True, blank=True, verbose_name=_('Date created'))
|
2020-10-19 03:37:27 +00:00
|
|
|
|
comment = models.TextField(default='', blank=True, verbose_name=_('Comment'))
|
2016-12-20 16:43:52 +00:00
|
|
|
|
|
2019-12-16 08:53:29 +00:00
|
|
|
|
objects = AssetManager.from_queryset(AssetQuerySet)()
|
2018-04-27 03:41:47 +00:00
|
|
|
|
|
2017-11-01 15:23:11 +00:00
|
|
|
|
def __str__(self):
|
2018-04-11 07:24:12 +00:00
|
|
|
|
return '{0.hostname}({0.ip})'.format(self)
|
2016-12-20 17:03:52 +00:00
|
|
|
|
|
2021-07-15 09:51:22 +00:00
|
|
|
|
def set_admin_user_relation(self):
|
|
|
|
|
from .authbook import AuthBook
|
|
|
|
|
if not self.admin_user:
|
2021-07-08 06:23:18 +00:00
|
|
|
|
return
|
2021-07-15 09:51:22 +00:00
|
|
|
|
if self.admin_user.type != 'admin':
|
2021-07-08 06:23:18 +00:00
|
|
|
|
raise ValidationError('System user should be type admin')
|
2021-07-15 09:51:22 +00:00
|
|
|
|
|
|
|
|
|
defaults = {'asset': self, 'systemuser': self.admin_user, 'org_id': self.org_id}
|
|
|
|
|
AuthBook.objects.get_or_create(defaults=defaults, asset=self, systemuser=self.admin_user)
|
2021-07-08 06:23:18 +00:00
|
|
|
|
|
2021-07-12 07:55:29 +00:00
|
|
|
|
@property
|
|
|
|
|
def admin_user_display(self):
|
|
|
|
|
if not self.admin_user:
|
|
|
|
|
return ''
|
|
|
|
|
return str(self.admin_user)
|
|
|
|
|
|
2016-12-20 16:43:52 +00:00
|
|
|
|
@property
|
|
|
|
|
def is_valid(self):
|
|
|
|
|
warning = ''
|
|
|
|
|
if not self.is_active:
|
|
|
|
|
warning += ' inactive'
|
2019-07-05 10:07:10 +00:00
|
|
|
|
if warning:
|
|
|
|
|
return False, warning
|
|
|
|
|
return True, warning
|
2019-06-13 10:58:43 +00:00
|
|
|
|
|
2019-12-21 11:44:25 +00:00
|
|
|
|
@lazyproperty
|
|
|
|
|
def platform_base(self):
|
|
|
|
|
return self.platform.base
|
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
|
@lazyproperty
|
|
|
|
|
def admin_user_username(self):
|
|
|
|
|
"""求可连接性时,直接用用户名去取,避免再查一次admin user
|
|
|
|
|
serializer 中直接通过annotate方式返回了这个
|
|
|
|
|
"""
|
|
|
|
|
return self.admin_user.username
|
|
|
|
|
|
2019-06-13 10:58:43 +00:00
|
|
|
|
def is_windows(self):
|
2019-12-21 11:44:25 +00:00
|
|
|
|
return self.platform.is_windows()
|
2018-12-10 02:11:54 +00:00
|
|
|
|
|
2018-02-27 11:39:27 +00:00
|
|
|
|
def is_unixlike(self):
|
2019-12-21 11:44:25 +00:00
|
|
|
|
return self.platform.is_unixlike()
|
2018-02-27 11:39:27 +00:00
|
|
|
|
|
2019-06-13 10:58:43 +00:00
|
|
|
|
def is_support_ansible(self):
|
2019-12-16 08:53:29 +00:00
|
|
|
|
return self.has_protocol('ssh') and self.platform_base not in ("Other",)
|
|
|
|
|
|
2018-03-28 11:37:29 +00:00
|
|
|
|
def get_auth_info(self):
|
2019-06-13 10:58:43 +00:00
|
|
|
|
if not self.admin_user:
|
|
|
|
|
return {}
|
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
|
self.admin_user.load_asset_special_auth(self)
|
2019-06-13 10:58:43 +00:00
|
|
|
|
info = {
|
|
|
|
|
'username': self.admin_user.username,
|
|
|
|
|
'password': self.admin_user.password,
|
|
|
|
|
'private_key': self.admin_user.private_key_file,
|
|
|
|
|
}
|
|
|
|
|
return info
|
2018-03-28 11:37:29 +00:00
|
|
|
|
|
2020-10-30 02:16:49 +00:00
|
|
|
|
def nodes_display(self):
|
|
|
|
|
names = []
|
|
|
|
|
for n in self.nodes.all():
|
2020-10-30 02:43:44 +00:00
|
|
|
|
names.append(n.full_value)
|
2020-10-30 02:16:49 +00:00
|
|
|
|
return names
|
|
|
|
|
|
2021-11-26 07:16:14 +00:00
|
|
|
|
def labels_display(self):
|
|
|
|
|
names = []
|
|
|
|
|
for n in self.labels.all():
|
|
|
|
|
names.append(n.name + ':' + n.value)
|
|
|
|
|
return names
|
|
|
|
|
|
2018-10-30 04:06:39 +00:00
|
|
|
|
def as_node(self):
|
|
|
|
|
from .node import Node
|
|
|
|
|
fake_node = Node()
|
|
|
|
|
fake_node.id = self.id
|
|
|
|
|
fake_node.key = self.id
|
|
|
|
|
fake_node.value = self.hostname
|
|
|
|
|
fake_node.asset = self
|
|
|
|
|
fake_node.is_node = False
|
|
|
|
|
return fake_node
|
|
|
|
|
|
2018-12-17 10:20:44 +00:00
|
|
|
|
def as_tree_node(self, parent_node):
|
|
|
|
|
from common.tree import TreeNode
|
|
|
|
|
icon_skin = 'file'
|
2019-12-16 08:53:29 +00:00
|
|
|
|
if self.platform_base.lower() == 'windows':
|
2018-12-17 10:20:44 +00:00
|
|
|
|
icon_skin = 'windows'
|
2019-12-16 08:53:29 +00:00
|
|
|
|
elif self.platform_base.lower() == 'linux':
|
2018-12-17 10:20:44 +00:00
|
|
|
|
icon_skin = 'linux'
|
|
|
|
|
data = {
|
|
|
|
|
'id': str(self.id),
|
|
|
|
|
'name': self.hostname,
|
|
|
|
|
'title': self.ip,
|
|
|
|
|
'pId': parent_node.key,
|
|
|
|
|
'isParent': False,
|
|
|
|
|
'open': False,
|
|
|
|
|
'iconSkin': icon_skin,
|
|
|
|
|
'meta': {
|
|
|
|
|
'type': 'asset',
|
2021-07-30 07:19:00 +00:00
|
|
|
|
'data': {
|
2018-12-17 10:20:44 +00:00
|
|
|
|
'id': self.id,
|
|
|
|
|
'hostname': self.hostname,
|
|
|
|
|
'ip': self.ip,
|
2019-07-05 10:07:10 +00:00
|
|
|
|
'protocols': self.protocols_as_list,
|
2019-12-16 08:53:29 +00:00
|
|
|
|
'platform': self.platform_base,
|
2018-12-17 10:20:44 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
tree_node = TreeNode(**data)
|
|
|
|
|
return tree_node
|
|
|
|
|
|
2021-11-26 07:16:14 +00:00
|
|
|
|
def get_all_system_users(self):
|
2021-07-20 06:48:38 +00:00
|
|
|
|
from .user import SystemUser
|
|
|
|
|
system_user_ids = SystemUser.assets.through.objects.filter(asset=self)\
|
|
|
|
|
.values_list('systemuser_id', flat=True)
|
|
|
|
|
system_users = SystemUser.objects.filter(id__in=system_user_ids)
|
|
|
|
|
return system_users
|
|
|
|
|
|
2016-12-20 16:43:52 +00:00
|
|
|
|
class Meta:
|
2018-07-20 02:54:16 +00:00
|
|
|
|
unique_together = [('org_id', 'hostname')]
|
2018-01-05 09:57:02 +00:00
|
|
|
|
verbose_name = _("Asset")
|
2021-02-08 06:59:20 +00:00
|
|
|
|
ordering = ["hostname", ]
|