Always remove temp_file on failure, other cleanup
This commit is contained in:
parent
95d7bae7b3
commit
1fba041aa7
1 changed files with 26 additions and 23 deletions
49
yadm
49
yadm
|
@ -364,6 +364,7 @@ function template_default() {
|
||||||
input="$1"
|
input="$1"
|
||||||
output="$2"
|
output="$2"
|
||||||
|
|
||||||
|
local awk_pgm
|
||||||
# the explicit "space + tab" character class used below is used because not
|
# the explicit "space + tab" character class used below is used because not
|
||||||
# all versions of awk seem to support the POSIX character classes [[:blank:]]
|
# all versions of awk seem to support the POSIX character classes [[:blank:]]
|
||||||
read -r -d '' awk_pgm << "EOF"
|
read -r -d '' awk_pgm << "EOF"
|
||||||
|
@ -451,10 +452,10 @@ function conditions() {
|
||||||
}
|
}
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
local source_dir=$(dirname "$input")
|
local source_dir yadm_classes content
|
||||||
local yadm_classes out
|
source_dir=$(dirname "$input")
|
||||||
yadm_classes="$(join_string $'\n' "${local_classes[@]}")"
|
yadm_classes=$(join_string $'\n' "${local_classes[@]}")
|
||||||
out=$("${AWK_PROGRAM[0]}" \
|
content=$("${AWK_PROGRAM[0]}" \
|
||||||
-v class="$local_class" \
|
-v class="$local_class" \
|
||||||
-v arch="$local_arch" \
|
-v arch="$local_arch" \
|
||||||
-v os="$local_system" \
|
-v os="$local_system" \
|
||||||
|
@ -468,15 +469,16 @@ EOF
|
||||||
"$awk_pgm" \
|
"$awk_pgm" \
|
||||||
"$input")
|
"$input")
|
||||||
|
|
||||||
move_file "$input" "$output" "$out"
|
move_file "$input" "$output" "$content"
|
||||||
}
|
}
|
||||||
|
|
||||||
function template_j2cli() {
|
function template_j2cli() {
|
||||||
local input="$1"
|
local input="$1"
|
||||||
local output="$2"
|
local output="$2"
|
||||||
local yadm_classes out
|
local yadm_classes content
|
||||||
yadm_classes="$(join_string $'\n' "${local_classes[@]}")"
|
|
||||||
out=$(YADM_CLASS="$local_class" \
|
yadm_classes=$(join_string $'\n' "${local_classes[@]}")
|
||||||
|
content=$(YADM_CLASS="$local_class" \
|
||||||
YADM_ARCH="$local_arch" \
|
YADM_ARCH="$local_arch" \
|
||||||
YADM_OS="$local_system" \
|
YADM_OS="$local_system" \
|
||||||
YADM_HOSTNAME="$local_host" \
|
YADM_HOSTNAME="$local_host" \
|
||||||
|
@ -487,15 +489,16 @@ function template_j2cli() {
|
||||||
YADM_CLASSES="$yadm_classes" \
|
YADM_CLASSES="$yadm_classes" \
|
||||||
"$J2CLI_PROGRAM" "$input")
|
"$J2CLI_PROGRAM" "$input")
|
||||||
|
|
||||||
move_file "$input" "$output" "$out" "$?"
|
move_file "$input" "$output" "$content" "$?"
|
||||||
}
|
}
|
||||||
|
|
||||||
function template_envtpl() {
|
function template_envtpl() {
|
||||||
local input="$1"
|
local input="$1"
|
||||||
local output="$2"
|
local output="$2"
|
||||||
local yadm_classes out
|
local yadm_classes content
|
||||||
yadm_classes="$(join_string $'\n' "${local_classes[@]}")"
|
|
||||||
out=$(YADM_CLASS="$local_class" \
|
yadm_classes=$(join_string $'\n' "${local_classes[@]}")
|
||||||
|
content=$(YADM_CLASS="$local_class" \
|
||||||
YADM_ARCH="$local_arch" \
|
YADM_ARCH="$local_arch" \
|
||||||
YADM_OS="$local_system" \
|
YADM_OS="$local_system" \
|
||||||
YADM_HOSTNAME="$local_host" \
|
YADM_HOSTNAME="$local_host" \
|
||||||
|
@ -506,16 +509,16 @@ function template_envtpl() {
|
||||||
YADM_CLASSES="$yadm_classes" \
|
YADM_CLASSES="$yadm_classes" \
|
||||||
"$ENVTPL_PROGRAM" --keep-template "$input")
|
"$ENVTPL_PROGRAM" --keep-template "$input")
|
||||||
|
|
||||||
move_file "$input" "$output" "$out" "$?"
|
move_file "$input" "$output" "$content" "$?"
|
||||||
}
|
}
|
||||||
|
|
||||||
function template_esh() {
|
function template_esh() {
|
||||||
local input="$1"
|
local input="$1"
|
||||||
local output="$2"
|
local output="$2"
|
||||||
|
local yadm_classes content
|
||||||
|
|
||||||
local yadm_classes out
|
|
||||||
yadm_classes="$(join_string $'\n' "${local_classes[@]}")"
|
yadm_classes="$(join_string $'\n' "${local_classes[@]}")"
|
||||||
out=$("$ESH_PROGRAM" "$input" \
|
content=$("$ESH_PROGRAM" "$input" \
|
||||||
YADM_CLASS="$local_class" \
|
YADM_CLASS="$local_class" \
|
||||||
YADM_ARCH="$local_arch" \
|
YADM_ARCH="$local_arch" \
|
||||||
YADM_OS="$local_system" \
|
YADM_OS="$local_system" \
|
||||||
|
@ -526,25 +529,25 @@ function template_esh() {
|
||||||
YADM_SOURCE="$input" \
|
YADM_SOURCE="$input" \
|
||||||
YADM_CLASSES="$yadm_classes")
|
YADM_CLASSES="$yadm_classes")
|
||||||
|
|
||||||
move_file "$input" "$output" "$out" "$?"
|
move_file "$input" "$output" "$content" "$?"
|
||||||
}
|
}
|
||||||
|
|
||||||
function move_file() {
|
function move_file() {
|
||||||
local input="$1"
|
local input="$1"
|
||||||
local output="$2"
|
local output="$2"
|
||||||
local new="$3"
|
local content="$3"
|
||||||
local err="${4:-}"
|
local err="${4:-}"
|
||||||
|
|
||||||
if [[ -s "$input" && -z "$new" ]]; then
|
if [[ -s "$input" && -z "$content" ]]; then
|
||||||
debug "Failed to create $output from template $input: error $err"
|
debug "Failed to create $output from template $input: error $err"
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ -r "$output" ]]; then
|
if [[ -r "$output" ]]; then
|
||||||
local old
|
local old_content
|
||||||
old=$(< "$output")
|
old_content=$(< "$output")
|
||||||
|
|
||||||
if [[ "$old" == "$new" ]]; then
|
if [[ "$old_content" == "$content" ]]; then
|
||||||
debug "Not rewriting file as contents have not changed: $output"
|
debug "Not rewriting file as contents have not changed: $output"
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
|
@ -567,16 +570,16 @@ function move_file() {
|
||||||
fi
|
fi
|
||||||
|
|
||||||
local temp_file="${output}.$$.$RANDOM"
|
local temp_file="${output}.$$.$RANDOM"
|
||||||
if printf '%s' "${new}" >"$temp_file"; then
|
if printf '%s' "$content" >"$temp_file"; then
|
||||||
if mv -f "$temp_file" "$output"; then
|
if mv -f "$temp_file" "$output"; then
|
||||||
copy_perms "$input" "$output"
|
copy_perms "$input" "$output"
|
||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
debug "Failed to rename '$temp_file' to '$output'"
|
debug "Failed to rename '$temp_file' to '$output'"
|
||||||
rm -f "$temp_file" &>/dev/null
|
|
||||||
else
|
else
|
||||||
debug "Failed to create '$temp_file' to generate '$output'"
|
debug "Failed to create '$temp_file' to generate '$output'"
|
||||||
fi
|
fi
|
||||||
|
rm -f "$temp_file" &>/dev/null
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue