From 13bc4ca34b2573e2d0d7b020f1e4cd558eb5766e Mon Sep 17 00:00:00 2001 From: Manuel Montecelo Date: Wed, 30 May 2018 17:49:51 +0200 Subject: [PATCH] TT#37401 Add safety measures, create tmp files before moving to output file In this way, if the file fails to be generated (e.g. because of a broken .yml file) the old file is preserved intact. It doesn't protect from all failures (in particular, it is ineffective against wrong content due to programming errors), but it's safer than having an empty file or truncated as a result of the parsing/generation being stopped due to incorrect files or external events. This is added in the context of creating new /etc/hosts and /etc/network/interfaces files because we found the problem at this point, and having those files empty completely breaks the system. Change-Id: I01e8ef9d6971506aa3f8d9da174ce890ea83393c --- helper/build_config | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/helper/build_config b/helper/build_config index 10628bbe..b3e83d9a 100755 --- a/helper/build_config +++ b/helper/build_config @@ -76,9 +76,16 @@ umask 0077 TT_WRAPPER="${HELPER}/tt2-wrapper" +# generate in a temporary location, then move if successful +tt_tmp_output_file_base="$(basename "${output_file}")" +tt_tmp_output_file="$(mktemp ngcpcfg."${tt_tmp_output_file_base}".XXXXXXXXXX)" + log_debug "Output file ${output_file} based on ${input_file}" -log_debug "Executing: $TT_WRAPPER ${input_file} > ${output_file}" -if "$TT_WRAPPER" "${input_file}" > "${output_file}" 2>/dev/null ; then +log_debug "Executing: $TT_WRAPPER ${input_file} > ${tt_tmp_output_file}" +log_debug " and: cat ${tt_tmp_output_file} > ${output_file}" +if "$TT_WRAPPER" "${input_file}" > "${tt_tmp_output_file}" 2>/dev/null && + cat "${tt_tmp_output_file}" > "${output_file}" 2>/dev/null ; then + rm -f "${tt_tmp_output_file}" log_info "Generating ${output_file}: OK" RC=0 else @@ -89,8 +96,9 @@ else for f in "${input_file}" ${host_conf:-} ${local_conf:-} "$NGCPCTL_CONFIG" "${NETWORK_CONFIG:-}" ${EXTRA_CONFIG_FILES:-} "$CONSTANTS_CONFIG" ; do [ -r "$f" ] && log_info "$f" done - log_info "Running /usr/share/ngcp-ngcpcfg/helper/tt2-wrapper should provide more details." - + log_info "Running /usr/share/ngcp-ngcpcfg/helper/tt2-wrapper " + log_info "or inspecting temporary ${tt_tmp_output_file}" + log_info "should provide more details." fi if [ -L "$output_file" ] ; then