From ab491739baaa94b02822d91cff47843ec1b742f6 Mon Sep 17 00:00:00 2001 From: Matthias Fulz Date: Thu, 1 Jul 2021 12:20:37 +0200 Subject: [PATCH] Added log level as param --- archbuilder.in | 12 +++++++++++- lib/archbuilder.inc.sh | 32 +++++++++++++++++++++++++++++++- lib/buildah.inc.sh | 3 +++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/archbuilder.in b/archbuilder.in index 6eec43c..57dc3dd 100644 --- a/archbuilder.in +++ b/archbuilder.in @@ -24,6 +24,7 @@ _OPT_DEPS=() _OPT_KEYS=() _OPT_CON_BUILD_USER="archbuilder" +_OPT_CON_LOG_LEVEL="" _OPT_CON_COPTIONS="" # actions to initialize runtime @@ -53,6 +54,7 @@ function usage() { echo -e " -r, --repo \t\t\t\tHost path to use as repository inside the container. This can be used to avoid" echo -e " \t\t\t\thanding over dependencies via command line arguments as they will be added to this repo" echo -e " -s, --silent \t\t\t\tMake container silent: No output from container commands will be send to shell." + echo -e " -l, --level \t\t\t\tLog level to use: Possible values are DEBUG, INFO, WARN, SUCCESS or ERROR" echo -e " --version \t\t\t\tPrint version information." echo echo "coptions:" @@ -61,7 +63,7 @@ function usage() { } options=$(getopt \ - -o hkn:m:p:d:r:e:s \ + -o hkn:m:p:d:r:e:sl: \ -l "help" \ -l keep \ -l name: \ @@ -69,6 +71,7 @@ options=$(getopt \ -l dep: \ -l repo: \ -l silent: \ + -l level: \ -l version \ -l key: -- "$@" 2>/dev/null) @@ -101,6 +104,13 @@ while true; do -s|--silent) _FLAG_SILENT=1 ;; + -l|--level) + shift + check_log_level "${1}" \ + || exit_error "${err}" + LOG_LEVEL_STDOUT="${1}" + LOG_LEVEL_LOG="${1}" + ;; --version) echo -e "archbuilder v${archbuilder_version}" exit 0 diff --git a/lib/archbuilder.inc.sh b/lib/archbuilder.inc.sh index dfc5559..9425082 100644 --- a/lib/archbuilder.inc.sh +++ b/lib/archbuilder.inc.sh @@ -6,7 +6,7 @@ function exit_error() { } function check_mode() { - log_debug "set_mode for val '${1}' called" + log_debug "check_mode for val '${1}' called" if [ -z "${1}" ] then err="Mode not given" @@ -33,6 +33,30 @@ function check_mode() { esac } +function check_log_level() { + log_debug "check_log_level for val '${1}' called" + if [ -z "${1}" ] + then + err="Log level cannot be empty" + return 1 + fi + + case "$1" in + "DEBUG" \ + | "INFO" \ + | "WARN" \ + | "SUCCESS" \ + | "ERROR") + log_debug "Log level set to '${1}'" + return 0 + ;; + *) + err="Log level '${1}' invalid" + return 1 + ;; + esac +} + function check_flag() { if [ -z "${2}" ] then @@ -44,6 +68,12 @@ function check_flag() { function set_env() { log_info "Checking environment and params" + + check_log_level "${LOG_LEVEL_STDOUT}" \ + || exit_error "Error setting log level for stdout: '${err}'" + check_log_level "${LOG_LEVEL_LOG}" \ + || exit_error "Error setting log level for log file: '${err}'" + log_debug "Verifying mode '${_OPT_MODE}'" check_mode "${_OPT_MODE}" \ || exit_error "${err}" 1 diff --git a/lib/buildah.inc.sh b/lib/buildah.inc.sh index c19eaab..d21f754 100644 --- a/lib/buildah.inc.sh +++ b/lib/buildah.inc.sh @@ -164,15 +164,18 @@ function buildah_build() { arrExt=(${arrExt//=/ }) ext=$(echo "${arrExt[1]}" | cut -d "'" -f 2) + log_debug "Running makepkg" exec_cmd buildah run --user "${_OPT_CON_BUILD_USER}" ${_BUILDAH_PARAMS} "${_BUILDAH_CONT}" \ bash -c "cd ${_BUILDAH_MKPKG_PATH} && ${_BUILDAH_MAKEPKG_ENV} makepkg ${_BUILDAH_MAKEPKG_FLAGS} ${_OPT_CON_COPTIONS}" + log_debug "Copying to cache repo" test_null "PKGDEST" "${PKGDEST}" && { exec_cmd buildah run --user "${_OPT_CON_BUILD_USER}" ${_BUILDAH_PARAMS} "${_BUILDAH_CONT}" bash -c "cp ${_BUILDAH_MKPKG_PATH}/*${ext} ${_BUILDAH_CACHE_REPO_PATH}" } || { exec_cmd buildah run --user "${_OPT_CON_BUILD_USER}" ${_BUILDAH_PARAMS} "${_BUILDAH_CONT}" bash -c "cp ${_BUILDAH_PKGDEST_PATH}/*${ext} ${_BUILDAH_CACHE_REPO_PATH}" } + log_debug "Updating cache repo" exec_cmd buildah run --user "${_OPT_CON_BUILD_USER}" ${_BUILDAH_PARAMS} "${_BUILDAH_CONT}" bash -c "repo-add -n ${_BUILDAH_CACHE_REPO} ${_BUILDAH_CACHE_REPO_PATH}/*${ext}" buildah_exit