From 546bd44047eb5dc9c4a0bf805f70c477e040dc2d Mon Sep 17 00:00:00 2001 From: Daniel Jiang Date: Thu, 9 May 2019 13:44:43 +0800 Subject: [PATCH] Require output path for harbor.cfg->harbor.yml migration Signed-off-by: Daniel Jiang --- tools/migration/cfg/run.py | 11 +++++--- tools/migration/migrator.py | 51 +++++++++++++++++++++++++++---------- 2 files changed, 44 insertions(+), 18 deletions(-) diff --git a/tools/migration/cfg/run.py b/tools/migration/cfg/run.py index 7a03030fd..2502e2f5f 100644 --- a/tools/migration/cfg/run.py +++ b/tools/migration/cfg/run.py @@ -19,16 +19,20 @@ def main(): parser.add_argument('--output','-o', action="store", dest='output_path', required=False, help='The path of the migrated harbor.cfg, if not set the input file will be overwritten') parser.add_argument('--target', action="store", dest='target_version', help='The target version that the harbor.cfg will be migrated to.') args = parser.parse_args() - if args.output_path is None: - args.output_path = args.input_path if args.target_version is not None: target_version = args.target_version input_version = utils.get_conf_version(args.input_path) curr_dir = os.path.dirname(__file__) - chain = [] if input_version == target_version: print ("Version of input harbor.cfg is identical to target %s, no need to upgrade" % input_version) sys.exit(0) + if args.output_path is None: + if float(input_version[:input_version.rfind('.')]) < 1.8 and \ + float(target_version[:target_version.rfind('.')]) >= 1.8: + print("Migrating config file from < 1.8.0 to >= 1.8.0, the output path cannot be empty") + sys.exit(1) + args.output_path = args.input_path + chain = [] if not search(curr_dir, input_version, target_version, chain): print ("No migrator for version: %s" % input_version) sys.exit(1) @@ -36,7 +40,6 @@ def main(): print ("input version: %s, migrator chain: %s" % (input_version, chain)) curr_input_path = args.input_path for c in chain: - #TODO: more real-world testing needed for chained-migration. m = importlib.import_module(to_module_path(c)) curr_output_path = "harbor.cfg.%s.tmp" % c print("migrating to version %s" % c) diff --git a/tools/migration/migrator.py b/tools/migration/migrator.py index 595fa15b2..eba38a7ec 100644 --- a/tools/migration/migrator.py +++ b/tools/migration/migrator.py @@ -1,6 +1,3 @@ -import abc -import subprocess -from optparse import OptionParser from shutil import copyfile import os import sys @@ -60,15 +57,38 @@ class DBMigrator(): class CfgMigrator(): def __init__(self, target, output): + cfg_dir = "/harbor-migration/harbor-cfg" + cfg_out_dir = "/harbor-migration/harbor-cfg-out" + self.target = target - self.output = output - self.cfg_path = "/harbor-migration/harbor-cfg/harbor.cfg" + self.cfg_path = self.__config_filepath(cfg_dir) + if not self.cfg_path: + self.cfg_path = os.path.join(cfg_dir, "harbor.cfg") + + if output: + self.output = output + elif self.__config_filepath(cfg_out_dir): + self.output = self.__config_filepath(cfg_out_dir) + elif os.path.isdir(cfg_out_dir): + self.output = os.path.join(cfg_out_dir, os.path.basename(self.cfg_path)) + else: + self.output = "" + self.backup_path = "/harbor-migration/backup" - self.output_path = "/harbor-migration/output" + self.restore_src = self.__config_filepath(self.backup_path) + self.restore_tgt = os.path.join(os.path.dirname(self.cfg_path), os.path.basename(self.restore_src)) + + @staticmethod + def __config_filepath(d): + if os.path.isfile(os.path.join(d, "harbor.yml")): + return os.path.join(d, "harbor.yml") + elif os.path.isfile(os.path.join(d, "harbor.cfg")): + return os.path.join(d, "harbor.cfg") + return "" def backup(self): try: - copyfile(self.cfg_path, self.backup_path+"/harbor.cfg") + copyfile(self.cfg_path, os.path.join(self.backup_path, os.path.basename(self.cfg_path))) print ("Success to backup harbor.cfg.") return True except Exception as e: @@ -76,11 +96,12 @@ class CfgMigrator(): return False def restore(self): - if not os.path.exists(self.backup_path+"/harbor.cfg"): - print ("Unable to restore as there is no harbor.cfg") + if not self.restore_src: + print("unable to locate harbor config file in directory: %s" % self.backup_path) return False + try: - copyfile(self.backup_path+"/harbor.cfg", self.cfg_path) + copyfile(self.restore_src, self.restore_tgt) print ("Success to restore harbor.cfg.") return True except Exception as e: @@ -92,21 +113,23 @@ class CfgMigrator(): print ("Skip cfg up as no harbor.cfg in the path.") return True - if self.output and os.path.isdir(self.output_path): - cmd = "python ./cfg/run.py --input " + self.cfg_path + " --output " + self.output_path + "/harbor.cfg" + if self.output and os.path.isdir(self.output): + cmd = "python ./cfg/run.py --input " + self.cfg_path + " --output " + os.path.join(self.output, os.path.basename(self.cfg_path)) + elif self.output and os.path.isfile(self.output): + cmd = "python ./cfg/run.py --input " + self.cfg_path + " --output " + self.output else: print ("The path of the migrated harbor.cfg is not set, the input file will be overwritten.") cmd = "python ./cfg/run.py --input " + self.cfg_path if self.target != '': cmd = cmd + " --target " + self.target + print("Command for config file migration: %s" % cmd) return run_cmd(cmd) == 0 def validate(self): if not os.path.exists(self.cfg_path): - print ("Unable to loacte the harbor.cfg, please check.") + print ("Unable to locate the input harbor configuration file: %s, please check." % self.cfg_path) return False - print ("Success to validate harbor.cfg.") return True class Parameters(object):