From 9a88cb6f0fbaf5fc8c336723cc7b831c6aacf19b Mon Sep 17 00:00:00 2001 From: Stepan Blyshchak <38952541+stepanblyschak@users.noreply.github.com> Date: Thu, 6 May 2021 11:30:42 +0300 Subject: [PATCH] [sonic_installer] dont fail package migration (#1591) - What I did Do not fail when user is doing downgrade. Fix Azure/sonic-buildimage#7518 - How I did it Ignoring failures. - How to verify it On master image install 202012 image. Signed-off-by: Stepan Blyschak --- sonic_installer/common.py | 4 ++-- sonic_installer/main.py | 16 ++++++++++------ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/sonic_installer/common.py b/sonic_installer/common.py index ac1416789f1c..5e36cedb8c65 100644 --- a/sonic_installer/common.py +++ b/sonic_installer/common.py @@ -31,13 +31,13 @@ def run_command(command): sys.exit(proc.returncode) # Run bash command and return output, raise if it fails -def run_command_or_raise(argv): +def run_command_or_raise(argv, raise_exception=True): click.echo(click.style("Command: ", fg='cyan') + click.style(' '.join(argv), fg='green')) proc = subprocess.Popen(argv, text=True, stdout=subprocess.PIPE) out, _ = proc.communicate() - if proc.returncode != 0: + if proc.returncode != 0 and raise_exception: raise SonicRuntimeException("Failed to run command '{0}'".format(argv)) return out.rstrip("\n") diff --git a/sonic_installer/main.py b/sonic_installer/main.py index 12a2ab7e0e12..5f89878344f5 100644 --- a/sonic_installer/main.py +++ b/sonic_installer/main.py @@ -232,7 +232,7 @@ def mount_squash_fs(squashfs_path, mount_point): run_command_or_raise(["mount", "-t", "squashfs", squashfs_path, mount_point]) -def umount(mount_point, read_only=True, recursive=False, force=True, remove_dir=True): +def umount(mount_point, read_only=True, recursive=False, force=True, remove_dir=True, raise_exception=True): flags = [] if read_only: flags.append("-r") @@ -240,9 +240,9 @@ def umount(mount_point, read_only=True, recursive=False, force=True, remove_dir= flags.append("-f") if recursive: flags.append("-R") - run_command_or_raise(["umount", *flags, mount_point]) + run_command_or_raise(["umount", *flags, mount_point], raise_exception=raise_exception) if remove_dir: - run_command_or_raise(["rm", "-rf", mount_point]) + run_command_or_raise(["rm", "-rf", mount_point], raise_exception=raise_exception) def mount_overlay_fs(lowerdir, upperdir, workdir, mount_point): @@ -350,14 +350,18 @@ def get_path(path): run_command_or_raise(["mount", "--bind", os.path.join(VAR_RUN_PATH, DOCKERD_SOCK), os.path.join(new_image_mount, "tmp", DOCKERD_SOCK)]) + run_command_or_raise(["chroot", new_image_mount, "sh", "-c", "command -v {}".format(SONIC_PACKAGE_MANAGER)]) + except SonicRuntimeException as err: + echo_and_log("Warning: SONiC Application Extension is not supported in this image: {}".format(err), LOG_ERR, fg="red") + else: run_command_or_raise(["chroot", new_image_mount, SONIC_PACKAGE_MANAGER, "migrate", os.path.join("/", tmp_dir, packages_file), "--dockerd-socket", os.path.join("/", tmp_dir, DOCKERD_SOCK), "-y"]) finally: - run_command("chroot {} {} stop".format(new_image_mount, DOCKER_CTL_SCRIPT)) - umount(new_image_mount, recursive=True, read_only=False, remove_dir=False) - umount(new_image_mount) + run_command_or_raise(["chroot", new_image_mount, DOCKER_CTL_SCRIPT, "stop"], raise_exception=False) + umount(new_image_mount, recursive=True, read_only=False, remove_dir=False, raise_exception=False) + umount(new_image_mount, raise_exception=False) # Main entrypoint