perf: 其他 asset model

pull/8605/head
ibuler 2022-04-26 21:30:11 +08:00
parent 0a2b2ad127
commit 770d2508d7
2 changed files with 122 additions and 0 deletions

View File

@ -0,0 +1,47 @@
# Generated by Django 3.1.14 on 2022-04-26 07:54
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('assets', '0097_auto_20220407_1726'),
]
operations = [
migrations.CreateModel(
name='Database',
fields=[
('asset_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='assets.asset')),
('db_name', models.CharField(blank=True, max_length=1024, verbose_name='Database')),
],
options={
'verbose_name': 'Database',
},
bases=('assets.asset',),
),
migrations.CreateModel(
name='Network',
fields=[
('asset_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='assets.asset')),
],
options={
'abstract': False,
},
bases=('assets.asset',),
),
migrations.CreateModel(
name='RemoteApp',
fields=[
('asset_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='assets.asset')),
('app_path', models.CharField(max_length=1024, verbose_name='App path')),
('attrs', models.JSONField(default=dict, verbose_name='Attrs')),
],
options={
'abstract': False,
},
bases=('assets.asset',),
),
]

View File

@ -0,0 +1,75 @@
# Generated by Django 3.1.14 on 2022-04-26 07:58
from django.db import migrations
from django.db.utils import IntegrityError
def create_app_platform(apps, *args):
platform_model = apps.get_model('assets', 'Platform')
apps = [
# DB
{'name': 'MySQL', 'category': 'database', 'type': 'mysql'},
{'name': 'MariaDB', 'category': 'database', 'type': 'mariadb'},
{'name': 'PostgreSQL', 'category': 'database', 'type': 'postgresql'},
{'name': 'Oracle', 'category': 'database', 'type': 'oracle'},
{'name': 'SQLServer', 'category': 'database', 'type': 'sqlserver'},
{'name': 'MongoDB', 'category': 'database', 'type': 'mongodb'},
{'name': 'Redis', 'category': 'database', 'type': 'redis'},
{'name': 'RemoteApp', 'category': 'remote_app', 'type': 'remote_app'},
{'name': 'Kubernetes', 'category': 'cloud', 'type': 'kubernetes'},
]
for app in apps:
app['internal'] = True
platform_model.objects.update_or_create(defaults=app, name=app['name'])
def migrate_database_to_asset(apps, *args):
app_model = apps.get_model('applications', 'Application')
db_model = apps.get_model('assets', 'Database')
platform_model = apps.get_model('assets', 'Platform')
applications = app_model.objects.filter(category='db')
platforms = platform_model.objects.all()
platforms_map = {p.type: p for p in platforms}
dbs = []
for app in applications:
attrs = {'host': '', 'port': 0, 'database': ''}
_attrs = app.attrs or {}
attrs.update(_attrs)
print("Create: ", app.name)
db = db_model(
id=app.id, hostname=app.name, ip=attrs['host'],
protocols='{}/{}'.format(app.type, attrs['port']),
category='database', type=app.type,
db_name=attrs['database'] or '',
platform=platforms_map[app.type],
org_id=app.org_id
)
for i in range(3):
try:
db.save()
break
except IntegrityError:
db.name = 'DB-' + db.name
def migrate_remote_app_to_asset(apps, *args):
pass
def migrate_cloud_to_asset(apps, *args):
pass
class Migration(migrations.Migration):
dependencies = [
('assets', '0098_auto_20220426_1550'),
('applications', '0020_auto_20220316_2028')
]
operations = [
migrations.RunPython(create_app_platform),
migrations.RunPython(migrate_database_to_asset),
]