|
| 1 | +#!/bin/bash |
| 2 | +set -e |
| 3 | + |
| 4 | +# URL for the primary database, in the format expected by sqlalchemy |
| 5 | +: "${CKAN_SQLALCHEMY_URL:=}" |
| 6 | +: "${CKAN_SOLR_URL:=}" |
| 7 | +: "${CKAN_REDIS_URL:=}" |
| 8 | +: "${CKAN_DATAPUSHER_URL:=}" |
| 9 | + |
| 10 | +export CKAN_HOME=/usr/lib/adx |
| 11 | +export CKAN_VENV=$CKAN_HOME/venv |
| 12 | +export PATH=${CKAN_VENV}/bin:${PATH} |
| 13 | + |
| 14 | +# Combine base config with secrets using Python ConfigParser |
| 15 | +# secrets.ini values override production.ini |
| 16 | +echo "Combining configuration files..." |
| 17 | +python3 << 'PYEOF' |
| 18 | +import configparser |
| 19 | +import sys |
| 20 | +
|
| 21 | +config = configparser.ConfigParser() |
| 22 | +config.read('/etc/ckan/production.ini') |
| 23 | +config.read('/etc/ckan/secrets.ini') # Later values override earlier |
| 24 | +
|
| 25 | +with open('/tmp/ckan.ini', 'w') as f: |
| 26 | + config.write(f) |
| 27 | +PYEOF |
| 28 | +export CONFIG="/tmp/ckan.ini" |
| 29 | +export CKAN_INI="/tmp/ckan.ini" |
| 30 | + |
| 31 | +abort () { |
| 32 | + echo "$@" >&2 |
| 33 | + exit 1 |
| 34 | +} |
| 35 | + |
| 36 | +set_environment () { |
| 37 | + export CKAN_SITE_ID=${CKAN_SITE_ID} |
| 38 | + export CKAN_SITE_URL=${CKAN_SITE_URL} |
| 39 | + export CKAN_SQLALCHEMY_URL=${CKAN_SQLALCHEMY_URL} |
| 40 | + export CKAN_SOLR_URL=${CKAN_SOLR_URL} |
| 41 | + export CKAN_REDIS_URL=${CKAN_REDIS_URL} |
| 42 | + export CKAN_STORAGE_PATH=/var/lib/ckan |
| 43 | + export CKAN_DATAPUSHER_URL=${CKAN_DATAPUSHER_URL} |
| 44 | + export CKAN_DATASTORE_WRITE_URL=${CKAN_DATASTORE_WRITE_URL} |
| 45 | + export CKAN_DATASTORE_READ_URL=${CKAN_DATASTORE_READ_URL} |
| 46 | + export CKAN_SMTP_SERVER=${CKAN_SMTP_SERVER} |
| 47 | + export CKAN_SMTP_STARTTLS=${CKAN_SMTP_STARTTLS} |
| 48 | + export CKAN_SMTP_USER=${CKAN_SMTP_USER} |
| 49 | + export CKAN_SMTP_PASSWORD=${CKAN_SMTP_PASSWORD} |
| 50 | + export CKAN_SMTP_MAIL_FROM=${CKAN_SMTP_MAIL_FROM} |
| 51 | + export CKAN_MAX_UPLOAD_SIZE_MB=${CKAN_MAX_UPLOAD_SIZE_MB} |
| 52 | + if [ -n "${ADR_CKAN_SAML_IDP_CERT}" ]; then |
| 53 | + echo "${ADR_CKAN_SAML_IDP_CERT}" > /tmp/saml_idp.crt || echo "Warning: Could not write SAML IDP cert" |
| 54 | + fi |
| 55 | +} |
| 56 | + |
| 57 | +# Validate required environment variables |
| 58 | +if [ -z "$CKAN_SQLALCHEMY_URL" ]; then |
| 59 | + abort "ERROR: no CKAN_SQLALCHEMY_URL specified" |
| 60 | +fi |
| 61 | + |
| 62 | +if [ -z "$CKAN_SOLR_URL" ]; then |
| 63 | + abort "ERROR: no CKAN_SOLR_URL specified" |
| 64 | +fi |
| 65 | + |
| 66 | +if [ -z "$CKAN_REDIS_URL" ]; then |
| 67 | + abort "ERROR: no CKAN_REDIS_URL specified" |
| 68 | +fi |
| 69 | + |
| 70 | +if [ -z "$CKAN_DATAPUSHER_URL" ]; then |
| 71 | + abort "ERROR: no CKAN_DATAPUSHER_URL specified" |
| 72 | +fi |
| 73 | + |
| 74 | +set_environment |
| 75 | +echo "CKAN production environment ready" |
| 76 | + |
| 77 | +# Initialize CKAN database and run plugin migrations |
| 78 | +echo "Initializing CKAN database..." |
| 79 | +ckan --config="$CONFIG" db init || echo "CKAN database already initialized" |
| 80 | + |
| 81 | +echo "Setting up DataStore permissions..." |
| 82 | +ckan --config="$CONFIG" datastore set-permissions | psql "${CKAN_DATASTORE_WRITE_URL}" || echo "Warning: DataStore set-permissions failed or already applied" |
| 83 | + |
| 84 | +echo "Running database migrations for plugins..." |
| 85 | +ckan --config="$CONFIG" db upgrade -p pages || echo "Warning: ckanext-pages migration failed or already applied" |
| 86 | +# ckan --config="$CONFIG" versions initdb || echo "Warning: ckanext-versions initdb failed or already applied" |
| 87 | +# ckan --config="$CONFIG" validation init-db || echo "Warning: ckanext-validation init-db failed or already applied" |
| 88 | +ckan --config="$CONFIG" unaids initdb || echo "Warning: ckanext-unaids initdb failed or already applied" |
| 89 | + |
| 90 | +exec "$@" |
0 commit comments