2022-04-26 13:30:11 +00:00
|
|
|
# Generated by Django 3.1.14 on 2022-04-26 07:58
|
2022-04-28 04:47:39 +00:00
|
|
|
import uuid
|
2022-04-26 13:30:11 +00:00
|
|
|
|
|
|
|
from django.db import migrations
|
2022-04-28 04:47:39 +00:00
|
|
|
|
|
|
|
failed_apps = []
|
2022-04-26 13:30:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
def create_app_platform(apps, *args):
|
|
|
|
platform_model = apps.get_model('assets', 'Platform')
|
2022-04-28 04:47:39 +00:00
|
|
|
platforms = [
|
2022-04-26 13:30:11 +00:00
|
|
|
# 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'},
|
2022-04-28 04:47:39 +00:00
|
|
|
{'name': 'Kubernetes', 'category': 'cloud', 'type': 'k8s'},
|
2022-08-24 06:23:42 +00:00
|
|
|
{'name': 'Web', 'category': 'web', 'type': 'general'},
|
2022-04-26 13:30:11 +00:00
|
|
|
]
|
|
|
|
|
2022-04-28 04:47:39 +00:00
|
|
|
for platform in platforms:
|
|
|
|
platform['internal'] = True
|
|
|
|
print("Create platform: {}".format(platform['name']))
|
|
|
|
platform_model.objects.update_or_create(defaults=platform, name=platform['name'])
|
|
|
|
|
|
|
|
|
|
|
|
def get_prop_name_id(apps, app, category):
|
|
|
|
asset_model = apps.get_model('assets', 'Asset')
|
|
|
|
_id = app.id
|
|
|
|
id_exists = asset_model.objects.filter(id=_id).exists()
|
2022-08-24 06:23:42 +00:00
|
|
|
if id_exists:
|
2022-04-28 04:47:39 +00:00
|
|
|
_id = uuid.uuid4()
|
|
|
|
name = app.name
|
|
|
|
name_exists = asset_model.objects.filter(hostname=name).exists()
|
|
|
|
if name_exists:
|
|
|
|
name = category + '-' + app.name
|
|
|
|
return _id, name
|
2022-04-26 13:30:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
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}
|
|
|
|
|
|
|
|
for app in applications:
|
|
|
|
attrs = {'host': '', 'port': 0, 'database': ''}
|
|
|
|
_attrs = app.attrs or {}
|
|
|
|
attrs.update(_attrs)
|
2022-04-28 04:47:39 +00:00
|
|
|
|
2022-04-26 13:30:11 +00:00
|
|
|
db = db_model(
|
|
|
|
id=app.id, hostname=app.name, ip=attrs['host'],
|
|
|
|
protocols='{}/{}'.format(app.type, attrs['port']),
|
|
|
|
db_name=attrs['database'] or '',
|
|
|
|
platform=platforms_map[app.type],
|
|
|
|
org_id=app.org_id
|
|
|
|
)
|
2022-04-28 04:47:39 +00:00
|
|
|
try:
|
|
|
|
print("Create database: ", app.name)
|
|
|
|
db.save()
|
|
|
|
except:
|
|
|
|
failed_apps.append(app)
|
|
|
|
pass
|
2022-04-26 13:30:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
def migrate_cloud_to_asset(apps, *args):
|
2022-04-28 04:47:39 +00:00
|
|
|
app_model = apps.get_model('applications', 'Application')
|
|
|
|
cloud_model = apps.get_model('assets', 'Cloud')
|
|
|
|
platform_model = apps.get_model('assets', 'Platform')
|
|
|
|
|
|
|
|
applications = app_model.objects.filter(category='cloud')
|
|
|
|
platform = platform_model.objects.filter(type='k8s').first()
|
2022-08-30 02:07:03 +00:00
|
|
|
print()
|
2022-04-28 04:47:39 +00:00
|
|
|
|
|
|
|
for app in applications:
|
|
|
|
attrs = app.attrs
|
|
|
|
print("Create cloud: {}".format(app.name))
|
|
|
|
cloud = cloud_model(
|
|
|
|
id=app.id, hostname=app.name, ip='',
|
2022-04-28 14:54:18 +00:00
|
|
|
protocols='',
|
2022-04-28 04:47:39 +00:00
|
|
|
platform=platform,
|
|
|
|
org_id=app.org_id,
|
|
|
|
cluster=attrs.get('cluster', '')
|
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
|
|
|
cloud.save()
|
|
|
|
except Exception as e:
|
|
|
|
failed_apps.append(cloud)
|
|
|
|
print("Error: ", e)
|
|
|
|
|
|
|
|
|
|
|
|
def create_app_nodes(apps, org_id):
|
|
|
|
node_model = apps.get_model('assets', 'Node')
|
|
|
|
|
|
|
|
child_pattern = r'^[0-9]+:[0-9]+$'
|
|
|
|
node_keys = node_model.objects.filter(org_id=org_id) \
|
|
|
|
.filter(key__regex=child_pattern) \
|
|
|
|
.values_list('key', flat=True)
|
|
|
|
if not node_keys:
|
|
|
|
return
|
|
|
|
node_key_split = [key.split(':') for key in node_keys]
|
2022-06-16 03:32:36 +00:00
|
|
|
next_value = max([int(k[1]) for k in node_key_split]) + 1
|
2022-04-28 04:47:39 +00:00
|
|
|
parent_key = node_key_split[0][0]
|
2022-06-16 03:32:36 +00:00
|
|
|
next_key = '{}:{}'.format(parent_key, next_value)
|
2022-04-28 04:47:39 +00:00
|
|
|
name = 'Apps'
|
2022-06-16 03:32:36 +00:00
|
|
|
parent = node_model.objects.get(key=parent_key)
|
2022-04-28 04:47:39 +00:00
|
|
|
full_value = parent.full_value + '/' + name
|
|
|
|
defaults = {
|
|
|
|
'key': next_key, 'value': name, 'parent_key': parent_key,
|
|
|
|
'full_value': full_value, 'org_id': org_id
|
|
|
|
}
|
|
|
|
node, created = node_model.objects.get_or_create(
|
|
|
|
defaults=defaults, value=name, org_id=org_id,
|
|
|
|
)
|
|
|
|
node.parent = parent
|
|
|
|
return node
|
|
|
|
|
|
|
|
|
|
|
|
def migrate_to_nodes(apps, *args):
|
|
|
|
org_model = apps.get_model('orgs', 'Organization')
|
|
|
|
asset_model = apps.get_model('assets', 'Asset')
|
|
|
|
orgs = org_model.objects.all()
|
|
|
|
|
2022-06-16 03:32:36 +00:00
|
|
|
# Todo: 优化一些
|
2022-04-28 04:47:39 +00:00
|
|
|
for org in orgs:
|
|
|
|
node = create_app_nodes(apps, org.id)
|
|
|
|
assets = asset_model.objects.filter(
|
2022-08-24 08:14:32 +00:00
|
|
|
platform__category__in=['remote_app', 'database', 'cloud'],
|
2022-04-28 04:47:39 +00:00
|
|
|
org_id=org.id
|
|
|
|
)
|
2022-06-14 11:49:07 +00:00
|
|
|
if not node:
|
|
|
|
continue
|
2022-04-28 04:47:39 +00:00
|
|
|
print("Set node asset: ", node)
|
2022-06-14 11:49:07 +00:00
|
|
|
node.assets_amount = len(assets)
|
|
|
|
node.save()
|
|
|
|
node.assets.set(assets)
|
|
|
|
parent = node.parent
|
|
|
|
parent.assets_amount += len(assets)
|
|
|
|
parent.save()
|
2022-04-26 13:30:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
|
|
dependencies = [
|
2022-08-24 08:14:32 +00:00
|
|
|
('assets', '0096_auto_20220426_1550'),
|
2022-04-26 13:30:11 +00:00
|
|
|
('applications', '0020_auto_20220316_2028')
|
|
|
|
]
|
|
|
|
|
|
|
|
operations = [
|
2022-04-28 04:50:41 +00:00
|
|
|
migrations.RunPython(create_app_platform),
|
|
|
|
migrations.RunPython(migrate_database_to_asset),
|
|
|
|
migrations.RunPython(migrate_cloud_to_asset),
|
2022-04-28 04:47:39 +00:00
|
|
|
migrations.RunPython(migrate_to_nodes)
|
2022-04-26 13:30:11 +00:00
|
|
|
]
|