From b8f5d1dcae2b45c9046836fe9c7700365050a650 Mon Sep 17 00:00:00 2001 From: Olof hagsand Date: Thu, 17 Dec 2020 12:13:06 +0100 Subject: [PATCH] * Added CLICON_BACKEND_RESTCONF_PROCESS to clixon-config.yang --- CHANGELOG.md | 5 ++- apps/backend/backend_main.c | 50 +++++++++++++++++++++++ example/main/example_backend.c | 30 -------------- lib/clixon/clixon_datastore.h | 2 +- lib/src/clixon_datastore.c | 2 +- test/test_nacm_default.sh | 26 +++++++----- test/test_restconf_rpc.sh | 2 + yang/clixon/clixon-config@2020-11-03.yang | 12 +++++- 8 files changed, 83 insertions(+), 46 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a75d1719..8108987c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,7 +35,7 @@ * This is how it works: * Register a process via `clixon_process_register(h, name, namespace, argv, argc)` * Use process-control RPC defined in clixon-lib.yang to start/stop/restart or query status on that process - * Example code in the main example + * Enable in backend for restconf using `CLICON_BACKEND_RESTCONF_PROCESS`. * More YANG extension functionality, * See [Augment auto-cli for hiding/modifying cli syntax #156](https://github.com/clicon/clixon/issues/156) and [hiding auto-generated CLI entries #153](https://github.com/clicon/clixon/issues/153) * Extensions can be used in augmentations @@ -57,7 +57,8 @@ Users may have to change how they access the system * Added: autocli-op extension (see new features) * Added: rpc process-control for process/daemon management * New clixon-config@2020-11-03.yang revision - * Moved to clixon-restconf.yang and marked as obsolete: + * Added CLICON_BACKEND_RESTCONF_PROCESS + * Copied to clixon-restconf.yang and marked as obsolete: - CLICON_RESTCONF_IPV4_ADDR - CLICON_RESTCONF_IPV6_ADDR - CLICON_RESTCONF_HTTP_PORT diff --git a/apps/backend/backend_main.c b/apps/backend/backend_main.c index dbf25745..025e803f 100644 --- a/apps/backend/backend_main.c +++ b/apps/backend/backend_main.c @@ -178,6 +178,50 @@ backend_server_socket(clicon_handle h) return ss; } +/*! Enable process-control of restconf daemon, ie start/stop restconf + * @param[in] h Clicon handle + * @note Could also look in clixon-restconf and start process if enable is true, but that needs to + * be in start callback using a pseudo plugin. + */ +static int +backend_restconf_process_control(clicon_handle h) +{ + int retval = -1; + char **argv = NULL; + int i; + int nr; + char dbgstr[8]; + char wwwstr[64]; + + nr = 4; + if (clicon_debug_get() != 0) + nr += 2; + if ((argv = calloc(nr, sizeof(char *))) == NULL){ + clicon_err(OE_UNIX, errno, "calloc"); + goto done; + } + i = 0; + snprintf(wwwstr, sizeof(wwwstr)-1, "%s/clixon_restconf", clicon_option_str(h, "CLICON_WWWDIR")); + argv[i++] = wwwstr; + argv[i++] = "-f"; + argv[i++] = clicon_option_str(h, "CLICON_CONFIGFILE"); + if (clicon_debug_get() != 0){ + argv[i++] = "-D"; + snprintf(dbgstr, sizeof(dbgstr)-1, "%d", clicon_debug_get()); + argv[i++] = dbgstr; + } + argv[i++] = NULL; + if (clixon_process_register(h, "restconf", + NULL /* XXX network namespace */, + argv, nr) < 0) + goto done; + if (argv != NULL) + free(argv); + retval = 0; + done: + return retval; +} + /*! Load external NACM file */ static int @@ -745,6 +789,12 @@ main(int argc, clicon_option_str(h, "CLICON_BACKEND_REGEXP")) < 0) goto done; + /* Enable process-control of restconf daemon, ie start/stop restconf */ + if (clicon_option_bool(h, "CLICON_BACKEND_RESTCONF_PROCESS")){ + if (backend_restconf_process_control(h) < 0) + goto done; + } + /* Load Yang modules * 1. Load a yang module as a specific absolute filename */ if ((str = clicon_yang_main_file(h)) != NULL) diff --git a/example/main/example_backend.c b/example/main/example_backend.c index 17689d60..38c31180 100644 --- a/example/main/example_backend.c +++ b/example/main/example_backend.c @@ -1153,36 +1153,6 @@ clixon_plugin_init(clicon_handle h) else if (upgrade_callback_register(h, xml_changelog_upgrade, NULL, NULL) < 0) goto done; - { - char **argv = NULL; - int i; - int nr; - char dbgstr[8]; - char wwwstr[64]; - - nr = 4; - if (clicon_debug_get() != 0) - nr += 2; - if ((argv = calloc(nr, sizeof(char *))) == NULL){ - clicon_err(OE_UNIX, errno, "calloc"); - goto done; - } - i = 0; - snprintf(wwwstr, sizeof(wwwstr)-1, "%s/clixon_restconf", clicon_option_str(h, "CLICON_WWWDIR")); - argv[i++] = wwwstr; - argv[i++] = "-f"; - argv[i++] = clicon_option_str(h, "CLICON_CONFIGFILE"); - if (clicon_debug_get() != 0){ - argv[i++] = "-D"; - snprintf(dbgstr, sizeof(dbgstr)-1, "%d", clicon_debug_get()); - argv[i++] = dbgstr; - } - argv[i++] = NULL; - if (clixon_process_register(h, "restconf", _proc_netns, argv, nr) < 0) - goto done; - if (argv != NULL) - free(argv); - } /* Return plugin API */ return &api; done: diff --git a/lib/clixon/clixon_datastore.h b/lib/clixon/clixon_datastore.h index 30bbf4e3..75fb19eb 100644 --- a/lib/clixon/clixon_datastore.h +++ b/lib/clixon/clixon_datastore.h @@ -67,7 +67,7 @@ int xmldb_clear(clicon_handle h, const char *db); int xmldb_delete(clicon_handle h, const char *db); int xmldb_create(clicon_handle h, const char *db); /* utility functions */ -int xmldb_db_reset(clicon_handle h, char *db); +int xmldb_db_reset(clicon_handle h, const char *db); cxobj *xmldb_cache_get(clicon_handle h, const char *db); diff --git a/lib/src/clixon_datastore.c b/lib/src/clixon_datastore.c index 49537779..4aec04cd 100644 --- a/lib/src/clixon_datastore.c +++ b/lib/src/clixon_datastore.c @@ -466,7 +466,7 @@ xmldb_create(clicon_handle h, */ int xmldb_db_reset(clicon_handle h, - char *db) + const char *db) { if (xmldb_exists(h, db) == 1){ if (xmldb_delete(h, db) != 0 && errno != ENOENT) diff --git a/test/test_nacm_default.sh b/test/test_nacm_default.sh index 121ba011..bc5b970c 100755 --- a/test/test_nacm_default.sh +++ b/test/test_nacm_default.sh @@ -100,22 +100,24 @@ EOF fi new "start backend -s $db -f $cfg" start_backend -s $db -f $cfg + + new "wait backend" + wait_backend else new "Restart backend as eg follows: -Ff $cfg -s $db" sleep 2 fi - new "waiting" - wait_backend - - new "kill old restconf daemon" - stop_restconf_pre + if [ $RC -ne 0 ]; then # Bring your own restconf + new "kill old restconf daemon" + stop_restconf_pre - new "start restconf daemon (-a is enable basic authentication)" - start_restconf -f $cfg -- -a + new "start restconf daemon (-a is enable basic authentication)" + start_restconf -f $cfg -- -a - new "waiting" - wait_restconf + new "wait restconf" + wait_restconf + fi # Use POST (instead of startup) # Note this only works because CLICON_NACM_DISABLED_ON_EMPTY is true @@ -176,8 +178,10 @@ EOF new "get 99" expectpart "$(curl -u guest:bar $CURLOPTS -X GET $RCPROTO://localhost/restconf/data/nacm-example:x)" 0 "$status" "$ret" - new "Kill restconf daemon" - stop_restconf + if [ $RC -ne 0 ]; then # Bring your own restconf + new "Kill restconf daemon" + stop_restconf + fi if [ $BE -ne 0 ]; then # Bring your own backend new "Kill backend" diff --git a/test/test_restconf_rpc.sh b/test/test_restconf_rpc.sh index 14515037..d89aa876 100755 --- a/test/test_restconf_rpc.sh +++ b/test/test_restconf_rpc.sh @@ -33,6 +33,8 @@ cat < $cfg /usr/local/var/$APPNAME/$APPNAME.pidfile /usr/local/var/$APPNAME true + + true $RESTCONFIG EOF diff --git a/yang/clixon/clixon-config@2020-11-03.yang b/yang/clixon/clixon-config@2020-11-03.yang index 8f8f0c2c..f230a836 100644 --- a/yang/clixon/clixon-config@2020-11-03.yang +++ b/yang/clixon/clixon-config@2020-11-03.yang @@ -45,7 +45,8 @@ module clixon-config { revision 2020-11-03 { description - "Moved to clixon-restconf.yang and marked as obsolete: + "Added CLICON_BACKEND_RESTCONF_PROCESS + Copied to clixon-restconf.yang and marked as obsolete: CLICON_RESTCONF_IPV4_ADDR CLICON_RESTCONF_IPV6_ADDR CLICON_RESTCONF_HTTP_PORT @@ -712,6 +713,15 @@ module clixon-config { mandatory true; description "Process-id file of backend daemon"; } + leaf CLICON_BACKEND_RESTCONF_PROCESS { + type boolean; + default false; + description + "If set: + - enable process-control of restconf daemon, ie start/stop restconf + daemon internally using fork/exec. + Set to false if you start the restconf daemon by other means."; + } leaf CLICON_AUTOCOMMIT { type int32; default 0;