From d1a4c3274e3088ece2ab2f6ecba547354f0d312d Mon Sep 17 00:00:00 2001 From: Andrii Syrovatko Date: Fri, 9 Oct 2026 15:43:27 +0300 Subject: [PATCH] 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. --- .../network-manager/docker-network-manager.sh | 105 +++++++++++++++--- 1 file changed, 90 insertions(+), 15 deletions(-) diff --git a/tools/linux/docker-ce/network-manager/docker-network-manager.sh b/tools/linux/docker-ce/network-manager/docker-network-manager.sh index c5e394d..61839c7 100755 --- a/tools/linux/docker-ce/network-manager/docker-network-manager.sh +++ b/tools/linux/docker-ce/network-manager/docker-network-manager.sh @@ -1,15 +1,16 @@ #!/usr/bin/env bash # ============================================================================= # Script Name : docker-network-manager.sh -# Description : Manage Docker external networks with automatic subnet allocation and tracking. -# Usage: : ./docker-network-manager.sh [ARGUMENTS] +# Description : Manage Docker external networks with automatic subnet allocation, +# auto-syncing, and subnet-based sorting. +# Usage : ./docker-network-manager.sh [ARGUMENTS] # Commands: # 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. # info - Show current status and configuration. # For details - see README.md # Author : syr4ok (Andrii Syrovatko) -# Version : 1.1.0 +# Version : 1.3.0 # ============================================================================= # --- STRICT MODE --- @@ -21,7 +22,7 @@ if [[ -f "$CONFIG_FILE" ]]; then # shellcheck source=/dev/null source "$CONFIG_FILE" 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 fi @@ -42,19 +43,77 @@ confirm_action() { esac } -# Network check +# Check if a Docker network exists network_exists() { docker network ls --format '{{.Name}}' | grep -Fxq "$1" } -used_subnets() { - local escaped_base="${BASE_NET//./\\.}" - - # Get all subnets from existing Docker networks and filter those that match our base pattern - docker network ls -q | xargs -r docker network inspect 2>/dev/null \ - | grep -oP '"Subnet":\s*"\K'"${escaped_base}"'\.[0-9]+\.0/24' || echo "" +# Extract subnet for a specific network +get_network_subnet() { + local name="$1" + docker network inspect "$name" --format '{{range .IPAM.Config}}{{.Subnet}}{{end}}' 2>/dev/null || echo "N/A" } +# 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> + 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() { local used="$1" for i in $(seq "$START_OCTET" "$END_OCTET"); do @@ -68,6 +127,7 @@ next_free_subnet() { exit 1 } +# Validate Linux bridge interface name length limit name_length_check() { local name="$1" if [[ ${#name} -gt 15 ]]; then @@ -77,6 +137,7 @@ name_length_check() { return 0 } +# Provision a single Docker network provision_single() { local name="$1" if ! name_length_check "$name"; then @@ -102,8 +163,11 @@ provision_single() { log INFO "network name=$name added to $NET_FILE" sed -i '/^$/d' "$NET_FILE" fi + + sort_and_rewrite_config } +# Provision all networks listed in NET_FILE provision_from_file() { [[ ! -s "$NET_FILE" ]] && { log WARN "NET_FILE is empty"; return; } log INFO "starting mass provisioning from $NET_FILE" @@ -113,6 +177,7 @@ provision_from_file() { done 3< "$NET_FILE" } +# Delete a single Docker network delete_network() { local name="$1" if network_exists "$name"; then @@ -132,6 +197,7 @@ delete_network() { fi } +# Delete all networks listed in NET_FILE delete_all_from_file() { log INFO "deleting all networks listed in $NET_FILE" while read -u 3 -r line; do @@ -140,11 +206,15 @@ delete_all_from_file() { done 3< "$NET_FILE" } +# Show status and information show_info() { + # Sync and sort networks before display + sync_networks + local tracked_count 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 "Config File : $(basename "$NET_FILE")" echo "Log File : $(basename "$LOG_FILE")" @@ -152,7 +222,7 @@ show_info() { echo -e "\n[Current Status]" 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 "--------------------------------------------------------" while read -u 3 -r line; do [[ -z "$line" || "$line" =~ ^# ]] && continue @@ -162,9 +232,9 @@ show_info() { if network_exists "$line"; then status="ONLINE" - subnet=$(docker network inspect "$line" --format '{{(index .IPAM.Config 0).Subnet}}' 2>/dev/null || echo "error") + subnet=$(get_network_subnet "$line") fi - printf "%-20s %-15s %-10s\n" "$line" "$subnet" "$status" + printf "%-20s %-18s %-10s\n" "$line" "$subnet" "$status" done 3< "$NET_FILE" else echo "No networks tracked in $NET_FILE" @@ -175,6 +245,7 @@ show_info() { echo " $0 create (c) - Create all networks from file" echo " $0 delete (d) [name] - Remove network and clean 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 @@ -200,6 +271,10 @@ case "${1:-info}" in delete_all_from_file fi ;; + [Ss][Yy][Nn][Cc]|[Ss]) + sync_networks + log INFO "Synchronization and sorting complete." + ;; [Ii][Nn][Ff][Oo]|[Ii]) show_info ;;