perf: push account (#9020)

Co-authored-by: feng <1304903146@qq.com>
pull/9023/head
fit2bot 2022-11-04 19:18:15 +08:00 committed by GitHub
parent a255bd21b4
commit dca92a1e04
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 19 additions and 15 deletions

View File

@ -23,6 +23,7 @@ class PushOrVerifyHostCallbackMixin:
execution: callable
host_account_mapper: dict
ignore_account: bool
need_privilege_account: bool
generate_public_key: callable
generate_private_key_path: callable
@ -32,7 +33,7 @@ class PushOrVerifyHostCallbackMixin:
return host
accounts = asset.accounts.all()
if self.ignore_account and account:
if self.need_privilege_account and accounts.count() > 1 and account:
accounts = accounts.exclude(id=account.id)
if '*' not in self.execution.snapshot['accounts']:
@ -114,9 +115,9 @@ class BasePlaybookManager:
method_attr = '{}_method'.format(self.__class__.method_type())
method_enabled = automation and \
getattr(automation, enabled_attr) and \
getattr(automation, method_attr) and \
getattr(automation, method_attr) in self.method_id_meta_mapper
getattr(automation, enabled_attr) and \
getattr(automation, method_attr) and \
getattr(automation, method_attr) in self.method_id_meta_mapper
if not method_enabled:
host['error'] = _('{} disabled'.format(self.__class__.method_type()))
@ -198,6 +199,9 @@ class BasePlaybookManager:
result = cb.host_results.get(host)
if state == 'ok':
self.on_host_success(host, result)
elif state == 'skipped':
# TODO
print('skipped: ', hosts)
else:
error = hosts.get(host)
self.on_host_error(host, error, result)

View File

@ -2,8 +2,8 @@
gather_facts: no
tasks:
- name: Add user account.username
ansible.builtin.user:
name: "{{ account.username }}"
ansible.builtin.user:
name: "{{ account.username }}"
- name: Set account.username password
ansible.builtin.user:

View File

@ -6,7 +6,7 @@ logger = get_logger(__name__)
class PushAccountManager(PushOrVerifyHostCallbackMixin, BasePlaybookManager):
ignore_account = True
need_privilege_account = True
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

View File

@ -6,7 +6,7 @@ logger = get_logger(__name__)
class VerifyAccountManager(PushOrVerifyHostCallbackMixin, BasePlaybookManager):
ignore_account = False
need_privilege_account = False
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

View File

@ -9,7 +9,7 @@ __all__ = ['PushAccountAutomation']
class PushAccountAutomation(BaseAutomation):
def save(self, *args, **kwargs):
self.type = AutomationTypes.verify_account
self.type = AutomationTypes.push_account
super().save(*args, **kwargs)
class Meta:

View File

@ -65,8 +65,8 @@ class AssetSerializer(OrgResourceSerializerMixin, WritableNestedModelSerializer)
platform = ObjectRelatedField(required=False, queryset=Platform.objects, label=_('Platform'))
nodes = ObjectRelatedField(many=True, required=False, queryset=Node.objects, label=_('Nodes'))
labels = AssetLabelSerializer(many=True, required=False, label=_('Labels'))
accounts = AssetAccountSerializer(many=True, required=False, label=_('Accounts'))
protocols = AssetProtocolsSerializer(many=True, required=False, label=_('Protocols'))
accounts = AssetAccountSerializer(many=True, required=False, label=_('Accounts'))
class Meta:
model = Asset
@ -74,7 +74,7 @@ class AssetSerializer(OrgResourceSerializerMixin, WritableNestedModelSerializer)
fields_small = fields_mini + ['is_active', 'comment']
fields_fk = ['domain', 'platform', 'platform']
fields_m2m = [
'nodes', 'labels', 'accounts', 'protocols', 'nodes_display',
'nodes', 'labels', 'protocols', 'accounts', 'nodes_display',
]
read_only_fields = [
'category', 'type', 'specific',

View File

@ -31,7 +31,7 @@ def push_accounts_to_assets_util(accounts, assets):
def push_accounts_to_assets(account_ids, asset_ids):
from assets.models import Asset, Account
with tmp_to_root_org():
assets = Asset.objects.get(id=asset_ids)
accounts = Account.objects.get(id=account_ids)
assets = Asset.objects.filter(id__in=asset_ids)
accounts = Account.objects.filter(id__in=account_ids)
return push_accounts_to_assets_util(accounts, assets)

View File

@ -30,8 +30,8 @@ def verify_accounts_connectivity_util(accounts, assets, task_name):
def verify_accounts_connectivity(account_ids, asset_ids):
from assets.models import Asset, Account
with tmp_to_root_org():
assets = Asset.objects.get(id=asset_ids)
accounts = Account.objects.get(id=account_ids)
assets = Asset.objects.filter(id__in=asset_ids)
accounts = Account.objects.filter(id__in=account_ids)
task_name = gettext_noop("Verify accounts connectivity")
return verify_accounts_connectivity_util(accounts, assets, task_name)