MT#10071 Add 'check' option for YAML validations

Change-Id: I52938bfeb99a6c45d7227a3b02739cbd5f6232fa
changes/60/1660/3
Alexander Lutay 11 years ago
parent 92536298f7
commit 0a3867a8a4

@ -13,6 +13,7 @@ sbin/ngcp-network usr/sbin/
sbin/ngcp-sync-constants usr/sbin/
sbin/ngcpcfg usr/sbin/
scripts/build usr/share/ngcp-ngcpcfg/scripts/
scripts/check usr/share/ngcp-ngcpcfg/scripts/
scripts/commit usr/share/ngcp-ngcpcfg/scripts/
scripts/diff usr/share/ngcp-ngcpcfg/scripts/
scripts/etckeeper usr/share/ngcp-ngcpcfg/scripts/

@ -190,15 +190,20 @@ Actions
**apply**::
Executes the _build_, _services_ and _commit_ commands in a batch (assuming each
command worked as expected). This option serves as a shortcut for the most
commonly executed commands.
Executes the _check_, _build_, _services_ and _commit_ commands in a batch
(assuming each command worked as expected). This option serves as a shortcut for
the most commonly executed commands.
**check** [<config_files_or_directories>|<pattern>]::
Validate YAML files before performing any further actions.
**build** [--modified-only] [<files_or_directories>|<pattern>]::
Generate configuration files, based on values defined the central yml files and
based on the templates in the configuration tree (/etc/ngcp-config/templates by
default). The *--modified-only* option checks for _modified_ and _uncommitted_
default). Central yml files will be validated for integrity before building using
_check_ action. The *--modified-only* option checks for _modified_ and _uncommitted_
configuration files (central yml files and templates) and builds the relevant
files only. If a central yml file is modified it builds all configuration files.
If changes are in template files only just the according template files are

@ -78,6 +78,11 @@ status() {
"${SCRIPTS}"/status $*
}
check() {
log_debug "${SCRIPTS}/check $*"
"${SCRIPTS}"/check $*
}
usage() {
# make sure to output errors on stderr
[ "${1:-}" = "1" ] && exec >&2
@ -91,6 +96,7 @@ usage() {
$PN help - display this help screen and exit
$PN version - display program version and exit
$PN initialise - initialise setup (to be executed only once on setup)
$PN check - validate YAML configuration files
"
# display only if ngcp-ngcpcfg-ha is available
@ -111,7 +117,7 @@ usage() {
fi
printf " $PN services [opts] - execute service handlers for modified configuration files
$PN apply - a short-cut for build-services-commit-etckeeper
$PN apply - a short-cut for check-build-services-commit-etckeeper
$PN status - display status of configuration file\n\n"
printf "For further usage information and options visit the ngcpcfg manpage.\n"
@ -124,7 +130,8 @@ version() {
}
case ${1:-} in
build) shift ; build "$*" ;;
build) shift ; check && build "$*" ;;
check) shift ; check "$*" ;;
commit) shift ; commit "$*" ;;
initialise) initialise;;
push) shift ; push "$*" ;;

@ -24,34 +24,6 @@ if [ -n "${1:-}" ] ; then
esac
fi
# make sure encoding is OK
for f in ${NGCPCTL_CONFIG:-} ${HOST_CONFIG:-} ${LOCAL_CONFIG:-} ${NETWORK_CONFIG:-} ${EXTRA_CONFIG_FILES:-} ${CONSTANTS_CONFIG:-} ; do
if [ -r "$f" ] && ! file "$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 for valid YAML syntax
for f in ${NGCPCTL_CONFIG:-} ${HOST_CONFIG:-} ${LOCAL_CONFIG:-} ${NETWORK_CONFIG:-} ${EXTRA_CONFIG_FILES:-} ${CONSTANTS_CONFIG:-} ; 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
# Kill all previous started tt2-daemon Perl processes if they were not stopped properly
killall tt2-daemon 2>/dev/null || true

@ -0,0 +1,66 @@
#!/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: ${FUNCTIONS}/main could not be read. Exiting.\n" >&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 "$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
}
if [ "$*" = "" ] ; then
config_files="${NGCPCTL_CONFIG:-} ${HOST_CONFIG:-} ${LOCAL_CONFIG:-} ${NETWORK_CONFIG:-} ${EXTRA_CONFIG_FILES:-} ${CONSTANTS_CONFIG:-}"
else
config_files="$*"
fi
check_configs
exit 0
## END OF FILE #################################################################
Loading…
Cancel
Save