Files
myceliumcloud-examples/scripts/fetch-ip.sh

125 lines
3.9 KiB
Bash
Executable File

#!/bin/bash
# Simple Mycelium IPv6 Address Fetcher with Master/Worker Categorization
# This script processes kubectl output to categorize Mycelium IPv6 addresses by node role
set -e
echo "🔍 Discovering Mycelium IPv6 addresses by node role..."
# Check if kubectl is available
if ! command -v kubectl &> /dev/null; then
echo "❌ kubectl command not found. Please ensure kubectl is installed and configured."
exit 1
fi
echo "📡 Connected to cluster"
# Arrays to store IPs
master_ips=()
worker_ips=()
# Get node metadata for role detection
declare -A node_roles
while IFS= read -r node_line; do
if [ -z "$node_line" ]; then
continue
fi
node_name=$(echo "$node_line" | awk '{print $1}')
# Check if this is a master node by name
if echo "$node_name" | grep -qE '(master|control-plane)'; then
node_roles["$node_name"]="master"
else
node_roles["$node_name"]="worker"
fi
done < <(kubectl get nodes -o jsonpath='{.items[*].metadata.name}' | tr ' ' '\n')
echo "🔎 Processing cluster nodes..."
# Process the specific kubectl output
current_node=""
while IFS= read -r line; do
# Skip empty lines
if [ -z "$line" ]; then
continue
fi
# Check if this line has a tab (node name + IP)
if echo "$line" | grep -q $'\t'; then
current_node=$(echo "$line" | cut -f1)
# Check if this is a Mycelium IPv6 address
ip_address=$(echo "$line" | cut -f2)
if echo "$ip_address" | grep -qE '^[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+$'; then
if [ "${node_roles[$current_node]}" = "master" ]; then
master_ips+=("$ip_address")
echo " 🔧 MASTER: $ip_address"
else
worker_ips+=("$ip_address")
echo " ⚙️ WORKER: $ip_address"
fi
fi
else
# This is a Mycelium IPv6 address on its own line
if echo "$line" | grep -qE '^[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+$'; then
if [ "${node_roles[$current_node]}" = "master" ]; then
master_ips+=("$line")
echo " 🔧 MASTER: $line"
else
worker_ips+=("$line")
echo " ⚙️ WORKER: $line"
fi
fi
fi
done < <(kubectl get nodes -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{range .status.addresses[?(@.type=="InternalIP")]}{.address}{"\n"}{end}{end}' | head -10)
echo ""
echo "📊 Results Summary:"
echo "=================="
# Display Master IPs
if [ ${#master_ips[@]} -gt 0 ]; then
echo ""
echo "🔧 MASTER NODES (${#master_ips[@]} found):"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
for ip in "${master_ips[@]}"; do
echo " $ip"
done
else
echo ""
echo "⚠️ No master nodes found with Mycelium IPv6 addresses"
fi
# Display Worker IPs
if [ ${#worker_ips[@]} -gt 0 ]; then
echo ""
echo "⚙️ WORKER NODES (${#worker_ips[@]} found):"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
for ip in "${worker_ips[@]}"; do
echo " $ip"
done
else
echo ""
echo "⚠️ No worker nodes found with Mycelium IPv6 addresses"
fi
# Final summary
echo ""
echo "📈 TOTAL SUMMARY:"
echo "================="
total_found=$((${#master_ips[@]} + ${#worker_ips[@]}))
echo "Total Mycelium IPv6 addresses found: $total_found"
echo " - Master nodes: ${#master_ips[@]}"
echo " - Worker nodes: ${#worker_ips[@]}"
# Exit with error if no IPs found
if [ $total_found -eq 0 ]; then
echo ""
echo "❌ No Mycelium IPv6 addresses found in the cluster!"
exit 1
fi
echo ""
echo "✅ Mycelium IPv6 address discovery completed successfully!"