From a86d5c14561ab1b8a69ffcebafb5b1d626c24e03 Mon Sep 17 00:00:00 2001 From: ibuler Date: Fri, 16 Sep 2022 11:45:50 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BF=AE=E6=94=B9=20models?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../migrations/0093_auto_20220403_1627.py | 16 +++++------- .../migrations/0099_auto_20220711_1409.py | 12 +++++++-- apps/assets/models/utils.py | 25 +++++++++++++------ .../host/change_password_linux/main.yml | 0 .../change_password/roles/linux/main.yml | 2 +- 5 files changed, 35 insertions(+), 20 deletions(-) delete mode 100644 apps/assets/playbooks/change_password/host/change_password_linux/main.yml diff --git a/apps/assets/migrations/0093_auto_20220403_1627.py b/apps/assets/migrations/0093_auto_20220403_1627.py index b013efdfe..0fe74ed50 100644 --- a/apps/assets/migrations/0093_auto_20220403_1627.py +++ b/apps/assets/migrations/0093_auto_20220403_1627.py @@ -9,19 +9,16 @@ def migrate_to_host(apps, schema_editor): host_model = apps.get_model("assets", 'Host') db_alias = schema_editor.connection.alias - created = 0 + count = 0 batch_size = 1000 while True: - start = created - end = created + batch_size - assets = asset_model.objects.using(db_alias).all()[start:end] + assets = asset_model.objects.using(db_alias).all()[count:count+batch_size] if not assets: break - + count += len(assets) hosts = [host_model(asset_ptr=asset) for asset in assets] host_model.objects.using(db_alias).bulk_create(hosts, ignore_conflicts=True) - created += len(hosts) def migrate_hardware_info(apps, *args): @@ -36,15 +33,14 @@ def migrate_hardware_info(apps, *args): ] while True: - start = count - end = count + batch_size - assets = asset_model.objects.all()[start:end] + assets = asset_model.objects.all()[count:count+batch_size] if not assets: break + count += len(assets) updated = [] for asset in assets: - info = {getattr(asset, field) for field in hardware_fields if getattr(asset, field)} + info = {field: getattr(asset, field) for field in hardware_fields if getattr(asset, field)} if not info: continue asset.info = info diff --git a/apps/assets/migrations/0099_auto_20220711_1409.py b/apps/assets/migrations/0099_auto_20220711_1409.py index 78495549c..7e5e7e0e6 100644 --- a/apps/assets/migrations/0099_auto_20220711_1409.py +++ b/apps/assets/migrations/0099_auto_20220711_1409.py @@ -39,7 +39,6 @@ class Migration(migrations.Migration): ('history_type', models.CharField(choices=[('+', 'Created'), ('~', 'Changed'), ('-', 'Deleted')], max_length=1)), ('asset', models.ForeignKey(blank=True, db_constraint=False, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='+', to='assets.asset', verbose_name='Asset')), ('history_user', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to=settings.AUTH_USER_MODEL)), - ('su_from', models.ForeignKey(blank=True, db_constraint=False, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='+', to='assets.account', verbose_name='Su from')), ], options={ 'verbose_name': 'historical Account', @@ -67,7 +66,6 @@ class Migration(migrations.Migration): ('privileged', models.BooleanField(default=False, verbose_name='Privileged')), ('version', models.IntegerField(default=0, verbose_name='Version')), ('asset', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='accounts', to='assets.asset', verbose_name='Asset')), - ('su_from', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='su_to', to='assets.account', verbose_name='Su from')), ], options={ 'verbose_name': 'Account', @@ -75,4 +73,14 @@ class Migration(migrations.Migration): 'unique_together': {('username', 'asset'), ('name', 'asset')}, }, ), + migrations.AddField( + model_name='account', + name='su_from', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='su_to', to='assets.account', verbose_name='Su from'), + ), + migrations.AddField( + model_name='historicalaccount', + name='su_from', + field=models.ForeignKey(blank=True, db_constraint=False, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='+', to='assets.account', verbose_name='Su from'), + ), ] diff --git a/apps/assets/models/utils.py b/apps/assets/models/utils.py index e2c89cd63..c8e8c6a08 100644 --- a/apps/assets/models/utils.py +++ b/apps/assets/models/utils.py @@ -36,9 +36,20 @@ def update_internal_platforms(platform_model): 'change_password_method': 'change_password_aix', }, {'name': 'Windows', 'category': 'host', 'type': 'windows'}, - {'name': 'Windows-TLS', 'category': 'host', 'type': 'windows'}, - {'name': 'Windows-RDP', 'category': 'host', 'type': 'windows'}, - + { + 'name': 'Windows-TLS', 'category': 'host', 'type': 'windows', + 'protocols': [ + {'name': 'rdp', 'port': 3389, 'setting': {'security': 'tls'}}, + {'name': 'ssh', 'port': 22}, + ] + }, + { + 'name': 'Windows-RDP', 'category': 'host', 'type': 'windows', + 'protocols': [ + {'name': 'rdp', 'port': 3389, 'setting': {'security': 'rdp'}}, + {'name': 'ssh', 'port': 22}, + ] + }, # 数据库 {'name': 'MySQL', 'category': 'database', 'type': 'mysql'}, {'name': 'PostgreSQL', 'category': 'database', 'type': 'postgresql'}, @@ -48,10 +59,10 @@ def update_internal_platforms(platform_model): {'name': 'Redis', 'category': 'database', 'type': 'redis'}, # 网络设备 - {'name': 'Generic', 'category': 'networking', 'type': 'general'}, - {'name': 'Huawei', 'category': 'networking', 'type': 'general'}, - {'name': 'Cisco', 'category': 'networking', 'type': 'general'}, - {'name': 'H3C', 'category': 'networking', 'type': 'general'}, + {'name': 'Generic', 'category': 'networking', 'type': 'general', 'brand': 'other'}, + {'name': 'Huawei', 'category': 'networking', 'type': 'general', 'brand': 'huawei'}, + {'name': 'Cisco', 'category': 'networking', 'type': 'general', 'brand': 'cisco'}, + {'name': 'H3C', 'category': 'networking', 'type': 'general', 'brand': 'h3c'}, # Web diff --git a/apps/assets/playbooks/change_password/host/change_password_linux/main.yml b/apps/assets/playbooks/change_password/host/change_password_linux/main.yml deleted file mode 100644 index e69de29bb..000000000 diff --git a/apps/assets/playbooks/strategy/change_password/roles/linux/main.yml b/apps/assets/playbooks/strategy/change_password/roles/linux/main.yml index cb229d9d6..16f0d1037 100644 --- a/apps/assets/playbooks/strategy/change_password/roles/linux/main.yml +++ b/apps/assets/playbooks/strategy/change_password/roles/linux/main.yml @@ -2,7 +2,7 @@ vars: connection_type: ssh password: - value: {{ password}} + value: {{ password }} public_key: value: {{ jms_key }} exclusive: {{ exclusive }}