Files
bootstrap-scripts/docker-network.sh

78 lines
2.0 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
set -euo pipefail
DOCKER_CONFIG="/etc/docker/daemon.json"
JQ=$(command -v jq || true)
if [[ -z "$JQ" ]]; then
echo "❌ jq is required but not installed."
exit 1
fi
# Step 1: Check if default-address-pools exists and extract current second octet
DEFAULT_OCTET=""
if [[ -f "$DOCKER_CONFIG" ]] && grep -q '"default-address-pools"' "$DOCKER_CONFIG"; then
BASE=$(jq -r '.["default-address-pools"][0].base' "$DOCKER_CONFIG")
if [[ "$BASE" =~ ^10\.([0-9]+)\. ]]; then
DEFAULT_OCTET="${BASH_REMATCH[1]}"
echo "🔍 Found existing default-address-pools base: $BASE"
fi
fi
# Step 2: Ask for second octet (suggest default if available)
if [[ -n "$DEFAULT_OCTET" ]]; then
read -rp "Use existing second octet ($DEFAULT_OCTET)? [Y/n]: " confirm
confirm=${confirm:-Y}
if [[ "$confirm" =~ ^[Nn]$ ]]; then
read -rp "Enter new second octet (e.g. 102): " OCTET
else
OCTET="$DEFAULT_OCTET"
fi
else
read -rp "Enter second octet for Docker address pool (e.g. 102): " OCTET
fi
# Validate input
if ! [[ "$OCTET" =~ ^[0-9]+$ ]] || ((OCTET < 1 || OCTET > 254)); then
echo "❌ Invalid octet: $OCTET"
exit 1
fi
# Step 3: Write daemon.json (overwrite or create)
echo "💾 Writing Docker daemon config with base: 10.${OCTET}.0.0/16"
sudo mkdir -p /etc/docker
sudo tee "$DOCKER_CONFIG" > /dev/null <<EOF
{
"default-address-pools": [
{
"base": "10.${OCTET}.0.0/16",
"size": 24
}
]
}
EOF
# Step 4: Restart Docker
echo "🔄 Restarting Docker..."
sudo systemctl restart docker
# Step 5: Create standard networks
declare -A networks
networks[internet]="10.${OCTET}.100.0/24"
networks[pangolin_edge]="10.${OCTET}.200.0/24"
for name in "${!networks[@]}"; do
subnet="${networks[$name]}"
if docker network inspect "$name" >/dev/null 2>&1; then
echo "✅ Network '$name' already exists."
else
echo " Creating network '$name' with subnet $subnet"
docker network create \
--driver=bridge \
--subnet="$subnet" \
"$name"
fi
done
echo "✅ Done. Docker daemon configured and standard networks created."