#!/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_debug "Checking merge conflicts in ngcp configs:" if grep -rP --exclude-dir='.git' --exclude='*.dpkg-*' \ '^>>>>>>> [0-9a-fA-F]+' /etc/ngcp-config/ >/dev/null 2>&1 then log_error "ERROR: ngcp configs with Git merge conflicts found:" grep -rP --exclude-dir='.git' --exclude='*.dpkg-*' \ '^>>>>>>> [0-9a-fA-F]+' /etc/ngcp-config/ >&2 exit 1 else log_debug "No ngcp configs with merge conflicts found." fi } check_shared_storage() { # ensure there are no outstanding pull actions, # unless --no-action-failure is used (ignore then) if ! type -p execute_check_shared_storage &>/dev/null ; then log_debug "execute_check_shared_storage not available" return 0 fi log_debug "execute_check_shared_storage function, action pull" if [ "${NO_ACTION_FAILURE:-}" = "1" ] ; then if execute_check_shared_storage pull ; then log_debug "No outstanding pull actions identified (NO_ACTION_FAILURE=1)." else log_info "Ignoring outstanding pull actions as --no-action-failure option is enabled." fi return 0 fi execute_check_shared_storage pull && RC=0 || RC=$? if [ "$RC" = "0" ] ; then log_debug "No outstanding pull actions identified (NO_ACTION_FAILURE unset)." else log_info "Outstanding pull actions have been identified (see ACTION_NEEDED), exiting." log_info "TIP: '--no-action-failure' forces execution within 'apply' anyway (use with care!)." return $RC 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 check_shared_storage || exit $? exit 0 ## END OF FILE #################################################################