From 3fa3c73d0e8ae57ee462b46f28dd91743c4971c7 Mon Sep 17 00:00:00 2001 From: Mike Lang Date: Sun, 23 Jun 2019 19:35:50 -0700 Subject: [PATCH] Fix nginx when some services are disabled nginx tries to resolve everything at startup, which doesn't work if some of the services aren't present. we instead generate the config file from a passed in env var, so that only enabled services are present. --- docker-compose.jsonnet | 16 ++++++++++++++++ nginx/Dockerfile | 3 ++- nginx/generate-config | 35 +++++++++++++++++++++++++++++++++++ nginx/nginx.conf | 37 ------------------------------------- 4 files changed, 53 insertions(+), 38 deletions(-) create mode 100755 nginx/generate-config delete mode 100644 nginx/nginx.conf diff --git a/docker-compose.jsonnet b/docker-compose.jsonnet index eea2b09..444ed04 100644 --- a/docker-compose.jsonnet +++ b/docker-compose.jsonnet @@ -208,9 +208,25 @@ }, [if $.enabled.nginx then "nginx"]: { + # mapping of services to internal ports for nginx to forward + local forward_ports = { + restreamer: 8000, + downloader: 8001, + backfiller: 8002, + cutter: 8003, + thrimshim: 8004, + sheetsync: 8005, + }, image: "quay.io/ekimekim/wubloader-nginx:%s" % $.image_tag, restart: "on-failure", [if "nginx" in $.ports then "ports"]: ["%s:80" % $.ports.nginx], + environment: { + SERVICES: std.join("\n", [ + "%s %s" % [service, forward_ports[service]] + for service in std.objectFields(forward_ports) + if service in $.enabled && $.enabled[service] + ]), + }, }, [if $.enabled.postgres then "postgres"]: { diff --git a/nginx/Dockerfile b/nginx/Dockerfile index 6a6a9ff..7a12782 100644 --- a/nginx/Dockerfile +++ b/nginx/Dockerfile @@ -1,3 +1,4 @@ # nginx container contains config that exposes all the various services metrics FROM nginx:latest -ADD nginx/nginx.conf /etc/nginx/nginx.conf +ADD nginx/generate-config / +ENTRYPOINT ["/bin/sh", "-c", "/generate-config && nginx -g \"daemon off;\""] diff --git a/nginx/generate-config b/nginx/generate-config new file mode 100755 index 0000000..b837428 --- /dev/null +++ b/nginx/generate-config @@ -0,0 +1,35 @@ +#!/bin/bash + +# This script expects a mapping of services and ports in the SERVICES env var, +# with one line per service containing "NAME PORT". + +generate_location() { + # generate_location PATH URL + echo -e "\t\tlocation $1 { proxy_pass $2; }" +} + +LOCATIONS=$( + echo "$SERVICES" | while read name port; do + # restreamer is the catch-all + [ "$name" == "restreamer" ] && generate_location / "http://restreamer:$port" + # thrimshim takes any calls to thrimshim/ + [ "$name" == "thrimshim" ] && generate_location /thrimshim "http://thrimshim:$port" + # all services have metrics under /metrics/SERVICE + generate_location "/metrics/$name" "http://$name:$port/metrics" + done +) + +cat > /etc/nginx/nginx.conf <