Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor fixes for CKAN 2.9 #116

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file removed images/ckan/2.9/patches/.gitkeep
Empty file.
2 changes: 1 addition & 1 deletion images/ckan/2.9/setup/app/extra_scripts.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash

# this is called before uwsgi is executed
# uset his to add extra scripts before ckan is started
# use this to add extra scripts before ckan is started
38 changes: 18 additions & 20 deletions images/ckan/2.9/setup/app/prerun.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@
limitations under the License.
"""

from multiprocessing import connection
import base64
import json
import os
import psycopg2
import re
import sys
import subprocess
import psycopg2
from sqlalchemy.engine.url import make_url
import time
import urllib.request, urllib.error, urllib.parse, base64
import re
import json

import time
from multiprocessing import connection
from sqlalchemy.engine.url import make_url


ckan_ini = os.environ.get('CKAN_INI', '/srv/app/production.ini')

Expand All @@ -34,7 +36,7 @@ def check_db_connection(retry=None):
if retry is None:
retry = RETRY
elif retry == 0:
print('[prerun] Giving up after 5 tries...')
print('[prerun] Giving up after {count} tries...'.format(count=RETRY))
sys.exit(1)

conn_str = os.environ.get('CKAN_SQLALCHEMY_URL', '')
Expand All @@ -44,14 +46,13 @@ def check_db_connection(retry=None):
db_host = make_url(conn_str).host
db_name = make_url(conn_str).database
connection = psycopg2.connect(user=db_user,
host=db_host,
password=db_passwd,
database=db_name)
host=db_host,
password=db_passwd,
database=db_name)

except psycopg2.Error as e:
print((str(e)))
print('[prerun] Unable to connect to the database...try again in a while.')
import time
time.sleep(10)
check_db_connection(retry = retry - 1)
else:
Expand All @@ -64,16 +65,14 @@ def check_solr_connection(retry=None):
if retry is None:
retry = RETRY
elif retry == 0:
print('[prerun] Giving up after 5 tries...')
print('[prerun] Giving up after {count} tries...'.format(count=RETRY))
sys.exit(1)

url = os.environ.get('CKAN_SOLR_URL', '')
username = os.environ.get('CKAN_SOLR_USER', '')
password = os.environ.get('CKAN_SOLR_PASSWORD', '')
search_url = '{url}/schema/name?wt=json'.format(url=url)



try:
if not username:
connection = urllib.request.urlopen(search_url)
Expand All @@ -84,7 +83,6 @@ def check_solr_connection(retry=None):
connection = urllib.request.urlopen(request)
except urllib.error.URLError as e:
print('[prerun] Unable to connect to solr...try again in a while.')
import time
time.sleep(10)
check_solr_connection(retry = retry - 1)
else:
Expand Down Expand Up @@ -113,7 +111,6 @@ def init_db():
if 'OperationalError' in str(e.output):
print(e.output.decode('utf-8'))
print('[prerun] Database not ready, waiting a bit before exit...')
import time
time.sleep(5)
sys.exit(1)
else:
Expand All @@ -137,9 +134,9 @@ def init_datastore():
db_host = make_url(conn_str).host
db_name = make_url(conn_str).database
connection = psycopg2.connect(user=db_user,
host=db_host,
password=db_passwd,
database=db_name)
host=db_host,
password=db_passwd,
database=db_name)
cursor = connection.cursor()

print('[prerun] Initializing datastore db - start')
Expand Down Expand Up @@ -225,4 +222,5 @@ def create_sysadmin():
init_db()
if os.environ.get('CKAN_DATASTORE_WRITE_URL'):
init_datastore()
create_sysadmin()
create_sysadmin()

45 changes: 23 additions & 22 deletions images/ckan/2.9/setup/app/start_ckan.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
# Run any startup scripts provided by images extending this one
if [[ -d "${APP_DIR}/docker-entrypoint.d" ]]
then
for f in ${APP_DIR}/docker-entrypoint.d/*; do
case "$f" in
*.sh) echo "$0: Running init file $f"; . "$f" ;;
*.py) echo "$0: Running init file $f"; python "$f"; echo ;;
*) echo "$0: Ignoring $f (not an sh or py file)" ;;
esac
echo
done
for f in ${APP_DIR}/docker-entrypoint.d/*; do
case "$f" in
*.sh) echo "$0: Running init file $f"; . "$f" ;;
*.py) echo "$0: Running init file $f"; python "$f"; echo ;;
*) echo "$0: Ignoring $f (not an sh or py file)" ;;
esac
echo
done
fi

# Add session secret from chart
Expand All @@ -24,12 +24,13 @@ fi

if grep -E "beaker.session.secret ?= ?$" $APP_DIR/production.ini
then
echo "Setting secrets in ini file"
ckan config-tool $APP_DIR/production.ini "beaker.session.secret=$(python3 -c 'import secrets; print(secrets.token_urlsafe())')"
ckan config-tool $APP_DIR/production.ini "WTF_CSRF_SECRET_KEY=$(python3 -c 'import secrets; print(secrets.token_urlsafe())')"
JWT_SECRET=$(python3 -c 'import secrets; print("string:" + secrets.token_urlsafe())')
ckan config-tool $APP_DIR/production.ini "api_token.jwt.encode.secret=$JWT_SECRET"
ckan config-tool $APP_DIR/production.ini "api_token.jwt.decode.secret=$JWT_SECRET"
echo "Setting secrets in ini file"
ckan config-tool $APP_DIR/production.ini "beaker.session.secret=$(python3 -c 'import secrets; print(secrets.token_urlsafe())')"
ckan config-tool $APP_DIR/production.ini "WTF_CSRF_SECRET_KEY=$(python3 -c 'import secrets; print(secrets.token_urlsafe())')"

JWT_SECRET=$(python3 -c 'import secrets; print("string:" + secrets.token_urlsafe())')
ckan config-tool $APP_DIR/production.ini "api_token.jwt.encode.secret=$JWT_SECRET"
ckan config-tool $APP_DIR/production.ini "api_token.jwt.decode.secret=$JWT_SECRET"
fi

# Run the prerun script to init CKAN and create the default admin user
Expand All @@ -41,14 +42,14 @@ if [ "$MAINTENANCE_MODE" = true ]; then PYTHONUNBUFFERED=1 python maintenance/se
# Run any after prerun/init scripts provided by images extending this one
if [[ -d "${APP_DIR}/docker-afterinit.d" ]]
then
for f in ${APP_DIR}/docker-afterinit.d/*; do
case "$f" in
*.sh) echo "$0: Running after prerun init file $f"; . "$f" ;;
*.py) echo "$0: Running after prerun init file $f"; python "$f"; echo ;;
*) echo "$0: Ignoring $f (not an sh or py file)" ;;
esac
echo
done
for f in ${APP_DIR}/docker-afterinit.d/*; do
case "$f" in
*.sh) echo "$0: Running after prerun init file $f"; . "$f" ;;
*.py) echo "$0: Running after prerun init file $f"; python "$f"; echo ;;
*) echo "$0: Ignoring $f (not an sh or py file)" ;;
esac
echo
done
fi

# Check whether http basic auth password protection is enabled and enable basicauth routing on uwsgi respecfully
Expand Down