diff --git a/setup/migrate.py b/setup/migrate.py index 3f581a82..9826d10f 100755 --- a/setup/migrate.py +++ b/setup/migrate.py @@ -197,55 +197,74 @@ def migration_15(env): import yaml with open(backup_configuration_file, 'r') as f: - custom_config = yaml.load(f, Loader=yaml.SafeLoader) - if not isinstance(custom_config, dict): raise ValueError() + old_configuration = yaml.load(f, Loader=yaml.SafeLoader) # Converting the previous configuration (which was not very clear) # into the new configuration format which also provides # a "type" attribute to distinguish the type of backup. - scheme = custom_config["target"].split(":")[0] - if scheme == "off": - custom_config = { - "type": "off" - } - elif scheme == "file": - custom_config = { - "type": "local", - "min_age_in_days": custom_config["min_age_in_days"] - } - elif scheme == "rsync": - custom_config = { - "type": "rsync", - "target_url": custom_config["target"], - "min_age_in_days": custom_config["min_age_in_days"] - } - elif scheme == "s3": - import urllib.parse - url = urllib.parse.urlparse(custom_config["target"]) - target_url = url.scheme + ":/" + url.path - s3_endpoint_url = "https://" + url.netloc - s3_access_key_id = custom_config["target_user"] - s3_secret_access_key = custom_config["target_pass"] - custom_config = { - "type": "s3", - "target_url": target_url, - "s3_endpoint_url": s3_endpoint_url, - "s3_access_key_id": custom_config["target_user"], - "s3_secret_access_key": custom_config["target_pass"], - "min_age_in_days": custom_config["min_age_in_days"] - } - elif scheme == "b2": - custom_config = { - "type": "b2", - "target_url": custom_config["target"], - "min_age_in_days": custom_config["min_age_in_days"] - } + conversion_succeded = True + if not isinstance(old_configuration, dict): + conversion_succeded = False + print("Exception when converting to the new configuration format: the previous configuration has an unexpected format.") + elif "type" in old_configuration: + print("The previous configuration appears to have already been converted.") + return else: - raise ValueError("Unexpected scheme during the conversion of the previous config to the new format.") + try: + scheme = old_configuration["target"].split(":")[0] + if scheme == "off": + new_configuration = { + "type": "off" + } + elif scheme == "file": + new_configuration = { + "type": "local", + "min_age_in_days": old_configuration["min_age_in_days"] + } + elif scheme == "rsync": + new_configuration = { + "type": "rsync", + "target_url": old_configuration["target"], + "min_age_in_days": old_configuration["min_age_in_days"] + } + elif scheme == "s3": + import urllib.parse + url = urllib.parse.urlparse(old_configuration["target"]) + target_url = url.scheme + ":/" + url.path + s3_endpoint_url = "https://" + url.netloc + new_configuration = { + "type": "s3", + "target_url": target_url, + "s3_endpoint_url": s3_endpoint_url, + "s3_access_key_id": old_configuration["target_user"], + "s3_secret_access_key": old_configuration["target_pass"], + "min_age_in_days": old_configuration["min_age_in_days"] + } + elif scheme == "b2": + new_configuration = { + "type": "b2", + "target_url": old_configuration["target"], + "min_age_in_days": old_configuration["min_age_in_days"] + } + else: + conversion_succeded = False + print("Exception when converting to the new configuration format: the previous configuration has an unexpected scheme.") + except KeyError: + conversion_succeded = False + print("Exception when converting to the new configuration format: the previous configuration is missing one or more keys.") + except: + conversion_succeded = False + print("Exception when converting to the new configuration format: generic exception.") - - with open(backup_configuration_file, "w") as f: - f.write(yaml.dump(custom_config)) + if conversion_succeded: + with open(backup_configuration_file, "w") as f: + f.write(yaml.dump(new_configuration)) + else: + with open(backup_configuration_file, "r") as f: + lines = list(map(lambda line:"# " + line, f.readlines())) + with open(backup_configuration_file, "w") as f: + f.writelines(lines) + print("The previous configuration inside the", backup_configuration_file, "file has been commented out.") ###########################################################