#!/bin/bash # --- Configuration --- PYTHON_SCRIPT="active/aws_route53/unifi_to_aws.py" ZONE_ID_FILE="active/aws_route53/secrets/reeselink-zoneid" RECORDS_FILE="active/aws_route53/secrets/unifi_reeselink_records.json" # --- Colors for logging --- GREEN='\033[0;32m' RED='\033[0;31m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # --- Logging Function --- log() { echo -e "[$(date +'%Y-%m-%d %H:%M:%S')] $1" } error_exit() { echo -e "[$(date +'%Y-%m-%d %H:%M:%S')] ${RED}ERROR: $1${NC}" >&2 exit 1 } # --- 1. Pre-flight Checks --- log "${YELLOW}Starting Route53 update process...${NC}" if [[ ! -f "$PYTHON_SCRIPT" ]]; then error_exit "Python script not found at $PYTHON_SCRIPT" fi if [[ ! -f "$ZONE_ID_FILE" ]]; then error_exit "Zone ID file not found at $ZONE_ID_FILE" fi # --- 2. Run Python Script --- log "Running $PYTHON_SCRIPT to generate JSON records..." # Execute the python script python "$PYTHON_SCRIPT" # Check the exit code of the python script if [[ $? -eq 0 ]]; then log "${GREEN}Python script executed successfully.${NC}" else error_exit "Python script failed. Aborting AWS update to prevent corrupting DNS." fi # Verify the output file actually exists after the python run if [[ ! -f "$RECORDS_FILE" ]]; then error_exit "Python script reported success, but $RECORDS_FILE was not found." fi # --- 3. Update Route53 --- # Read the Zone ID from the secret file ZONE_ID=$(cat "$ZONE_ID_FILE" | tr -d '\n\r ') if [[ -z "$ZONE_ID" ]]; then error_exit "Zone ID file is empty or could not be read." fi log "Updating Route53 records for Zone ID: $ZONE_ID..." # Run the AWS CLI command # Using file:// prefix as required by AWS CLI for local files aws route53 change-resource-record-sets \ --hosted-zone-id "$ZONE_ID" \ --change-batch "file://$RECORDS_FILE" # Check the exit code of the AWS command if [[ $? -eq 0 ]]; then log "${GREEN}Route53 records updated successfully!${NC}" else error_exit "AWS CLI command failed. Check your AWS credentials and JSON formatting." fi log "${GREEN}Process complete.${NC}"