2022-10-25 04:57:34 +00:00
|
|
|
|
import os.path
|
2022-12-07 07:09:01 +00:00
|
|
|
|
import random
|
2023-01-16 11:02:09 +00:00
|
|
|
|
import shutil
|
2023-08-24 10:00:19 +00:00
|
|
|
|
from collections import defaultdict
|
2022-10-25 04:57:34 +00:00
|
|
|
|
|
2022-12-07 07:09:01 +00:00
|
|
|
|
import yaml
|
2022-10-25 11:31:13 +00:00
|
|
|
|
from django.conf import settings
|
2022-12-07 07:09:01 +00:00
|
|
|
|
from django.core.cache import cache
|
2022-10-25 11:31:13 +00:00
|
|
|
|
from django.core.files.storage import default_storage
|
2022-10-22 03:17:02 +00:00
|
|
|
|
from django.db import models
|
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
2022-12-20 08:48:18 +00:00
|
|
|
|
from rest_framework.serializers import ValidationError
|
2022-10-22 03:17:02 +00:00
|
|
|
|
|
2023-05-31 08:21:24 +00:00
|
|
|
|
from assets.models import Platform
|
2022-10-22 03:17:02 +00:00
|
|
|
|
from common.db.models import JMSBaseModel
|
2023-02-10 11:40:35 +00:00
|
|
|
|
from common.utils import lazyproperty, get_logger
|
2023-04-20 03:13:28 +00:00
|
|
|
|
from common.utils.yml import yaml_load_with_i18n
|
2023-02-10 11:40:35 +00:00
|
|
|
|
|
|
|
|
|
logger = get_logger(__name__)
|
2022-10-22 03:17:02 +00:00
|
|
|
|
|
|
|
|
|
__all__ = ['Applet', 'AppletPublication']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Applet(JMSBaseModel):
|
|
|
|
|
class Type(models.TextChoices):
|
2022-10-25 04:57:34 +00:00
|
|
|
|
general = 'general', _('General')
|
2022-10-22 03:17:02 +00:00
|
|
|
|
web = 'web', _('Web')
|
2022-10-25 04:57:34 +00:00
|
|
|
|
|
2023-06-09 07:40:41 +00:00
|
|
|
|
class Edition(models.TextChoices):
|
2023-09-11 07:26:50 +00:00
|
|
|
|
community = 'community', _('Community edition')
|
2023-06-09 07:40:41 +00:00
|
|
|
|
enterprise = 'enterprise', _('Enterprise')
|
|
|
|
|
|
2022-11-02 07:01:52 +00:00
|
|
|
|
name = models.SlugField(max_length=128, verbose_name=_('Name'), unique=True)
|
2022-10-25 11:31:13 +00:00
|
|
|
|
display_name = models.CharField(max_length=128, verbose_name=_('Display name'))
|
2022-10-22 03:17:02 +00:00
|
|
|
|
version = models.CharField(max_length=16, verbose_name=_('Version'))
|
|
|
|
|
author = models.CharField(max_length=128, verbose_name=_('Author'))
|
2023-08-10 06:41:43 +00:00
|
|
|
|
edition = models.CharField(max_length=128, choices=Edition.choices, default=Edition.community,
|
|
|
|
|
verbose_name=_('Edition'))
|
2022-10-25 04:57:34 +00:00
|
|
|
|
type = models.CharField(max_length=16, verbose_name=_('Type'), default='general', choices=Type.choices)
|
2022-10-26 09:21:52 +00:00
|
|
|
|
is_active = models.BooleanField(default=True, verbose_name=_('Is active'))
|
2022-12-20 08:48:18 +00:00
|
|
|
|
builtin = models.BooleanField(default=False, verbose_name=_('Builtin'))
|
2022-10-22 03:17:02 +00:00
|
|
|
|
protocols = models.JSONField(default=list, verbose_name=_('Protocol'))
|
2023-08-08 06:30:14 +00:00
|
|
|
|
can_concurrent = models.BooleanField(default=False, verbose_name=_('Can concurrent'))
|
2022-10-25 04:57:34 +00:00
|
|
|
|
tags = models.JSONField(default=list, verbose_name=_('Tags'))
|
2022-10-22 03:17:02 +00:00
|
|
|
|
comment = models.TextField(default='', blank=True, verbose_name=_('Comment'))
|
2022-11-01 12:37:04 +00:00
|
|
|
|
hosts = models.ManyToManyField(
|
|
|
|
|
through_fields=('applet', 'host'), through='AppletPublication',
|
|
|
|
|
to='AppletHost', verbose_name=_('Hosts')
|
|
|
|
|
)
|
2022-10-22 03:17:02 +00:00
|
|
|
|
|
2022-12-23 07:49:32 +00:00
|
|
|
|
class Meta:
|
|
|
|
|
verbose_name = _("Applet")
|
|
|
|
|
|
2022-10-22 03:17:02 +00:00
|
|
|
|
def __str__(self):
|
|
|
|
|
return self.name
|
|
|
|
|
|
2022-10-25 11:31:13 +00:00
|
|
|
|
@property
|
|
|
|
|
def path(self):
|
2022-12-20 08:48:18 +00:00
|
|
|
|
if self.builtin:
|
|
|
|
|
return os.path.join(settings.APPS_DIR, 'terminal', 'applets', self.name)
|
|
|
|
|
else:
|
|
|
|
|
return default_storage.path('applets/{}'.format(self.name))
|
2022-10-25 11:31:13 +00:00
|
|
|
|
|
2023-01-30 05:01:06 +00:00
|
|
|
|
@lazyproperty
|
|
|
|
|
def readme(self):
|
|
|
|
|
readme_file = os.path.join(self.path, 'README.md')
|
|
|
|
|
if os.path.isfile(readme_file):
|
|
|
|
|
with open(readme_file, 'r') as f:
|
|
|
|
|
return f.read()
|
|
|
|
|
return ''
|
|
|
|
|
|
2022-10-25 04:57:34 +00:00
|
|
|
|
@property
|
|
|
|
|
def manifest(self):
|
|
|
|
|
path = os.path.join(self.path, 'manifest.yml')
|
|
|
|
|
if not os.path.exists(path):
|
|
|
|
|
return None
|
|
|
|
|
with open(path, 'r') as f:
|
|
|
|
|
return yaml.safe_load(f)
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def icon(self):
|
|
|
|
|
path = os.path.join(self.path, 'icon.png')
|
|
|
|
|
if not os.path.exists(path):
|
|
|
|
|
return None
|
2022-10-25 11:31:13 +00:00
|
|
|
|
return os.path.join(settings.MEDIA_URL, 'applets', self.name, 'icon.png')
|
2022-10-25 04:57:34 +00:00
|
|
|
|
|
2022-12-20 08:48:18 +00:00
|
|
|
|
@staticmethod
|
|
|
|
|
def validate_pkg(d):
|
2023-04-20 03:13:28 +00:00
|
|
|
|
files = ['manifest.yml', 'icon.png', 'setup.yml']
|
2022-12-20 08:48:18 +00:00
|
|
|
|
for name in files:
|
|
|
|
|
path = os.path.join(d, name)
|
|
|
|
|
if not os.path.exists(path):
|
2023-01-30 07:19:05 +00:00
|
|
|
|
raise ValidationError({'error': _('Applet pkg not valid, Missing file {}').format(name)})
|
2022-12-20 08:48:18 +00:00
|
|
|
|
|
2023-05-27 03:53:00 +00:00
|
|
|
|
with open(os.path.join(d, 'manifest.yml'), encoding='utf8') as f:
|
2023-04-20 03:13:28 +00:00
|
|
|
|
manifest = yaml_load_with_i18n(f)
|
2022-12-20 08:48:18 +00:00
|
|
|
|
|
|
|
|
|
if not manifest.get('name', ''):
|
|
|
|
|
raise ValidationError({'error': 'Missing name in manifest.yml'})
|
|
|
|
|
return manifest
|
|
|
|
|
|
2023-05-31 08:21:24 +00:00
|
|
|
|
def load_platform_if_need(self, d):
|
2023-04-10 02:57:44 +00:00
|
|
|
|
from assets.serializers import PlatformSerializer
|
2023-05-25 07:11:54 +00:00
|
|
|
|
from assets.const import CustomTypes
|
2023-04-10 02:57:44 +00:00
|
|
|
|
|
|
|
|
|
if not os.path.exists(os.path.join(d, 'platform.yml')):
|
|
|
|
|
return
|
|
|
|
|
try:
|
2023-05-27 03:53:00 +00:00
|
|
|
|
with open(os.path.join(d, 'platform.yml'), encoding='utf8') as f:
|
2023-04-21 03:18:04 +00:00
|
|
|
|
data = yaml_load_with_i18n(f)
|
2023-04-10 02:57:44 +00:00
|
|
|
|
except Exception as e:
|
|
|
|
|
raise ValidationError({'error': _('Load platform.yml failed: {}').format(e)})
|
|
|
|
|
|
|
|
|
|
if data['category'] != 'custom':
|
|
|
|
|
raise ValidationError({'error': _('Only support custom platform')})
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
tp = data['type']
|
|
|
|
|
except KeyError:
|
|
|
|
|
raise ValidationError({'error': _('Missing type in platform.yml')})
|
|
|
|
|
|
2023-05-25 07:11:54 +00:00
|
|
|
|
if not data.get('automation'):
|
|
|
|
|
data['automation'] = CustomTypes._get_automation_constrains()['*']
|
|
|
|
|
|
2023-05-31 08:21:24 +00:00
|
|
|
|
created_by = 'Applet:{}'.format(self.name)
|
2023-05-31 08:35:42 +00:00
|
|
|
|
instance = self.get_related_platform()
|
2023-05-31 08:21:24 +00:00
|
|
|
|
s = PlatformSerializer(data=data, instance=instance)
|
2023-04-10 02:57:44 +00:00
|
|
|
|
s.add_type_choices(tp, tp)
|
|
|
|
|
s.is_valid(raise_exception=True)
|
2023-05-31 08:21:24 +00:00
|
|
|
|
p = s.save()
|
|
|
|
|
p.created_by = created_by
|
|
|
|
|
p.save(update_fields=['created_by'])
|
2023-04-10 02:57:44 +00:00
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def install_from_dir(cls, path, builtin=True):
|
2022-12-20 08:48:18 +00:00
|
|
|
|
from terminal.serializers import AppletSerializer
|
|
|
|
|
|
|
|
|
|
manifest = cls.validate_pkg(path)
|
|
|
|
|
name = manifest['name']
|
|
|
|
|
instance = cls.objects.filter(name=name).first()
|
|
|
|
|
serializer = AppletSerializer(instance=instance, data=manifest)
|
2023-06-12 11:00:59 +00:00
|
|
|
|
serializer.is_valid(raise_exception=True)
|
2023-05-31 08:21:24 +00:00
|
|
|
|
instance = serializer.save(builtin=builtin)
|
|
|
|
|
instance.load_platform_if_need(path)
|
2023-01-16 11:02:09 +00:00
|
|
|
|
|
2023-04-10 02:57:44 +00:00
|
|
|
|
pkg_path = default_storage.path('applets/{}'.format(name))
|
2023-01-16 11:02:09 +00:00
|
|
|
|
if os.path.exists(pkg_path):
|
|
|
|
|
shutil.rmtree(pkg_path)
|
|
|
|
|
shutil.copytree(path, pkg_path)
|
2023-04-10 02:57:44 +00:00
|
|
|
|
return instance, serializer
|
2022-12-20 08:48:18 +00:00
|
|
|
|
|
2023-08-24 10:00:19 +00:00
|
|
|
|
host_prefer_key_tpl = 'applet_host_prefer_{}'
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def clear_host_prefer(cls):
|
|
|
|
|
cache.delete_pattern(cls.host_prefer_key_tpl.format('*'))
|
|
|
|
|
|
|
|
|
|
def _select_by_load(self, hosts):
|
|
|
|
|
using_keys = cache.keys(self.host_prefer_key_tpl.format('*'))
|
|
|
|
|
using_host_ids = cache.get_many(using_keys)
|
|
|
|
|
counts = defaultdict(int)
|
|
|
|
|
for host_id in using_host_ids.values():
|
|
|
|
|
counts[host_id] += 1
|
|
|
|
|
|
|
|
|
|
hosts = list(sorted(hosts, key=lambda h: counts[h.id]))
|
|
|
|
|
return hosts[0]
|
|
|
|
|
|
2023-06-20 05:54:53 +00:00
|
|
|
|
def select_host(self, user, asset):
|
|
|
|
|
hosts = self.hosts.filter(is_active=True)
|
|
|
|
|
hosts = [host for host in hosts if host.load != 'offline']
|
2022-12-07 07:09:01 +00:00
|
|
|
|
if not hosts:
|
|
|
|
|
return None
|
|
|
|
|
|
2023-06-20 05:54:53 +00:00
|
|
|
|
spec_label = asset.labels.filter(name__in=['AppletHost', '发布机']).first()
|
|
|
|
|
if spec_label:
|
2023-08-08 07:41:55 +00:00
|
|
|
|
matched = [host for host in hosts if host.name == spec_label.value]
|
|
|
|
|
if matched:
|
|
|
|
|
return matched[0]
|
2023-06-20 05:54:53 +00:00
|
|
|
|
|
2023-08-08 07:41:55 +00:00
|
|
|
|
hosts = [h for h in hosts if h.auto_create_accounts]
|
2023-08-24 10:00:19 +00:00
|
|
|
|
prefer_key = self.host_prefer_key_tpl.format(user.id)
|
2023-05-09 11:46:34 +00:00
|
|
|
|
prefer_host_id = cache.get(prefer_key, None)
|
|
|
|
|
pref_host = [host for host in hosts if host.id == prefer_host_id]
|
2023-08-24 10:00:19 +00:00
|
|
|
|
|
2023-05-09 11:46:34 +00:00
|
|
|
|
if pref_host:
|
|
|
|
|
host = pref_host[0]
|
|
|
|
|
else:
|
2023-08-24 10:00:19 +00:00
|
|
|
|
host = self._select_by_load(hosts)
|
|
|
|
|
cache.set(prefer_key, str(host.id), timeout=None)
|
2023-05-09 11:46:34 +00:00
|
|
|
|
return host
|
2023-02-10 11:40:35 +00:00
|
|
|
|
|
2023-05-31 08:21:24 +00:00
|
|
|
|
def get_related_platform(self):
|
|
|
|
|
created_by = 'Applet:{}'.format(self.name)
|
|
|
|
|
platform = Platform.objects.filter(created_by=created_by).first()
|
|
|
|
|
return platform
|
|
|
|
|
|
2023-05-09 11:46:34 +00:00
|
|
|
|
@staticmethod
|
|
|
|
|
def random_select_prefer_account(user, host, accounts):
|
|
|
|
|
msg = 'Applet host remain public accounts: {}: {}'.format(host.name, len(accounts))
|
2023-02-10 11:40:35 +00:00
|
|
|
|
if len(accounts) == 0:
|
|
|
|
|
logger.error(msg)
|
2023-05-09 11:46:34 +00:00
|
|
|
|
return None
|
|
|
|
|
prefer_host_account_key = 'applet_host_prefer_account_{}_{}'.format(user.id, host.id)
|
|
|
|
|
prefer_account_id = cache.get(prefer_host_account_key, None)
|
2023-05-09 11:53:29 +00:00
|
|
|
|
prefer_account = None
|
|
|
|
|
if prefer_account_id:
|
|
|
|
|
prefer_account = accounts.filter(id=prefer_account_id).first()
|
2023-05-09 11:46:34 +00:00
|
|
|
|
if prefer_account:
|
|
|
|
|
account = prefer_account
|
2023-02-10 11:40:35 +00:00
|
|
|
|
else:
|
2023-05-09 11:46:34 +00:00
|
|
|
|
account = random.choice(accounts)
|
|
|
|
|
cache.set(prefer_host_account_key, account.id, timeout=None)
|
|
|
|
|
return account
|
2022-12-07 07:09:01 +00:00
|
|
|
|
|
2023-08-08 06:30:14 +00:00
|
|
|
|
accounts_using_key_tmpl = 'applet_host_accounts_{}_{}_{}'
|
|
|
|
|
|
|
|
|
|
def select_a_public_account(self, user, host, valid_accounts):
|
|
|
|
|
using_keys = cache.keys(self.accounts_using_key_tmpl.format(host.id, '*', '*')) or []
|
|
|
|
|
accounts_username_used = list(cache.get_many(using_keys).values())
|
|
|
|
|
logger.debug('Applet host account using: {}: {}'.format(host.name, accounts_username_used))
|
|
|
|
|
accounts = valid_accounts.exclude(username__in=accounts_username_used)
|
|
|
|
|
public_accounts = accounts.filter(username__startswith='jms_')
|
|
|
|
|
if not public_accounts:
|
|
|
|
|
public_accounts = accounts.exclude(username__in=['Administrator', 'root'])
|
|
|
|
|
account = self.random_select_prefer_account(user, host, public_accounts)
|
|
|
|
|
return account
|
|
|
|
|
|
|
|
|
|
def try_to_use_private_account(self, user, host, valid_accounts):
|
|
|
|
|
host_can_concurrent = str(host.deploy_options.get('RDS_fSingleSessionPerUser', 0)) == '0'
|
|
|
|
|
app_can_concurrent = self.can_concurrent or self.type == 'web'
|
|
|
|
|
all_can_concurrent = host_can_concurrent and app_can_concurrent
|
|
|
|
|
|
|
|
|
|
private_account = valid_accounts.filter(username='js_{}'.format(user.username)).first()
|
|
|
|
|
if not private_account:
|
2023-08-08 07:41:55 +00:00
|
|
|
|
logger.debug('Private account not found ...')
|
2023-08-08 06:30:14 +00:00
|
|
|
|
return None
|
|
|
|
|
# 优先使用 private account,支持并发或者不支持并发时,如果私有没有被占用,则使用私有
|
|
|
|
|
account = None
|
|
|
|
|
# 如果都支持,不管私有是否被占用,都使用私有
|
|
|
|
|
if all_can_concurrent:
|
2023-08-08 07:41:55 +00:00
|
|
|
|
logger.debug('All can concurrent, use private account')
|
2023-08-08 06:30:14 +00:00
|
|
|
|
account = private_account
|
|
|
|
|
# 如果主机都不支持并发,则查询一下私有账号有没有任何应用使用,如果没有被使用,则使用私有
|
|
|
|
|
elif not host_can_concurrent:
|
|
|
|
|
private_using_key = self.accounts_using_key_tmpl.format(host.id, private_account.username, '*')
|
2023-08-08 07:41:55 +00:00
|
|
|
|
private_is_using = len(cache.keys(private_using_key))
|
|
|
|
|
logger.debug("Private account is using: {}".format(private_is_using))
|
2023-08-08 06:30:14 +00:00
|
|
|
|
if not private_is_using:
|
|
|
|
|
account = private_account
|
|
|
|
|
# 如果主机支持,但是应用不支持并发,则查询一下私有账号有没有被这个应用使用, 如果没有被使用,则使用私有
|
|
|
|
|
elif host_can_concurrent and not app_can_concurrent:
|
|
|
|
|
private_app_using_key = self.accounts_using_key_tmpl.format(host.id, private_account.username, self.name)
|
|
|
|
|
private_is_using_by_this_app = cache.get(private_app_using_key, False)
|
2023-08-08 07:41:55 +00:00
|
|
|
|
logger.debug("Private account is using {} by {}".format(private_is_using_by_this_app, self.name))
|
2023-08-08 06:30:14 +00:00
|
|
|
|
if not private_is_using_by_this_app:
|
|
|
|
|
account = private_account
|
|
|
|
|
return account
|
|
|
|
|
|
2023-06-20 05:54:53 +00:00
|
|
|
|
def select_host_account(self, user, asset):
|
2023-05-09 11:46:34 +00:00
|
|
|
|
# 选择激活的发布机
|
2023-06-20 05:54:53 +00:00
|
|
|
|
host = self.select_host(user, asset)
|
2023-05-09 11:46:34 +00:00
|
|
|
|
if not host:
|
2022-12-07 07:09:01 +00:00
|
|
|
|
return None
|
2023-08-24 10:00:19 +00:00
|
|
|
|
logger.info('Select applet host: {}'.format(host.name))
|
2023-05-09 11:46:34 +00:00
|
|
|
|
|
2023-08-08 06:30:14 +00:00
|
|
|
|
valid_accounts = host.accounts.all().filter(is_active=True, privileged=False)
|
|
|
|
|
account = self.try_to_use_private_account(user, host, valid_accounts)
|
|
|
|
|
if not account:
|
2023-08-08 07:41:55 +00:00
|
|
|
|
logger.debug('No private account, try to use public account')
|
2023-08-08 06:30:14 +00:00
|
|
|
|
account = self.select_a_public_account(user, host, valid_accounts)
|
2023-05-09 11:46:34 +00:00
|
|
|
|
|
2023-08-08 07:41:55 +00:00
|
|
|
|
if not account:
|
|
|
|
|
logger.debug('No available account for applet host: {}'.format(host.name))
|
|
|
|
|
return None
|
|
|
|
|
|
2022-12-07 07:09:01 +00:00
|
|
|
|
ttl = 60 * 60 * 24
|
2023-08-08 06:30:14 +00:00
|
|
|
|
lock_key = self.accounts_using_key_tmpl.format(host.id, account.username, self.name)
|
2022-12-07 07:09:01 +00:00
|
|
|
|
cache.set(lock_key, account.username, ttl)
|
2022-12-20 08:48:18 +00:00
|
|
|
|
|
2023-08-08 07:41:55 +00:00
|
|
|
|
res = {
|
2022-12-07 07:09:01 +00:00
|
|
|
|
'host': host,
|
|
|
|
|
'account': account,
|
|
|
|
|
'lock_key': lock_key,
|
|
|
|
|
'ttl': ttl
|
|
|
|
|
}
|
2023-08-08 07:41:55 +00:00
|
|
|
|
logger.debug('Select host and account: {}'.format(res))
|
|
|
|
|
return res
|
2022-12-07 07:09:01 +00:00
|
|
|
|
|
2023-05-31 08:21:24 +00:00
|
|
|
|
def delete(self, using=None, keep_parents=False):
|
|
|
|
|
platform = self.get_related_platform()
|
|
|
|
|
if platform and platform.assets.count() == 0:
|
|
|
|
|
platform.delete()
|
|
|
|
|
return super().delete(using, keep_parents)
|
|
|
|
|
|
2022-10-22 03:17:02 +00:00
|
|
|
|
|
|
|
|
|
class AppletPublication(JMSBaseModel):
|
2023-01-16 11:02:09 +00:00
|
|
|
|
applet = models.ForeignKey('Applet', on_delete=models.CASCADE, related_name='publications',
|
2022-12-07 07:09:01 +00:00
|
|
|
|
verbose_name=_('Applet'))
|
2023-01-16 11:02:09 +00:00
|
|
|
|
host = models.ForeignKey('AppletHost', on_delete=models.CASCADE, related_name='publications',
|
|
|
|
|
verbose_name=_('Hosting'))
|
|
|
|
|
status = models.CharField(max_length=16, default='pending', verbose_name=_('Status'))
|
2022-10-22 03:17:02 +00:00
|
|
|
|
comment = models.TextField(default='', blank=True, verbose_name=_('Comment'))
|
|
|
|
|
|
|
|
|
|
class Meta:
|
2022-10-25 04:57:34 +00:00
|
|
|
|
unique_together = ('applet', 'host')
|