All checks were successful
continuous-integration/drone/push Build is passing
120 lines
3.5 KiB
Bash
120 lines
3.5 KiB
Bash
#!/bin/bash
|
|
|
|
TARGET_CFG="/etc/dtms/vctp.yml"
|
|
SOURCE_CFG="${TARGET_CFG}.rpmnew"
|
|
|
|
extract_setting_key_lines() {
|
|
local file="$1"
|
|
awk '
|
|
/^settings:[[:space:]]*$/ { in_settings = 1; next }
|
|
in_settings && /^[^[:space:]]/ { in_settings = 0 }
|
|
in_settings && $0 ~ /^ [A-Za-z0-9_]+:[[:space:]]*/ {
|
|
key = $1
|
|
sub(":", "", key)
|
|
print key "\t" $0
|
|
}
|
|
' "$file"
|
|
}
|
|
|
|
merge_missing_settings_from_rpmnew() {
|
|
local target="$1"
|
|
local source="$2"
|
|
|
|
if [ ! -f "$target" ] || [ ! -f "$source" ]; then
|
|
return 0
|
|
fi
|
|
|
|
local src_pairs target_pairs missing_lines merged_file
|
|
src_pairs="$(mktemp /tmp/vctp-postinstall-src-XXXXXX)" || return 0
|
|
target_pairs="$(mktemp /tmp/vctp-postinstall-target-XXXXXX)" || { rm -f "$src_pairs"; return 0; }
|
|
missing_lines="$(mktemp /tmp/vctp-postinstall-missing-XXXXXX)" || {
|
|
rm -f "$src_pairs" "$target_pairs"
|
|
return 0
|
|
}
|
|
merged_file="$(mktemp /tmp/vctp-postinstall-merged-XXXXXX)" || {
|
|
rm -f "$src_pairs" "$target_pairs" "$missing_lines"
|
|
return 0
|
|
}
|
|
|
|
extract_setting_key_lines "$source" > "$src_pairs"
|
|
extract_setting_key_lines "$target" > "$target_pairs"
|
|
|
|
declare -A existing_keys=()
|
|
while IFS=$'\t' read -r key _; do
|
|
[ -n "$key" ] || continue
|
|
existing_keys["$key"]=1
|
|
done < "$target_pairs"
|
|
|
|
local added=0
|
|
: > "$missing_lines"
|
|
while IFS=$'\t' read -r key line; do
|
|
[ -n "$key" ] || continue
|
|
if [ -z "${existing_keys[$key]+x}" ]; then
|
|
if [ "$added" -eq 0 ]; then
|
|
echo " # Added automatically by RPM postinstall from vctp.yml.rpmnew defaults." >> "$missing_lines"
|
|
fi
|
|
echo "$line" >> "$missing_lines"
|
|
existing_keys["$key"]=1
|
|
added=$((added + 1))
|
|
fi
|
|
done < "$src_pairs"
|
|
|
|
if [ "$added" -gt 0 ]; then
|
|
awk -v missing_file="$missing_lines" '
|
|
function print_missing( line) {
|
|
while ((getline line < missing_file) > 0) {
|
|
print line
|
|
}
|
|
close(missing_file)
|
|
}
|
|
BEGIN { in_settings = 0; inserted = 0 }
|
|
{
|
|
if ($0 ~ /^settings:[[:space:]]*$/) {
|
|
in_settings = 1
|
|
print
|
|
next
|
|
}
|
|
if (in_settings && $0 ~ /^[^[:space:]]/) {
|
|
if (!inserted) {
|
|
print_missing()
|
|
inserted = 1
|
|
}
|
|
in_settings = 0
|
|
}
|
|
print
|
|
}
|
|
END {
|
|
if (in_settings && !inserted) {
|
|
print_missing()
|
|
}
|
|
}
|
|
' "$target" > "$merged_file" && cat "$merged_file" > "$target"
|
|
|
|
if [ "$?" -eq 0 ]; then
|
|
echo "vCTP postinstall: added ${added} missing settings key(s) to ${target}"
|
|
fi
|
|
fi
|
|
|
|
rm -f "$src_pairs" "$target_pairs" "$missing_lines" "$merged_file"
|
|
}
|
|
|
|
merge_missing_settings_from_rpmnew "$TARGET_CFG" "$SOURCE_CFG" || :
|
|
|
|
if [ -f "$TARGET_CFG" ]; then
|
|
chown root:dtms "$TARGET_CFG" || :
|
|
chmod 640 "$TARGET_CFG" || :
|
|
fi
|
|
if [ -f "$SOURCE_CFG" ]; then
|
|
chown root:dtms "$SOURCE_CFG" || :
|
|
chmod 640 "$SOURCE_CFG" || :
|
|
fi
|
|
|
|
if command -v systemctl >/dev/null 2>&1; then
|
|
systemctl daemon-reload || :
|
|
if [ "${1:-0}" -eq 1 ]; then
|
|
systemctl enable --now vctp.service || :
|
|
else
|
|
systemctl try-restart vctp.service || :
|
|
fi
|
|
fi
|