2022-11-03 08:55:38 +00:00
|
|
|
|
from collections import defaultdict
|
|
|
|
|
|
2022-10-22 03:17:02 +00:00
|
|
|
|
from django.db import models
|
2022-11-03 08:55:38 +00:00
|
|
|
|
from django.utils import timezone
|
2022-11-11 10:16:11 +00:00
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
2022-11-03 08:55:38 +00:00
|
|
|
|
from rest_framework.exceptions import ValidationError
|
|
|
|
|
from simple_history.utils import bulk_create_with_history
|
2022-10-22 03:17:02 +00:00
|
|
|
|
|
2022-11-11 10:16:11 +00:00
|
|
|
|
from assets.models import Host
|
2022-11-01 03:52:51 +00:00
|
|
|
|
from common.db.models import JMSBaseModel
|
2022-11-03 08:55:38 +00:00
|
|
|
|
from common.utils import random_string
|
2023-02-21 04:12:38 +00:00
|
|
|
|
from terminal.const import PublishStatus
|
2022-10-22 03:17:02 +00:00
|
|
|
|
|
2022-11-01 03:52:51 +00:00
|
|
|
|
__all__ = ['AppletHost', 'AppletHostDeployment']
|
2022-10-22 03:17:02 +00:00
|
|
|
|
|
|
|
|
|
|
2022-10-27 08:26:15 +00:00
|
|
|
|
class AppletHost(Host):
|
2022-11-01 09:04:44 +00:00
|
|
|
|
deploy_options = models.JSONField(default=dict, verbose_name=_('Deploy options'))
|
2023-07-28 02:41:37 +00:00
|
|
|
|
auto_create_accounts = models.BooleanField(default=True, verbose_name=_('Auto create accounts'))
|
|
|
|
|
accounts_create_amount = models.IntegerField(default=100, verbose_name=_('Accounts create amount'))
|
2022-11-01 03:52:51 +00:00
|
|
|
|
inited = models.BooleanField(default=False, verbose_name=_('Inited'))
|
|
|
|
|
date_inited = models.DateTimeField(null=True, blank=True, verbose_name=_('Date inited'))
|
2022-10-22 03:17:02 +00:00
|
|
|
|
date_synced = models.DateTimeField(null=True, blank=True, verbose_name=_('Date synced'))
|
2022-11-02 12:33:27 +00:00
|
|
|
|
terminal = models.OneToOneField(
|
|
|
|
|
'terminal.Terminal', on_delete=models.PROTECT, null=True, blank=True,
|
|
|
|
|
related_name='applet_host', verbose_name=_('Terminal')
|
|
|
|
|
)
|
2023-10-30 03:37:37 +00:00
|
|
|
|
using_same_account = models.BooleanField(default=False, verbose_name=_('Using same account'))
|
2022-10-22 03:17:02 +00:00
|
|
|
|
applets = models.ManyToManyField(
|
|
|
|
|
'Applet', verbose_name=_('Applet'),
|
2022-11-08 09:54:04 +00:00
|
|
|
|
through='AppletPublication', through_fields=('host', 'applet'),
|
2022-10-22 03:17:02 +00:00
|
|
|
|
)
|
2022-11-03 08:55:38 +00:00
|
|
|
|
LOCKING_ORG = '00000000-0000-0000-0000-000000000004'
|
2022-10-22 03:17:02 +00:00
|
|
|
|
|
2022-12-23 07:49:32 +00:00
|
|
|
|
class Meta:
|
2023-01-16 11:02:09 +00:00
|
|
|
|
verbose_name = _('Hosting')
|
2022-12-23 07:49:32 +00:00
|
|
|
|
|
2022-10-25 04:57:34 +00:00
|
|
|
|
def __str__(self):
|
2022-10-28 10:19:44 +00:00
|
|
|
|
return self.name
|
2022-10-25 04:57:34 +00:00
|
|
|
|
|
2022-11-03 08:55:38 +00:00
|
|
|
|
@property
|
2022-11-07 12:41:18 +00:00
|
|
|
|
def load(self):
|
|
|
|
|
if not self.terminal:
|
|
|
|
|
return 'offline'
|
|
|
|
|
return self.terminal.load
|
2022-11-03 08:55:38 +00:00
|
|
|
|
|
|
|
|
|
def check_terminal_binding(self, request):
|
|
|
|
|
request_terminal = getattr(request.user, 'terminal', None)
|
|
|
|
|
if not request_terminal:
|
|
|
|
|
raise ValidationError('Request user has no terminal')
|
|
|
|
|
|
|
|
|
|
self.date_synced = timezone.now()
|
2022-11-11 10:16:11 +00:00
|
|
|
|
if self.terminal == request_terminal:
|
|
|
|
|
self.save(update_fields=['date_synced'])
|
|
|
|
|
else:
|
2022-11-03 08:55:38 +00:00
|
|
|
|
self.terminal = request_terminal
|
|
|
|
|
self.save(update_fields=['terminal', 'date_synced'])
|
|
|
|
|
|
|
|
|
|
def check_applets_state(self, applets_value_list):
|
|
|
|
|
applets = self.applets.all()
|
|
|
|
|
name_version_mapper = {
|
|
|
|
|
value['name']: value['version']
|
|
|
|
|
for value in applets_value_list
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
status_applets = defaultdict(list)
|
|
|
|
|
for applet in applets:
|
|
|
|
|
if applet.name not in name_version_mapper:
|
2023-02-21 04:12:38 +00:00
|
|
|
|
status_applets[PublishStatus.failed.value].append(applet)
|
2022-11-03 08:55:38 +00:00
|
|
|
|
elif applet.version != name_version_mapper[applet.name]:
|
2023-02-21 04:12:38 +00:00
|
|
|
|
status_applets[PublishStatus.mismatch.value].append(applet)
|
2022-11-03 08:55:38 +00:00
|
|
|
|
else:
|
2023-02-21 04:12:38 +00:00
|
|
|
|
status_applets[PublishStatus.success.value].append(applet)
|
2022-11-03 08:55:38 +00:00
|
|
|
|
|
|
|
|
|
for status, applets in status_applets.items():
|
2022-11-08 09:54:04 +00:00
|
|
|
|
self.publications.filter(applet__in=applets) \
|
|
|
|
|
.exclude(status=status) \
|
2022-11-03 08:55:38 +00:00
|
|
|
|
.update(status=status)
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def random_username():
|
|
|
|
|
return 'jms_' + random_string(8)
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def random_password():
|
|
|
|
|
return random_string(16, special_char=True)
|
|
|
|
|
|
|
|
|
|
def generate_accounts(self):
|
2023-07-28 02:41:37 +00:00
|
|
|
|
if not self.auto_create_accounts:
|
|
|
|
|
return
|
2023-05-09 11:46:34 +00:00
|
|
|
|
self.generate_public_accounts()
|
|
|
|
|
self.generate_private_accounts()
|
|
|
|
|
|
|
|
|
|
def generate_public_accounts(self):
|
|
|
|
|
now_count = self.accounts.filter(privileged=False, username__startswith='jms').count()
|
2023-07-28 02:41:37 +00:00
|
|
|
|
need = self.accounts_create_amount - now_count
|
2022-11-03 08:55:38 +00:00
|
|
|
|
|
|
|
|
|
accounts = []
|
|
|
|
|
account_model = self.accounts.model
|
|
|
|
|
for i in range(need):
|
|
|
|
|
username = self.random_username()
|
|
|
|
|
password = self.random_password()
|
|
|
|
|
account = account_model(
|
|
|
|
|
username=username, secret=password, name=username,
|
|
|
|
|
asset_id=self.id, secret_type='password', version=1,
|
2022-11-08 09:54:04 +00:00
|
|
|
|
org_id=self.LOCKING_ORG, is_active=False,
|
2022-11-03 08:55:38 +00:00
|
|
|
|
)
|
|
|
|
|
accounts.append(account)
|
2023-05-09 11:46:34 +00:00
|
|
|
|
bulk_create_with_history(accounts, account_model, batch_size=20, ignore_conflicts=True)
|
|
|
|
|
|
|
|
|
|
def generate_private_accounts_by_usernames(self, usernames):
|
|
|
|
|
accounts = []
|
|
|
|
|
account_model = self.accounts.model
|
|
|
|
|
for username in usernames:
|
|
|
|
|
password = self.random_password()
|
|
|
|
|
username = 'js_' + username
|
|
|
|
|
account = account_model(
|
|
|
|
|
username=username, secret=password, name=username,
|
|
|
|
|
asset_id=self.id, secret_type='password', version=1,
|
|
|
|
|
org_id=self.LOCKING_ORG, is_active=False,
|
|
|
|
|
)
|
|
|
|
|
accounts.append(account)
|
|
|
|
|
bulk_create_with_history(accounts, account_model, batch_size=20, ignore_conflicts=True)
|
|
|
|
|
|
|
|
|
|
def generate_private_accounts(self):
|
|
|
|
|
from users.models import User
|
|
|
|
|
usernames = User.objects \
|
|
|
|
|
.filter(is_active=True, is_service_account=False) \
|
2023-08-10 10:23:53 +00:00
|
|
|
|
.exclude(username__startswith='[') \
|
2023-05-09 11:46:34 +00:00
|
|
|
|
.values_list('username', flat=True)
|
|
|
|
|
account_usernames = self.accounts.all().values_list('username', flat=True)
|
2023-05-10 02:47:50 +00:00
|
|
|
|
account_usernames = [username[3:] for username in account_usernames if username.startswith('js_')]
|
2023-05-09 11:46:34 +00:00
|
|
|
|
not_exist_users = set(usernames) - set(account_usernames)
|
|
|
|
|
self.generate_private_accounts_by_usernames(not_exist_users)
|
2022-11-03 08:55:38 +00:00
|
|
|
|
|
2022-11-01 03:52:51 +00:00
|
|
|
|
|
|
|
|
|
class AppletHostDeployment(JMSBaseModel):
|
|
|
|
|
host = models.ForeignKey('AppletHost', on_delete=models.CASCADE, verbose_name=_('Hosting'))
|
2022-11-01 09:04:44 +00:00
|
|
|
|
initial = models.BooleanField(default=False, verbose_name=_('Initial'))
|
2023-01-16 11:02:09 +00:00
|
|
|
|
status = models.CharField(max_length=16, default='pending', verbose_name=_('Status'))
|
2022-11-01 03:52:51 +00:00
|
|
|
|
date_start = models.DateTimeField(null=True, verbose_name=_('Date start'), db_index=True)
|
|
|
|
|
date_finished = models.DateTimeField(null=True, verbose_name=_("Date finished"))
|
|
|
|
|
comment = models.TextField(default='', blank=True, verbose_name=_('Comment'))
|
2022-11-15 08:01:01 +00:00
|
|
|
|
task = models.UUIDField(null=True, verbose_name=_('Task'))
|
2022-11-01 03:52:51 +00:00
|
|
|
|
|
2022-11-17 08:38:46 +00:00
|
|
|
|
class Meta:
|
|
|
|
|
ordering = ('-date_start',)
|
|
|
|
|
|
2022-11-01 03:52:51 +00:00
|
|
|
|
def start(self, **kwargs):
|
2023-01-30 06:42:42 +00:00
|
|
|
|
# 重新初始化部署,applet host 关联的终端需要删除
|
2023-08-15 05:49:56 +00:00
|
|
|
|
# 否则 tinker 会因组件注册名称相同,造成冲突,执行任务失败
|
2023-01-30 06:42:42 +00:00
|
|
|
|
if self.host.terminal:
|
|
|
|
|
terminal = self.host.terminal
|
|
|
|
|
self.host.terminal = None
|
|
|
|
|
self.host.save()
|
|
|
|
|
terminal.delete()
|
2022-11-01 03:52:51 +00:00
|
|
|
|
from ...automations.deploy_applet_host import DeployAppletHostManager
|
2024-03-15 05:57:22 +00:00
|
|
|
|
manager = DeployAppletHostManager(self, **kwargs)
|
|
|
|
|
manager.run()
|
2022-11-14 10:48:21 +00:00
|
|
|
|
|
|
|
|
|
def install_applet(self, applet_id, **kwargs):
|
2024-03-21 03:35:14 +00:00
|
|
|
|
manager = self.create_deploy_manager(applet_id, **kwargs)
|
2022-11-14 10:48:21 +00:00
|
|
|
|
manager.install_applet(**kwargs)
|
2022-11-15 08:01:01 +00:00
|
|
|
|
|
2024-03-20 11:23:44 +00:00
|
|
|
|
def uninstall_applet(self, applet_id, **kwargs):
|
2024-03-21 03:35:14 +00:00
|
|
|
|
manager = self.create_deploy_manager(applet_id, **kwargs)
|
|
|
|
|
manager.uninstall_applet(**kwargs)
|
|
|
|
|
|
|
|
|
|
def create_deploy_manager(self, applet_id, **kwargs):
|
2024-03-20 11:23:44 +00:00
|
|
|
|
from ...automations.deploy_applet_host import DeployAppletHostManager
|
|
|
|
|
from .applet import Applet
|
|
|
|
|
if applet_id:
|
|
|
|
|
applet = Applet.objects.get(id=applet_id)
|
|
|
|
|
else:
|
|
|
|
|
applet = None
|
2024-03-21 03:35:14 +00:00
|
|
|
|
return DeployAppletHostManager(self, applet=applet)
|
2024-03-20 11:23:44 +00:00
|
|
|
|
|
2022-11-15 08:01:01 +00:00
|
|
|
|
def save_task(self, task):
|
|
|
|
|
self.task = task
|
|
|
|
|
self.save(update_fields=['task'])
|