Update registry scripts

This commit is contained in:
Simon Marsh 2026-01-23 10:45:44 +00:00
parent f8199a0902
commit 36e3570111
No known key found for this signature in database
GPG key ID: E9B4156C1659C079
6 changed files with 258 additions and 56 deletions

View file

@ -1,19 +1,90 @@
#!/bin/sh #!/bin/sh -e
###########################################################################
#
# dn42 registry - object validation
#
###########################################################################
if [ "$#" -eq "0" ] mntner="$1"
then
echo "Usage: $0 YOUR-MNT" if [ -z "$mntner" ]
exit then
>&2 echo "Usage: $0 YOUR-MNT"
exit 1
fi fi
BASE="$(readlink -f "$0" 2>/dev/null || python -c 'import os,sys;print(os.path.realpath(sys.argv[1]))' "$0")" check_script='utils/schema-check/dn42_schema_local.py'
BASE="$(dirname "$BASE")"
cd "$BASE" || exit 1 ###########################################################################
# determine registry directory
#
# this will fail if the script is in the PATH or is sourced but those
# both seem unlikely. In any case if it does fail an env var can be used
# to override the check
rdir="$REGDIR"
if [ -z "$rdir" ]
then
rdir=$(cd -- "$(dirname -- "$0")" && pwd)
fi
if ! [ -x "${rdir}/${check_script}" ]
then
>&2 cat <<EOF
ERROR: Unable to automatically find the registry directory,
or the script '$check_script' is not executable
You can set the directory manually using the
REGDIR environment variable.
For example:
REGDIR='path/to/registry' $0 $mntner
EOF
exit 1
fi
# switch to the registry directory
cd "$rdir"
###########################################################################
# perform the validation
if [ "$mntner" = "--all" ]
then
# check everything
if ! "$check_script" -v scan data/
then
>&2 echo 'Schema validation failed!'
exit 1
fi
if [ "$1" = "--all" ]; then
utils/schema-check/dn42-schema.py -v scan data/ || ( echo "Schema validation failed, please check above!" ; exit 1 )
else else
utils/schema-check/dn42-schema.py -v scan data/ -f "data/mntner/$1" || ( echo "Schema validation for mntner object failed, please check above!" ; exit 1 ) # check single mntner
utils/schema-check/dn42-schema.py -v scan data/ -m "$1" || ( echo "Schema validation for related objects failed, please check above!" ; exit 1 ) mfile="data/mntner/$mntner"
if ! [ -f "$mfile" ]
then
>&2 echo "MNTNER object does not exist: $mfile"
exit 1
fi
if ! "$check_script" -v scan data/ -f "$mfile"
then
>&2 echo 'Schema validation for mntner object failed!'
exit 1
fi
if ! "$check_script" -v scan data/ -m "$mntner"
then
>&2 echo 'Schema validation for related objects failed!'
exit 1
fi
fi fi
# all good
exit 0
###########################################################################
# end of file

113
check-pol
View file

@ -1,18 +1,105 @@
#!/usr/bin/env bash #!/bin/sh -e
set -o pipefail ###########################################################################
#
# dn42 registry - policy checks
#
###########################################################################
if [ $# -eq 0 ] commit="$1"
then mntner="$2"
echo "Usage: $0 COMMIT YOUR-MNT"
exit if [ -z "$commit" ] || [ -z "$mntner" ]
then
>&2 echo "Usage: $0 COMMIT YOUR-MNT"
exit 1
fi fi
BASE="$(readlink -f "$0" 2>/dev/null || python -c 'import os,sys;print(os.path.realpath(sys.argv[1]))' "$0")" check_script='utils/schema-check/dn42_schema_local.py'
BASE="$(dirname "$BASE")" exitcode=0
cd "$BASE" || exit 1
git diff --name-only "$1" | while IFS='/' read -ra LINE; do ###########################################################################
if [[ "${LINE[0]}" = "data" && -n "${LINE[2]}" ]]; then # determine registry directory
utils/schema-check/dn42-schema.py -v policy "${LINE[1]}" "${LINE[2]}" "$2" #
# this will fail if the script is in the PATH or is sourced but those
# both seem unlikely. In any case if it does fail an env var can be used
# to override the check
rdir="$REGDIR"
if [ -z "$rdir" ]
then
rdir=$(cd -- "$(dirname -- "$0")" && pwd)
fi fi
done
if ! [ -x "${rdir}/${check_script}" ]
then
>&2 cat <<EOF
ERROR: Unable to automatically find the registry directory,
or the script '$check_script' is not executable
You can set the directory manually using the
REGDIR environment variable.
For example:
REGDIR='path/to/registry' $0 $commit $mntner
EOF
exit 1
fi
# switch to the registry directory
cd "$rdir"
###########################################################################
# find each changed file, using git diff, and then run the policy
# check against each object that has changed
#
# the shell loop is a bit contrived but is required to maintain POSIX
# compatibility and avoid the need for subshells
# loop through each file that has changed
while IFS= read -r filename
do
# extract the object type and name from the filename
IFS='/'
# shellcheck disable=SC2086
set -- $filename
IFS=
path="$1"
type="$2"
object="$3"
# check the file really is a registry object
# (including if it still exists, as it may have been deleted)
if [ -f "$filename" ] && [ "$path" = 'data' ] && \
[ -n "$type" ] && [ -n "$object" ]
then
# run the check script
if ! "$check_script" -v policy \
"$type" "$object" "$mntner" "$commit"
then
# update exit code on failure
exitcode=1
fi
fi
done <<EOF
$(git diff --name-only "$commit")
EOF
###########################################################################
# output a message and set exit code on failure
if [ "$exitcode" -ne 0 ]
then
>&2 echo 'FAILED: check the output for details'
exit "$exitcode"
fi
# all good
exit 0
###########################################################################
# end of file

View file

@ -1,15 +1,54 @@
#!/bin/sh #!/bin/sh -e
###########################################################################
#
# dn42 registry - object formatting
#
###########################################################################
if [ "$#" -eq "0" ] mntner="$1"
then
echo "Usage: $0 YOUR-MNT" if [ -z "$mntner" ]
exit then
>&2 echo "Usage: $0 YOUR-MNT"
exit 1
fi fi
BASE="$(readlink -f "$0" 2>/dev/null || python -c 'import os,sys;print(os.path.realpath(sys.argv[1]))' "$0")" check_script='utils/schema-check/dn42_schema_local.py'
BASE="$(dirname "$BASE")"
grep -lrE "(\s|:)$1(\s|\$)" "$BASE/data/" | while read -r line; do ###########################################################################
utils/schema-check/dn42-schema.py fmt -i "$line" # determine registry directory
done #
# this will fail if the script is in the PATH or is sourced but those
# both seem unlikely. In any case if it does fail an env var can be used
# to override the check
rdir="$REGDIR"
if [ -z "$rdir" ]
then
rdir=$(cd -- "$(dirname -- "$0")" && pwd)
fi
if ! [ -x "${rdir}/${check_script}" ]
then
>&2 cat <<EOF
ERROR: Unable to automatically find the registry directory,
or the script '$check_script' is not executable
You can set the directory manually using the
REGDIR environment variable.
For example:
REGDIR='path/to/registry' $0 $mntner
EOF
exit 1
fi
###########################################################################
grep -lrE "(\s|:)$mntner(\s|\$)" "${rdir}/data/" | \
while read -r line; do
"$check_script" fmt -i "$line"
done
###########################################################################
# end of file

View file

@ -13,6 +13,6 @@ cd "$BASE" || exit 1
git diff --name-only "$1" | while IFS='/' read -ra LINE; do git diff --name-only "$1" | while IFS='/' read -ra LINE; do
if [[ "${LINE[0]}" = "data" && -n "${LINE[2]}" ]]; then if [[ "${LINE[0]}" = "data" && -n "${LINE[2]}" ]]; then
utils/schema-check/dn42_schema_local.py -v policy "${LINE[1]}" "${LINE[2]}" "$2" "$1" utils/schema-check/dn42-schema.py -v policy "${LINE[1]}" "${LINE[2]}" "$2"
fi fi
done done

View file

@ -22,6 +22,11 @@ SCHEMA_NAMESPACE = "dn42."
REGISTRY_URL = "git@git.dn42.dev:dn42/registry.git" if not "REG_URL" in os.environ else os.environ["REG_URL"] REGISTRY_URL = "git@git.dn42.dev:dn42/registry.git" if not "REG_URL" in os.environ else os.environ["REG_URL"]
REGISTRY_COMMIT = "dn42registry/master" REGISTRY_COMMIT = "dn42registry/master"
# CLEVEL contains terminal escape codes for coloring log levels (overwriting it with LEVEL which doesn't)
log.CLEVEL = log.LEVEL if "DN42REG_NO_COLOR" in os.environ else log.CLEVEL
log.CMSG = log.MSG if "DN42REG_NO_COLOR" in os.environ else log.CMSG # not actually used in this context
log.CMULTI = "[{1}] {2}" if "DN42REG_NO_COLOR" in os.environ else log.CMULTI
class SchemaDOM: class SchemaDOM:
"schema" "schema"