Files
Varaverk/Docker_essentials/docker_network_connect.sh
T

159 lines
6.2 KiB
Bash

#!/bin/bash
# -----------------------------------------------------------------------------------------------
# --------------------------------- Docker Network Connect -------------------------------------
# -----------------------------------------------------------------------------------------------
# Connects specified Docker containers to extra networks on array start.
# Useful when containers need to communicate across networks they were not
# originally configured with — e.g. memcached needing access to nextcloud-aio network.
#
# Every container in NETWORK_CONNECT_CONTAINERS is connected to every network
# in NETWORK_CONNECT_NETWORKS. Already-connected containers are skipped cleanly.
#
# Run once at array start via User Scripts plugin.
# All configuration in Master.conf under Docker Network Connect section.
# Supports --dry-run to preview connections without making them.
# -----------------------------------------------------------------------------------------------
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/../Master.conf"
source "$SCRIPT_DIR/../common.sh"
parse_args "$@"
# -----------------------------------------------------------------------------------------------
# ━━━ $ICON_GEAR Setup ━━━
# -----------------------------------------------------------------------------------------------
echo ""
echo "━━━ $ICON_GEAR Setup ━━━"
if [[ "$EUID" -ne 0 ]]; then
error "Must be run as root"
exit 1
fi
success "Running as root"
if ! command -v docker &>/dev/null; then
error "Docker not found"
exit 1
fi
success "Docker found"
# -----------------------------------------------------------------------------------------------
# ━━━ $ICON_SUMMARY Status ━━━
# -----------------------------------------------------------------------------------------------
if [[ "$SHOW_STATUS" == true ]]; then
echo ""
echo "━━━━━ $ICON_SUMMARY STATUS ━━━━━"
echo "$ICON_CONTAINERS Containers: ${NETWORK_CONNECT_CONTAINERS[*]}"
echo "$ICON_DOCKER_NET Networks: ${NETWORK_CONNECT_NETWORKS[*]}"
echo "$ICON_GEAR Dry Run: $DRY_RUN"
echo ""
echo "━━━ Current Connections ━━━"
for container in "${NETWORK_CONNECT_CONTAINERS[@]}"; do
[[ -z "$container" ]] && continue
echo "$ICON_CONTAINERS $container:"
docker inspect "$container" \
--format '{{range $k, $v := .NetworkSettings.Networks}} {{$k}}{{"\n"}}{{end}}' \
2>/dev/null || echo " not found"
done
echo "━━━━━━━━━━━━━━━━━━━━━━━"
exit 0
fi
[[ "$DRY_RUN" == true ]] && warn "DRY RUN — no network connections will be made"
# -----------------------------------------------------------------------------------------------
# ━━━ $ICON_DOCKER_NET Network Connect ━━━
# -----------------------------------------------------------------------------------------------
echo ""
echo "━━━ $ICON_DOCKER_NET Network Connect — $(date '+%Y-%m-%d %H:%M:%S') ━━━"
echo "$ICON_CONTAINERS Containers: ${NETWORK_CONNECT_CONTAINERS[*]}"
echo "$ICON_DOCKER_NET Networks: ${NETWORK_CONNECT_NETWORKS[*]}"
echo ""
START=$(date +%s)
CONNECTED=()
SKIPPED=()
FAILED=()
for container in "${NETWORK_CONNECT_CONTAINERS[@]}"; do
[[ -z "$container" ]] && continue
echo "━━━ $ICON_CONTAINERS $container ━━━"
# Verify container exists
if ! docker inspect "$container" &>/dev/null; then
warn "$container not found — skipping"
FAILED+=("$container")
echo ""
continue
fi
for network in "${NETWORK_CONNECT_NETWORKS[@]}"; do
[[ -z "$network" ]] && continue
# Verify network exists
if ! docker network inspect "$network" &>/dev/null; then
warn "Network $network not found — skipping"
FAILED+=("$container:$network")
continue
fi
# Check if already connected
if docker network inspect "$network" \
--format '{{range .Containers}}{{.Name}} {{end}}' 2>/dev/null \
| grep -qw "$container"; then
info "$ICON_DOCKER_NET $container already connected to $network — skipping"
SKIPPED+=("$container$network")
continue
fi
# Connect
if [[ "$DRY_RUN" == true ]]; then
warn "DRY RUN — would connect $container to $network"
continue
fi
info "$ICON_DOCKER_NET Connecting $container to $network..."
if docker network connect "$network" "$container" 2>/dev/null; then
success "$ICON_DOCKER_NET $container connected to $network"
CONNECTED+=("$container$network")
else
error "Failed to connect $container to $network"
FAILED+=("$container:$network")
fi
done
echo ""
done
END=$(date +%s)
# -----------------------------------------------------------------------------------------------
# ━━━ $ICON_SUMMARY Summary ━━━
# -----------------------------------------------------------------------------------------------
echo "━━━━━ $ICON_SUMMARY NETWORK CONNECT SUMMARY ━━━━━"
echo "$ICON_TIME Duration: $(format_duration $((END - START)))"
[[ ${#CONNECTED[@]} -gt 0 ]] && echo "$ICON_DOCKER_NET Connected: ${CONNECTED[*]}"
[[ ${#SKIPPED[@]} -gt 0 ]] && echo "$ICON_RUNNING Skipped: ${SKIPPED[*]}"
[[ ${#FAILED[@]} -gt 0 ]] && echo "$ICON_ERROR Failed: ${FAILED[*]}"
echo ""
if [[ "$DRY_RUN" == true ]]; then
echo "$ICON_WARN Status: DRY RUN — no changes made"
elif [[ ${#FAILED[@]} -gt 0 ]]; then
echo "$ICON_ERROR Status: $ICON_ERROR SOME CONNECTIONS FAILED"
notify "Docker network connect failed on $(hostname)${FAILED[*]}" "Network Connect" "warning"
else
echo "$ICON_DONE Status: $ICON_SUCCESS DONE"
if [[ ${#CONNECTED[@]} -gt 0 ]]; then
notify "Docker networks connected on $(hostname)${CONNECTED[*]}" "Network Connect" "normal"
fi
fi
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
[[ ${#FAILED[@]} -gt 0 ]] && exit 1
exit 0