#!/bin/bash
# Dynamic Mycelium IPv6 Address Discovery Script for NodePort
# This script fetches Mycelium IPv6 addresses from worker nodes and generates HTML content
set -e
echo "🔍 Discovering Mycelium IPv6 addresses from worker nodes..."
# Fetch IPv6 addresses from cluster worker nodes
IPV6_ADDRESSES=$(kubectl get nodes -l kubernetes.io/role!=master -o jsonpath='{range .items[*]}{range .status.addresses[?(@.type=="InternalIP")]}{.address}{"\n"}{end}{end}' | grep -E '^[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+$')
if [ -z "$IPV6_ADDRESSES" ]; then
echo "⚠️ No IPv6 addresses found from worker nodes!"
echo "Trying all nodes..."
IPV6_ADDRESSES=$(kubectl get nodes -o jsonpath='{range .items[*]}{range .status.addresses[?(@.type=="InternalIP")]}{.address}{"\n"}{end}{end}' | grep -E '^[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+$')
fi
if [ -z "$IPV6_ADDRESSES" ]; then
echo "❌ No IPv6 addresses found!"
exit 1
fi
echo "✅ Found IPv6 addresses:"
echo "$IPV6_ADDRESSES"
# Generate HTML content with dynamic addresses
cat > /tmp/index.html << 'HTML_EOF'
🌐 Mycelium Cloud
Secure NodePort Website Hosting with IPv6!
✅ NODEPORT SECURE
🔒 ENHANCED SECURITY
Connected via IPv6:
Loading...
🌐 Global Access URLs (NodePort: 30091)
Your website is accessible via these Mycelium worker node IPv6 addresses:
HTML_EOF
# Add each IPv6 address to the HTML
while IFS= read -r ipv6; do
echo " http://[$ipv6]:30091 ✅ " >> /tmp/index.html
done <<< "$IPV6_ADDRESSES"
cat >> /tmp/index.html << 'HTML_EOF'
Anyone with Mycelium installed can access your website from anywhere!
🚀 Key Features:
🛡️ Enhanced security with network isolation
🌍 Peer-to-peer global access via NodePort
🔒 Standard Kubernetes service patterns
⚡ Clean pod networking without hostNetwork
🖥️ Multi-node Kubernetes cluster
🔄 Dynamic IPv6 discovery and routing
Loading timestamp...
Mycelium Cloud NodePort Demo
Security-First IPv6 Website Hosting
Auto-updated every 30 seconds
HTML_EOF
echo "📝 Generated HTML content with $(echo "$IPV6_ADDRESSES" | wc -l) IPv6 addresses"
# Update the ConfigMap
echo "🔄 Updating ConfigMap..."
kubectl create configmap nginx-nodeport-content --from-file=index.html=/tmp/index.html --dry-run=client -o yaml | kubectl apply -f -
echo "✅ Successfully updated nginx-nodeport-content ConfigMap"
echo ""
echo "🔄 To apply changes to running pods, restart the deployment:"
echo " kubectl rollout restart deployment/nginx-nodeport"
echo ""
echo "🌐 Website will be accessible at: http://[worker-node-ipv6]:30091"
echo ""
echo "📊 Discovered IPv6 addresses:"
echo "$IPV6_ADDRESSES" | nl
# Cleanup
rm -f /tmp/index.html
echo ""
echo "✅ Update complete!"