mirror of
https://github.com/jumpserver/jumpserver.git
synced 2026-04-02 18:32:34 +00:00
fix: 修改原来 platform 为 device 时,导致的 asset 类型不对
This commit is contained in:
37
utils/clean_host_to_device.py
Normal file
37
utils/clean_host_to_device.py
Normal file
@@ -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()
|
||||
Reference in New Issue
Block a user