From e8d717d17422233a2fd3cfcf2a791a7000363ed7 Mon Sep 17 00:00:00 2001 From: wangruidong <940853815@qq.com> Date: Wed, 17 Apr 2024 19:59:23 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=B5=84=E4=BA=A7=E4=B8=8D=E5=AD=98?= =?UTF-8?q?=E5=9C=A8=E6=8C=87=E5=AE=9A=E7=94=A8=E6=88=B7=E7=9A=84=E6=97=B6?= =?UTF-8?q?=E5=80=99=EF=BC=8C=E6=B2=A1=E6=9C=89=E7=94=A8=E7=89=B9=E6=9D=83?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E4=BB=BB=E5=8A=A1=E6=9C=AA=E6=89=A7=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/ops/ansible/inventory.py | 9 +++++++-- apps/ops/models/job.py | 3 ++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/apps/ops/ansible/inventory.py b/apps/ops/ansible/inventory.py index 804f1ba18..963406e6a 100644 --- a/apps/ops/ansible/inventory.py +++ b/apps/ops/ansible/inventory.py @@ -214,13 +214,18 @@ class JMSInventory: ) return host - def get_asset_sorted_accounts(self, asset): - accounts = list(asset.accounts.filter(is_active=True)) + @staticmethod + def sorted_accounts(accounts): connectivity_score = {'ok': 2, '-': 1, 'err': 0} sort_key = lambda x: (x.privileged, connectivity_score.get(x.connectivity, 0), x.date_updated) accounts_sorted = sorted(accounts, key=sort_key, reverse=True) return accounts_sorted + def get_asset_sorted_accounts(self, asset): + accounts = list(asset.accounts.filter(is_active=True)) + accounts_sorted = self.sorted_accounts(accounts) + return accounts_sorted + @staticmethod def get_account_prefer(account_prefer): account_usernames = [] diff --git a/apps/ops/models/job.py b/apps/ops/models/job.py index cb81a565d..e7245568b 100644 --- a/apps/ops/models/job.py +++ b/apps/ops/models/job.py @@ -94,7 +94,8 @@ class JMSPermedInventory(JMSInventory): def get_asset_sorted_accounts(self, asset): accounts = self.assets_accounts_mapper.get(asset.id, []) - return list(accounts) + accounts_sorted = self.sorted_accounts(accounts) + return list(accounts_sorted) def get_assets_accounts_mapper(self): mapper = defaultdict(set)