Add pre-run

This commit is contained in:
Marko Bocevski 2016-08-25 22:05:30 +02:00
parent de7eaa90d5
commit acb5913a64
3 changed files with 92 additions and 18 deletions

View File

@ -13,39 +13,38 @@ WORKDIR ${APP_DIR}
# Install necessary packages to run CKAN
RUN apk add --no-cache git \
gettext \
postgresql-client \
python \
py-pip \
py-gunicorn
py-gunicorn && \
# Temporary packages to build CKAN requirements
RUN apk add --no-cache --virtual .build-deps \
postgresql-client \
apk add --no-cache --virtual .build-deps \
postgresql-dev \
gcc \
musl-dev \
python-dev
python-dev && \
# Fetch CKAN and install
RUN mkdir ${APP_DIR}/src && cd ${APP_DIR}/src && \
mkdir ${APP_DIR}/src && cd ${APP_DIR}/src && \
git clone -b ${GIT_BRANCH} --depth=1 --single-branch ${GIT_URL} ckan && \
cd ckan && \
cp who.ini ${APP_DIR} && \
python setup.py install && \
pip install --no-cache-dir testrepository && \
pip install --no-cache-dir --upgrade -r requirements.txt && \
pip install --no-cache-dir html5lib==0.9999999
# Fix issue with html5lib in 2.5.2
pip install --no-cache-dir html5lib==0.9999999 && \
# Remove temporary packages and files
RUN apk del .build-deps && \
apk del .build-deps && \
rm -rf ${APP_DIR}/src
# Default Extensions
RUN pip install --no-cache-dir git+https://github.com/okfn/ckanext-envvars.git#egg=ckanext-envvars
RUN pip install --no-cache-dir git+https://github.com/okfn/ckanext-envvars.git#egg=ckanext-envvars && \
# Create and update CKAN config
RUN paster --plugin=ckan make-config ckan ${APP_DIR}/production.ini
RUN paster --plugin=ckan config-tool ${APP_DIR}/production.ini "ckan.plugins = ${CKAN__PLUGINS}"
paster --plugin=ckan make-config ckan ${APP_DIR}/production.ini && \
paster --plugin=ckan config-tool ${APP_DIR}/production.ini "ckan.plugins = ${CKAN__PLUGINS}"
COPY setup ${APP_DIR}
EXPOSE 8000
CMD ["gunicorn", "--paste", "production.ini"]
CMD ["/srv/app/start_ckan.sh"]

72
rootfs/setup/prerun.py Normal file
View File

@ -0,0 +1,72 @@
import os
import sys
import subprocess
ckan_ini = os.environ.get('CKAN_INI', '')
def init_db():
db_command = ['paster', '--plugin=ckan', 'db',
'init', '-c', ckan_ini]
print '[prerun] Initializing or upgrading db - start'
try:
subprocess.check_output(db_command, stderr=subprocess.STDOUT)
print '[prerun] Initializing or upgrading db - end'
except subprocess.CalledProcessError, e:
if 'OperationalError' in e.output:
print e.output
print '[prerun] Database not ready, waiting a bit before exit...'
import time
time.sleep(5)
sys.exit(1)
else:
print e.output
raise e
def create_sysadmin():
name = os.environ.get('CKAN_SYSADMIN_NAME')
password = os.environ.get('CKAN_SYSADMIN_PASSWORD')
email = os.environ.get('CKAN_SYSADMIN_EMAIL')
if name and password and email:
# Check if user exists
command = ['paster', '--plugin=ckan', 'user', name, '-c', ckan_ini]
out = subprocess.check_output(command)
if 'User: \nNone\n' not in out:
print '[prerun] Sysadmin user exists, skipping creation'
return
# Create user
command = ['paster', '--plugin=ckan', 'user', 'add',
name,
'password=' + password,
'email=' + email,
'-c', ckan_ini]
subprocess.call(command)
print '[prerun] Created user {0}'.format(name)
# Make it sysadmin
command = ['paster', '--plugin=ckan', 'sysadmin', 'add',
name,
'-c', ckan_ini]
subprocess.call(command)
print '[prerun] Made user {0} a sysadmin'.format(name)
if __name__ == '__main__':
maintenance = os.environ.get('MAINTENANCE_MODE', '').lower() == 'true'
if maintenance:
print '[prerun] Maintenance mode, skipping setup...'
else:
init_db()
create_sysadmin()

3
rootfs/setup/start_ckan.sh Executable file
View File

@ -0,0 +1,3 @@
#!/bin/bash
python prerun.py
gunicorn --log-file=- --paste production.ini