TT#63758 Add --summary-only option support

Sometimes (e.g. on 'push-parallel') we need to call ngcpcfg on
remote host and generating summary only output about the results.
It simplifies complex 'push-parallel' codebase which is critical
for huge Carrier installation.

P.S. we cannot print summary message all the time to console output,
as a lot of code depends on the ngcpcfg output, e.g.:

> root@web01a:~# ngcp-ppa gerrit_alutay_63758_push_parallel_2
> ERROR: There are uncommitted changes in config files:
> 2019-10-02 11:59:38 web01a: Successfully executed 'diff' on 'web01a'
> ERROR: Please commit them or discard
> root@web01a:~#

Instead, let's be backward compatible with printing on console,
but always print this message to log file (as we sometimes need to know
ngcpcfg exit code and it was impossible to find it in log file previously.)

Change-Id: I9aad0912796500cddb23d1f3eaa428b0b39152d0
changes/49/33849/11
Alexander Lutay 7 years ago
parent 15537ab533
commit fa3593aea1

@ -277,6 +277,11 @@ some initial files have to be generated from templates before ngcpcfg final setu
Ignore schema validation results for YAML files (syntax check is still performed).
**--summary-only** <action> [<further_options>]::
Hide all the output except the last summary line.
Useful for integration of ngcpcfg in other scripts/tools.
**--validate** <action> [<further_options>]::
Force schema validation for YAML files even if validation is disabled in config.

@ -21,35 +21,40 @@ console_output() {
# }}}
## logging functions {{{
log_info() {
log_only() {
logger -t ngcpcfg --id="${NGCPCFG_PID}" -- "$*"
}
log_info() {
log_only "$*"
console_output "$*\n"
}
# info without ending newline
log_info_n() {
logger -t ngcpcfg --id="${NGCPCFG_PID}" -- "$*"
log_only "$*"
console_output "$*"
}
log_warn() {
logger -t ngcpcfg --id="${NGCPCFG_PID}" -- "Warning: $*"
log_only "Warning: $*"
console_output "Warning: $*\n"
}
# warning without ending newline
log_warn_n() {
logger -t ngcpcfg --id="${NGCPCFG_PID}" -- "Warning: $*"
log_only "Warning: $*"
console_output "Warning: $*"
}
log_error() {
logger -t ngcpcfg --id="${NGCPCFG_PID}" -- "Error: $*"
log_only "Error: $*"
console_output "Error: $*\n" >&2
}
log_debug() {
if [ -n "${DEBUG:-}" ] ; then
logger -t ngcpcfg --id="${NGCPCFG_PID}" -- "Debug: $*"
log_only "Debug: $*"
console_output "DEBUG: $*\n" >&2
fi
}

@ -33,10 +33,30 @@ ngcpcfg_restore_perms() {
}
action() {
local rc=0
ACTION="$1"
shift
log_debug "${SCRIPTS}/${ACTION} $*"
"${SCRIPTS}/${ACTION}" "$@"
[ -z "${SUMMARY_ONLY:-}" ] && SUMMARY_ONLY=0
if [ "${SUMMARY_ONLY}" == "1" ]; then
log_debug "${SCRIPTS}/${ACTION} $* >/dev/null 2>&1"
"${SCRIPTS}/${ACTION}" "$@" >/dev/null 2>&1 || rc=$?
else
log_debug "${SCRIPTS}/${ACTION} $*"
"${SCRIPTS}/${ACTION}" "$@" || rc=$?
fi
if [ "${rc}" == "0" ]; then
local msg="Successfully executed '${ACTION}' on '${HNAME}'"
[ "${SUMMARY_ONLY}" == "1" ] || log_only "${msg}"
[ "${SUMMARY_ONLY}" == "0" ] || log_info "${msg}"
else
local msg="Failed to call action '${ACTION}' on '${HNAME}' (see logs on '${HNAME}')"
[ "${SUMMARY_ONLY}" == "1" ] || log_only "Error: ${msg}"
[ "${SUMMARY_ONLY}" == "0" ] || log_error "${msg}"
exit "${rc}"
fi
}
usage() {
@ -172,6 +192,7 @@ case ${1:-} in
--no-action-failure) export NO_ACTION_FAILURE=1; shift ; "$0" "$@" ;;
--no-check-origin) export NO_CHECK_ORIGIN=1; shift ; "$0" "$@" ;;
--validate) export VALIDATE=1; shift ; "$0" "$@" ;;
--summary-only) export SUMMARY_ONLY=1; shift ; "$0" "$@" ;;
-h|--help|help) usage ; exit 0;;
-v|--version|version) version ; exit 0;;
*) usage 1; exit 1;;

Loading…
Cancel
Save