2022-12-06 03:03:14 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2023-07-24 03:52:25 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2022-12-06 03:03:14 +00:00
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
from assets.const import GATEWAY_NAME
|
2023-03-23 06:13:08 +00:00
|
|
|
from assets.models.platform import Platform
|
2023-01-16 11:02:09 +00:00
|
|
|
from common.utils import get_logger, lazyproperty
|
2023-03-23 06:13:08 +00:00
|
|
|
from orgs.mixins.models import OrgManager
|
2022-12-06 03:03:14 +00:00
|
|
|
from .asset.host import Host
|
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
|
|
|
__all__ = ['Gateway']
|
|
|
|
|
|
|
|
|
|
|
|
class GatewayManager(OrgManager):
|
|
|
|
def get_queryset(self):
|
|
|
|
queryset = super().get_queryset()
|
|
|
|
queryset = queryset.filter(platform__name=GATEWAY_NAME)
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
def bulk_create(self, objs, batch_size=None, ignore_conflicts=False):
|
|
|
|
default_platform = Gateway.default_platform()
|
|
|
|
for obj in objs:
|
|
|
|
obj.platform = default_platform
|
|
|
|
return super().bulk_create(objs, batch_size, ignore_conflicts)
|
|
|
|
|
|
|
|
|
|
|
|
class Gateway(Host):
|
|
|
|
objects = GatewayManager()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
proxy = True
|
2022-12-23 07:49:32 +00:00
|
|
|
verbose_name = _("Gateway")
|
2022-12-06 03:03:14 +00:00
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
self.platform = self.default_platform()
|
|
|
|
return super().save(*args, **kwargs)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def default_platform(cls):
|
|
|
|
return Platform.objects.get(name=GATEWAY_NAME, internal=True)
|
|
|
|
|
|
|
|
@lazyproperty
|
|
|
|
def select_account(self):
|
|
|
|
account = self.accounts.active().order_by('-privileged', '-date_updated').first()
|
|
|
|
return account
|
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
@lazyproperty
|
|
|
|
def username(self):
|
|
|
|
account = self.select_account
|
|
|
|
return account.username if account else None
|
|
|
|
|
|
|
|
@lazyproperty
|
|
|
|
def password(self):
|
|
|
|
account = self.select_account
|
|
|
|
return account.password if account else None
|
|
|
|
|
2023-03-23 06:13:08 +00:00
|
|
|
@lazyproperty
|
|
|
|
def port(self):
|
|
|
|
protocol = self.protocols.filter(name='ssh').first()
|
|
|
|
if protocol:
|
|
|
|
return protocol.port
|
|
|
|
else:
|
|
|
|
return '22'
|
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
@lazyproperty
|
|
|
|
def private_key(self):
|
|
|
|
account = self.select_account
|
|
|
|
return account.private_key if account else None
|
|
|
|
|
|
|
|
@lazyproperty
|
|
|
|
def private_key_path(self):
|
|
|
|
account = self.select_account
|
|
|
|
return account.private_key_path if account else None
|
2024-04-11 09:37:46 +00:00
|
|
|
|
|
|
|
def get_private_key_path(self, path):
|
|
|
|
account = self.select_account
|
|
|
|
return account.get_private_key_path(path) if account else None
|