From 87378b4fe3b8f7b0c65ea89cb2f20da64da5e091 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Wed, 1 Nov 2023 10:52:37 +0100 Subject: [PATCH] confd: fix and simplify .json snippet collation to factory-config First, fix collation of .json snippets to ensure they are sorted by number, regardless of which directory they originate from. Second, and with an unexpected twist, use /etc as the target directory for factory-config.cfg and failure-config.cfg. At first just to avoid having the resulting .gen and .cfg files in /run/confd/, but it also unintentionally gives us a way to provide a static /etc/factory-confg in the image. As the TODO says, not perfect but better than before. Third, update load script to use the same base path as bootstrap by sourcing the system /etc/confdrc. Signed-off-by: Joachim Wiberg --- src/confd/bin/bootstrap | 46 +++++++++++++++++++++++++++-------------- src/confd/bin/load | 9 +++++--- src/confd/confdrc | 5 +++-- 3 files changed, 40 insertions(+), 20 deletions(-) diff --git a/src/confd/bin/bootstrap b/src/confd/bin/bootstrap index 42cb34c7..32cf29df 100755 --- a/src/confd/bin/bootstrap +++ b/src/confd/bin/bootstrap @@ -1,6 +1,7 @@ #!/bin/sh # Bootstrap system factory-config, failure-config and sysrepo db. # +######################################################################## # The system factory-config and failure-config are derived from default # settings snippets, from /usr/share/confd/factory.d, and some generated # snippets, e.g., device unique password, hostname (based on base MAC @@ -9,7 +10,16 @@ # The resulting factory-config is used to create the syrepo db (below) # {factory} datastore. Hence, the factory-config file must match the # the YANG models of the active image. +######################################################################## +# NOTE: with the Infix defaults, a br2-external can provide a build-time +# /etc/factory-config.cfg to override the behavior of this script. # +# This applies also for /etc/failure-config.cfg, but we recommend +# strongly that you instead provide gen-err-custom, see below. +# +# TODO: Look for statically defined factory-config, based on system's +# product ID, or just custom site-specific factory on /cfg. +######################################################################## # /etc/confdrc controls the behavior or most of the gen-scripts, # customize in an overlay when using Infix as an br2-external. @@ -27,12 +37,24 @@ fi # shellcheck disable=SC1090 . "$RC" +# Gather all .json files in $dir, sort them numerically, and use jq +# magic to create a configuration file without duplicates. Allowing +# overrides of the Infix defaults in a br2-external. +# +# 10-foo.json -- Static Infix default +# 20-bar.json -- Generated Infix bar +# 30-config.json -- By br2-external provided gen-cfg-custom +# 30-foo.json -- Static br2-external replacing 10-foo.json +# +# Note: to override just the base hostname, used in gen-hostname, set +# BR2_TARGET_GENERIC_HOSTNAME in your br2-external's defconfig. +# # shellcheck disable=SC2046,SC2086 collate() { gen=$1; shift cfg=$1; shift - dir=$* + dir=$1 rm -f "$gen" jq -s 'reduce .[] as $item ({}; . * $item)' $(find $dir -name '*.json' | sort) >"$gen" @@ -43,15 +65,13 @@ collate() fi } -# TODO: Look for statically defined factory-config, based on the -# system's product ID, or just custom site-specific factory. -# -# If we haven't found a more specific, better match, settle for -# factory-config.gen as the system's factory-config. factory() { gen=$1 + # Fetch defaults, simplifies sort in collate() + cp "$FACTORY_DEFAULTS_D"/* "$FACTORY_D/" + # Create an overlay for /etc/hostname to change the default in an br2-external gen-hostname >"$FACTORY_D/20-hostname.json" # shellcheck disable=SC2086 @@ -63,20 +83,16 @@ factory() [ -x "$(command -v gen-ifs-custom)" ] && gen-ifs-custom >"$FACTORY_D/20-interfaces.json" [ -x "$(command -v gen-cfg-custom)" ] && gen-cfg-custom >"$FACTORY_D/30-config.json" - rm -f "$FACTORY_GEN" - # shellcheck disable=SC2046 - jq -s 'reduce .[] as $item ({}; . * $item)' \ - $(find "$FACTORY_DEFAULTS_D" "$FACTORY_D" -name '*.json' | sort) \ - >"$FACTORY_GEN" - chmod 444 "$FACTORY_GEN" - - collate "$FACTORY_GEN" "$FACTORY_CFG" "$FACTORY_DEFAULTS_D" "$FACTORY_D" + collate "$FACTORY_GEN" "$FACTORY_CFG" "$FACTORY_D" } failure() { gen=$1 + # Fetch defaults, simplifies sort in collate() + cp "$FAILURE_DEFAULTS_D"/* "$FAILURE_D" + gen-hostname "$FAIL_HOSTNAME" >"$FAILURE_D/20-hostname.json" gen-interfaces >"$FAILURE_D/20-interfaces.json" @@ -85,7 +101,7 @@ failure() # Optional failure/error config to generate (or override) for br2-externals [ -x "$(command -v gen-err-custom)" ] && gen-err-custom >"$FAILURE_D/30-error.json" - collate "$FAILURE_GEN" "$FAILURE_CFG" "$FAILURE_DEFAULTS_D" "$FAILURE_D" + collate "$FAILURE_GEN" "$FAILURE_CFG" "$FAILURE_D" } factory "$FACTORY_GEN" diff --git a/src/confd/bin/load b/src/confd/bin/load index 3cd2daa4..7e021666 100755 --- a/src/confd/bin/load +++ b/src/confd/bin/load @@ -9,6 +9,9 @@ # set -e +# shellcheck disable=SC1091 +. /etc/confdrc + if [ "$1" = "-b" ]; then bootstrap=true shift @@ -20,10 +23,10 @@ config=$1 if [ -f "$config" ]; then fn="$config" else - if [ -f "/cfg/${config}.cfg" ]; then - fn="/cfg/${config}.cfg" + if [ -f "$CFG_PATH_/${config}.cfg" ]; then + fn="$CFG_PATH_/${config}.cfg" else - fn="/run/confd/${config}.cfg" + fn="$SYS_PATH_/${config}.cfg" fi fi diff --git a/src/confd/confdrc b/src/confd/confdrc index 3ecf652c..2b4b7881 100644 --- a/src/confd/confdrc +++ b/src/confd/confdrc @@ -6,6 +6,7 @@ INIT_DATA=/etc/sysrepo/factory-default.json SEARCH=/usr/share/yang/modules/confd:/usr/share/yang/modules/libnetconf2:/usr/share/yang/modules/libyang:/usr/share/yang/modules/netopeer2:/usr/share/yang/modules/sysrepo CFG_PATH_=/cfg +SYS_PATH_=/etc RUN_PATH_=/run/confd # Static defaults, base Infix and any br2-external derivative. @@ -22,8 +23,8 @@ FACTORY_GEN=$RUN_PATH_/factory-config.gen FAILURE_GEN=$RUN_PATH_/failure-config.gen # The resulting .cfg files can be peristent (factory-config) or not. -FACTORY_CFG=$RUN_PATH_/factory-config.cfg -FAILURE_CFG=$RUN_PATH_/failure-config.cfg +FACTORY_CFG=$SYS_PATH_/factory-config.cfg +FAILURE_CFG=$SYS_PATH_/failure-config.cfg STARTUP_CFG=$CFG_PATH_/startup-config.cfg # Uncomment this line in to create a bridge (br0) with all (classified