#!/usr/bin/awk -f # Transform docker-compose.yml to use an external overlay network for all services # - Remove top-level networks definition # - Remove per-service networks block (including ipv4_address and sysnet refs) # - Insert per-service networks: [argus-sys-net] # - Append external networks mapping at the end BEGIN{ in_top_networks=0; in_services=0; in_service=0; svc_indent=0; curr_name=""; } function is_service_header(line){ return svc_name(line)!=""; } function svc_name(line, m){ if (match(line, /^ ([A-Za-z0-9_-]+):[ ]*$/, m)) return m[1]; return ""; } function indent_len(s, n){ n=match(s,/[^ ]/)-1; if(n<0) n=0; return n; } { # Detect entry into top-level sections if ($0 ~ /^[A-Za-z0-9_-]+:[ ]*$/ && $0 !~ /^\s/) { in_services = ($0 ~ /^services:[ ]*$/); # If a new top-level section starts, stop skipping top networks in_top_networks = 0; } # Handle removal of initial top-level 'networks:' block if ($0 ~ /^networks:[ ]*$/ && $0 !~ /^\s/) { in_top_networks = 1; next; } if (in_top_networks) { # skip until next top-level section (non-indented key) next; } if (in_services) { # Track service boundaries if (is_service_header($0)) { in_service=1; svc_indent=2; networks_inserted=0; curr_name=svc_name($0); print; next; } if (in_service) { # If line is indented <= service indent, we've left this service if (indent_len($0) <= svc_indent && $0 !~ /^\s*$/) { in_service=0; } } if (in_service) { # Skip any existing networks block under the service if ($0 ~ /^\s{4}networks:[ ]*$/) { skipping_nets=1; next; } if (skipping_nets) { if (indent_len($0) <= 4) { skipping_nets=0; } else next; } # After container_name or image, inject networks once if (!networks_inserted && ($0 ~ /^\s{4}container_name:/ || $0 ~ /^\s{4}image:/)) { print; print " networks:"; print " - argus-sys-net"; networks_inserted=1; next; } # no host port injection; bind serves DNS inside overlay only } } print; } END{ print ""; print "networks:"; print " argus-sys-net:"; print " external: true"; print " name: ${OVERLAY_NET_NAME:-argus-sys-net}"; }