Merge pull request #10232 from jumpserver/pr@dev@perf_platform_internal_create

perf: 修改 platforms
pull/10235/head
老广 2023-04-18 16:06:36 +08:00 committed by GitHub
commit 52891bfca3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 1308 additions and 14 deletions

View File

@ -1,3 +1,4 @@
import json
from collections import defaultdict
from copy import deepcopy
@ -270,7 +271,7 @@ class AllTypes(ChoicesMixin):
return data
@classmethod
def create_or_update_by_platform_data(cls, name, platform_data, platform_cls=None):
def create_or_update_by_platform_data(cls, platform_data, platform_cls=None):
# 不直接用 Platform 是因为可能在 migrations 中使用
from assets.models import Platform
if platform_cls is None:
@ -279,6 +280,7 @@ class AllTypes(ChoicesMixin):
automation_data = platform_data.pop('automation', {})
protocols_data = platform_data.pop('protocols', [])
name = platform_data['name']
platform, created = platform_cls.objects.update_or_create(
defaults=platform_data, name=name
)
@ -294,7 +296,6 @@ class AllTypes(ChoicesMixin):
platform.protocols.all().delete()
for p in protocols_data:
p.pop('primary', None)
platform.protocols.create(**p)
@classmethod
@ -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(name, 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):
@ -350,5 +352,6 @@ class AllTypes(ChoicesMixin):
for platform in user_platforms:
print("\t- Update platform: {}".format(platform.name))
platform_data = cls.get_type_default_platform(platform.category, platform.type)
cls.create_or_update_by_platform_data(platform.name, platform_data, platform_cls=platform_cls)
platform_data['name'] = platform.name
cls.create_or_update_by_platform_data(platform_data, platform_cls=platform_cls)
user_platforms.update(internal=False)

File diff suppressed because it is too large Load Diff

View File

@ -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')
@ -15,6 +13,9 @@ def migrate_platform_protocol_primary(apps, schema_editor):
platforms = platform_model.objects.all()
for platform in platforms:
p = platform.protocols.filter(primary=True).first()
if p:
continue
p = platform.protocols.first()
if not p:
continue
@ -22,9 +23,57 @@ 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')
AllTypes.create_or_update_internal_platforms(platform_cls)
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')
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_enabled = 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()
def migrate_web_login_button_error(apps, *args):
protocol_cls = apps.get_model('assets', 'PlatformProtocol')
protocols = protocol_cls.objects.filter(name='http')
for protocol in protocols:
submit_selector = protocol.setting.get('submit_selector', '')
submit_selector = submit_selector.replace('id=longin_button', 'id=login_button')
protocol.setting['submit_selector'] = submit_selector
protocol.save()
class Migration(migrations.Migration):
@ -45,5 +94,7 @@ class Migration(migrations.Migration):
),
migrations.RunPython(migrate_platform_charset),
migrations.RunPython(migrate_platform_protocol_primary),
migrations.RunPython(migrate_internal_platforms),
migrations.RunPython(migrate_winrm_for_win),
migrations.RunPython(migrate_device_platform_automation),
migrations.RunPython(migrate_web_login_button_error),
]

52
utils/diff_internal.py Normal file
View File

@ -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)