2020-03-19 20:04:10 +01:00
|
|
|
import os, sys, importlib, shutil, glob
|
2020-04-01 09:35:49 +02:00
|
|
|
from packaging import version
|
2020-03-19 20:04:10 +01:00
|
|
|
|
|
|
|
import click
|
|
|
|
|
|
|
|
from utils.misc import get_realpath
|
2020-03-23 14:26:28 +01:00
|
|
|
from utils.migration import read_conf, search
|
2020-04-01 09:35:49 +02:00
|
|
|
from migrations import accept_versions
|
2020-03-19 20:04:10 +01:00
|
|
|
|
|
|
|
@click.command()
|
2020-04-01 10:51:13 +02:00
|
|
|
@click.option('-i', '--input', 'input_', required=True, help="The path of original config file")
|
2020-03-19 20:04:10 +01:00
|
|
|
@click.option('-o', '--output', default='', help="the path of output config file")
|
2020-07-31 09:54:45 +02:00
|
|
|
@click.option('-t', '--target', default='2.1.0', help="target version of input path")
|
2020-03-19 20:04:10 +01:00
|
|
|
def migrate(input_, output, target):
|
2020-04-01 10:51:13 +02:00
|
|
|
"""
|
|
|
|
migrate command will migrate config file style to specific version
|
2020-06-28 11:49:14 +02:00
|
|
|
:input_: is the path of the original config file
|
|
|
|
:output: is the destination path of config file, the generated configs will storage in it
|
|
|
|
:target: is the the target version of config file will upgrade to
|
2020-04-01 10:51:13 +02:00
|
|
|
"""
|
2020-04-01 09:35:49 +02:00
|
|
|
if target not in accept_versions:
|
|
|
|
click.echo('target version {} not supported'.format(target))
|
|
|
|
sys.exit(-1)
|
|
|
|
|
2020-03-20 08:20:32 +01:00
|
|
|
if not output:
|
|
|
|
output = input_
|
2020-03-19 20:04:10 +01:00
|
|
|
input_path = get_realpath(input_)
|
2020-03-20 08:20:32 +01:00
|
|
|
output_path = get_realpath(output)
|
2020-03-19 20:04:10 +01:00
|
|
|
|
|
|
|
configs = read_conf(input_path)
|
|
|
|
input_version = configs.get('_version')
|
2020-04-01 09:35:49 +02:00
|
|
|
if version.parse(input_version) < version.parse('1.9.0'):
|
|
|
|
click.echo('the version {} not supported, make sure the version in input file above 1.8.0'.format(input_version))
|
2020-04-01 10:51:13 +02:00
|
|
|
sys.exit(-1)
|
2020-03-19 20:04:10 +01:00
|
|
|
if input_version == target:
|
|
|
|
click.echo("Version of input harbor.yml is identical to target {}, no need to upgrade".format(input_version))
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
current_input_path = input_path
|
|
|
|
for m in search(input_version, target):
|
|
|
|
current_output_path = "harbor.yml.{}.tmp".format(m.revision)
|
|
|
|
click.echo("migrating to version {}".format(m.revision))
|
|
|
|
m.migrate(current_input_path, current_output_path)
|
|
|
|
current_input_path = current_output_path
|
|
|
|
shutil.copy(current_input_path, output_path)
|
2020-03-20 08:20:32 +01:00
|
|
|
click.echo("Written new values to {}".format(output))
|
2020-03-19 20:04:10 +01:00
|
|
|
for tmp_f in glob.glob("harbor.yml.*.tmp"):
|
|
|
|
os.remove(tmp_f)
|
|
|
|
|