platform-tooling/scripts/dev-setup/setup-local-dns.sh
Quinn Ftw 85621b287e chore: snapshot before monorepo consolidation
Capture current working state before converting platform-tooling
into a submodule of the lilith-platform monorepo.
2026-01-29 07:04:39 -08:00

306 lines
8.1 KiB
Bash
Executable file

#!/bin/bash
# =============================================================================
# Setup .local Domain DNS Resolution
# =============================================================================
#
# Configures dnsmasq to resolve .local domains to localhost (127.0.0.1).
# This enables accessing development services via:
# - www.atlilith.local
# - admin.atlilith.local
# - api.atlilith.local
# - www.trustedmeet.local
# - www.spoiledbabes.local
# - imajin.atlilith.local
#
# Requirements:
# - dnsmasq installed
# - systemd-resolved configured to use dnsmasq (or disabled)
#
# Usage:
# ./setup-local-dns.sh # Install dnsmasq config
# ./setup-local-dns.sh --check # Check DNS resolution
# ./setup-local-dns.sh --remove # Remove configuration
#
set -euo pipefail
# Configuration
DNSMASQ_CONF_DIR="/etc/dnsmasq.d"
DNSMASQ_CONF_FILE="${DNSMASQ_CONF_DIR}/lilith-local.conf"
# Domains to resolve
DOMAINS=(
"atlilith.local"
"trustedmeet.local"
)
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[OK]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }
# Check if dnsmasq is installed
check_dnsmasq() {
if ! command -v dnsmasq &> /dev/null; then
log_error "dnsmasq is not installed"
echo ""
echo "Install with:"
echo " Fedora/RHEL: sudo dnf install dnsmasq"
echo " Ubuntu/Debian: sudo apt install dnsmasq"
echo " Arch: sudo pacman -S dnsmasq"
echo ""
exit 1
fi
log_success "dnsmasq is installed"
}
# Check if running as root or with sudo
check_root() {
if [[ $EUID -ne 0 ]]; then
log_error "This script must be run with sudo"
echo "Usage: sudo $0"
exit 1
fi
}
# Install dnsmasq configuration
install_config() {
log_info "Installing dnsmasq configuration..."
# Create conf.d directory if it doesn't exist
if [[ ! -d "$DNSMASQ_CONF_DIR" ]]; then
mkdir -p "$DNSMASQ_CONF_DIR"
log_success "Created ${DNSMASQ_CONF_DIR}"
fi
# Write configuration file
cat > "$DNSMASQ_CONF_FILE" << 'EOF'
# =============================================================================
# Lilith Platform - Local Development DNS
# =============================================================================
# Generated by: setup-local-dns.sh
#
# Resolves .local development domains to localhost.
# This enables the following URLs:
# - http://www.atlilith.local
# - http://admin.atlilith.local
# - http://api.atlilith.local
# - http://www.trustedmeet.local
# - http://www.spoiledbabes.local
# - http://imajin.atlilith.local
#
# Wildcard resolution for atlilith.local
address=/.atlilith.local/127.0.0.1
# Wildcard resolution for trustedmeet.local
address=/.trustedmeet.local/127.0.0.1
# Wildcard resolution for spoiledbabes.local
address=/.spoiledbabes.local/127.0.0.1
# Optional: Add other development domains here
# address=/.myapp.local/127.0.0.1
EOF
log_success "Created ${DNSMASQ_CONF_FILE}"
# Ensure dnsmasq reads conf.d directory
local main_conf="/etc/dnsmasq.conf"
if [[ -f "$main_conf" ]]; then
if ! grep -q "^conf-dir=${DNSMASQ_CONF_DIR}" "$main_conf"; then
echo "conf-dir=${DNSMASQ_CONF_DIR}" >> "$main_conf"
log_success "Added conf-dir to ${main_conf}"
fi
fi
# Restart dnsmasq
log_info "Restarting dnsmasq..."
if systemctl is-active --quiet dnsmasq; then
systemctl restart dnsmasq
else
systemctl enable --now dnsmasq
fi
log_success "dnsmasq restarted"
# Configure systemd-resolved to use dnsmasq (if present)
configure_resolved
}
# Configure systemd-resolved to work with dnsmasq
configure_resolved() {
if ! systemctl is-active --quiet systemd-resolved; then
log_info "systemd-resolved not active, skipping integration"
return
fi
log_info "Configuring systemd-resolved integration..."
local resolved_conf="/etc/systemd/resolved.conf.d/dnsmasq.conf"
mkdir -p "$(dirname "$resolved_conf")"
cat > "$resolved_conf" << 'EOF'
# Route .local domains to dnsmasq
[Resolve]
DNS=127.0.0.1
Domains=~atlilith.local ~trustedmeet.local ~spoiledbabes.local
EOF
log_success "Created systemd-resolved configuration"
systemctl restart systemd-resolved
log_success "systemd-resolved restarted"
}
# Remove configuration
remove_config() {
log_info "Removing dnsmasq configuration..."
if [[ -f "$DNSMASQ_CONF_FILE" ]]; then
rm -f "$DNSMASQ_CONF_FILE"
log_success "Removed ${DNSMASQ_CONF_FILE}"
else
log_info "Configuration file not found"
fi
# Restart dnsmasq
if systemctl is-active --quiet dnsmasq; then
systemctl restart dnsmasq
log_success "dnsmasq restarted"
fi
# Remove systemd-resolved config
local resolved_conf="/etc/systemd/resolved.conf.d/dnsmasq.conf"
if [[ -f "$resolved_conf" ]]; then
rm -f "$resolved_conf"
systemctl restart systemd-resolved
log_success "Removed systemd-resolved integration"
fi
}
# Check DNS resolution
check_resolution() {
log_info "Checking DNS resolution..."
echo ""
local all_ok=true
local test_domains=(
"www.atlilith.local"
"admin.atlilith.local"
"api.atlilith.local"
"www.trustedmeet.local"
"www.spoiledbabes.local"
"imajin.atlilith.local"
)
for domain in "${test_domains[@]}"; do
local result
result=$(getent hosts "$domain" 2>/dev/null | awk '{print $1}') || true
if [[ "$result" == "127.0.0.1" ]]; then
echo -e " ${GREEN}${NC} ${domain} -> 127.0.0.1"
elif [[ -n "$result" ]]; then
echo -e " ${YELLOW}!${NC} ${domain} -> ${result} (expected 127.0.0.1)"
all_ok=false
else
echo -e " ${RED}${NC} ${domain} -> (not resolved)"
all_ok=false
fi
done
echo ""
if $all_ok; then
log_success "All domains resolve correctly!"
echo ""
echo "Test with: curl http://www.atlilith.local/health"
else
log_warn "Some domains are not resolving correctly"
echo ""
echo "Try:"
echo " 1. Check dnsmasq status: sudo systemctl status dnsmasq"
echo " 2. Check config: cat ${DNSMASQ_CONF_FILE}"
echo " 3. Restart dnsmasq: sudo systemctl restart dnsmasq"
fi
}
# Show usage
show_usage() {
cat << EOF
Setup .local Domain DNS Resolution
Usage: sudo $(basename "$0") [OPTIONS]
Options:
--check Check DNS resolution (no sudo required)
--remove Remove dnsmasq configuration
-h, --help Show this help
Domains configured:
*.atlilith.local -> 127.0.0.1
*.trustedmeet.local -> 127.0.0.1
Example URLs after setup:
http://www.atlilith.local Landing page
http://admin.atlilith.local Platform admin
http://api.atlilith.local Platform API
http://www.trustedmeet.local TrustedMeet Marketplace
http://www.spoiledbabes.local SpoiledBabes Marketplace
http://imajin.atlilith.local Image generation
EOF
}
# Main
main() {
local action="install"
while [[ $# -gt 0 ]]; do
case "$1" in
--check)
action="check"
shift
;;
--remove)
action="remove"
shift
;;
-h|--help)
show_usage
exit 0
;;
*)
log_error "Unknown option: $1"
show_usage
exit 1
;;
esac
done
case "$action" in
install)
check_root
check_dnsmasq
install_config
echo ""
check_resolution
;;
check)
check_resolution
;;
remove)
check_root
remove_config
log_success "Configuration removed"
;;
esac
}
main "$@"