From a23dc895923ac87c5f8b6b7d83420f9c346f53ea Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Thu, 28 Nov 2024 15:59:24 +0100 Subject: [PATCH] confd: disabling a container should not delete volumes When disabling a container in the configuration we should not remove it, because then any volumes used by it may be lost. Instead, we now simply disable the service to prevent it from starting, or stopping it, if it was running. Signed-off-by: Joachim Wiberg --- src/confd/src/infix-containers.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/confd/src/infix-containers.c b/src/confd/src/infix-containers.c index 5843e0ef..0724690c 100644 --- a/src/confd/src/infix-containers.c +++ b/src/confd/src/infix-containers.c @@ -26,12 +26,21 @@ static int add(const char *name, struct lyd_node *cif) { - const char *image = lydx_get_cattr(cif, "image"); const char *restart_policy, *string; struct lyd_node *node, *nets, *caps; char script[strlen(name) + 5]; FILE *fp, *ap; + /* + * If running already, disable the service, keeping the created + * container and any volumes for later if the user re-enables + * it again. + */ + if (!lydx_is_enabled(cif, "enabled")) { + systemf("initctl -bnq disable container@%s.conf", name); + return 0; + } + snprintf(script, sizeof(script), "%s.sh", name); fp = fopenf("w", "%s/%s", _PATH_CONT, script); if (!fp) { @@ -188,7 +197,7 @@ static int add(const char *name, struct lyd_node *cif) fprintf(fp, " --checksum sha512:%s", string); } - fprintf(fp, " create %s %s", name, image); + fprintf(fp, " create %s %s", name, lydx_get_cattr(cif, "image")); if ((string = lydx_get_cattr(cif, "command"))) fprintf(fp, " %s", string); @@ -256,15 +265,10 @@ static int change(sr_session_ctx_t *session, uint32_t sub_id, const char *module } LYX_LIST_FOR_EACH(cifs, cif, "container") { - const char *nm = lydx_get_cattr(cif, "name"); - - if (strcmp(name, nm)) + if (strcmp(name, lydx_get_cattr(cif, "name"))) continue; - if (!lydx_is_enabled(cif, "enabled")) - del(name); - else - add(name, cif); + add(name, cif); break; } }