From fc32d8a9db8ca36386f9c3eb167ad3f1039bf708 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Sat, 22 Feb 2025 14:50:55 +0100 Subject: [PATCH] container: add meta data to container shell script For tasks like upgrade we need, e.g., the image URI. Instead of guessing where on the container command line the image is, use a meta-data field in the container shell script. Signed-off-by: Joachim Wiberg --- board/common/rootfs/usr/sbin/container | 7 ++++--- src/confd/src/infix-containers.c | 13 ++++++++++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/board/common/rootfs/usr/sbin/container b/board/common/rootfs/usr/sbin/container index ed1a1b4c..7905b712 100755 --- a/board/common/rootfs/usr/sbin/container +++ b/board/common/rootfs/usr/sbin/container @@ -436,6 +436,7 @@ commands: stat Show continuous stats about containers (Ctrl-C aborts) start [NAME] Start a container, see -n stop [NAME] Stop a container, see -n + upgrade NAME Upgrade a running container (stop, pull, restart) volume [prune] Prune unused volumes EOF } @@ -821,15 +822,15 @@ case $cmd in # Likely an OCI archive, or local directory, assume user has updated image. if echo "$img" | grep -Eq '^localhost/'; then - file=$(awk '{s=$NF} END{print s}' "$script") - echo "Upgrading container ${1} with local archive: $file ..." + file=$(awk '/^# meta-image:/ {print $3}' "$script") + echo ">> Upgrading container $1 using $file ..." else printf ">> Stopping ... " podman stop "$1" printf ">> " podman pull "$img" || (echo "Failed fetching $img, check your network (settings)."; exit 1) + echo ">> Starting $1 ..." fi - echo ">> Starting $1 ..." if ! "$script"; then echo ">> Failed recreating container $1" exit 1 diff --git a/src/confd/src/infix-containers.c b/src/confd/src/infix-containers.c index 431ece2b..9c4620cc 100644 --- a/src/confd/src/infix-containers.c +++ b/src/confd/src/infix-containers.c @@ -33,7 +33,7 @@ */ static int add(const char *name, struct lyd_node *cif) { - const char *restart_policy, *string; + const char *restart_policy, *string, *image; struct lyd_node *node, *nets, *caps; char script[strlen(name) + 5]; FILE *fp, *ap; @@ -60,10 +60,17 @@ static int add(const char *name, struct lyd_node *cif) * setup at creation/boot and for manual upgrade. The delete * command ensures any already running container is stopped and * deleted so that it releases all claimed resources. + * + * The odd meta data is not used by the script itself, instead + * it is used by the /usr/sbin/container wrapper when upgrading + * a running container instance. */ + image = lydx_get_cattr(cif, "image"); fprintf(fp, "#!/bin/sh\n" + "# meta-name: %s\n" + "# meta-image: %s\n" "container --quiet delete %s >/dev/null\n" - "container --quiet", name); + "container --quiet", name, image, name); LYX_LIST_FOR_EACH(lyd_child(cif), node, "dns") fprintf(fp, " --dns %s", lyd_get_value(node)); @@ -204,7 +211,7 @@ static int add(const char *name, struct lyd_node *cif) fprintf(fp, " --checksum sha512:%s", string); } - fprintf(fp, " create %s %s", name, lydx_get_cattr(cif, "image")); + fprintf(fp, " create %s %s", name, image); if ((string = lydx_get_cattr(cif, "command"))) fprintf(fp, " %s", string);