tools/dev_container: stop old container if new image is available

When the container image is updated, developers may not notice and
continue running a different version. Detect this and restart the
container.

BUG=b:217465164
TEST=./tools/dev_container while changing image_version

Change-Id: Ib1df871c5c6fba9421c436d39ab7065be966f41e
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3469050
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Dennis Kempin <denniskempin@google.com>
This commit is contained in:
Dennis Kempin 2022-02-16 14:31:27 -08:00 committed by Commit Bot
parent 9b7471e51a
commit 2b1b78c6ef

View file

@ -59,6 +59,13 @@ DOCKER_ARGS = [
]
def container_revision(container_id: str):
image = docker("container inspect -f {{.Config.Image}}", container_id).stdout()
parts = image.split(":")
assert len(parts) == 2, f"Invalid image name {image}"
return parts[1]
@arg("command", nargs=argparse.REMAINDER)
def main(command: tuple[str, ...], stop: bool = False, hermetic: bool = False):
chdir(CROSVM_ROOT)
@ -79,6 +86,11 @@ def main(command: tuple[str, ...], stop: bool = False, hermetic: bool = False):
if hermetic:
docker(f"run --rm", *DOCKER_ARGS, *command).fg()
else:
if container_id and container_revision(container_id) != IMAGE_VERSION:
print(f"New image is available. Stopping old container ({container_id}).")
docker("rm -f", container_id).fg(quiet=True)
container_id = None
if not container_id:
container_id = docker(f"run --detach --name {CONTAINER_NAME}", *DOCKER_ARGS).stdout()
print(f"Started dev-container ({container_id}).")