jumpserver/apps/assets/models/asset.py

193 lines
7.4 KiB
Python
Raw Normal View History

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
import logging
2017-11-23 06:08:01 +00:00
2016-12-20 16:43:52 +00:00
from django.db import models
from django.utils.translation import ugettext_lazy as _
2017-12-10 16:29:25 +00:00
from django.core.cache import cache
2016-12-20 16:43:52 +00:00
2017-12-15 07:50:15 +00:00
from ..const import ASSET_ADMIN_CONN_CACHE_KEY
from .cluster import Cluster
from .group import AssetGroup
from .user import AdminUser, SystemUser
2016-12-20 16:43:52 +00:00
2017-03-24 06:48:18 +00:00
__all__ = ['Asset']
2016-12-20 16:43:52 +00:00
logger = logging.getLogger(__name__)
2017-12-07 08:25:50 +00:00
def get_default_cluster():
return Cluster.initial()
2016-12-20 16:43:52 +00:00
class Asset(models.Model):
2017-12-10 16:29:25 +00:00
# Todo: Move them to settings
2016-12-20 16:43:52 +00:00
STATUS_CHOICES = (
('In use', _('In use')),
('Out of use', _('Out of use')),
)
TYPE_CHOICES = (
('Server', _('Server')),
('VM', _('VM')),
('Switch', _('Switch')),
('Router', _('Router')),
('Firewall', _('Firewall')),
('Storage', _("Storage")),
)
ENV_CHOICES = (
('Prod', 'Production'),
('Dev', 'Development'),
('Test', 'Testing'),
)
2017-03-15 16:19:47 +00:00
# Important
2017-11-23 06:08:01 +00:00
id = models.UUIDField(default=uuid.uuid4, primary_key=True)
2016-12-20 16:43:52 +00:00
ip = models.GenericIPAddressField(max_length=32, verbose_name=_('IP'), db_index=True)
hostname = models.CharField(max_length=128, unique=True, verbose_name=_('Hostname'))
port = models.IntegerField(default=22, verbose_name=_('Port'))
2017-12-07 05:01:33 +00:00
groups = models.ManyToManyField(AssetGroup, blank=True, related_name='assets', verbose_name=_('Asset groups'))
2017-12-07 08:25:50 +00:00
cluster = models.ForeignKey(Cluster, blank=True, null=True, related_name='assets', on_delete=models.SET_NULL, verbose_name=_('Cluster'),)
2017-03-15 16:19:47 +00:00
is_active = models.BooleanField(default=True, verbose_name=_('Is active'))
2017-12-07 05:01:33 +00:00
type = models.CharField(choices=TYPE_CHOICES, max_length=16, blank=True, null=True, default='Server', verbose_name=_('Asset type'),)
env = models.CharField(choices=ENV_CHOICES, max_length=8, blank=True, null=True, default='Prod', verbose_name=_('Asset environment'),)
status = models.CharField(choices=STATUS_CHOICES, max_length=12, null=True, blank=True, default='In use', verbose_name=_('Asset status'))
2017-03-15 16:19:47 +00:00
# Auth
2017-12-12 04:19:45 +00:00
admin_user = models.ForeignKey('assets.AdminUser', null=True, blank=True, on_delete=models.SET_NULL, verbose_name=_("Admin user"))
2017-03-15 16:19:47 +00:00
# Some information
2017-12-07 05:01:33 +00:00
public_ip = models.GenericIPAddressField(max_length=32, blank=True, null=True, verbose_name=_('Public IP'))
remote_card_ip = models.CharField(max_length=16, null=True, blank=True, verbose_name=_('Remote control card IP'))
2017-03-15 16:19:47 +00:00
cabinet_no = models.CharField(max_length=32, null=True, blank=True, verbose_name=_('Cabinet number'))
cabinet_pos = models.IntegerField(null=True, blank=True, verbose_name=_('Cabinet position'))
number = models.CharField(max_length=32, null=True, blank=True, verbose_name=_('Asset number'))
# 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'))
2016-12-20 16:43:52 +00:00
sn = models.CharField(max_length=128, null=True, blank=True, verbose_name=_('Serial number'))
2017-03-15 16:19:47 +00:00
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'))
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'))
platform = models.CharField(max_length=128, null=True, blank=True, verbose_name=_('Platform'))
2017-03-15 16:19:47 +00:00
os = models.CharField(max_length=128, null=True, blank=True, verbose_name=_('OS'))
2017-04-07 08:59:09 +00:00
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'))
2017-03-15 16:19:47 +00:00
hostname_raw = models.CharField(max_length=128, blank=True, null=True, verbose_name=_('Hostname raw'))
2016-12-20 16:43:52 +00:00
created_by = models.CharField(max_length=32, null=True, blank=True, verbose_name=_('Created by'))
2017-04-07 08:59:09 +00:00
date_created = models.DateTimeField(auto_now_add=True, null=True, blank=True, verbose_name=_('Date created'))
2016-12-20 16:43:52 +00:00
comment = models.TextField(max_length=128, default='', blank=True, verbose_name=_('Comment'))
2017-11-01 15:23:11 +00:00
def __str__(self):
2017-03-09 06:55:33 +00:00
return '%s <%s: %s>' % (self.hostname, self.ip, self.port)
2016-12-20 17:03:52 +00:00
2016-12-20 16:43:52 +00:00
@property
def is_valid(self):
warning = ''
if not self.is_active:
warning += ' inactive'
else:
return True, ''
return False, warning
@property
2017-12-12 04:19:45 +00:00
def hardware_info(self):
if self.cpu_count:
return '{} Core {} {}'.format(
self.cpu_count * self.cpu_cores,
self.memory, self.disk_total
)
else:
return ''
@property
2017-12-12 04:19:45 +00:00
def is_connective(self):
2017-12-15 07:50:15 +00:00
val = cache.get(ASSET_ADMIN_CONN_CACHE_KEY.format(self.hostname))
2017-12-12 04:19:45 +00:00
if val == 1:
return True
else:
2017-12-12 04:19:45 +00:00
return False
2017-12-19 04:41:00 +00:00
@property
def admin_user_avail(self):
if self.admin_user:
admin_user = self.admin_user
elif self.cluster and self.cluster.admin_user:
admin_user = self.cluster.admin_user
else:
return None
return admin_user
@property
def is_has_private_admin_user(self):
if self.admin_user:
return True
else:
return False
2016-12-20 17:03:52 +00:00
def to_json(self):
return {
'id': self.id,
'hostname': self.hostname,
'ip': self.ip,
'port': self.port,
2017-12-07 08:25:50 +00:00
'groups': [group.name for group in self.groups.all()],
}
2016-12-20 16:43:52 +00:00
2017-03-05 03:38:02 +00:00
def _to_secret_json(self):
2017-12-07 08:25:50 +00:00
"""
2017-12-12 04:19:45 +00:00
Ansible use it create inventory, First using asset user,
otherwise using cluster admin user
2017-03-05 03:38:02 +00:00
2017-12-07 08:25:50 +00:00
Todo: May be move to ops implements it
"""
data = self.to_json()
2017-12-19 04:41:00 +00:00
if self.admin_user_avail:
admin_user = self.admin_user_avail
2017-12-07 05:01:33 +00:00
data.update({
2017-12-12 04:19:45 +00:00
'username': admin_user.username,
'password': admin_user.password,
'private_key': admin_user.private_key_file,
2017-12-07 05:01:33 +00:00
'become': {
2017-12-12 04:19:45 +00:00
'method': admin_user.become_method,
'user': admin_user.become_user,
'pass': admin_user.become_pass,
2017-12-07 05:01:33 +00:00
}
})
return data
2016-12-20 16:43:52 +00:00
class Meta:
unique_together = ('ip', 'port')
@classmethod
def generate_fake(cls, count=100):
from random import seed, choice
import forgery_py
from django.db import IntegrityError
seed()
for i in range(count):
asset = cls(ip='%s.%s.%s.%s' % (i, i, i, i),
hostname=forgery_py.internet.user_name(True),
admin_user=choice(AdminUser.objects.all()),
2017-12-07 08:25:50 +00:00
cluster=choice(Cluster.objects.all()),
2016-12-20 16:43:52 +00:00
port=22,
created_by='Fake')
try:
asset.save()
asset.system_users = [choice(SystemUser.objects.all()) for i in range(3)]
asset.groups = [choice(AssetGroup.objects.all()) for i in range(3)]
logger.debug('Generate fake asset : %s' % asset.ip)
except IntegrityError:
print('Error continue')
continue