diff --git a/apps/accounts/filters.py b/apps/accounts/filters.py index dc28d78aa..e71a5b9fb 100644 --- a/apps/accounts/filters.py +++ b/apps/accounts/filters.py @@ -14,6 +14,7 @@ class AccountFilterSet(BaseFilterSet): username = drf_filters.CharFilter(field_name="username", lookup_expr='exact') address = drf_filters.CharFilter(field_name="asset__address", lookup_expr='exact') asset_id = drf_filters.CharFilter(field_name="asset", lookup_expr='exact') + asset = drf_filters.CharFilter(field_name='asset', lookup_expr='exact') assets = drf_filters.CharFilter(field_name='asset_id', lookup_expr='exact') nodes = drf_filters.CharFilter(method='filter_nodes') node_id = drf_filters.CharFilter(method='filter_nodes') diff --git a/apps/terminal/models/applet/applet.py b/apps/terminal/models/applet/applet.py index 07d63bce1..ccd1a8e52 100644 --- a/apps/terminal/models/applet/applet.py +++ b/apps/terminal/models/applet/applet.py @@ -156,10 +156,11 @@ class Applet(JMSBaseModel): spec_label = asset.labels.filter(name__in=['AppletHost', '发布机']).first() if spec_label: - host = [host for host in hosts if host.name == spec_label.value] - if host: - return host[0] + matched = [host for host in hosts if host.name == spec_label.value] + if matched: + return matched[0] + hosts = [h for h in hosts if h.auto_create_accounts] prefer_key = 'applet_host_prefer_{}'.format(user.id) prefer_host_id = cache.get(prefer_key, None) pref_host = [host for host in hosts if host.id == prefer_host_id] @@ -213,22 +214,26 @@ class Applet(JMSBaseModel): private_account = valid_accounts.filter(username='js_{}'.format(user.username)).first() if not private_account: + logger.debug('Private account not found ...') return None # 优先使用 private account,支持并发或者不支持并发时,如果私有没有被占用,则使用私有 account = None # 如果都支持,不管私有是否被占用,都使用私有 if all_can_concurrent: + logger.debug('All can concurrent, use private account') account = private_account # 如果主机都不支持并发,则查询一下私有账号有没有任何应用使用,如果没有被使用,则使用私有 elif not host_can_concurrent: private_using_key = self.accounts_using_key_tmpl.format(host.id, private_account.username, '*') - private_is_using = len(cache.keys(private_using_key, [])) > 0 + private_is_using = len(cache.keys(private_using_key)) + logger.debug("Private account is using: {}".format(private_is_using)) 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) + logger.debug("Private account is using {} by {}".format(private_is_using_by_this_app, self.name)) if not private_is_using_by_this_app: account = private_account return account @@ -236,24 +241,32 @@ class Applet(JMSBaseModel): def select_host_account(self, user, asset): # 选择激活的发布机 host = self.select_host(user, asset) + logger.info('Select applet host: {}'.format(host.name)) if not host: return None 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: + logger.debug('No private account, try to use public account') account = self.select_a_public_account(user, host, valid_accounts) + if not account: + logger.debug('No available account for applet host: {}'.format(host.name)) + return None + ttl = 60 * 60 * 24 lock_key = self.accounts_using_key_tmpl.format(host.id, account.username, self.name) cache.set(lock_key, account.username, ttl) - return { + res = { 'host': host, 'account': account, 'lock_key': lock_key, 'ttl': ttl } + logger.debug('Select host and account: {}'.format(res)) + return res def delete(self, using=None, keep_parents=False): platform = self.get_related_platform()