mirror of
https://github.com/kernelkit/infix.git
synced 2026-07-31 04:53:01 +02:00
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 <troglobit@gmail.com>
This commit is contained in:
@@ -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"
|
||||
;;
|
||||
*)
|
||||
|
||||
@@ -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] <usr/container:%s> 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 <usr/container:%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)) {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -219,6 +219,11 @@
|
||||
<ACTION sym="script" out="tty" interrupt="true">container remove $KLISH_PARAM_name</ACTION>
|
||||
</COMMAND>
|
||||
|
||||
<COMMAND name="restart" help="Restart a crashed container">
|
||||
<PARAM name="name" ptype="/CONTAINERSa" help="Container name" />
|
||||
<ACTION sym="script" out="tty" interrupt="true">container restart $KLISH_PARAM_name</ACTION>
|
||||
</COMMAND>
|
||||
|
||||
<COMMAND name="run" help="Run a container image, with optional command">
|
||||
<PARAM name="image" ptype="/IMAGES" help="Image name" />
|
||||
<SWITCH name="optional" min="0">
|
||||
|
||||
Reference in New Issue
Block a user