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 <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg
2025-02-24 22:24:58 +01:00
parent 4b2f140140
commit fc32d8a9db
2 changed files with 14 additions and 6 deletions
+4 -3
View File
@@ -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
+10 -3
View File
@@ -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);