diff --git a/CHANGELOG.md b/CHANGELOG.md index c55ab250..1fcfa141 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -86,6 +86,7 @@ Expected: July 2020 ### Minor changes +* New backend switch: `-q` : Quit startup directly after upgrading and print result on stdout. * Enhanced Clixon if-feature handling: * If-feature now supports and/or lists, such as: `if-feature "a and b"` and `if-feature "a or b or c"`. However, full if-feature-expr including `not` and nested boolean experessions is still not supported. * Sanity check: if an `if-feature` statement exists, a corresponding `feature` statement must exists that declares that feature. diff --git a/apps/backend/backend_commit.c b/apps/backend/backend_commit.c index 9a285c4d..0581b364 100644 --- a/apps/backend/backend_commit.c +++ b/apps/backend/backend_commit.c @@ -206,6 +206,14 @@ startup_common(clicon_handle h, if (ret == 0) goto fail; } +#if 1 + if (clicon_quit_upgrade_get(h) == 1){ /* Print upgraded db */ + if (xmldb_dump(h, stdout, xt) < 0) + goto done; + exit(0); /* This is fairly abrupt , but need to avoid side-effects of rewinding + stack. Alternative is to make a separate function stack for this. */ + } +#endif /* If empty skip. Note upgrading can add children, so it may be empty before that. */ if (xml_child_nr(xt) == 0){ td->td_target = xt; diff --git a/apps/backend/backend_main.c b/apps/backend/backend_main.c index 96b889fc..c5b1ef70 100644 --- a/apps/backend/backend_main.c +++ b/apps/backend/backend_main.c @@ -76,7 +76,7 @@ #include "backend_startup.h" /* Command line options to be passed to getopt(3) */ -#define BACKEND_OPTS "hD:f:l:d:p:b:Fza:u:P:1s:c:U:g:y:o:" +#define BACKEND_OPTS "hD:f:l:d:p:b:Fza:u:P:1qs:c:U:g:y:o:" #define BACKEND_LOGFILE "/usr/local/var/clixon_backend.log" @@ -414,6 +414,7 @@ usage(clicon_handle h, "\t-1\t\tRun once and then quit (dont wait for events)\n" "\t-s \tSpecify backend startup mode: none|startup|running|init)\n" "\t-c \tLoad extra xml configuration, but don't commit.\n" + "\t-q \tQuit startup directly after upgrading and print result on stdout\n" "\t-U \tRun backend daemon as this user AND drop privileges permanently\n" "\t-g \tClient membership required to this group (default: %s)\n" @@ -579,6 +580,10 @@ main(int argc, case '1' : /* Quit after reading database once - dont wait for events */ once = 1; break; + case 'q': /* Quit directly after startup upgrading and print result on stdout */ + if (clicon_quit_upgrade_set(h, 1) < 0) + goto done; + break; case 's' : /* startup mode */ if (clicon_option_add(h, "CLICON_STARTUP_MODE", optarg) < 0) goto done; @@ -843,6 +848,10 @@ main(int argc, goto done; /* if status = STARTUP_INVALID, cbret contains info */ } + /* Quit after upgrade catch-all, running/startup quits in upgrade code */ + if (clicon_quit_upgrade_get(h) == 1) + goto done; + /* * Disable unknown to anydata auto-creation after startup */ diff --git a/lib/clixon/clixon_data.h b/lib/clixon/clixon_data.h index e71c63ae..bf46003d 100644 --- a/lib/clixon/clixon_data.h +++ b/lib/clixon/clixon_data.h @@ -117,4 +117,8 @@ int clicon_argv_set(clicon_handle h, char *argv0, int argc, char **argv); int clicon_session_id_set(clicon_handle h, uint32_t id); int clicon_session_id_get(clicon_handle h, uint32_t *id); +/* If set, quit startup directly after upgrade */ +int clicon_quit_upgrade_get(clicon_handle h); +int clicon_quit_upgrade_set(clicon_handle h, int val); + #endif /* _CLIXON_DATA_H_ */ diff --git a/lib/clixon/clixon_datastore.h b/lib/clixon/clixon_datastore.h index 8b014dda..4b5e238c 100644 --- a/lib/clixon/clixon_datastore.h +++ b/lib/clixon/clixon_datastore.h @@ -74,5 +74,6 @@ cxobj *xmldb_cache_get(clicon_handle h, const char *db); int xmldb_modified_get(clicon_handle h, const char *db); int xmldb_modified_set(clicon_handle h, const char *db, int value); +int xmldb_dump(clicon_handle h, FILE *f, cxobj *xt); #endif /* _CLIXON_DATASTORE_H */ diff --git a/lib/src/clixon_data.c b/lib/src/clixon_data.c index 325079da..cada69b6 100644 --- a/lib/src/clixon_data.c +++ b/lib/src/clixon_data.c @@ -713,3 +713,37 @@ clicon_session_id_set(clicon_handle h, clicon_hash_add(cdat, "session-id", &id, sizeof(uint32_t)); return 0; } + +/*! Get quit-after-upgrade flag + * @param[in] h Clicon handle + * @retval 1 Flag set: quit startup directly after upgrade + * @retval 0 Flag not set + * If set, quit startup directly after upgrade + */ +int +clicon_quit_upgrade_get(clicon_handle h) +{ + clicon_hash_t *cdat = clicon_data(h); + void *p; + + if ((p = clicon_hash_value(cdat, "quit-after-upgrade", NULL)) == NULL) + return 0; + return *(int*)p; +} + +/*! Set quit-after-upgrade flag + * @param[in] h Clicon handle + * @param[in] val Set or reset flag + * @retval 0 OK + * @retval -1 Error + * If set, quit startup directly after upgrade + */ +int +clicon_quit_upgrade_set(clicon_handle h, + int val) +{ + clicon_hash_t *cdat = clicon_data(h); + + clicon_hash_add(cdat, "quit-after-upgrade", &val, sizeof(int)); + return 0; +} diff --git a/test/test_upgrade_quit.sh b/test/test_upgrade_quit.sh new file mode 100755 index 00000000..dce92d99 --- /dev/null +++ b/test/test_upgrade_quit.sh @@ -0,0 +1,310 @@ +#!/usr/bin/env bash +# Test quit startup after upgrade (-q) functionality +# The yang is from test_upgrade_interfaces and the upgrade code +# is implemented in the main example +# The test is just having 2014 xml syntax in file and the backend printing the upgraded +# syntax to stdout +# The output syntax is datastore syntax, as a running db would have looked like +# Note that the admin and stats field are non-config in the original, not here + +# Magic line must be first in script (see README.md) +s="$_" ; . ./lib.sh || if [ "$s" = $0 ]; then exit 0; else return 0; fi + +APPNAME=example + +cfg=$dir/conf.xml +if2014=$dir/interfaces@2014-05-08.yang +if2018=$dir/interfaces@2018-02-20.yang + +# Original simplified version - note all is config to allow for storing in +# datastore +cat < $if2014 +module interfaces{ + yang-version 1.1; + namespace "urn:example:interfaces"; + prefix "if"; + + import ietf-yang-types { + prefix yang; + } + revision 2014-05-08 { + description + "Initial revision."; + reference + "RFC 7223: A YANG Data Model for Interface Management"; + } + feature if-mib { + description + "This feature indicates that the device implements + the IF-MIB."; + reference + "RFC 2863: The Interfaces Group MIB"; + } + container interfaces { + description + "Interface configuration parameters."; + + list interface { + key "name"; + leaf name { + type string; + } + leaf description { + type string; + } + + + leaf type { + type string; + mandatory true; + } + leaf link-up-down-trap-enable { + if-feature if-mib; + type enumeration { + enum enabled; + enum disabled; + } + } + } + } + container interfaces-state { + list interface { + key "name"; + leaf name { + type string; + } + leaf admin-status { + if-feature if-mib; + type enumeration { + enum up; + enum down; + enum testing; + } + mandatory true; + } + container statistics { + leaf in-octets { + type yang:counter64; + } + leaf in-unicast-pkts { + type yang:counter64; + } + } + } + } +} + +EOF + +cat < $if2018 +module interfaces{ + yang-version 1.1; + namespace "urn:example:interfaces"; + prefix "if"; + + import ietf-yang-types { + prefix yang; + } + revision 2018-02-20 { + description + "Updated to support NMDA."; + reference + "RFC 8343: A YANG Data Model for Interface Management"; + } + revision 2014-05-08 { + description + "Initial revision."; + reference + "RFC 7223: A YANG Data Model for Interface Management"; + } + feature if-mib { + description + "This feature indicates that the device implements + the IF-MIB."; + reference + "RFC 2863: The Interfaces Group MIB"; + } + container interfaces { + description + "Interface configuration parameters."; + + list interface { + key "name"; + leaf name { + type string; + } + container docs{ + description "Original description is wrapped and renamed"; + leaf descr { + type string; + } + } + leaf type { + type string; + mandatory true; + } + leaf link-up-down-trap-enable { + if-feature if-mib; + type enumeration { + enum enabled; + enum disabled; + } + } + leaf admin-status { + if-feature if-mib; + type enumeration { + enum up; + enum down; + enum testing; + } + mandatory true; + } + container statistics { + leaf in-octets { + type decimal64{ + fraction-digits 3; + } + } + leaf in-unicast-pkts { + type yang:counter64; + } + } + } + } +} +EOF + +# Create startup db revision from 2014-05-08 to be upgraded to 2018-02-20 +# This is 2014 syntax +cat < $dir/startup_db + + + 42 + + interfaces + 2014-05-08 + urn:example:interfaces + + + + + e0 + eth + First interface + + + e1 + eth + + + + + e0 + up + + 54326432 + 8458765 + + + + e1 + down + + + e2 + testing + + + +EOF + +# Create configuration +cat < $cfg + + $cfg + ietf-netconf:startup + /usr/local/share/clixon + interfaces:if-mib + $dir + /usr/local/var/$APPNAME/$APPNAME.sock + /usr/local/lib/example/backend + /usr/local/var/$APPNAME/$APPNAME.pidfile + $dir + true + false + false + /usr/local/lib/$APPNAME/clispec + /usr/local/lib/$APPNAME/cli + $APPNAME + +EOF + + +# This is 2014 syntax +cat < $dir/startup_db + + + 42 + + interfaces + 2014-05-08 + urn:example:interfaces + + + + + e0 + eth + First interface + + + e1 + eth + + + + + e0 + up + + 54326432 + 8458765 + + + + e1 + down + + + e2 + testing + + + +EOF + +MODSTATE='0clixon-lib2020-04-23http://clicon.org/libietf-inet-types2013-07-15urn:ietf:params:xml:ns:yang:ietf-inet-typesietf-netconf2011-06-01urn:ietf:params:xml:ns:netconf:base:1.0ietf-restconf2017-01-26urn:ietf:params:xml:ns:yang:ietf-restconfietf-yang-library2016-06-21urn:ietf:params:xml:ns:yang:ietf-yang-libraryietf-yang-types2013-07-15urn:ietf:params:xml:ns:yang:ietf-yang-typesinterfaces2018-02-20urn:example:interfaces' + +XML='e0ethup54326.4328458765First interfacee1ethdown' + +ALL="$MODSTATE$XML" + +# -u means trigger example upgrade +new "test params: -s startup -f $cfg -- -u" + +# kill old backend (if any) +new "kill old backend" +sudo clixon_backend -zf $cfg +if [ $? -ne 0 ]; then + err +fi +new "start backend -s startup -f $cfg -q -- -u" +output=$(sudo $clixon_backend -F -D $DBG -s startup -f $cfg -q -- -u) +#echo "$output" +if [ "$ALL" != "$output" ]; then + err "$ALL" "$output" +fi + +rm -rf $dir + +