mirror of
https://github.com/andsyrovatko/s4k-admin-toolbox.git
synced 2026-12-01 02:24:29 +01:00
Enhance docker-network-manager with auto-syncing and sorting
Updated the script to include auto-syncing of networks and improved subnet sorting. Version bumped from 1.1.0 to 1.3.0.
This commit is contained in:
1 parent
7ac0ad8922
commit
d1a4c3274e
1 file changed
+90
-15
@@ -1,15 +1,16 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
# =============================================================================
|
# =============================================================================
|
||||||
# Script Name : docker-network-manager.sh
|
# Script Name : docker-network-manager.sh
|
||||||
# Description : Manage Docker external networks with automatic subnet allocation and tracking.
|
# Description : Manage Docker external networks with automatic subnet allocation,
|
||||||
# Usage: : ./docker-network-manager.sh <COMMAND> [ARGUMENTS]
|
# auto-syncing, and subnet-based sorting.
|
||||||
|
# Usage : ./docker-network-manager.sh <COMMAND> [ARGUMENTS]
|
||||||
# Commands:
|
# Commands:
|
||||||
# create [NAME] - Create a specific network by name or all networks from file if NO NAME provided.
|
# create [NAME] - Create a specific network by name or all networks from file if NO NAME provided.
|
||||||
# delete [NAME] - Delete a specific network by name or all networks from file if NO NAME provided.
|
# delete [NAME] - Delete a specific network by name or all networks from file if NO NAME provided.
|
||||||
# info - Show current status and configuration.
|
# info - Show current status and configuration.
|
||||||
# For details - see README.md
|
# For details - see README.md
|
||||||
# Author : syr4ok (Andrii Syrovatko)
|
# Author : syr4ok (Andrii Syrovatko)
|
||||||
# Version : 1.1.0
|
# Version : 1.3.0
|
||||||
# =============================================================================
|
# =============================================================================
|
||||||
|
|
||||||
# --- STRICT MODE ---
|
# --- STRICT MODE ---
|
||||||
@@ -21,7 +22,7 @@ if [[ -f "$CONFIG_FILE" ]]; then
|
|||||||
# shellcheck source=/dev/null
|
# shellcheck source=/dev/null
|
||||||
source "$CONFIG_FILE"
|
source "$CONFIG_FILE"
|
||||||
else
|
else
|
||||||
echo "[Error]: Configuration file not found. Create ip_manager.conf from example."
|
echo "[Error]: Configuration file not found. Create docker-network-manager.conf from example."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -42,19 +43,77 @@ confirm_action() {
|
|||||||
esac
|
esac
|
||||||
}
|
}
|
||||||
|
|
||||||
# Network check
|
# Check if a Docker network exists
|
||||||
network_exists() {
|
network_exists() {
|
||||||
docker network ls --format '{{.Name}}' | grep -Fxq "$1"
|
docker network ls --format '{{.Name}}' | grep -Fxq "$1"
|
||||||
}
|
}
|
||||||
|
|
||||||
used_subnets() {
|
# Extract subnet for a specific network
|
||||||
local escaped_base="${BASE_NET//./\\.}"
|
get_network_subnet() {
|
||||||
|
local name="$1"
|
||||||
# Get all subnets from existing Docker networks and filter those that match our base pattern
|
docker network inspect "$name" --format '{{range .IPAM.Config}}{{.Subnet}}{{end}}' 2>/dev/null || echo "N/A"
|
||||||
docker network ls -q | xargs -r docker network inspect 2>/dev/null \
|
|
||||||
| grep -oP '"Subnet":\s*"\K'"${escaped_base}"'\.[0-9]+\.0/24' || echo ""
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Sort networks by the 3rd octet of their IP subnet and rewrite NET_FILE
|
||||||
|
sort_and_rewrite_config() {
|
||||||
|
[[ ! -s "$NET_FILE" ]] && return 0
|
||||||
|
|
||||||
|
local temp_file
|
||||||
|
temp_file=$(mktemp)
|
||||||
|
|
||||||
|
# Read each network from NET_FILE, fetch its subnet, and format for sorting
|
||||||
|
while read -u 3 -r line; do
|
||||||
|
[[ -z "$line" || "$line" =~ ^# ]] && continue
|
||||||
|
|
||||||
|
local subnet
|
||||||
|
subnet=$(get_network_subnet "$line")
|
||||||
|
|
||||||
|
# Extract 3rd octet if subnet matches BASE_NET pattern, otherwise assign high rank (999)
|
||||||
|
local octet=999
|
||||||
|
if [[ "$subnet" =~ ^${BASE_NET//./\\.}\.([0-9]+)\. ]]; then
|
||||||
|
octet="${BASH_REMATCH[1]}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Format: <3rd_octet> <network_name>
|
||||||
|
printf "%03d %s\n" "$octet" "$line" >> "$temp_file"
|
||||||
|
done 3< "$NET_FILE"
|
||||||
|
|
||||||
|
# Sort numerically by 3rd octet and overwrite NET_FILE with sorted names
|
||||||
|
sort -n "$temp_file" | awk '{print $2}' > "$NET_FILE"
|
||||||
|
rm -f "$temp_file"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Auto-discover active Docker bridge networks and add missing ones to NET_FILE
|
||||||
|
sync_networks() {
|
||||||
|
local active_nets
|
||||||
|
active_nets=$(docker network ls --filter "driver=bridge" --format '{{.Name}}' | grep -vE '^(bridge|host|none)$' || true)
|
||||||
|
|
||||||
|
while read -r net; do
|
||||||
|
[[ -z "$net" ]] && continue
|
||||||
|
if ! grep -Fxq "$net" "$NET_FILE"; then
|
||||||
|
echo "$net" >> "$NET_FILE"
|
||||||
|
log INFO "Auto-synced: added missing network '$net' to $NET_FILE"
|
||||||
|
fi
|
||||||
|
done <<< "$active_nets"
|
||||||
|
|
||||||
|
# Clean empty lines
|
||||||
|
sed -i '/^$/d' "$NET_FILE"
|
||||||
|
|
||||||
|
# Sort config file by subnet rank
|
||||||
|
sort_and_rewrite_config
|
||||||
|
}
|
||||||
|
|
||||||
|
# Get list of all subnets currently in use
|
||||||
|
used_subnets() {
|
||||||
|
local escaped_base="${BASE_NET//./\\.}"
|
||||||
|
|
||||||
|
# Safely extract subnets via Docker Go template
|
||||||
|
docker network ls -q | while read -r net_id; do
|
||||||
|
docker network inspect "$net_id" --format '{{range .IPAM.Config}}{{.Subnet}}{{"\n"}}{{end}}' 2>/dev/null
|
||||||
|
done | grep -E "^${escaped_base}\." || echo ""
|
||||||
|
}
|
||||||
|
|
||||||
|
# Find next available /24 subnet in configured range
|
||||||
next_free_subnet() {
|
next_free_subnet() {
|
||||||
local used="$1"
|
local used="$1"
|
||||||
for i in $(seq "$START_OCTET" "$END_OCTET"); do
|
for i in $(seq "$START_OCTET" "$END_OCTET"); do
|
||||||
@@ -68,6 +127,7 @@ next_free_subnet() {
|
|||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Validate Linux bridge interface name length limit
|
||||||
name_length_check() {
|
name_length_check() {
|
||||||
local name="$1"
|
local name="$1"
|
||||||
if [[ ${#name} -gt 15 ]]; then
|
if [[ ${#name} -gt 15 ]]; then
|
||||||
@@ -77,6 +137,7 @@ name_length_check() {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Provision a single Docker network
|
||||||
provision_single() {
|
provision_single() {
|
||||||
local name="$1"
|
local name="$1"
|
||||||
if ! name_length_check "$name"; then
|
if ! name_length_check "$name"; then
|
||||||
@@ -102,8 +163,11 @@ provision_single() {
|
|||||||
log INFO "network name=$name added to $NET_FILE"
|
log INFO "network name=$name added to $NET_FILE"
|
||||||
sed -i '/^$/d' "$NET_FILE"
|
sed -i '/^$/d' "$NET_FILE"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
sort_and_rewrite_config
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Provision all networks listed in NET_FILE
|
||||||
provision_from_file() {
|
provision_from_file() {
|
||||||
[[ ! -s "$NET_FILE" ]] && { log WARN "NET_FILE is empty"; return; }
|
[[ ! -s "$NET_FILE" ]] && { log WARN "NET_FILE is empty"; return; }
|
||||||
log INFO "starting mass provisioning from $NET_FILE"
|
log INFO "starting mass provisioning from $NET_FILE"
|
||||||
@@ -113,6 +177,7 @@ provision_from_file() {
|
|||||||
done 3< "$NET_FILE"
|
done 3< "$NET_FILE"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Delete a single Docker network
|
||||||
delete_network() {
|
delete_network() {
|
||||||
local name="$1"
|
local name="$1"
|
||||||
if network_exists "$name"; then
|
if network_exists "$name"; then
|
||||||
@@ -132,6 +197,7 @@ delete_network() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Delete all networks listed in NET_FILE
|
||||||
delete_all_from_file() {
|
delete_all_from_file() {
|
||||||
log INFO "deleting all networks listed in $NET_FILE"
|
log INFO "deleting all networks listed in $NET_FILE"
|
||||||
while read -u 3 -r line; do
|
while read -u 3 -r line; do
|
||||||
@@ -140,11 +206,15 @@ delete_all_from_file() {
|
|||||||
done 3< "$NET_FILE"
|
done 3< "$NET_FILE"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Show status and information
|
||||||
show_info() {
|
show_info() {
|
||||||
|
# Sync and sort networks before display
|
||||||
|
sync_networks
|
||||||
|
|
||||||
local tracked_count
|
local tracked_count
|
||||||
tracked_count=$(grep -cE '^\s*[^#]' "$NET_FILE" || true)
|
tracked_count=$(grep -cE '^\s*[^#]' "$NET_FILE" || true)
|
||||||
|
|
||||||
echo "--- Docker Network Manager v1.1.0 ---"
|
echo "--- Docker Network Manager v1.3.0 ---"
|
||||||
echo "Configured Base : ${BASE_NET}.0.0/16 (Range: .${START_OCTET}.x to .${END_OCTET}.x)"
|
echo "Configured Base : ${BASE_NET}.0.0/16 (Range: .${START_OCTET}.x to .${END_OCTET}.x)"
|
||||||
echo "Config File : $(basename "$NET_FILE")"
|
echo "Config File : $(basename "$NET_FILE")"
|
||||||
echo "Log File : $(basename "$LOG_FILE")"
|
echo "Log File : $(basename "$LOG_FILE")"
|
||||||
@@ -152,7 +222,7 @@ show_info() {
|
|||||||
|
|
||||||
echo -e "\n[Current Status]"
|
echo -e "\n[Current Status]"
|
||||||
if [[ "$tracked_count" -gt 0 ]]; then
|
if [[ "$tracked_count" -gt 0 ]]; then
|
||||||
printf "%-20s %-15s %-10s\n" "NETWORK NAME" "IP RANGE" "STATUS"
|
printf "%-20s %-18s %-10s\n" "NETWORK NAME" "IP RANGE" "STATUS"
|
||||||
echo "--------------------------------------------------------"
|
echo "--------------------------------------------------------"
|
||||||
while read -u 3 -r line; do
|
while read -u 3 -r line; do
|
||||||
[[ -z "$line" || "$line" =~ ^# ]] && continue
|
[[ -z "$line" || "$line" =~ ^# ]] && continue
|
||||||
@@ -162,9 +232,9 @@ show_info() {
|
|||||||
|
|
||||||
if network_exists "$line"; then
|
if network_exists "$line"; then
|
||||||
status="ONLINE"
|
status="ONLINE"
|
||||||
subnet=$(docker network inspect "$line" --format '{{(index .IPAM.Config 0).Subnet}}' 2>/dev/null || echo "error")
|
subnet=$(get_network_subnet "$line")
|
||||||
fi
|
fi
|
||||||
printf "%-20s %-15s %-10s\n" "$line" "$subnet" "$status"
|
printf "%-20s %-18s %-10s\n" "$line" "$subnet" "$status"
|
||||||
done 3< "$NET_FILE"
|
done 3< "$NET_FILE"
|
||||||
else
|
else
|
||||||
echo "No networks tracked in $NET_FILE"
|
echo "No networks tracked in $NET_FILE"
|
||||||
@@ -175,6 +245,7 @@ show_info() {
|
|||||||
echo " $0 create (c) - Create all networks from file"
|
echo " $0 create (c) - Create all networks from file"
|
||||||
echo " $0 delete (d) [name] - Remove network and clean file"
|
echo " $0 delete (d) [name] - Remove network and clean file"
|
||||||
echo " $0 delete (d) - Remove all networks from file"
|
echo " $0 delete (d) - Remove all networks from file"
|
||||||
|
echo " $0 sync (s) - Sync active Docker networks with config file"
|
||||||
}
|
}
|
||||||
|
|
||||||
case "${1:-info}" in
|
case "${1:-info}" in
|
||||||
@@ -200,6 +271,10 @@ case "${1:-info}" in
|
|||||||
delete_all_from_file
|
delete_all_from_file
|
||||||
fi
|
fi
|
||||||
;;
|
;;
|
||||||
|
[Ss][Yy][Nn][Cc]|[Ss])
|
||||||
|
sync_networks
|
||||||
|
log INFO "Synchronization and sorting complete."
|
||||||
|
;;
|
||||||
[Ii][Nn][Ff][Oo]|[Ii])
|
[Ii][Nn][Ff][Oo]|[Ii])
|
||||||
show_info
|
show_info
|
||||||
;;
|
;;
|
||||||
|
|||||||
Reference in new issue
Block a user