From 1c2feb02fa8b1a5a8c9f6f091c12eada5e0b0738 Mon Sep 17 00:00:00 2001 From: Mike Danese Date: Mon, 31 Jul 2017 13:14:50 -0700 Subject: [PATCH] gce: make append_or_replace.. atomic Before this change, * the final echo is not atomically written to the target file * two concurrent callers will use the same tempfile --- cluster/gce/gci/configure-helper.sh | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/cluster/gce/gci/configure-helper.sh b/cluster/gce/gci/configure-helper.sh index beaee497d76..cebd8893572 100644 --- a/cluster/gce/gci/configure-helper.sh +++ b/cluster/gce/gci/configure-helper.sh @@ -239,10 +239,13 @@ function append_or_replace_prefixed_line { local -r file="${1:-}" local -r prefix="${2:-}" local -r suffix="${3:-}" + local -r dirname="$(dirname ${file})" + local -r tmpfile="$(mktemp -t filtered.XXXX --tmpdir=${dirname})" touch "${file}" - awk "substr(\$0,0,length(\"${prefix}\")) != \"${prefix}\" { print }" "${file}" > "${file}.filtered" && mv "${file}.filtered" "${file}" - echo "${prefix}${suffix}" >> "${file}" + awk "substr(\$0,0,length(\"${prefix}\")) != \"${prefix}\" { print }" "${file}" > "${tmpfile}" + echo "${prefix}${suffix}" >> "${tmpfile}" + mv "${tmpfile}" "${file}" } function create-node-pki {