From ae79584faa80491213fb9242029ddf253c017d83 Mon Sep 17 00:00:00 2001 From: Bai Date: Tue, 31 Jan 2023 18:07:55 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BF=AE=E6=94=B9=E8=B4=A6=E5=8F=B7?= =?UTF-8?q?=E5=88=97=E8=A1=A8=20secret=20=E5=AD=97=E6=AE=B5=E5=90=8D?= =?UTF-8?q?=E7=A7=B0=20=E5=AF=86=E9=92=A5/=E5=AF=86=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../migrations/0100_auto_20220711_1413.py | 76 +++++++++++++++++-- 1 file changed, 71 insertions(+), 5 deletions(-) diff --git a/apps/assets/migrations/0100_auto_20220711_1413.py b/apps/assets/migrations/0100_auto_20220711_1413.py index 62fa1cbd5..16e75cbae 100644 --- a/apps/assets/migrations/0100_auto_20220711_1413.py +++ b/apps/assets/migrations/0100_auto_20220711_1413.py @@ -1,17 +1,17 @@ # Generated by Django 3.2.12 on 2022-07-11 06:13 import time -from django.db import migrations +from django.db import migrations, models from assets.models import Platform -def migrate_accounts(apps, schema_editor): +def migrate_asset_accounts(apps, schema_editor): auth_book_model = apps.get_model('assets', 'AuthBook') account_model = apps.get_model('accounts', 'Account') count = 0 bulk_size = 1000 - print("\n\tStart migrate accounts") + print("\n\tStart migrate asset accounts") while True: start = time.time() auth_books = auth_book_model.objects \ @@ -71,11 +71,76 @@ def migrate_accounts(apps, schema_editor): accounts.append(account) account_model.objects.bulk_create(accounts, ignore_conflicts=True) - print("\t - Create accounts: {}-{} using: {:.2f}s".format( + print("\t - Create asset accounts: {}-{} using: {:.2f}s".format( count - len(auth_books), count, time.time() - start )) +def migrate_db_accounts(apps, schema_editor): + app_perm_model = apps.get_model('perms', 'ApplicationPermission') + account_model = apps.get_model('accounts', 'Account') + perms = app_perm_model.objects.filter(category__in=['db', 'cloud']) + + same_attrs = [ + 'id', 'username', 'comment', 'date_created', 'date_updated', + 'created_by', 'org_id', + ] + auth_attrs = ['password', 'private_key', 'token'] + all_attrs = same_attrs + auth_attrs + + print("\n\tStart migrate app accounts") + + index = 0 + total = perms.count() + + for perm in perms: + index += 1 + start = time.time() + + system_users = perm.system_users.all() + accounts = [] + for s in system_users: + values = {'version': 1} + values.update({attr: getattr(s, attr, '') for attr in all_attrs}) + values['created_by'] = str(s.id) + + auth_infos = [] + username = values['username'] + for attr in auth_attrs: + secret = values.pop(attr, None) + if not secret: + continue + + if attr == 'private_key': + secret_type = 'ssh_key' + name = f'{username}(ssh key)' + elif attr == 'token': + secret_type = 'token' + name = f'{username}(token)' + else: + secret_type = attr + name = username + auth_infos.append((name, secret_type, secret)) + + if not auth_infos: + auth_infos.append((username, 'password', '')) + + for name, secret_type, secret in auth_infos: + account = account_model(**values, name=name, secret=secret, secret_type=secret_type) + accounts.append(account) + + apps = perm.applications.all() + for app in apps: + for account in accounts: + setattr(account, 'asset_id', str(app.id)) + + account_model.objects.bulk_create(accounts, ignore_conflicts=True) + + print("\t - Progress ({}/{}), Create app accounts: {} using: {:.2f}s".format( + index, total, len(accounts), time.time() - start + )) + + class Migration(migrations.Migration): dependencies = [ ('accounts', '0001_initial'), @@ -83,5 +148,6 @@ class Migration(migrations.Migration): ] operations = [ - migrations.RunPython(migrate_accounts), + migrations.RunPython(migrate_asset_accounts), + migrations.RunPython(migrate_db_accounts), ]