jumpserver/apps/assets/migrations/0099_auto_20220426_1558.py

76 lines
2.5 KiB
Python
Raw Normal View History

2022-04-26 13:30:11 +00:00
# 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),
]