confd: add NETCONF actions to start/stop/restart containers

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg
2024-02-25 19:49:27 +01:00
parent ff32314676
commit b7d119b23f
2 changed files with 48 additions and 29 deletions
+42
View File
@@ -240,6 +240,44 @@ err_abandon:
return err;
}
static int action(sr_session_ctx_t *session, uint32_t sub_id, const char *xpath,
const sr_val_t *input, const size_t input_cnt,
sr_event_t event, unsigned request_id,
sr_val_t **output, size_t *output_cnt,
void *priv)
{
char buf[strlen(xpath) + 1];
char *cmd, *name, *ptr;
char quote;
/* /infix-containers:container/container[name='ntpd']/restart */
strlcpy(buf, xpath, sizeof(buf));
name = strstr(buf, "[name=");
if (!name)
return SR_ERR_INTERNAL;
ptr = &name[6];
quote = ptr[0]; /* Quote char to look for: ' or " */
name = &ptr[1];
ptr = strchr(name, quote);
if (!ptr)
return SR_ERR_INTERNAL;
*ptr++ = 0;
cmd = strstr(ptr, "]/");
if (!cmd)
return SR_ERR_INTERNAL;
cmd += 2;
ERROR("CALLING 'container %s %s' (xpath %s)", cmd, name, xpath);
if (systemf("container %s %s", cmd, name))
return SR_ERR_INTERNAL;
return SR_ERR_OK;
}
void infix_containers_launch(void)
{
struct dirent *d;
@@ -280,6 +318,10 @@ int infix_containers_init(struct confd *confd)
goto fail;
REGISTER_CHANGE(confd->session, MODULE, CFG_XPATH, 0, change, confd, &confd->sub);
REGISTER_RPC(confd->session, CFG_XPATH "/container/start", action, NULL, &confd->sub);
REGISTER_RPC(confd->session, CFG_XPATH "/container/stop", action, NULL, &confd->sub);
REGISTER_RPC(confd->session, CFG_XPATH "/container/restart", action, NULL, &confd->sub);
return SR_ERR_OK;
fail:
ERROR("init failed: %s", sr_strerror(rc));
@@ -248,38 +248,15 @@ module infix-containers {
type yang:date-and-time;
}
}
}
}
rpc container-pull {
description "Upgrade (fetch) a container image";
input {
leaf image {
description "Name of the container image to upgrade.";
type string;
action start {
description "Start a stopped container.";
}
}
}
rpc container-stop {
description "Stop a container";
input {
leaf name {
description "Name of the container to stop";
type string;
action stop {
description "Stop a running container.";
}
}
}
rpc container-start {
description "Start a container";
input {
leaf name {
description "Name of the container to start";
type string;
action restart {
description "Restart a running, or start, a stopped container.";
}
}
}