Skip to content

Commit

Permalink
[sonic-installer] print output from spm migrate (sonic-net#2960)
Browse files Browse the repository at this point in the history
### 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.
  • Loading branch information
stepanblyschak authored Sep 24, 2023
1 parent 7d7a971 commit 7a12424
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
10 changes: 7 additions & 3 deletions sonic_installer/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion sonic_installer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_sonic_installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 7a12424

Please sign in to comment.