From ffd212861426e9cdc9e18c22d3c2ae4ae2b5191e Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Thu, 18 Jan 2024 21:12:03 +0100 Subject: [PATCH] confd: add support for container restart and restart policy Also, ensure the deleted container is actually deleted before recreating it with a new configuration. Signed-off-by: Joachim Wiberg --- board/common/rootfs/usr/sbin/container | 21 +++------- src/confd/src/infix-containers.c | 39 ++++++++++++------- .../yang/infix-containers@2023-12-14.yang | 16 ++++---- src/klish-plugin-infix/xml/infix.xml | 5 +++ 4 files changed, 44 insertions(+), 37 deletions(-) diff --git a/board/common/rootfs/usr/sbin/container b/board/common/rootfs/usr/sbin/container index 3fa09f51..665f926f 100755 --- a/board/common/rootfs/usr/sbin/container +++ b/board/common/rootfs/usr/sbin/container @@ -7,20 +7,6 @@ log() logger -I $PPID -t container -p local1.notice -- "$*" } -verify() -{ - name=$1 - - for nm in $(podman ps -a --format "{{.Names}}"); do - if [ "$name" = "$nm" ]; then - return; - fi - done - - >&2 echo "$name: no such container." - exit 1 -} - # shellcheck disable=SC2086 create() { @@ -61,6 +47,7 @@ create() log "Calling \"podman create --name $name --conmon-pidfile=$pidfn $network $args $image\"" if podman create --name "$name" --conmon-pidfile="$pidfn" $network $args "$image"; then log "Successfully created container $name from $image" + initctl show "container:$name" |grep -q manual:yes || initctl -bq start "container:$name" initctl -nbq cond set container:$name exit 0 fi @@ -104,6 +91,7 @@ commands: help Show this help text list [image] List names (only) of containers or images remove IMAGE Remove an (unused) container image + restart NAME Restart a crashed container run NAME CMD Execute a command (interactively) in container show [image] Show containers or container images start NAME Start a container @@ -206,11 +194,12 @@ case $cmd in esac ;; start) - verify "$1" || exit 1 initctl -bq start "container:$1" ;; + restart) + initctl -bq restart "container:$1" + ;; stop) - verify "$1" || exit 1 initctl -bq stop "container:$1" || podman kill "$1" ;; *) diff --git a/src/confd/src/infix-containers.c b/src/confd/src/infix-containers.c index 54639af8..d2f270a4 100644 --- a/src/confd/src/infix-containers.c +++ b/src/confd/src/infix-containers.c @@ -29,7 +29,9 @@ static const struct srx_module_requirement reqs[] = { static int add(const char *name, struct lyd_node *cif) { const char *image = lydx_get_cattr(cif, "image"); - struct lyd_node *net; + const char *restart_policy; + struct lyd_node *node; + char *restart = ""; /* Default restart:10 */ FILE *fp; fp = fopenf("w", "%s/%s.sh", INBOX_QUEUE, name); @@ -41,24 +43,25 @@ static int add(const char *name, struct lyd_node *cif) /* Stop any running container gracefully so it releases its IP addresses. */ fprintf(fp, "#!/bin/sh\n" "container stop %s\n" - "container ", name); + "container delete %s\n" + "container", name, name); - LYX_LIST_FOR_EACH(lyd_child(cif), net, "dns") - fprintf(fp, "--dns %s ", lyd_get_value(net)); + LYX_LIST_FOR_EACH(lyd_child(cif), node, "dns") + fprintf(fp, " --dns %s", lyd_get_value(node)); - LYX_LIST_FOR_EACH(lyd_child(cif), net, "search") - fprintf(fp, "--dns-search %s ", lyd_get_value(net)); + LYX_LIST_FOR_EACH(lyd_child(cif), node, "search") + fprintf(fp, " --dns-search %s", lyd_get_value(node)); - fprintf(fp, "create %s %s", name, image); + fprintf(fp, " create %s %s", name, image); - LYX_LIST_FOR_EACH(lyd_child(cif), net, "network") { + LYX_LIST_FOR_EACH(lyd_child(cif), node, "network") { struct lyd_node *opt; const char *name; int first = 1; - name = lydx_get_cattr(net, "name"); + name = lydx_get_cattr(node, "name"); fprintf(fp, " %s", name); - LYX_LIST_FOR_EACH(lyd_child(net), opt, "option") { + LYX_LIST_FOR_EACH(lyd_child(node), opt, "option") { const char *option = lyd_get_value(opt); fprintf(fp, "%s%s", first ? ":" : ",", option); @@ -76,9 +79,19 @@ static int add(const char *name, struct lyd_node *cif) return SR_ERR_SYS; } - fprintf(fp, "service name:container :%s log:prio:local1.err,tag:%s %s pid:!/run/container:%s.pid \\\n" - " [2345] podman start -a %s -- Container %s\n", - name, name, lydx_is_enabled(cif, "manual") ? "manual:yes" : "", name, name, name, name); + restart_policy = lydx_get_cattr(cif, "restart-policy"); + if (restart_policy) { + if (!strcmp(restart_policy, "no")) + restart = "norestart"; + else if (!strcmp(restart_policy, "always")) + restart = "restart:always"; + /* default is to restart up to 10 times */ + } + + fprintf(fp, "service name:container :%s log:prio:local1.err,tag:%s pid:!/run/container:%s.pid \\\n" + " [2345] %s %s podman start -a %s -- Container %s\n", + name, name, name, lydx_is_enabled(cif, "manual") ? "manual:yes" : "", restart, + name, name, name); fclose(fp); if (systemf("initctl -nbq enable container:%s", name)) { diff --git a/src/confd/yang/infix-containers@2023-12-14.yang b/src/confd/yang/infix-containers@2023-12-14.yang index 6bbbd710..357fe758 100644 --- a/src/confd/yang/infix-containers@2023-12-14.yang +++ b/src/confd/yang/infix-containers@2023-12-14.yang @@ -34,15 +34,15 @@ module infix-containers { typedef restart-policy { type enumeration { enum no { - description "Do not restart containers on exit."; + description "Do not restart containers that exit/crash."; value 1; } - enum always { - description "Restart containers when they exit, regardless of status."; + enum retry { + description "Restart containers up to 10 times before giving up."; value 2; } - enum on-failure { - description "Restart containers when they exit with a non-0 exit code."; + enum always { + description "Always restart containers when they exit."; value 3; } } @@ -73,10 +73,10 @@ module infix-containers { type string; } - leaf restart { - description "Restart policy to follow when containers exit."; + leaf restart-policy { + description "Restart policy to when containers exit/crash."; type restart-policy; - default no; + default retry; } leaf manual { diff --git a/src/klish-plugin-infix/xml/infix.xml b/src/klish-plugin-infix/xml/infix.xml index 5093030e..a1d8384c 100644 --- a/src/klish-plugin-infix/xml/infix.xml +++ b/src/klish-plugin-infix/xml/infix.xml @@ -219,6 +219,11 @@ container remove $KLISH_PARAM_name + + + container restart $KLISH_PARAM_name + +