From a3bbf46f0fe978898b20c0062e0b540a01db2011 Mon Sep 17 00:00:00 2001 From: junchao Date: Wed, 20 Jul 2022 18:16:06 +0800 Subject: [PATCH] Fix issues for sonic_installer upgrade-docker and sonic_installer rollback-docker --- sonic_installer/common.py | 2 ++ sonic_installer/main.py | 19 ++++++++++--------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/sonic_installer/common.py b/sonic_installer/common.py index 685063587c..887e55496b 100644 --- a/sonic_installer/common.py +++ b/sonic_installer/common.py @@ -31,6 +31,8 @@ def run_command(command): if proc.returncode != 0: sys.exit(proc.returncode) + return out.rstrip("\n") + # Run bash command and return output, raise if it fails def run_command_or_raise(argv, raise_exception=True): click.echo(click.style("Command: ", fg='cyan') + click.style(' '.join(argv), fg='green')) diff --git a/sonic_installer/main.py b/sonic_installer/main.py index 0056bb77e5..0da1c2859a 100644 --- a/sonic_installer/main.py +++ b/sonic_installer/main.py @@ -134,7 +134,7 @@ def get_docker_tag_name(image): def echo_and_log(msg, priority=LOG_NOTICE, fg=None): - if priority >= LOG_ERR: + if priority == LOG_ERR: # Print to stderr if priority is error click.secho(msg, fg=fg, err=True) else: @@ -872,17 +872,17 @@ def upgrade_docker(container_name, url, cleanup_image, skip_check, tag, warm): # this is image_id for image with "latest" tag image_id_latest = get_container_image_id(image_latest) - for id in image_id_all: - if id != image_id_latest: - # Unless requested, the previoud docker image will be preserved - if not cleanup_image and id == image_id_previous: - continue - run_command("docker rmi -f %s" % id) + if cleanup_image: + # Unless requested, the previoud docker image will be preserved + for id in image_id_all: + if id != image_id_latest and id == image_id_previous: + run_command("docker rmi -f %s" % id) + break exp_state = "reconciled" state = "" # post warm restart specific procssing for swss, bgp and teamd dockers, wait for reconciliation state. - if warm_configured is True or warm: + if warm_app_names and (warm_configured is True or warm): count = 0 for warm_app_name in warm_app_names: state = "" @@ -938,7 +938,8 @@ def rollback_docker(container_name): version_tag = "" for id in image_id_all: if id != image_id_previous: - version_tag = get_docker_tag_name(id) + version_tag = run_command("docker images --format '{{{{.ID}}}} {{{{.Tag}}}}' | grep {} | awk '{{print $2}}'".format(id)) + break # make previous image as latest run_command("docker tag %s:%s %s:latest" % (image_name, version_tag, image_name))