From 9765596a9c783874f95948fb71d2f5582e912efe Mon Sep 17 00:00:00 2001 From: sergey kryazhev Date: Sun, 3 Mar 2024 22:20:55 +0300 Subject: [PATCH] formatting --- scripts/migrate.py | 162 ++++++++++++++++++++++++++++++--------------- 1 file changed, 107 insertions(+), 55 deletions(-) diff --git a/scripts/migrate.py b/scripts/migrate.py index 999cae493..f12b0194c 100644 --- a/scripts/migrate.py +++ b/scripts/migrate.py @@ -7,86 +7,138 @@ import requests import yaml -KubemarineVersions:list = ["0.17.0","0.18.0","0.18.1","0.18.2","0.19.0","0.20.0","0.21.0","0.21.1","0.22.0","v0.23.0","v0.24.1","v0.25.0","v0.25.1","v0.26.0","v0.27.0"] #TODO to get from github/gitlab/git/custom/file? - -Envs: list = ["src", "pip", "bin", "docker"] +# TODO +# update /etc/kubemarine/procedures/latest_dump/version every migrate even when no patches +# software upgrade patches --describe doesn't print exact 3rd party version, only patched k8s + +KubemarineVersions: list = [ + "0.17.0", + "0.18.0", + "0.18.1", + "0.18.2", + "0.19.0", + "0.20.0", + "0.21.0", + "0.21.1", + "0.22.0", + "v0.23.0", + "v0.24.1", + "v0.25.0", + "v0.25.1", + "v0.26.0", + "v0.27.0", +] # TODO to get from github/gitlab/git/custom/file? + +Envs: list = ["src", "pip", "bin", "docker", "brew"] MigrationProcedure: dict = { -# "v0.25.1":{"procedure":{}, -# "patches":[]}, -# "v0.26.0":{"procedure":{}, -# "patches":[ -# {"patch0": -# f"Description0"}} + # "v0.25.1":{"procedure":"", + # "patches":[], + # "env":"bin"} + # "v0.26.0":{"procedure":"", + # "patches":[ + # {"patch0": + # f"Description0"}, + # "env":"docker"} } -def migrate_kubemarine(toVersion, env, procedureyaml, patches ,clusteryaml): - print(locals()) - pass - - -def distant_migrate(): - clusteryaml = "" - for plan in MigrationProcedure: - toVersion, env = next(iter(plan.items())) - env = get_patches_info(toVersion, env) - clusteryaml = migrate_kubemarine(toVersion, - env, - MigrationProcedure[toVersion]["procedure"], - MigrationProcedure[toVersion]["patches"], - clusteryaml) - -def get_patches_info(version, env): - patches_info = {"patches":[]} +def distant_migrate(MigrationProcedure): + MigrationProcedure = json.loads(MigrationProcedure) + for version in MigrationProcedure: + if MigrationProcedure[version]["patches"]: + path = download_kubemarine(version, "bin") # TODO check cache + print(version, path, get_patches_info(path)) # + if MigrationProcedure[version]["procedure"]: + with open("procedure.yaml", "w") as file: + file.write(MigrationProcedure[version]["procedure"]) + process = subprocess.Popen( + [path, "migrate_kubemarine", "procedure.yaml"], + stdout=subprocess.PIPE, text=True, + ) + for line in process.stdout: + print(line.strip()) + process.wait() + os.remove("procedure.yaml") if os.path.exists("procedure.yaml") else None + else: + print(f"No patches. Skipping migration for {version}") + + # clusteryaml = migrate_kubemarine(toVersion, + # env, + # MigrationProcedure[toVersion]["procedure"], + # MigrationProcedure[toVersion]["patches"], + # clusteryaml) + + +def download_kubemarine(version, env): + filename = f"kubemarine-{'macos11' if platform.system().lower() == 'darwin' else 'linux'}-{platform.machine().lower()}" + filepath = os.path.join(tempfile.gettempdir(), filename + f"-{version}") if env in "bin": - #TODO kubemarine-win64.exe support - filename = f"kubemarine-{'macos11' if platform.system().lower() == 'darwin' else 'linux'}-{platform.machine().lower()}" - filepath = os.path.join(tempfile.gettempdir(),filename) - try: # Download the file - response = requests.get(f"https://github.com/Netcracker/KubeMarine/releases/download/{version}/{filename}") - response.raise_for_status() - + response = requests.get( + f"https://github.com/Netcracker/KubeMarine/releases/download/{version}/{filename}" + ) + response.raise_for_status() + with open(filepath, "wb") as file: file.write(response.content) - - os.chmod(filepath, os.stat(filepath).st_mode | 0o111) # set executable - + + os.chmod(filepath, os.stat(filepath).st_mode | 0o111) # Set executable + return filepath + except Exception as e: + print(f"Error: {e}") + return None + return None + + +def get_patches_info(filepath): + patches_info = {"patches": []} + + try: + if filepath: # Assuming subprocess.run returns the patches list - patches_list = subprocess.run([filepath, "migrate_kubemarine", "--list"], capture_output=True, text=True).stdout.splitlines() + patches_list = subprocess.run( + [filepath, "migrate_kubemarine", "--list"], + capture_output=True, text=True, + ).stdout.splitlines() if "No patches available." in patches_list: patches_list = [] else: - patches_list.remove("Available patches list:") - + patches_list.remove("Available patches list:") + for patch in patches_list: - description = subprocess.run([filepath, "migrate_kubemarine", "--describe", patch], capture_output=True, text=True).stdout.strip() + description = subprocess.run( + [filepath, "migrate_kubemarine", "--describe", patch], + capture_output=True, text=True, + ).stdout.strip() patches_info["patches"].append({patch.strip(): description}) - - except Exception as e: - print(f"Error: {e}") - - finally: - os.unlink(filepath) + except Exception as e: + print(f"Error: {e}") return patches_info - -def list(old_version='', new_version=''): - ## get the patch list and migration procedure - index_from = KubemarineVersions.index(old_version) if old_version in KubemarineVersions else 0 - index_to = KubemarineVersions.index(new_version)+1 if new_version in KubemarineVersions else -1 +def list(old_version: str = "", new_version: str = "") -> dict: + index_from = ( + KubemarineVersions.index(old_version) + if old_version in KubemarineVersions + else 0 + ) + index_to = ( + KubemarineVersions.index(new_version) + 1 + if new_version in KubemarineVersions + else -1 + ) + ## get the patch list and migration procedure for version in KubemarineVersions[index_from:index_to]: - print(version,file=sys.stderr) - MigrationProcedure[version] = get_patches_info(version,"bin") + filepath = download_kubemarine(version, "bin") + MigrationProcedure[version] = get_patches_info(filepath) return MigrationProcedure # function name, parameters ... -print(json.dumps(globals()[sys.argv[1]](*sys.argv[2:]),indent=3)) +print(json.dumps(globals()[sys.argv[1]](*sys.argv[2:])))