-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-network.sh
More file actions
executable file
·172 lines (148 loc) · 6.75 KB
/
setup-network.sh
File metadata and controls
executable file
·172 lines (148 loc) · 6.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/bin/bash
# XaresAICoder Network Setup Script
# Creates persistent external Docker network for workspace containers
#
# Features:
# - Detects and prevents subnet conflicts with existing Docker networks
# - Automatically suggests alternative subnets if conflicts are found
# - Uses intelligent overlap detection via Python ipaddress module
# - Falls back to basic conflict detection if Python is not available
set -e
NETWORK_NAME="xares-aicoder-network"
NETWORK_SUBNET="172.19.0.0/16"
echo "🔧 Setting up XaresAICoder Docker network..."
# Function to check if a subnet overlaps with existing networks
check_subnet_conflicts() {
local requested_subnet="$1"
local conflicting_networks=""
echo "🔍 Checking for subnet conflicts..."
# Get all existing network subnets
existing_subnets=$(docker network ls --format "{{.Name}}" | while read network; do
if [ "$network" != "$NETWORK_NAME" ]; then
docker network inspect "$network" --format '{{.Name}}: {{range .IPAM.Config}}{{.Subnet}} {{end}}' 2>/dev/null | grep -v ": $" || true
fi
done)
if [ -n "$existing_subnets" ]; then
echo "📋 Existing Docker networks and their subnets:"
echo "$existing_subnets" | sed 's/^/ /'
echo
# Check for overlaps using Python if available, or basic checks
if command -v python3 >/dev/null 2>&1; then
conflicts=$(echo "$existing_subnets" | python3 -c "
import sys
import ipaddress
requested = ipaddress.IPv4Network('$requested_subnet', strict=False)
conflicts = []
for line in sys.stdin:
if ':' in line:
network_name = line.split(':')[0].strip()
subnets = line.split(':')[1].strip().split()
for subnet in subnets:
if subnet and '/' in subnet:
try:
existing = ipaddress.IPv4Network(subnet.strip(), strict=False)
if requested.overlaps(existing):
conflicts.append(f'{network_name} ({subnet})')
except:
pass
if conflicts:
print('\\n'.join(conflicts))
" 2>/dev/null || true)
if [ -n "$conflicts" ]; then
echo "⚠️ SUBNET CONFLICT DETECTED!"
echo " The requested subnet $requested_subnet overlaps with:"
echo "$conflicts" | sed 's/^/ - /'
echo
echo " This could cause network connectivity issues."
echo " Consider using a different subnet or removing conflicting networks."
echo
return 1
fi
else
# Basic check without Python - just check for exact matches
if echo "$existing_subnets" | grep -q "$requested_subnet"; then
echo "⚠️ SUBNET CONFLICT DETECTED!"
echo " The subnet $requested_subnet is already in use."
echo " Consider using a different subnet."
echo
return 1
fi
fi
fi
echo "✅ No subnet conflicts detected"
return 0
}
# Check if network already exists
if docker network ls --format "{{.Name}}" | grep -q "^${NETWORK_NAME}$"; then
echo "✅ Network '${NETWORK_NAME}' already exists"
# Verify network configuration
EXISTING_SUBNET=$(docker network inspect ${NETWORK_NAME} --format '{{range .IPAM.Config}}{{.Subnet}}{{end}}' 2>/dev/null || echo "")
if [ "$EXISTING_SUBNET" = "$NETWORK_SUBNET" ]; then
echo "✅ Network configuration is correct (subnet: ${NETWORK_SUBNET})"
else
echo "⚠️ Network exists but with different subnet: ${EXISTING_SUBNET}"
echo " Expected: ${NETWORK_SUBNET}"
echo " This may cause connectivity issues."
read -p "Do you want to recreate the network? (y/N): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "🗑️ Removing existing network..."
docker network rm ${NETWORK_NAME}
echo "🔧 Creating new network..."
docker network create \
--driver bridge \
--subnet=${NETWORK_SUBNET} \
--opt com.docker.network.bridge.name=xares-aicoder0 \
${NETWORK_NAME}
echo "✅ Network '${NETWORK_NAME}' created successfully"
fi
fi
else
# Check for subnet conflicts before creating new network
if ! check_subnet_conflicts "$NETWORK_SUBNET"; then
echo "🔄 Attempting to find alternative subnet..."
# Try alternative subnets
alternative_subnets=("172.20.0.0/16" "172.21.0.0/16" "172.22.0.0/16" "10.100.0.0/16")
for alt_subnet in "${alternative_subnets[@]}"; do
echo " Trying subnet: $alt_subnet"
if check_subnet_conflicts "$alt_subnet"; then
NETWORK_SUBNET="$alt_subnet"
echo "✅ Using alternative subnet: $NETWORK_SUBNET"
break
fi
done
# If no alternative found, ask user
if ! check_subnet_conflicts "$NETWORK_SUBNET" >/dev/null 2>&1; then
echo "❌ No suitable alternative subnet found automatically."
echo
read -p "Enter a custom subnet (e.g., 172.25.0.0/16) or press Enter to proceed anyway: " custom_subnet
if [ -n "$custom_subnet" ]; then
NETWORK_SUBNET="$custom_subnet"
echo "Using custom subnet: $NETWORK_SUBNET"
else
echo "⚠️ Proceeding with potentially conflicting subnet: $NETWORK_SUBNET"
echo " Monitor for network connectivity issues."
fi
fi
fi
# Create new network
echo "🔧 Creating network '${NETWORK_NAME}' with subnet ${NETWORK_SUBNET}..."
docker network create \
--driver bridge \
--subnet=${NETWORK_SUBNET} \
--opt com.docker.network.bridge.name=xares-aicoder0 \
${NETWORK_NAME}
echo "✅ Network '${NETWORK_NAME}' created successfully"
fi
# Display network information
echo ""
echo "📋 Network Information:"
docker network inspect ${NETWORK_NAME} --format '{{printf "%-15s %s" "Name:" .Name}}'
docker network inspect ${NETWORK_NAME} --format '{{printf "%-15s %s" "Driver:" .Driver}}'
docker network inspect ${NETWORK_NAME} --format '{{range .IPAM.Config}}{{printf "%-15s %s" "Subnet:" .Subnet}}{{end}}'
docker network inspect ${NETWORK_NAME} --format '{{printf "%-15s %s" "Gateway:" .IPAM.Config.Gateway}}' 2>/dev/null || echo "Gateway: Auto-assigned"
echo ""
echo "🚀 Network setup complete! You can now start XaresAICoder with docker-compose."
echo ""
echo "💡 This network will persist across docker-compose restarts, ensuring"
echo " that stopped workspace containers can be restarted successfully."