mapletree/lxc1/keepalived/deploy_keepalived.sh
2026-01-30 13:21:51 -07:00

55 lines
No EOL
1.7 KiB
Bash

#!/bin/bash
# Move to the folder where this script lives
cd "$(dirname "$0")"
# Load the variables Komodo just wrote
if [ -f .env ]; then
export $(grep -v '^#' .env | xargs)
else
echo "ERROR: .env file not found in $(pwd)"
exit 1
fi
# Define paths relative to the current folder
CONFIG_DIR="/docker/keepalived/config"
CHECKS_DIR="/docker/keepalived/checks"
HEADER_TPL="/docker/keepalived/keepalived_header.tpl"
SLOT_TPL="/docker/keepalived/keepalived_slot.tpl"
CONFIG_FILE="${CONFIG_DIR}/keepalived.conf"
mkdir -p "$CONFIG_DIR" "$CHECKS_DIR"
# Build Header
cp "$HEADER_TPL" "$CONFIG_FILE"
sed -i "s/{{LXC_NUM}}/${LXC_NUM}/g" "$CONFIG_FILE"
# Parse Slots
while IFS=',' read -r s_num s_name s_dep s_state s_prio s_peer s_vip junk
do
[[ -z "$s_num" ]] && continue
[[ -z "$s_vip" ]] && continue
echo "Processing Slot $s_num: $s_name"
TEMP_SLOT="/tmp/slot_${s_num}.conf"
cp "$SLOT_TPL" "$TEMP_SLOT"
FULL_PASS="${BASE_PASSWORD}-${s_num}"
SHORT_PASS="${FULL_PASS:0:8}"
sed -i "s/{{SLOT_NUM}}/${s_num}/g" "$TEMP_SLOT"
sed -i "s/{{LXC_NUM}}/${LXC_NUM}/g" "$TEMP_SLOT"
sed -i "s/{{SVC_NAME}}/${s_name}/g" "$TEMP_SLOT"
sed -i "s/{{SVC_DEP}}/${s_dep}/g" "$TEMP_SLOT"
sed -i "s/{{STATE}}/${s_state}/g" "$TEMP_SLOT"
sed -i "s/{{PRIORITY}}/${s_prio}/g" "$TEMP_SLOT"
sed -i "s/{{PEER}}/${s_peer}/g" "$TEMP_SLOT"
sed -i "s/{{SVC_VIP}}/${s_vip}/g" "$TEMP_SLOT"
sed -i "s/{{AUTH_PASS}}/${SHORT_PASS}/g" "$TEMP_SLOT"
cat "$TEMP_SLOT" >> "$CONFIG_FILE"
rm "$TEMP_SLOT"
done <<< "$SLOT_DEFINITIONS"
# Move check script to its final home
cp ./check_services.sh "${CHECKS_DIR}/check_services.sh"
chmod +x "${CHECKS_DIR}/check_services.sh"