From f6266629548151d8388e4ced1b64c43c67990f16 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Thu, 25 Jun 2020 12:12:11 +1000 Subject: [PATCH] feat: allow the env var used to set the port to be called a different name to support running on Heroku --- Dockerfile | 2 +- docker-compose-dev.yml | 51 +++++++++++++++++++++++++++++ docker-compose-heroku.yml | 31 ++++++++++++++++++ docker-compose.yml | 11 ++----- pact_broker/config/puma.rb | 1 + pact_broker/docker_configuration.rb | 11 ++++--- pact_broker/entrypoint.sh | 2 +- ssl/nginx.conf | 4 +-- 8 files changed, 97 insertions(+), 16 deletions(-) create mode 100644 docker-compose-dev.yml create mode 100644 docker-compose-heroku.yml create mode 100644 pact_broker/config/puma.rb diff --git a/Dockerfile b/Dockerfile index 4026505b..6d203647 100644 --- a/Dockerfile +++ b/Dockerfile @@ -23,8 +23,8 @@ COPY pact_broker $HOME/ # Start Puma ENV RACK_ENV=production +ENV PACT_BROKER_PORT_ENVIRONMENT_VARIABLE_NAME=PACT_BROKER_PORT ENV PACT_BROKER_PORT=9292 USER ruby -EXPOSE $PACT_BROKER_PORT ENTRYPOINT ["./entrypoint.sh"] CMD ["config.ru"] diff --git a/docker-compose-dev.yml b/docker-compose-dev.yml new file mode 100644 index 00000000..c146e4a3 --- /dev/null +++ b/docker-compose-dev.yml @@ -0,0 +1,51 @@ +version: "3" + +services: + postgres: + image: postgres + healthcheck: + test: psql postgres --command "select 1" -U postgres + ports: + - "5432:5432" + # volumes: + # - postgres-volume:/var/lib/postgresql/data + environment: + POSTGRES_USER: postgres + POSTGRES_PASSWORD: password + POSTGRES_DB: postgres + + pact-broker: + # image: pactfoundation/pact-broker + build: . + ports: + - "9393:9393" + depends_on: + - postgres + environment: + PACT_BROKER_PORT_ENVIRONMENT_VARIABLE_NAME: PORT + PACT_BROKER_DATABASE_URL_ENVIRONMENT_VARIABLE_NAME: DATABASE_URL + DATABASE_URL: "postgres://postgres:password@postgres/postgres" + # PACT_BROKER_DATABASE_USERNAME: postgres + # PACT_BROKER_DATABASE_PASSWORD: password + # PACT_BROKER_DATABASE_HOST: postgres + # PACT_BROKER_DATABASE_NAME: postgres + # PACT_BROKER_PORT: "9292" + PORT: '9393' + PACT_BROKER_LOG_LEVEL: INFO + PACT_BROKER_SQL_LOG_LEVEL: DEBUG + + # Nginx is not necessary, but demonstrates how + # one might use a reverse proxy in front of the broker, + # and includes the use of a self-signed TLS certificate + nginx: + image: nginx:alpine + depends_on: + - pact-broker + volumes: + - ./ssl/nginx.conf:/etc/nginx/conf.d/default.conf:ro + - ./ssl:/etc/nginx/ssl + ports: + - "8443:443" + - "80:80" +volumes: + postgres-volume: diff --git a/docker-compose-heroku.yml b/docker-compose-heroku.yml new file mode 100644 index 00000000..ca860b53 --- /dev/null +++ b/docker-compose-heroku.yml @@ -0,0 +1,31 @@ +# This example demonstrates how you can run the Pact Broker docker image on Heroku +# by allowing the PORT and DATABASE_URL environment variables to be used instead of +# PACT_BROKER_PORT and PACT_BROKER_DATABASE_URL. + +version: "3" + +services: + postgres: + image: postgres + healthcheck: + test: psql postgres --command "select 1" -U postgres + ports: + - "5432:5432" + environment: + POSTGRES_USER: postgres + POSTGRES_PASSWORD: password + POSTGRES_DB: postgres + + pact-broker: + image: pactfoundation/pact-broker + ports: + - "9393:9393" + depends_on: + - postgres + environment: + PACT_BROKER_PORT_ENVIRONMENT_VARIABLE_NAME: "PORT" + PORT: "9393" + PACT_BROKER_DATABASE_URL_ENVIRONMENT_VARIABLE_NAME: "DATABASE_URL" + DATABASE_URL: "postgres://postgres:password@postgres/postgres" + PACT_BROKER_LOG_LEVEL: INFO + PACT_BROKER_SQL_LOG_LEVEL: DEBUG diff --git a/docker-compose.yml b/docker-compose.yml index f2e69ade..1360edf3 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -15,20 +15,15 @@ services: POSTGRES_DB: postgres pact-broker: - # image: pactfoundation/pact-broker + image: pactfoundation/pact-broker build: . ports: - "9292:9292" depends_on: - postgres environment: - PACT_BROKER_DATABASE_URL_ENVIRONMENT_VARIABLE_NAME: DATABASE_URL - DATABASE_URL: "postgres://postgres:password@postgres/postgres" - # PACT_BROKER_DATABASE_USERNAME: postgres - # PACT_BROKER_DATABASE_PASSWORD: password - # PACT_BROKER_DATABASE_HOST: postgres - # PACT_BROKER_DATABASE_NAME: postgres - PACT_BROKER_PORT: "9292" + PACT_BROKER_PORT: '9292' + PACT_BROKER_DATABASE_URL: "postgres://postgres:password@postgres/postgres" PACT_BROKER_LOG_LEVEL: INFO PACT_BROKER_SQL_LOG_LEVEL: DEBUG diff --git a/pact_broker/config/puma.rb b/pact_broker/config/puma.rb new file mode 100644 index 00000000..128f5c11 --- /dev/null +++ b/pact_broker/config/puma.rb @@ -0,0 +1 @@ +port ENV[ENV['PACT_BROKER_PORT_ENVIRONMENT_VARIABLE_NAME']] diff --git a/pact_broker/docker_configuration.rb b/pact_broker/docker_configuration.rb index 9bf860db..3561be6f 100644 --- a/pact_broker/docker_configuration.rb +++ b/pact_broker/docker_configuration.rb @@ -8,13 +8,16 @@ def initialize env, default_configuration end def pact_broker_environment_variables - @env.each_with_object({}) do | (key, value), hash | - if key.start_with?("PACT_BROKER_") - hash[key] = key =~ /password/i ? "*****" : value - end + pact_broker_environment_variable_names.sort.each_with_object({}) do | name, hash | + hash[name] = name =~ /password/i ? "*****" : @env[name] end end + def pact_broker_environment_variable_names + remapped_env_var_names = @env.keys.select { |k| k.start_with?('PACT_BROKER_') && k.end_with?('_ENVIRONMENT_VARIABLE_NAME') } + @env.keys.select{ |k| k.start_with?('PACT_BROKER_') } + remapped_env_var_names.collect{ |name| @env[name] }.compact + end + def webhook_host_whitelist space_delimited_string_list_or_default(:webhook_host_whitelist) end diff --git a/pact_broker/entrypoint.sh b/pact_broker/entrypoint.sh index 8c2799c4..8d7e0122 100755 --- a/pact_broker/entrypoint.sh +++ b/pact_broker/entrypoint.sh @@ -1,3 +1,3 @@ #!/bin/sh -bundle exec puma --port $PACT_BROKER_PORT +bundle exec puma diff --git a/ssl/nginx.conf b/ssl/nginx.conf index 3fce7e9c..47a89be3 100644 --- a/ssl/nginx.conf +++ b/ssl/nginx.conf @@ -11,7 +11,7 @@ server { ssl_stapling_verify on; location / { - proxy_pass http://pact_broker:9292; + proxy_pass http://pact-broker:9292; proxy_set_header Host $host; proxy_set_header X-Forwarded-Scheme "https"; proxy_set_header X-Forwarded-Port "443"; @@ -25,7 +25,7 @@ server { server_name localhost; location / { - proxy_pass http://pact_broker:9292; + proxy_pass http://pact-broker:9292; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; }