Browse Source

fix: 修改原来 platform 为 device 时,导致的 asset 类型不对

pull/10779/head
ibuler 1 year ago committed by Jiangjie.Bai
parent
commit
4d4644dddd
  1. 3
      apps/assets/migrations/0093_auto_20220403_1627.py
  2. 21
      apps/assets/migrations/0098_auto_20220430_2126.py
  3. 3
      apps/notifications/signal_handlers.py
  4. 37
      utils/clean_host_to_device.py

3
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(

21
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),
]

3
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

37
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()
Loading…
Cancel
Save