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.
ngcpcfg/functions/main

251 lines
6.8 KiB

# Filename: /usr/share/ngcp-ngcpcfg/functions/main
# Purpose: helper functions for ngcpcfg
################################################################################
# console output including timestamps {{{
timestamp_replacementchars='' # unset by default
console_output() {
if [ -z "${TIME_FORMAT:-}" ] ; then
printf -- "$*"
return 0
fi
local timestamp="$(date "$TIME_FORMAT")"
# indent depending on number of characters in date output
timestamp_replacementchars="$(printf -- "%s: " "$timestamp" | sed 's/./ /g')"
printf -- "$timestamp: $*"
}
# }}}
## logging functions {{{
log_info() {
logger -t ngcpcfg -- "$*"
console_output "$*\n"
}
# info without ending newline
log_info_n() {
logger -t ngcpcfg -- "$*"
console_output "$*"
}
log_warn() {
logger -t ngcpcfg -- "Warning: $*"
console_output "Warning: $*\n"
}
log_error() {
logger -t ngcpcfg -- "Error: $*"
console_output "Error: $*\n" >&2
}
log_debug() {
if [ -n "${DEBUG:-}" ] ; then
logger -t ngcpcfg -- "Debug: $*"
console_output "DEBUG: $*\n"
fi
}
get_branch_status() {
log_debug "cd $NGCPCTL_MAIN"
cd "$NGCPCTL_MAIN"
log_debug "git rev-parse HEAD"
local LOCAL=$(git rev-parse HEAD)
log_debug "git rev-parse @{u}"
local REMOTE=$(git rev-parse @{u})
log_debug "git merge-base HEAD @{u}"
local BASE=$(git merge-base HEAD @{u})
if [ "$LOCAL" = "$REMOTE" ]; then
# Up-to-date
return 0
elif [ "$LOCAL" = "$BASE" ]; then
# Need to pull
return 1
elif [ "$REMOTE" = "$BASE" ]; then
# Need to push
return 2
else
# Diverged
return 3
fi
}
## }}}
## important variables we depend on to operate successfully {{{
# support test suite which requires system independent configuration
if [ -r ngcpcfg-testsuite.cfg ] ; then
. ngcpcfg-testsuite.cfg
else
if [ -r /etc/ngcp-config/ngcpcfg.cfg ] ; then
. /etc/ngcp-config/ngcpcfg.cfg
log_debug "sourced configuration file /etc/ngcp-config/ngcpcfg.cfg"
if [ -d /etc/ngcp-config/ngcpcfg.d ] ; then
for file in /etc/ngcp-config/ngcpcfg.d/*.cfg ; do
if test -r $file ; then
. $file
log_debug "sourced configuration file $file"
fi
done
fi
elif [ -r /etc/ngcp-config-crypted.tgz.gpg ] ; then
log_error "Configuration pool locked. Please contact your distributor. Exiting."
exit 1
else
log_error "Could not read configuration file /etc/ngcp-config/ngcpcfg.cfg. Exiting."
exit 1
fi
fi
if ! [ -r "$NGCPCTL_CONFIG" ] ; then
log_error "Configuration file ${NGCPCTL_CONFIG} does not exist (unconfigured?) - exiting."
exit 1
fi
if ! [ -r "$CONSTANTS_CONFIG" ] ; then
log_error "Constants file ${CONSTANTS_CONFIG} does not exist (unconfigured?) - exiting."
exit 1
fi
if ! [ -n "${NETWORK_CONFIG:-}" ] ; then
log_warn "NETWORK_CONFIG is not configured in $NGCPCTL_CONFIG - continuing anyway."
elif ! [ -r "$NETWORK_CONFIG" ] ; then
log_error "Constants file ${NETWORK_CONFIG} does not exist (unconfigured?) - exiting."
exit 1
fi
if ! [ -d "$TEMPLATE_POOL_BASE" ] ; then
log_error "No template directory (${TEMPLATE_POOL_BASE}) found - exiting."
exit 1
fi
if [ -d "${EXTRA_CONFIG_DIR:-}" ] && ls ${EXTRA_CONFIG_DIR}/*.yml &>/dev/null ; then
log_debug "EXTRA_CONFIG_DIR is configured and *.yml files are present, setting EXTRA_CONFIG_FILES"
EXTRA_CONFIG_FILES=${EXTRA_CONFIG_DIR}/*.yml
fi
## }}}
## environment variables {{{
export PN="ngcpcfg"
export HNAME="$(hostname)"
export NNAME="$(ngcp-nodename)"
# avoid warnings by perl script complaining about locales
export LANG=C
export LC_ALL=C
# make sure it's available in all helper scripts
[ -n "${DEBUG:-}" ] && export DEBUG
[ -n "${NO_DB_SYNC:-}" ] && export NO_DB_SYNC
# export for access via build_config etc
export CONFIG_POOL
export HOST_CONFIG
export LOCAL_CONFIG
export NGCPCTL_CONFIG
export CONSTANTS_CONFIG
export NETWORK_CONFIG
export EXTRA_CONFIG_DIR
export EXTRA_CONFIG_FILES
## }}}
## HA / carrier features {{{
if [ -r /usr/share/ngcp-ngcpcfg/functions/ha_features ] ; then
. /usr/share/ngcp-ngcpcfg/functions/ha_features
set_ha_file # set $HA_FILE for usage in generate_template_list
fi
if [ -r /usr/share/ngcp-ngcpcfg/functions/carrier_features ] ; then
. /usr/share/ngcp-ngcpcfg/functions/carrier_features
fi
## }}}
## functions {{{
generate_template_list() {
[ -n "$TEMPLATE_POOL_BASE" ] || return 1
local filelist_prepared=$(mktemp)
local filelist_final=$(mktemp)
for dir in ${CONFIG_POOL} ; do
[ -n "${dir}" ] || ( echo "${dir} doesn't exist" >&2 ; continue )
# iterate over all files
for file in $(find "$TEMPLATE_POOL_BASE/${dir}" -name \*.tt2 -o -name \*.tt2"${HA_FILE:-}") ; do
# *NO* arguments provided via cmdline
if [ -z "${1:-}" ] ; then
echo "$file" >> "${filelist_prepared}"
else
# arguments (file list/pattern) provided via cmdline
for arg in $* ; do
if echo $file | grep -q -- "${arg}" ; then
echo "$file" >> "${filelist_prepared}"
fi
done
fi
done
done
# remove all filenames where a preferred filename exists
# foo.customtt.tt2.spX > foo.customtt.tt2 > foo.tt2.spX > foo.tt2
for line in $(cat ${filelist_prepared}); do
# reduce filenames from "foo.*" to "foo"
if [ -n "${HA_FILE:-}" ] ; then
normalized_filename="${line%${HA_FILE}}"
else
normalized_filename="${line}"
fi
normalized_filename="${normalized_filename%.customtt.tt2}"
normalized_filename="${normalized_filename%.tt2}"
# foo.customtt.tt2.sp{1,2}
if [ -n "${HA_FILE:-}" ] ; then
if grep -q -- "^${normalized_filename}.customtt.tt2${HA_FILE:-}" "${filelist_prepared}" ; then
echo "${normalized_filename}.customtt.tt2${HA_FILE:-}" >> "${filelist_final}"
continue
fi
fi
# foo.customtt.tt2
if grep -q -- "^${normalized_filename}.customtt.tt2$" "${filelist_prepared}" ; then
echo "${normalized_filename}.customtt.tt2" >> "${filelist_final}"
continue
fi
# foo.tt2.sp{1,2}
if [ -n "${HA_FILE:-}" ] ; then
if grep -q -- "^${normalized_filename}.tt2${HA_FILE}" "${filelist_prepared}" ; then
echo "${normalized_filename}.tt2${HA_FILE}" >> "${filelist_final}"
continue
fi
fi
# file should be used, so list it
echo "$line" >> "${filelist_final}"
done
# output file list, make sure we provide the file names just once
sort -u ${filelist_final}
if [ -n "${DEBUG:-}" ] ; then
# send to stderr since stdout is used from outside
log_debug "Not removing temporary filelist files since we are in debug mode:" >&2
log_debug " filelist_prepared = ${filelist_prepared}" >&2
log_debug " filelist_final = ${filelist_final}" >&2
else
rm -f "${filelist_prepared}" "${filelist_final}"
fi
unset filelist_prepared filelist_final
}
## }}}
## END OF FILE #################################################################