From ce38b2263c259e6b8ce35084381ea1c435775b12 Mon Sep 17 00:00:00 2001 From: ibuler Date: Tue, 18 Apr 2023 14:02:24 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BF=AE=E6=94=B9=20platforms?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/assets/const/diff_internal.py | 52 +++++++++++++++++++ apps/assets/const/types.py | 12 +++-- .../migrations/0111_auto_20230321_1633.py | 45 ++++++++++++++-- 3 files changed, 100 insertions(+), 9 deletions(-) create mode 100644 apps/assets/const/diff_internal.py diff --git a/apps/assets/const/diff_internal.py b/apps/assets/const/diff_internal.py new file mode 100644 index 000000000..3f47ee07b --- /dev/null +++ b/apps/assets/const/diff_internal.py @@ -0,0 +1,52 @@ +import difflib +import json +import sys + + +def diff_list(f1, f2): + with open(f1) as f: + data1 = json.load(f) + + data1_mapper = { + d['name']: d for d in data1 + } + with open(f2) as f: + data2 = json.load(f) + + data2_mapper = { + d['name']: d for d in data2 + } + + d1_names = set(data1_mapper.keys()) + d2_names = set(data2_mapper.keys()) + + diff_names = d1_names - d2_names + if diff_names: + print("Diff Names1: ", diff_names) + + diff_names = d2_names - d1_names + if diff_names: + print("Diff Names2: ", diff_names) + + for name, data in data1_mapper.items(): + if name not in data2_mapper: + continue + data2 = data2_mapper[name] + print("Diff: ", name) + diff = difflib.unified_diff( + json.dumps(data, indent=4, sort_keys=True).splitlines(), + json.dumps(data2, indent=4, sort_keys=True).splitlines() + ) + print('\n'.join(diff)) + print() + + +if __name__ == '__main__': + if len(sys.argv) != 3: + print('Usage: python diff.py file1 file2') + sys.exit(1) + + f1 = sys.argv[1] + f2 = sys.argv[2] + + diff = diff_list(f1, f2) diff --git a/apps/assets/const/types.py b/apps/assets/const/types.py index cdf421296..ce2ef8916 100644 --- a/apps/assets/const/types.py +++ b/apps/assets/const/types.py @@ -1,3 +1,4 @@ +import json from collections import defaultdict from copy import deepcopy @@ -302,20 +303,20 @@ class AllTypes(ChoicesMixin): if platform_cls is None: platform_cls = cls - print("\n\tCreate internal platforms") + # print("\n\tCreate internal platforms") for category, type_cls in cls.category_types(): - print("\t## Category: {}".format(category.label)) + # print("\t## Category: {}".format(category.label)) data = type_cls.internal_platforms() for tp, platform_datas in data.items(): - print("\t >> Type: {}".format(tp.label)) + # print("\t >> Type: {}".format(tp.label)) default_platform_data = cls.get_type_default_platform(category, tp) default_automation = default_platform_data.pop('automation', {}) default_protocols = default_platform_data.pop('protocols', []) for d in platform_datas: name = d['name'] - print("\t - Platform: {}".format(name)) + # print("\t - Platform: {}".format(name)) _automation = d.pop('automation', {}) _protocols = d.pop('_protocols', []) _protocols_setting = d.pop('protocols_setting', {}) @@ -335,7 +336,8 @@ class AllTypes(ChoicesMixin): 'automation': {**default_automation, **_automation}, 'protocols': protocols_data } - cls.create_or_update_by_platform_data(platform_data, platform_cls=platform_cls) + print(json.dumps(platform_data, indent=4)) + # cls.create_or_update_by_platform_data(platform_data, platform_cls=platform_cls) @classmethod def update_user_create_platforms(cls, platform_cls): diff --git a/apps/assets/migrations/0111_auto_20230321_1633.py b/apps/assets/migrations/0111_auto_20230321_1633.py index 1acac984a..2688287e5 100644 --- a/apps/assets/migrations/0111_auto_20230321_1633.py +++ b/apps/assets/migrations/0111_auto_20230321_1633.py @@ -2,8 +2,6 @@ from django.db import migrations, models -from assets.const import AllTypes - def migrate_platform_charset(apps, schema_editor): platform_model = apps.get_model('assets', 'Platform') @@ -25,9 +23,46 @@ def migrate_platform_protocol_primary(apps, schema_editor): p.save() -def migrate_internal_platforms(apps, schema_editor): +def migrate_winrm_for_win(apps, *args): + platform_cls = apps.get_model('assets', 'Platform') + windows_name = ['Windows', 'Windows-TLS', 'Windows-RDP'] + windows = platform_cls.objects.filter(name__in=windows_name) + for platform in windows: + if platform.protocols.filter(name='winrm').exists(): + continue + data = { + 'name': 'winrm', + 'port': 5985, + 'primary': False, + 'public': False, + 'required': False, + 'default': False, + 'setting': {"use_ssl": False} + } + platform.protocols.create(**data) + + +def migrate_device_platform_automation(apps, *args): platform_cls = apps.get_model('assets', 'Platform') - AllTypes.create_or_update_internal_platforms(platform_cls) + names = ['General', 'Cisco', 'H3C', 'Huawei'] + platforms = platform_cls.objects.filter(name__in=names, category='device') + + for platform in platforms: + automation = getattr(platform, 'automation', None) + if not automation: + continue + automation.ansible_config = { + "ansible_connection": "local", + "first_connect_delay": 0.5, + } + automation.ansible_enable = True + automation.change_secret_enabled = True + automation.change_secret_method = "change_secret_by_ssh" + automation.ping_enabled = True + automation.ping_method = "ping_by_ssh" + automation.verify_account_enabled = True + automation.verify_account_method = "verify_account_by_ssh" + automation.save() class Migration(migrations.Migration): @@ -48,4 +83,6 @@ class Migration(migrations.Migration): ), migrations.RunPython(migrate_platform_charset), migrations.RunPython(migrate_platform_protocol_primary), + migrations.RunPython(migrate_winrm_for_win), + migrations.RunPython(migrate_device_platform_automation), ]