postfwd defaults: keep lines with the wanted value, no reload if unchanged
- Never comment out a line that already has the wanted value, even if a differing setting of the same variable precedes it. - update_postfwd_defaults.sh: leave postfwd alone if nothing changed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+29
-20
@@ -1,6 +1,6 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# Update /etc/default/postfwd (idempotent) and reload postfwd.
|
||||
# Update /etc/default/postfwd (idempotent) and restart postfwd if needed.
|
||||
#
|
||||
# For every variable below:
|
||||
# - already set to the wanted value -> nothing to do
|
||||
@@ -14,8 +14,7 @@
|
||||
# - file changed -> 'systemctl restart postfwd'
|
||||
# (ARGS, INET and PORT are start parameters of the
|
||||
# daemon - a reload does NOT apply them)
|
||||
# - file unchanged -> 'systemctl reload postfwd'
|
||||
# (rereads postfwd.cf and the file: lists)
|
||||
# - file unchanged -> postfwd is left alone (no restart, no reload)
|
||||
#
|
||||
|
||||
postfwd_default_file="/etc/default/postfwd"
|
||||
@@ -80,19 +79,30 @@ set_postfwd_default() {
|
||||
_tmp_file="$(mktemp)"
|
||||
awk -v var="$_var" -v val="$_val" '
|
||||
function unq(s) {
|
||||
gsub(/^[ \t]+|[ \t]+$/, "", s)
|
||||
gsub(/^[ \t\r]+|[ \t\r]+$/, "", s)
|
||||
if (s ~ /^".*"$/ || s ~ /^\047.*\047$/) s = substr(s, 2, length(s) - 2)
|
||||
return s
|
||||
}
|
||||
$0 ~ "^[ \t]*" var "=" {
|
||||
cur = $0 ; sub("^[ \t]*" var "=", "", cur)
|
||||
if (! done && unq(cur) == unq(val)) { print ; done = 1 ; next }
|
||||
print "#" $0
|
||||
if (! done) { print var "=" val ; done = 1 }
|
||||
next
|
||||
function is_set(s) { return s ~ ("^[ \t]*" var "=") }
|
||||
function is_wanted(s) {
|
||||
sub("^[ \t]*" var "=", "", s)
|
||||
return unq(s) == unq(val)
|
||||
}
|
||||
# - Read the whole file first: a line with the wanted value must stay
|
||||
# - untouched, no matter which other settings of VAR precede it.
|
||||
{
|
||||
line[NR] = $0
|
||||
if (is_set($0) && is_wanted($0)) found = 1
|
||||
}
|
||||
END {
|
||||
for (i = 1; i <= NR; i++) {
|
||||
if (! is_set(line[i])) { print line[i] ; continue }
|
||||
if (is_wanted(line[i])) { print line[i] ; continue }
|
||||
print "#" line[i]
|
||||
if (! found) { print var "=" val ; found = 1 }
|
||||
}
|
||||
if (! found) print var "=" val
|
||||
}
|
||||
{ print }
|
||||
END { if (! done) print var "=" val }
|
||||
' "$postfwd_default_file" > "$_tmp_file" 2> "$log_file"
|
||||
|
||||
if [[ $? -ne 0 ]] ; then
|
||||
@@ -149,18 +159,17 @@ done
|
||||
echo ""
|
||||
|
||||
if $errors ; then
|
||||
fatal "Errors while updating '${postfwd_default_file}' - postfwd NOT reloaded."
|
||||
fatal "Errors while updating '${postfwd_default_file}' - postfwd NOT restarted."
|
||||
fi
|
||||
|
||||
if $file_changed ; then
|
||||
echononl " Restart postfwd (start parameters changed)"
|
||||
_action="restart"
|
||||
else
|
||||
echononl " Reload postfwd"
|
||||
_action="reload"
|
||||
if ! $file_changed ; then
|
||||
echo " Nothing changed - postfwd is left alone."
|
||||
echo ""
|
||||
clean_up 0
|
||||
fi
|
||||
|
||||
if systemctl $_action postfwd > "$log_file" 2>&1 ; then
|
||||
echononl " Restart postfwd (start parameters changed)"
|
||||
if systemctl restart postfwd > "$log_file" 2>&1 ; then
|
||||
echo_ok
|
||||
else
|
||||
echo_failed
|
||||
|
||||
Reference in New Issue
Block a user