diff --git a/apps/assets/migrations/0093_auto_20220403_1627.py b/apps/assets/migrations/0093_auto_20220403_1627.py index c34c70ed7..484da9740 100644 --- a/apps/assets/migrations/0093_auto_20220403_1627.py +++ b/apps/assets/migrations/0093_auto_20220403_1627.py @@ -2,6 +2,7 @@ import django.db from django.db import migrations, models + import common.db.fields @@ -118,7 +119,7 @@ class Migration(migrations.Migration): primary_key=True, serialize=False, to='assets.asset')), ], options={ - 'verbose_name': 'Host', + 'verbose_name': 'Host', }, ), migrations.CreateModel( diff --git a/apps/assets/migrations/0098_auto_20220430_2126.py b/apps/assets/migrations/0098_auto_20220430_2126.py index f388ca731..ed7de94e0 100644 --- a/apps/assets/migrations/0098_auto_20220430_2126.py +++ b/apps/assets/migrations/0098_auto_20220430_2126.py @@ -137,6 +137,24 @@ def migrate_to_nodes(apps, *args): parent.save() +def migrate_ori_host_to_devices(apps, *args): + device_model = apps.get_model('assets', 'Device') + asset_model = apps.get_model('assets', 'Asset') + host_model = apps.get_model('assets', 'Host') + hosts_need_migrate_to_device = host_model.objects.filter(asset_ptr__platform__category='device') + assets = asset_model.objects.filter(id__in=hosts_need_migrate_to_device.values_list('asset_ptr_id', flat=True)) + assets_map = {asset.id: asset for asset in assets} + + for host in hosts_need_migrate_to_device: + asset = assets_map.get(host.asset_ptr_id) + if not asset: + continue + device = device_model(asset_ptr_id=asset.id) + device.__dict__.update(asset.__dict__) + device.save() + host.delete(keep_parents=True) + + class Migration(migrations.Migration): dependencies = [ ('assets', '0097_auto_20220426_1558'), @@ -146,5 +164,6 @@ class Migration(migrations.Migration): operations = [ migrations.RunPython(migrate_database_to_asset), migrations.RunPython(migrate_cloud_to_asset), - migrations.RunPython(migrate_to_nodes) + migrations.RunPython(migrate_to_nodes), + migrations.RunPython(migrate_ori_host_to_devices), ] diff --git a/apps/notifications/signal_handlers.py b/apps/notifications/signal_handlers.py index 5b6b75a7e..797724755 100644 --- a/apps/notifications/signal_handlers.py +++ b/apps/notifications/signal_handlers.py @@ -72,8 +72,7 @@ def create_system_messages(app_config: AppConfig, **kwargs): sub, created = SystemMsgSubscription.objects.get_or_create(message_type=message_type) if created: obj.post_insert_to_db(sub) - logger.info( - f'Create SystemMsgSubscription: package={app_config.module.__package__} type={message_type}') + logger.info(f'Create MsgSubscription: package={app_config.module.__package__} type={message_type}') except ModuleNotFoundError: pass diff --git a/utils/clean_host_to_device.py b/utils/clean_host_to_device.py new file mode 100644 index 000000000..0bc767b6a --- /dev/null +++ b/utils/clean_host_to_device.py @@ -0,0 +1,37 @@ +import os +import sys + +import django + +if os.path.exists('../apps'): + sys.path.insert(0, '../apps') +elif os.path.exists('./apps'): + sys.path.insert(0, './apps') + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "jumpserver.settings") +django.setup() + +from assets.models import Asset as asset_model, Host as host_model, Device as device_model +from orgs.models import Organization + + +def clean_host(): + root = Organization.root() + root.change_to() + + devices = host_model.objects.filter(platform__category='device') + assets = asset_model.objects.filter(id__in=devices.values_list('asset_ptr_id', flat=True)) + assets_map = {asset.id: asset for asset in assets} + + for host in devices: + asset = assets_map.get(host.asset_ptr_id) + if not asset: + continue + device = device_model(asset_ptr_id=asset.id) + device.__dict__.update(asset.__dict__) + device.save() + host.delete(keep_parents=True) + + +if __name__ == "__main__": + clean_host()