#!/usr/bin/env bash # Describes the services of this host in the content. links/ gets one file per # domain nginx serves, services/ one Markdown file per directory in /srv with # its images and domains. A domain belongs to a service when nginx proxies it # to a port the service's compose file publishes, or when the directory has # the domain's name. Both directories belong to this script and are only # replaced when their contents change. # # Uses CONTENT_DIR and HOST_DIR from the jobs service. EXCLUDE may list /srv # directory names to leave out, separated by spaces. set -euo pipefail srv=$HOST_DIR/srv nginx=$HOST_DIR/nginx exclude=" ${EXCLUDE:-} " # Prints "domain port" for every server_name of nginx, with the local port its # server block proxies to, directly or through an upstream, or "-" without one. nginx_sites() { local files=() dir file for dir in sites-enabled conf.d; do for file in "$nginx/$dir"/*; do [[ -f $file ]] && files+=("$file") done done ((${#files[@]})) || return 0 awk ' { sub(/#.*/, "") if (match($0, /upstream[ \t]+[A-Za-z0-9_.-]+/)) { split(substr($0, RSTART, RLENGTH), words, /[ \t]+/) upstream = words[2]; upstream_depth = depth } if (upstream != "" && match($0, /server[ \t]+[^;]*:[0-9]+/)) { port = substr($0, RSTART, RLENGTH); sub(/.*:/, "", port); ports[upstream] = port } else if (upstream == "" && $0 ~ /(^|[ \t;{])server[ \t]*\{/) { blocks++; block_depth[blocks] = depth; current = blocks } if (current && match($0, /server_name[ \t]+[^;]+/)) { value = substr($0, RSTART, RLENGTH); sub(/server_name[ \t]+/, "", value); names[current] = names[current] " " value } if (current && match($0, /proxy_pass[ \t]+[a-z]+:\/\/[^;\/]+/)) { value = substr($0, RSTART, RLENGTH); sub(/.*:\/\//, "", value); targets[current] = value } depth += gsub(/\{/, "{") - gsub(/\}/, "}") if (upstream != "" && depth <= upstream_depth) upstream = "" if (current && depth <= block_depth[current]) current = 0 } END { for (block = 1; block <= blocks; block++) { target = targets[block]; port = "-" if (target ~ /:[0-9]+$/) { port = target; sub(/.*:/, "", port) } else if (target in ports) port = ports[target] count = split(names[block], list, /[ \t]+/) for (i = 1; i <= count; i++) if (list[i] ~ /\./ && list[i] !~ /[*~]/) print list[i], port } } ' "${files[@]}" } compose_file() { local name for name in compose.yml compose.yaml docker-compose.yml docker-compose.yaml; do [[ -f $1/$name ]] && { echo "$1/$name"; return; } done return 0 } out=$(mktemp -d "$CONTENT_DIR/.services.XXXXXX") trap 'rm -rf "$out"' EXIT mkdir "$out/links" "$out/services" # A domain served by a redirect block and a proxy block keeps the proxied port. declare -A domain_port=() while read -r domain port; do [[ ${domain_port[$domain]:--} == - ]] && domain_port[$domain]=$port done < <(nginx_sites | sort -u) domains=() ((${#domain_port[@]})) && mapfile -t domains < <(printf '%s\n' "${!domain_port[@]}" | sort) for domain in "${domains[@]}"; do echo "https://$domain" > "$out/links/$domain" done services=0 for dir in "$srv"/*/; do [[ -d $dir ]] || continue name=$(basename "$dir") [[ $name == .* || $exclude == *" $name "* ]] && continue compose=$(compose_file "$dir") images=() host_ports=() if [[ -n $compose ]]; then mapfile -t images < <(sed -nE 's/^[[:space:]]*image:[[:space:]]*["'\'']?([^"'\''[:space:]]+).*/\1/p' "$compose" | grep -v '\$' | sort -u) mapfile -t host_ports < <(grep -oE '[0-9]{2,5}\}?:[0-9]{1,5}(/(tcp|udp))?["'\'']?[[:space:]]*$' "$compose" | sed -E 's/\}?:.*//' | sort -u) fi { echo "# $name" echo found=0 for domain in "${domains[@]}"; do match=0 [[ $domain == "$name" ]] && match=1 for port in "${host_ports[@]}"; do [[ ${domain_port[$domain]} == "$port" ]] && match=1 done ((match)) && { echo "- url: https://$domain"; found=1; } done ((found)) || echo "- url: none, internal only" for image in "${images[@]}"; do echo "- image: $image"; done } > "$out/services/$name.md" services=$((services + 1)) done changed=() for part in links services; do diff -rq "$out/$part" "$CONTENT_DIR/$part" >/dev/null 2>&1 && continue rm -rf "$CONTENT_DIR/.$part.old" [[ -e $CONTENT_DIR/$part ]] && mv "$CONTENT_DIR/$part" "$CONTENT_DIR/.$part.old" mv "$out/$part" "$CONTENT_DIR/$part" rm -rf "$CONTENT_DIR/.$part.old" changed+=("$part") done echo "${#domain_port[@]} domain(s), $services service(s), updated: ${changed[*]:-nothing}"