From b1b2307a74f79b72007fcc5e592d9537669fbd85 Mon Sep 17 00:00:00 2001 From: Thiago Canozzo Lahr Date: Wed, 20 Nov 2024 08:47:16 -0300 Subject: [PATCH] feat: add redirect_stderr_to_stdout propery Added the new 'redirect_stderr_to_stdout' property, an optional feature available exclusively for the command collector. When set to true, this property redirects all error messages (stderr) to standard output (stdout), ensuring they are written to the output file. --- CHANGELOG.md | 3 +++ lib/command_collector.sh | 10 ++++---- lib/parse_artifact.sh | 18 +++++++++----- lib/presigned_url_transfer.sh | 44 ----------------------------------- lib/run_command.sh | 32 ++++++++++++------------- lib/validate_artifact.sh | 12 ++++++++++ 6 files changed, 49 insertions(+), 70 deletions(-) delete mode 100644 lib/presigned_url_transfer.sh diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f24ae3..7b95d86 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,3 +2,6 @@ ## DEVELOPMENT VERSION +### New Artifacts Properties + +- Added the new 'redirect_stderr_to_stdout' property, an optional feature available exclusively for the command collector. When set to true, this property redirects all error messages (stderr) to standard output (stdout), ensuring they are written to the output file. \ No newline at end of file diff --git a/lib/command_collector.sh b/lib/command_collector.sh index a443d8c..0b5bf96 100644 --- a/lib/command_collector.sh +++ b/lib/command_collector.sh @@ -9,6 +9,7 @@ # string output_directory: ful path to the output directory # string output_file: output file name (optional) # boolean compress_output_file: compress output file (optional) (default: false) +# boolean redirect_stderr_to_stdout: redirect stderr to stdout (optional) (default: false) # Returns: # none _command_collector() @@ -18,6 +19,7 @@ _command_collector() __cc_output_directory="${3:-}" __cc_output_file="${4:-}" __cc_compress_output_file="${5:-false}" + __cc_redirect_stderr_to_stdout="${6:-false}" if [ -z "${__cc_command}" ]; then _log_msg ERR "_command_collector: empty command parameter" @@ -59,7 +61,7 @@ _command_collector() fi _verbose_msg "${__UAC_VERBOSE_CMD_PREFIX}${__cc_new_command}" - _run_command "${__cc_new_command}" \ + _run_command "${__cc_new_command}" "${__cc_redirect_stderr_to_stdout}" \ >>"${__cc_new_output_directory}/${__cc_new_output_file}" # remove output file if it is empty @@ -77,7 +79,7 @@ _command_collector() _verbose_msg "${__UAC_VERBOSE_CMD_PREFIX}${__cc_new_command}" ( cd "${__cc_new_output_directory}" \ - && _run_command "${__cc_new_command}" + && _run_command "${__cc_new_command}" "${__cc_redirect_stderr_to_stdout}" ) fi done @@ -96,7 +98,7 @@ _command_collector() __cc_command="${__cc_command} | gzip - | cat -" fi _verbose_msg "${__UAC_VERBOSE_CMD_PREFIX}${__cc_command}" - _run_command "${__cc_command}" \ + _run_command "${__cc_command}" "${__cc_redirect_stderr_to_stdout}" \ >>"${__cc_output_directory}/${__cc_output_file}" # remove output file if it is empty @@ -114,7 +116,7 @@ _command_collector() _verbose_msg "${__UAC_VERBOSE_CMD_PREFIX}${__cc_command}" ( cd "${__cc_output_directory}" \ - && _run_command "${__cc_command}" + && _run_command "${__cc_command}" "${__cc_redirect_stderr_to_stdout}" ) fi diff --git a/lib/parse_artifact.sh b/lib/parse_artifact.sh index 390e49a..1dc3e16 100644 --- a/lib/parse_artifact.sh +++ b/lib/parse_artifact.sh @@ -40,6 +40,7 @@ _parse_artifact() __pa_path_pattern="" __pa_path="" __pa_permissions="" + __pa_redirect_stderr_to_stdout=false __pa_supported_os="" } _cleanup_local_vars @@ -93,14 +94,14 @@ _parse_artifact() # run global condition command and skip collection if exit code is greater than 0 if echo "${__pa_condition}" | grep -q -E "^!"; then __pa_condition=`echo "${__pa_condition}" | sed -e 's|^! *||' 2>/dev/null` - if _run_command "${__pa_condition}" >/dev/null; then + if _run_command "${__pa_condition}" true >/dev/null; then _log_msg DBG "Global condition '${__pa_condition}' not satisfied. Skipping..." return 1 else _log_msg DBG "Global condition '${__pa_condition}' satisfied" fi else - if _run_command "${__pa_condition}" >/dev/null; then + if _run_command "${__pa_condition}" true >/dev/null; then _log_msg DBG "Global condition '${__pa_condition}' satisfied" else _log_msg DBG "Global condition '${__pa_condition}' not satisfied. Skipping..." @@ -201,6 +202,9 @@ _parse_artifact() "permissions:") __pa_permissions=`echo "${__pa_value}" | _array_to_psv 2>/dev/null` ;; + "redirect_stderr_to_stdout:") + __pa_redirect_stderr_to_stdout="${__pa_value}" + ;; "supported_os:") __pa_supported_os=`echo "${__pa_value}" | _array_to_psv 2>/dev/null` ;; @@ -232,7 +236,7 @@ _parse_artifact() # run local condition command and skip collection if exit code greater than 0 if echo "${__pa_condition}" | grep -q -E "^!"; then __pa_condition=`echo "${__pa_condition}" | sed -e 's|^! *||' 2>/dev/null` - if _run_command "${__pa_condition}" false >/dev/null; then + if _run_command "${__pa_condition}" true >/dev/null; then _log_msg DBG "Condition '${__pa_condition}' not satisfied. Skipping..." _cleanup_local_vars continue @@ -240,7 +244,7 @@ _parse_artifact() _log_msg DBG "Condition '${__pa_condition}' satisfied" fi else - if _run_command "${__pa_condition}" false >/dev/null; then + if _run_command "${__pa_condition}" true >/dev/null; then _log_msg DBG "Condition '${__pa_condition}' satisfied" else _log_msg DBG "Condition '${__pa_condition}' not satisfied. Skipping..." @@ -326,7 +330,8 @@ _parse_artifact() "${__pa_new_command}" \ "${__pa_new_output_directory}" \ "${__pa_new_output_file}" \ - "${__pa_compress_output_file}" + "${__pa_compress_output_file}" \ + "${__pa_redirect_stderr_to_stdout}" elif [ "${__pa_collector}" = "file" ]; then _find_based_collector \ "file" \ @@ -408,7 +413,8 @@ _parse_artifact() "${__pa_command}" \ "${__pa_output_directory}" \ "${__pa_output_file}" \ - "${__pa_compress_output_file}" + "${__pa_compress_output_file}" \ + "${__pa_redirect_stderr_to_stdout}" elif [ "${__pa_collector}" = "file" ]; then _find_based_collector \ "file" \ diff --git a/lib/presigned_url_transfer.sh b/lib/presigned_url_transfer.sh deleted file mode 100644 index e74757d..0000000 --- a/lib/presigned_url_transfer.sh +++ /dev/null @@ -1,44 +0,0 @@ -#!/bin/sh -# SPDX-License-Identifier: Apache-2.0 - -# Transfer file to a presigned URL. -# Arguments: -# string source: source file path -# string url: presigned URL -# Returns: -# boolean: true on success -# false on fail -presigned_url_transfer() -{ - __pu_source="${1:-}" - __pu_url="${2:-}" - - if command_exists "curl"; then - __pu_command="curl \ ---fail \ ---insecure \ ---request PUT \ ---header \"x-ms-blob-type: BlockBlob\" \ ---header \"Content-Type: application/octet-stream\" \ ---header \"Accept: */*\" \ ---header \"Expect: 100-continue\" \ ---upload-file \"${__pu_source}\" \ -\"${__pu_url}\"" - else - __pu_command="wget \ --O - \ ---quiet \ ---no-check-certificate \ ---method PUT \ ---header \"x-ms-blob-type: BlockBlob\" \ ---header \"Content-Type: application/octet-stream\" \ ---header \"Accept: */*\" \ ---header \"Expect: 100-continue\" \ ---body-file \"${__pu_source}\" \ -\"${__pu_url}\"" - fi - - _verbose_msg "${__UAC_VERBOSE_CMD_PREFIX}${__pu_command}" - _run_command "${__pu_command}" - -} \ No newline at end of file diff --git a/lib/run_command.sh b/lib/run_command.sh index 4d7f6f7..0d8dd05 100644 --- a/lib/run_command.sh +++ b/lib/run_command.sh @@ -5,13 +5,14 @@ # Run command. # Arguments: # string command: command (including arguments) -# boolean log_stderr: send stderr to uac.log (optional) (default: true) +# boolean redirect_stderr_to_stdout: redirect stderr to stdout (optional) (default: false) # Returns: # integer: command exit code _run_command() { __rc_command="${1:-}" - __rc_log_stderr="${2:-true}" + __rc_redirect_stderr_to_stdout="${2:-false}" + __rc_stderr_file="${__UAC_TEMP_DATA_DIR}/run_command.stderr.txt" if [ -z "${__rc_command}" ]; then _log_msg ERR "_run_command: empty command parameter" @@ -22,24 +23,23 @@ _run_command() return 1 fi - __rc_stderr_file="/dev/null" - if ${__rc_log_stderr}; then - __rc_stderr_file="${__UAC_TEMP_DATA_DIR}/run_command.stderr.txt" - fi + if ${__rc_redirect_stderr_to_stdout}; then + eval "${__rc_command}" 2>&1 + __rc_exit_code="$?" + else + eval "${__rc_command}" 2>"${__rc_stderr_file}" + __rc_exit_code="$?" - eval "${__rc_command}" \ - 2>"${__rc_stderr_file}" - __rc_exit_code="$?" + __rc_stderr="" + if [ -s "${__rc_stderr_file}" ]; then + __rc_stderr=`awk 'BEGIN {ORS="/n"} {print $0}' "${__rc_stderr_file}" | sed -e 's|/n$||' 2>/dev/null` + __rc_stderr=" 2> ${__rc_stderr}" + fi - __rc_stderr="" - if [ -s "${__UAC_TEMP_DATA_DIR}/run_command.stderr.txt" ] && ${__rc_log_stderr}; then - __rc_stderr=`awk 'BEGIN {ORS="/n"} {print $0}' "${__UAC_TEMP_DATA_DIR}/run_command.stderr.txt" | sed -e 's|/n$||' 2>/dev/null` - __rc_stderr=" 2> ${__rc_stderr}" + __rc_command=`echo "${__rc_command}" | awk 'BEGIN {ORS="/n"} {print $0}' | sed -e 's| *| |g' -e 's|/n$||' 2>/dev/null` + _log_msg CMD "${__rc_command}${__rc_stderr}" fi - __rc_command=`echo "${__rc_command}" | awk 'BEGIN {ORS="/n"} {print $0}' | sed -e 's| *| |g' -e 's|/n$||' 2>/dev/null` - _log_msg CMD "${__rc_command}${__rc_stderr}" - return "${__rc_exit_code}" } \ No newline at end of file diff --git a/lib/validate_artifact.sh b/lib/validate_artifact.sh index 3eb2228..cfd7057 100644 --- a/lib/validate_artifact.sh +++ b/lib/validate_artifact.sh @@ -42,6 +42,7 @@ _validate_artifact() __va_path_pattern="" __va_path="" __va_permissions="" + __va_redirect_stderr_to_stdout="" __va_supported_os="" } _cleanup_local_vars @@ -336,6 +337,13 @@ _validate_artifact() done __va_permissions="${__va_value}" ;; + "redirect_stderr_to_stdout:") + if [ "${__va_value}" != true ] && [ "${__va_value}" != false ]; then + _error_msg "artifact: 'redirect_stderr_to_stdout' must be 'true' or 'false'." + return 1 + fi + __va_redirect_stderr_to_stdout="${__va_value}" + ;; "supported_os:") if echo "${__va_value}" | grep -q -v -E "^\[.*\]$"; then _error_msg "artifact: 'supported_os' must be an array/list." @@ -465,6 +473,10 @@ _validate_artifact() _error_msg "artifact: invalid 'compress_output_file' property for '${__va_collector}' collector." return 1 fi + if [ -n "${__va_redirect_stderr_to_stdout}" ]; then + _error_msg "artifact: invalid 'redirect_stderr_to_stdout' property for '${__va_collector}' collector." + return 1 + fi if [ -n "${__va_foreach}" ]; then _error_msg "artifact: invalid 'foreach' property for '${__va_collector}' collector." return 1