From 027da5985e7ebd661396b70218846b1be80248e5 Mon Sep 17 00:00:00 2001 From: spwoodcock Date: Mon, 29 Apr 2024 21:06:29 +0100 Subject: [PATCH] build: update osm entrypoint to be idempotent + output oauth secret --- osm-entrypoint.sh | 56 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 42 insertions(+), 14 deletions(-) diff --git a/osm-entrypoint.sh b/osm-entrypoint.sh index 6bd7d23..637dbfe 100755 --- a/osm-entrypoint.sh +++ b/osm-entrypoint.sh @@ -1,9 +1,7 @@ #!/bin/bash -# Start web server +# First start web server & run migrations bundle exec rails s -d -p 3000 -b '0.0.0.0' - -# Run migrations bundle exec rails db:migrate # Ruby script to create admin (to file) @@ -37,12 +35,16 @@ unless Oauth2Application.exists?(name: 'ID Dev') confidential: false, ) puts id_app.uid - # puts id_app.secret + puts id_app.secret end EOF -# Run script in Rails console -ID_EDITOR_CLIENT_ID=$(bundle exec rails runner create_admin_user.rb) +# Add output from Rails script to file, then extract OAuth app creds +if [ ! -e /tmp/create_admin_user.log ]; then + bundle exec rails runner create_admin_user.rb > /tmp/create_admin_user.log + ID_EDITOR_CLIENT_ID=$(sed -n '1p' /tmp/create_admin_user.log) + ID_EDITOR_CLIENT_SECRET=$(sed -n '2p' /tmp/create_admin_user.log) +fi # Stop web server gracefully kill -TERM $(cat /tmp/pids/server.pid) @@ -51,14 +53,40 @@ kill -TERM $(cat /tmp/pids/server.pid) # Further overrides can be made in a mounted settings.local.yml file # The oauth_application var is for OSM Notes / changeset comments # The id_application var is for ID editor -sed -i "s/#id_application: \"\"/id_application: \"${ID_EDITOR_CLIENT_ID}\"/" /app/config/settings.yml -sed -i "s/server_protocol: \"http\"/server_protocol: \"${PROTOCOL}\"/" /app/config/settings.yml -sed -i "s/server_url: \"openstreetmap.example.com\"/server_url: \"${DOMAIN}\"/" /app/config/settings.yml +if ! grep -q "id_application: \"${ID_EDITOR_CLIENT_ID}\"" /app/config/settings.yml; then + sed -i "s/#id_application: \"\"/id_application: \"${ID_EDITOR_CLIENT_ID}\"/" /app/config/settings.yml +fi + +if ! grep -q "server_protocol: \"${PROTOCOL}\"" /app/config/settings.yml; then + sed -i "s/server_protocol: \"http\"/server_protocol: \"${PROTOCOL}\"/" /app/config/settings.yml +fi + +if ! grep -q "server_url: \"${DOMAIN}\"" /app/config/settings.yml; then + sed -i "s/server_url: \"openstreetmap.example.com\"/server_url: \"${DOMAIN}\"/" /app/config/settings.yml +fi + # SMTP settings -sed -i "s/smtp_address: \"localhost\"/smtp_address: \"mail\"/" /app/config/settings.yml -sed -i "s/smtp_domain: \"localhost\"/smtp_domain: \"${DOMAIN}\"/" /app/config/settings.yml -sed -i "s/email_from: \"OpenStreetMap \"/email_from: \"HOTOSM Sandbox \"/" /app/config/settings.yml -sed -i "s/email_return_path: \"openstreetmap@example.com\"/email_return_path: \"no-reply@${DOMAIN}\"/" /app/config/settings.yml +if ! grep -q "smtp_address: \"mail\"" /app/config/settings.yml; then + sed -i "s/smtp_address: \"localhost\"/smtp_address: \"mail\"/" /app/config/settings.yml +fi + +if ! grep -q "smtp_domain: \"${DOMAIN}\"" /app/config/settings.yml; then + sed -i "s/smtp_domain: \"localhost\"/smtp_domain: \"${DOMAIN}\"/" /app/config/settings.yml +fi + +if ! grep -q "email_from: \"HOTOSM Sandbox \"" /app/config/settings.yml; then + sed -i "s/email_from: \"OpenStreetMap \"/email_from: \"HOTOSM Sandbox \"/" /app/config/settings.yml +fi + +if ! grep -q "email_return_path: \"no-reply@${DOMAIN}\"" /app/config/settings.yml; then + sed -i "s/email_return_path: \"openstreetmap@example.com\"/email_return_path: \"no-reply@${DOMAIN}\"/" /app/config/settings.yml +fi + +echo +echo "ID Editor OAuth App Details:" +echo +echo "Client ID: $ID_EDITOR_CLIENT_ID" +echo "Client Secret: $ID_EDITOR_CLIENT_SECRET" +echo -# Set exec to replace shell with the command passed as arguments exec "$@"