mirror of https://github.com/sipwise/ngcpcfg.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
112 lines
2.9 KiB
112 lines
2.9 KiB
#!/bin/bash
|
|
# Purpose: check/validate YAML config files (syntax, encoding, ...)
|
|
################################################################################
|
|
|
|
set -e
|
|
set -u
|
|
|
|
# support testsuite
|
|
FUNCTIONS="${FUNCTIONS:-/usr/share/ngcp-ngcpcfg/functions/}"
|
|
HELPER="${HELPER:-/usr/share/ngcp-ngcpcfg/helper/}"
|
|
|
|
if ! [ -r "${FUNCTIONS}"/main ] ; then
|
|
printf "Error: %s/main could not be read. Exiting.\n" "${FUNCTIONS}" >&2
|
|
exit 1
|
|
fi
|
|
|
|
. "${FUNCTIONS}"/main
|
|
|
|
check_config_encoding() {
|
|
log_debug "Checking encoding for $config_files"
|
|
for f in $config_files ; do
|
|
if [ -r "$f" ] && ! file -L "$f" | grep -qe "UTF-8" -qe "ASCII" ; then
|
|
log_error "Encoding check of ${f} fails: neither ASCII nor UTF-8."
|
|
log_error "Please convert ${f} to UTF-8."
|
|
log_info
|
|
log_info "NOTE:"
|
|
log_info "* Check encoding via:"
|
|
log_info " # file ${f}"
|
|
log_info "* To convert ISO-8859/latin1 to UTF-8 execute:"
|
|
log_info " # iconv -f latin1 -t utf8 < ${f} > ${f}.tmp && mv ${f}.tmp ${f}"
|
|
exit 1
|
|
fi
|
|
done
|
|
}
|
|
|
|
check_config_syntax() {
|
|
log_debug "Checking for valid YAML syntax for $config_files"
|
|
for f in $config_files ; do
|
|
if [ -r "$f" ] ; then
|
|
# use YAML::Tiny for checking
|
|
log_debug "Validating main YAML syntax of ${f}"
|
|
if ! "${HELPER}/validate-yml" "${f}" 2>/dev/null ; then
|
|
log_error "Invalid file syntax in ${f}:"
|
|
"${HELPER}/validate-yml" "${f}"
|
|
exit 1
|
|
fi
|
|
fi
|
|
done
|
|
}
|
|
|
|
check_configs() {
|
|
check_config_encoding
|
|
check_config_syntax
|
|
}
|
|
|
|
validate_config() {
|
|
local rc=0
|
|
log_debug "Validating schema for main YAML files"
|
|
|
|
for f in $config_files ; do
|
|
local schema
|
|
schema="/usr/share/ngcp-cfg-schema/validate/$(basename "${f}")"
|
|
|
|
if ! [ -f "${schema}" ] ; then
|
|
continue
|
|
fi
|
|
|
|
if ! pkwalify -s -f "${schema}" "${f}" >/dev/null 2>&1 ; then
|
|
log_error "Invalid schema detected for ${f}"
|
|
pkwalify -f "${schema}" "${f}" >&2 || true
|
|
rc=1
|
|
fi
|
|
done
|
|
|
|
if [ "$rc" != "0" ] ; then
|
|
if [ -n "${NO_VALIDATE:-}" ] ; then
|
|
log_info "DANGEROUS ZONE: invalid configs detected, continue anyway due to option '--no-validate'"
|
|
else
|
|
log_error "Aborted, please fix issue(s) above and repeat."
|
|
exit 1
|
|
fi
|
|
fi
|
|
}
|
|
|
|
check_configs_conflicts() {
|
|
log_info "Checking merge conflicts in ngcp configs:"
|
|
if grep -rP --exclude-dir='.git' --exclude='*.dpkg-*' \
|
|
'^>>>>>>> [0-9a-fA-F]+' /etc/ngcp-config/ 2>&1
|
|
then
|
|
log_error "ERROR: ngcp configs with Git merge conflicts found."
|
|
exit 1
|
|
else
|
|
log_info "No ngcp configs with merge conflicts found."
|
|
fi
|
|
}
|
|
|
|
if [ "$*" = "" ] ; then
|
|
config_files="${NGCPCTL_CONFIG:-} ${HOST_CONFIG:-} ${LOCAL_CONFIG:-} ${NETWORK_CONFIG:-} ${EXTRA_CONFIG_FILES:-} ${CONSTANTS_CONFIG:-}"
|
|
else
|
|
config_files="$*"
|
|
fi
|
|
|
|
check_configs
|
|
if "${VALIDATE_SCHEMA:-false}" ; then
|
|
validate_config
|
|
fi
|
|
check_configs_conflicts
|
|
|
|
exit 0
|
|
|
|
## END OF FILE #################################################################
|