diff --git a/board/common/rootfs/etc/profile.d/alias.sh b/board/common/rootfs/etc/profile.d/alias.sh index a20f7e5b..6ee78653 100644 --- a/board/common/rootfs/etc/profile.d/alias.sh +++ b/board/common/rootfs/etc/profile.d/alias.sh @@ -1,2 +1 @@ alias cli='clish' -alias cfg='sysrepocfg -f json' diff --git a/board/common/rootfs/usr/bin/cfg b/board/common/rootfs/usr/bin/cfg new file mode 100755 index 00000000..7e539b27 --- /dev/null +++ b/board/common/rootfs/usr/bin/cfg @@ -0,0 +1,58 @@ +#!/bin/sh +# User-friendly wrapper for sysrepocfg +# TODO: add import/export, copy, ... + +# Edit YANG binary types using sysrepo, base64, and duct tape. +edit() +{ + xpath=$1 + if [ -z "$xpath" ]; then + echo "Usage: cfg edit \"/full/xpath/to/binary/leaf\"" + exit 1 + fi + + if tmp=$(sysrepocfg -G "$xpath"); then + file=$(mktemp) + + echo "$tmp" | base64 -d > "$file" + if /usr/bin/editor "$file"; then + tmp=$(base64 -w0 < "$file") + sysrepocfg -S "$xpath" -u "$tmp" + fi + + rm -f "$file" + else + echo "Failed to retrieve value for $xpath" + exit 1 + fi +} + +usage() +{ + echo "Usage:" + echo " cfg CMD [ARG]" + echo + echo "Command:" + echo " edit XPATH Edit YANG binary type" + echo " help This help text" + echo + echo "As a backwards compatible fallback, this script forwards" + echo "all other commands as options to sysrepocfg." + echo + + exit 0 +} + +cmd=$1; shift +case $cmd in + edit) + edit "$1" + ;; + help) + usage + ;; + *) + set -- "$cmd" "$@" + exec sysrepocfg -f json "$@" + ;; +esac diff --git a/doc/scripting.md b/doc/scripting.md index e41794f0..a2258bc3 100644 --- a/doc/scripting.md +++ b/doc/scripting.md @@ -653,6 +653,70 @@ on interface *e0*. ~$ ``` +### Change a Binary Setting + +A YANG `binary` type setting is Base64 encoded and requires a little bit +more tricks. We take the opportunity to showcase a shell script helper: +`/usr/bin/text-editor`, which works just like the `text-editor` command +in the CLI, but this one takes an XPath argument to the binary leaf to +edit. + +Stripped down, it looks something like this: + +```bash +if tmp=$(sysrepocfg -G "$xpath"); then + file=$(mktemp) + + echo "$tmp" | base64 -d > "$file" + if edit "$file"; then + tmp=$(base64 -w0 < "$file") + sysrepocfg -S "$xpath" -u "$tmp" + fi + + rm -f "$file" +else + echo "Failed to retrieve value for $xpath" + exit 1 +fi +``` + +An example container configuration, with an embedded file that is +mounted to `/var/www/index.html` can look like this: + +```json + "infix-containers:containers": { + "container": [ + { + "name": "web", + "image": "oci-archive:/lib/oci/curios-httpd-latest.tar.gz", + "hostname": "web", + "network": { + "interface": [ + { + "name": "veth-sys0" + } + ] + }, + "mount": [ + { + "name": "index.html", + "content": "PCFET0NUWVBFIGh0bWwjibberish.shortened.down==", + "target": "/var/www/index.html" + } + ] + } + ] + } +``` + +The command to edit this file, and restart the container with the new +contents, look like this: + +``` +admin@infix:~$ cfg edit "/infix-containers:containers/container[name='web']/mount[name='index.html']/content" +``` + + ### Backup Configuration Using sysrepocfg And scp Displaying running or startup configuration is possible with