From 4e5a44bd988c9c62df0a9765e9e59dea9aee4f38 Mon Sep 17 00:00:00 2001 From: fit2bot <68588906+fit2bot@users.noreply.github.com> Date: Tue, 14 Nov 2023 14:50:33 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=B4=A6=E5=8F=B7=E6=94=B6=E9=9B=86?= =?UTF-8?q?=E9=80=9A=E7=9F=A5=20=E5=90=8C=E6=AD=A5=E8=B5=84=E4=BA=A7?= =?UTF-8?q?=E6=97=B6=20=E8=AE=A1=E7=AE=97=E6=96=B0=E5=A2=9E=E8=B4=A6?= =?UTF-8?q?=E5=8F=B7=E9=94=99=E8=AF=AF=20(#12101)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: feng <1304903146@qq.com> --- .../accounts/automations/gather_accounts/manager.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/apps/accounts/automations/gather_accounts/manager.py b/apps/accounts/automations/gather_accounts/manager.py index 3bc085850..c3af9ffe8 100644 --- a/apps/accounts/automations/gather_accounts/manager.py +++ b/apps/accounts/automations/gather_accounts/manager.py @@ -17,6 +17,7 @@ class GatherAccountsManager(AccountBasePlaybookManager): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.host_asset_mapper = {} + self.gathered_accounts = [] self.asset_username_mapper = defaultdict(set) self.is_sync_account = self.execution.snapshot.get('is_sync_account') @@ -48,17 +49,13 @@ class GatherAccountsManager(AccountBasePlaybookManager): def update_or_create_accounts(self, asset, result): data = self.generate_data(asset, result) with tmp_to_org(asset.org_id): - gathered_accounts = [] GatheredAccount.objects.filter(asset=asset, present=True).update(present=False) for d in data: username = d['username'] gathered_account, __ = GatheredAccount.objects.update_or_create( defaults=d, asset=asset, username=username, ) - gathered_accounts.append(gathered_account) - if not self.is_sync_account: - return - GatheredAccount.sync_accounts(gathered_accounts) + self.gathered_accounts.append(gathered_account) def on_host_success(self, host, result): info = result.get('debug', {}).get('res', {}).get('info', {}) @@ -72,6 +69,9 @@ class GatherAccountsManager(AccountBasePlaybookManager): def run(self, *args, **kwargs): super().run(*args, **kwargs) self.send_email_if_need() + if not self.is_sync_account: + return + GatheredAccount.sync_accounts(self.gathered_accounts) def send_email_if_need(self): recipients = self.execution.recipients @@ -102,6 +102,9 @@ class GatherAccountsManager(AccountBasePlaybookManager): remove_usernames = system_usernames - usernames k = f'{asset_id_map[asset_id]}[{asset_id}]' + if not add_usernames and not remove_usernames: + continue + change_info[k] = { 'add_usernames': ', '.join(add_usernames), 'remove_usernames': ', '.join(remove_usernames),