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
changes/68/21568/6
Manuel Montecelo 8 years ago
parent a015a4ac1d
commit 13bc4ca34b

@ -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 <file> should provide more details."
log_info "Running /usr/share/ngcp-ngcpcfg/helper/tt2-wrapper <file>"
log_info "or inspecting temporary ${tt_tmp_output_file}"
log_info "should provide more details."
fi
if [ -L "$output_file" ] ; then

Loading…
Cancel
Save