From e85bc8223bf7c0461e40b649c9208beb97abaca5 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Tue, 3 Oct 2023 14:25:38 +0200 Subject: [PATCH] confd: load startup-config with a new wrapper to detect errors The new confd-load.sh script handles bootstrapping Infix using startup-config or a failure-config (see later commits) on error. When used with the -b (bootstrap) option, and failure to load the give file, the script sets a Finit condition and goes to runlevel 9. The condition can be used to trigger loading of a failure-config to go to a Fail Secure mode. Signed-off-by: Joachim Wiberg --- package/confd/sysrepo.conf | 10 ++++++++-- src/confd/Makefile.am | 2 +- src/confd/confd-load.sh | 39 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 3 deletions(-) create mode 100755 src/confd/confd-load.sh diff --git a/package/confd/sysrepo.conf b/package/confd/sysrepo.conf index 2ac2af57..f12904a9 100644 --- a/package/confd/sysrepo.conf +++ b/package/confd/sysrepo.conf @@ -1,10 +1,16 @@ +#set DEBUG=1 + run name:bootstrap log:prio:user.notice \ [S] /lib/infix/cfg-bootstrap -- Bootstrapping YANG models + service name:sysrepo log \ - [S12345789] sysrepo-plugind -f -p /run/sysrepo.pid -n -- Configuration daemon + [S12345789] sysrepo-plugind -f -p /run/sysrepo.pid -n -v3 -- Configuration daemon + +# Bootstrap system with startup-config run name:startup log:prio:user.notice \ - [S] sysrepocfg -I/cfg/startup-config.cfg -f json \ + [S] /usr/libexec/confd/confd-load.sh -b startup-config \ -- Loading startup-config + service name:netopeer log \ [12345789] netopeer2-server -F -t 60 \ -- NETCONF server diff --git a/src/confd/Makefile.am b/src/confd/Makefile.am index 7f49515b..33fa7833 100644 --- a/src/confd/Makefile.am +++ b/src/confd/Makefile.am @@ -1,5 +1,5 @@ SUBDIRS = src yang dist_doc_DATA = README.md LICENSE -pkglibexec_SCRIPTS = confd-bootstrap.sh +pkglibexec_SCRIPTS = confd-bootstrap.sh confd-load.sh DISTCLEANFILES = *~ *.d ACLOCAL_AMFLAGS = -I m4 diff --git a/src/confd/confd-load.sh b/src/confd/confd-load.sh new file mode 100755 index 00000000..f2b42f81 --- /dev/null +++ b/src/confd/confd-load.sh @@ -0,0 +1,39 @@ +#!/bin/sh +# cond-load.sh [-b] +# +# Import a configuration to the sysrepo datastore using `sysrepocfg -Ifile` +# +# If the '-b' option is used we set the Finit condition if +# sysrepocfg returns OK. This to be able to detect and trigger the Infix +# Fail Secure Mode at boot. +# +set -e + +if [ "$1" = "-b" ]; then + bootstrap=true + shift +else + bootstrap=false +fi +config=$1 +fn=/cfg/${config}.cfg + +if [ ! -f "$fn" ]; then + if [ ! -f "$config" ]; then + logger -sik -p user.error "No such file, $fn, aborting!" + exit 1 + fi + fn=$config +fi + +if ! sysrepocfg -v3 -I"$fn" -f json; then + if eval $bootstrap; then + logger -sik -p user.error "Failed bootstrapping system, reverting to Fail Secure mode!" + initctl -nbq cond set fail-startup + initctl -nbq runlevel 9 + else + logger -sik -p user.error "Failed loading $fn, aborting!" + fi + exit 1 +fi +logger -sik -p user.notice "Loaded $fn successfully."