Require output path for harbor.cfg->harbor.yml migration

Signed-off-by: Daniel Jiang <jiangd@vmware.com>
This commit is contained in:
Daniel Jiang 2019-05-09 13:44:43 +08:00
parent b4d766c8a8
commit 546bd44047
2 changed files with 44 additions and 18 deletions

View File

@ -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('--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.') 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() args = parser.parse_args()
if args.output_path is None:
args.output_path = args.input_path
if args.target_version is not None: if args.target_version is not None:
target_version = args.target_version target_version = args.target_version
input_version = utils.get_conf_version(args.input_path) input_version = utils.get_conf_version(args.input_path)
curr_dir = os.path.dirname(__file__) curr_dir = os.path.dirname(__file__)
chain = []
if input_version == target_version: if input_version == target_version:
print ("Version of input harbor.cfg is identical to target %s, no need to upgrade" % input_version) print ("Version of input harbor.cfg is identical to target %s, no need to upgrade" % input_version)
sys.exit(0) 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): if not search(curr_dir, input_version, target_version, chain):
print ("No migrator for version: %s" % input_version) print ("No migrator for version: %s" % input_version)
sys.exit(1) sys.exit(1)
@ -36,7 +40,6 @@ def main():
print ("input version: %s, migrator chain: %s" % (input_version, chain)) print ("input version: %s, migrator chain: %s" % (input_version, chain))
curr_input_path = args.input_path curr_input_path = args.input_path
for c in chain: for c in chain:
#TODO: more real-world testing needed for chained-migration.
m = importlib.import_module(to_module_path(c)) m = importlib.import_module(to_module_path(c))
curr_output_path = "harbor.cfg.%s.tmp" % c curr_output_path = "harbor.cfg.%s.tmp" % c
print("migrating to version %s" % c) print("migrating to version %s" % c)

View File

@ -1,6 +1,3 @@
import abc
import subprocess
from optparse import OptionParser
from shutil import copyfile from shutil import copyfile
import os import os
import sys import sys
@ -60,15 +57,38 @@ class DBMigrator():
class CfgMigrator(): class CfgMigrator():
def __init__(self, target, output): def __init__(self, target, output):
cfg_dir = "/harbor-migration/harbor-cfg"
cfg_out_dir = "/harbor-migration/harbor-cfg-out"
self.target = target self.target = target
self.output = output self.cfg_path = self.__config_filepath(cfg_dir)
self.cfg_path = "/harbor-migration/harbor-cfg/harbor.cfg" 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.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): def backup(self):
try: 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.") print ("Success to backup harbor.cfg.")
return True return True
except Exception as e: except Exception as e:
@ -76,11 +96,12 @@ class CfgMigrator():
return False return False
def restore(self): def restore(self):
if not os.path.exists(self.backup_path+"/harbor.cfg"): if not self.restore_src:
print ("Unable to restore as there is no harbor.cfg") print("unable to locate harbor config file in directory: %s" % self.backup_path)
return False return False
try: try:
copyfile(self.backup_path+"/harbor.cfg", self.cfg_path) copyfile(self.restore_src, self.restore_tgt)
print ("Success to restore harbor.cfg.") print ("Success to restore harbor.cfg.")
return True return True
except Exception as e: except Exception as e:
@ -92,21 +113,23 @@ class CfgMigrator():
print ("Skip cfg up as no harbor.cfg in the path.") print ("Skip cfg up as no harbor.cfg in the path.")
return True return True
if self.output and os.path.isdir(self.output_path): if self.output and os.path.isdir(self.output):
cmd = "python ./cfg/run.py --input " + self.cfg_path + " --output " + self.output_path + "/harbor.cfg" 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: else:
print ("The path of the migrated harbor.cfg is not set, the input file will be overwritten.") 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 cmd = "python ./cfg/run.py --input " + self.cfg_path
if self.target != '': if self.target != '':
cmd = cmd + " --target " + self.target cmd = cmd + " --target " + self.target
print("Command for config file migration: %s" % cmd)
return run_cmd(cmd) == 0 return run_cmd(cmd) == 0
def validate(self): def validate(self):
if not os.path.exists(self.cfg_path): 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 return False
print ("Success to validate harbor.cfg.")
return True return True
class Parameters(object): class Parameters(object):