jumpserver/apps/assets/migrations/0102_auto_20220803_1859.py

53 lines
1.6 KiB
Python
Raw Normal View History

2022-08-04 02:44:11 +00:00
# Generated by Django 3.2.14 on 2022-08-03 10:59
import time
from django.db import migrations
def migrate_asset_protocols(apps, schema_editor):
asset_model = apps.get_model('assets', 'Asset')
protocol_model = apps.get_model('assets', 'Protocol')
count = 0
bulk_size = 1000
2022-10-19 03:39:11 +00:00
print("\n\tStart migrate asset protocols")
2022-08-04 02:44:11 +00:00
while True:
start = time.time()
assets = asset_model.objects.all()[count:count+bulk_size]
if not assets:
break
2022-08-18 03:15:17 +00:00
count += len(assets)
2022-08-05 07:46:36 +00:00
assets_protocols = []
2022-09-20 05:54:25 +00:00
2022-08-04 02:44:11 +00:00
for asset in assets:
2022-09-20 05:54:25 +00:00
old_protocols = asset._protocols or '{}/{}'.format(asset.protocol, asset.port) or 'ssh/22'
if ',' in old_protocols:
_protocols = old_protocols.split(',')
else:
_protocols = old_protocols.split()
2022-08-05 07:46:36 +00:00
2022-09-20 05:54:25 +00:00
for name_port in _protocols:
2022-08-05 07:46:36 +00:00
name_port_list = name_port.split('/')
if len(name_port_list) != 2:
continue
name, port = name_port_list
2022-09-20 05:54:25 +00:00
protocol = protocol_model(**{'name': name, 'port': port, 'asset': asset})
assets_protocols.append(protocol)
protocol_model.objects.bulk_create(assets_protocols, ignore_conflicts=True)
2022-10-19 03:39:11 +00:00
print("\t - Create asset protocols: {}-{} using: {:.2f}s".format(
2022-08-18 03:15:17 +00:00
count - len(assets), count, time.time()-start
2022-08-04 02:44:11 +00:00
))
class Migration(migrations.Migration):
dependencies = [
2022-08-24 08:14:32 +00:00
('assets', '0101_auto_20220803_1448'),
2022-08-04 02:44:11 +00:00
]
operations = [
migrations.RunPython(migrate_asset_protocols)
]