diff --git a/lxc1/p0-infrastructure/keepalived/deploy_keepalived.sh b/lxc1/p0-infrastructure/keepalived/deploy_keepalived.sh index 136ce72..5929a55 100644 --- a/lxc1/p0-infrastructure/keepalived/deploy_keepalived.sh +++ b/lxc1/p0-infrastructure/keepalived/deploy_keepalived.sh @@ -1,37 +1,47 @@ #!/bin/bash -cd /docker/keepalived +# 1. ROBUST ENV SOURCING (Strips quotes and exports vars) +NV_PATH="/docker/keepalived/.env" +if [ -f "$ENV_PATH" ]; then + while read -r line || [ -n "$line" ]; do + # Skip comments and empty lines + [[ "$line" =~ ^#.*$ ]] && continue + [[ -z "$line" ]] && continue + + # Export the variable, removing literal double quotes + export "${line//\"/}" + done < "$ENV_PATH" + echo "Successfully loaded environment from $ENV_PATH" +else + echo "ERROR: .env file not found!" + exit 1 +fi -# 1. MANUAL EXTRACTION -get_env_val() { - grep "^${1}=" .env | cut -d'=' -f2- | sed 's/^"//;s/"$//' -} - -LXC_NUM=$(get_env_val "LXC_NUM") -SLOT_DEFINITIONS=$(get_env_val "SLOT_DEFINITIONS") -BASE_PASSWORD=$(get_env_val "BASE_PASSWORD") - -# 2. PATHS +# 2. PATHS & HEADER CONFIG_FILE="./config/keepalived.conf" mkdir -p ./config ./checks cp "./keepalived_header.tpl" "$CONFIG_FILE" + +# Replace global header vars sed -i "s/{{LXC_NUM}}/${LXC_NUM}/g" "$CONFIG_FILE" # 3. DUAL-STAGE PARSING -# First, we split the string into individual slots using the semicolon +# Split the string into individual slots using the semicolon IFS=';' read -ra SLOTS <<< "$SLOT_DEFINITIONS" for SLOT in "${SLOTS[@]}"; do - # Now we split the individual slot by commas + # Split the individual slot by commas IFS=',' read -r s_num s_name s_dep s_state s_prio s_peer s_vip junk <<< "$SLOT" [[ -z "$s_num" ]] && continue - echo "Processing Slot $s_num: $s_name" + echo "Processing Slot $s_num: $s_name (VIP: $s_vip)" TEMP_SLOT="/tmp/slot_${s_num}.conf" cp "./keepalived_slot.tpl" "$TEMP_SLOT" + # Generate unique auth pass from the base password AUTH_PASS="${BASE_PASSWORD:0:7}-${s_num}" + # Perform replacements 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" @@ -42,10 +52,13 @@ for SLOT in "${SLOTS[@]}"; do sed -i "s/{{SVC_VIP}}/${s_vip}/g" "$TEMP_SLOT" sed -i "s/{{AUTH_PASS}}/${AUTH_PASS}/g" "$TEMP_SLOT" + # Append to main config and cleanup temp cat "$TEMP_SLOT" >> "$CONFIG_FILE" rm "$TEMP_SLOT" done -# 4. FINALIZE +# 4. FINALIZE SCRIPTS cp ./check_services.sh ./checks/check_services.sh -chmod +x ./checks/check_services.sh \ No newline at end of file +chmod +x ./checks/check_services.sh + +echo "Keepalived configuration generated successfully." \ No newline at end of file diff --git a/lxc1/p0-infrastructure/01_deploy_mgdb_dbs.sh b/lxc1/p0-infrastructure/mgdb/01_deploy_mgdb_dbs.sh similarity index 100% rename from lxc1/p0-infrastructure/01_deploy_mgdb_dbs.sh rename to lxc1/p0-infrastructure/mgdb/01_deploy_mgdb_dbs.sh diff --git a/lxc1/p0-infrastructure/02_deploy_minio_buckets.sh b/lxc1/p0-infrastructure/minio/02_deploy_minio_buckets.sh similarity index 100% rename from lxc1/p0-infrastructure/02_deploy_minio_buckets.sh rename to lxc1/p0-infrastructure/minio/02_deploy_minio_buckets.sh