2020-03-19 20:04:10 +01:00
|
|
|
import yaml
|
|
|
|
import click
|
|
|
|
import importlib
|
2020-03-23 14:26:28 +01:00
|
|
|
import os
|
2020-03-23 08:32:15 +01:00
|
|
|
from collections import deque
|
|
|
|
|
2020-03-23 14:26:28 +01:00
|
|
|
from migrations import MIGRATION_BASE_DIR
|
2020-03-19 20:04:10 +01:00
|
|
|
|
|
|
|
def read_conf(path):
|
|
|
|
with open(path) as f:
|
|
|
|
try:
|
|
|
|
d = yaml.safe_load(f)
|
|
|
|
except Exception as e:
|
|
|
|
click.echo("parse config file err, make sure your harbor config version is above 1.8.0", e)
|
|
|
|
exit(-1)
|
|
|
|
return d
|
|
|
|
|
2020-03-23 08:32:15 +01:00
|
|
|
def _to_module_path(ver):
|
2020-03-23 14:26:28 +01:00
|
|
|
return "migrations.version_{}".format(ver.replace(".","_"))
|
2020-03-19 20:04:10 +01:00
|
|
|
|
2020-03-23 08:32:15 +01:00
|
|
|
def search(input_ver: str, target_ver: str) -> deque :
|
2020-03-20 08:20:32 +01:00
|
|
|
"""
|
|
|
|
Search accept a input version and the target version.
|
|
|
|
Returns the module of migrations in the upgrade path
|
|
|
|
"""
|
2020-03-23 08:32:15 +01:00
|
|
|
upgrade_path, visited = deque(), set()
|
|
|
|
while True:
|
|
|
|
module_path = _to_module_path(target_ver)
|
|
|
|
visited.add(target_ver) # mark current version for loop finding
|
2020-03-23 14:26:28 +01:00
|
|
|
if os.path.isdir(os.path.join(MIGRATION_BASE_DIR, 'version_{}'.format(target_ver.replace(".","_")))):
|
2020-03-23 08:32:15 +01:00
|
|
|
module = importlib.import_module(module_path)
|
|
|
|
if module.revision == input_ver: # migration path found
|
|
|
|
break
|
|
|
|
elif module.down_revision is None: # migration path not found
|
|
|
|
raise Exception('no migration path found')
|
2020-03-19 20:04:10 +01:00
|
|
|
else:
|
2020-03-23 08:32:15 +01:00
|
|
|
upgrade_path.appendleft(module)
|
|
|
|
target_ver = module.down_revision
|
|
|
|
if target_ver in visited: # version visited before, loop found
|
|
|
|
raise Exception('find a loop caused by {} on migration path'.format(target_ver))
|
|
|
|
else:
|
2020-03-23 14:26:28 +01:00
|
|
|
raise Exception('{} not dir'.format(os.path.join(MIGRATION_BASE_DIR, 'versions', target_ver.replace(".","_"))))
|
2020-03-23 08:32:15 +01:00
|
|
|
return upgrade_path
|