From 7a12424b2fc22e9981758e07a06312e40a580ffa Mon Sep 17 00:00:00 2001 From: Stepan Blyshchak <38952541+stepanblyschak@users.noreply.github.com> Date: Sun, 24 Sep 2023 03:57:22 +0300 Subject: [PATCH] [sonic-installer] print output from spm migrate (#2960) ### What I did Don't capture output from migrate command for better debugging. #### How I did it Added ```nocapture``` flag to ```run_command_or_raise```. Pass ```nocapture``` when running ```spm migrate```. #### How to verify it Run sonic to sonic upgrade and observe the output of migrate command. --- sonic_installer/common.py | 10 +++++++--- sonic_installer/main.py | 2 +- tests/test_sonic_installer.py | 2 +- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/sonic_installer/common.py b/sonic_installer/common.py index f220a4b57e..50b736901f 100644 --- a/sonic_installer/common.py +++ b/sonic_installer/common.py @@ -41,16 +41,20 @@ def run_command(command, stdout=subprocess.PIPE, env=None, shell=False): sys.exit(proc.returncode) # Run bash command and return output, raise if it fails -def run_command_or_raise(argv, raise_exception=True): +def run_command_or_raise(argv, raise_exception=True, capture=True): click.echo(click.style("Command: ", fg='cyan') + click.style(' '.join(argv), fg='green')) - proc = subprocess.Popen(argv, text=True, stdout=subprocess.PIPE) + stdout = subprocess.PIPE if capture else None + proc = subprocess.Popen(argv, text=True, stdout=stdout) out, _ = proc.communicate() if proc.returncode != 0 and raise_exception: raise SonicRuntimeException("Failed to run command '{0}'".format(argv)) - return out.rstrip("\n") + if out is not None: + out = out.rstrip("\n") + + return out # Needed to prevent "broken pipe" error messages when piping # output of multiple commands using subprocess.Popen() diff --git a/sonic_installer/main.py b/sonic_installer/main.py index 9737e57de7..341111f265 100644 --- a/sonic_installer/main.py +++ b/sonic_installer/main.py @@ -384,7 +384,7 @@ def migrate_sonic_packages(bootloader, binary_image_version): 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"]) + "-y"], capture=False) finally: if docker_started: run_command_or_raise(["chroot", new_image_mount, DOCKER_CTL_SCRIPT, "stop"], raise_exception=False) diff --git a/tests/test_sonic_installer.py b/tests/test_sonic_installer.py index 3e257ae3b0..e9cb727d37 100644 --- a/tests/test_sonic_installer.py +++ b/tests/test_sonic_installer.py @@ -91,7 +91,7 @@ def rootfs_path_mock(path): call(["cp", f"{mounted_image_folder}/etc/resolv.conf", "/tmp/resolv.conf.backup"]), call(["cp", "/etc/resolv.conf", f"{mounted_image_folder}/etc/resolv.conf"]), call(["chroot", mounted_image_folder, "sh", "-c", "command -v sonic-package-manager"]), - call(["chroot", mounted_image_folder, "sonic-package-manager", "migrate", "/tmp/packages.json", "--dockerd-socket", "/tmp/docker.sock", "-y"]), + call(["chroot", mounted_image_folder, "sonic-package-manager", "migrate", "/tmp/packages.json", "--dockerd-socket", "/tmp/docker.sock", "-y"], capture=False), call(["chroot", mounted_image_folder, "/usr/lib/docker/docker.sh", "stop"], raise_exception=False), call(["cp", "/tmp/resolv.conf.backup", f"{mounted_image_folder}/etc/resolv.conf"], raise_exception=False), call(["umount", "-f", "-R", mounted_image_folder], raise_exception=False),