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 # Install necessary packages to run CKAN
RUN apk add --no-cache git \ RUN apk add --no-cache git \
gettext \ gettext \
postgresql-client \
python \ python \
py-pip \ py-pip \
py-gunicorn py-gunicorn && \
# Temporary packages to build CKAN requirements
# Temporary packages to build CKAN requirements apk add --no-cache --virtual .build-deps \
RUN apk add --no-cache --virtual .build-deps \
postgresql-client \
postgresql-dev \ postgresql-dev \
gcc \ gcc \
musl-dev \ musl-dev \
python-dev python-dev && \
# Fetch CKAN and install
# Fetch CKAN and install mkdir ${APP_DIR}/src && cd ${APP_DIR}/src && \
RUN mkdir ${APP_DIR}/src && cd ${APP_DIR}/src && \
git clone -b ${GIT_BRANCH} --depth=1 --single-branch ${GIT_URL} ckan && \ git clone -b ${GIT_BRANCH} --depth=1 --single-branch ${GIT_URL} ckan && \
cd ckan && \ cd ckan && \
cp who.ini ${APP_DIR} && \ cp who.ini ${APP_DIR} && \
python setup.py install && \ python setup.py install && \
pip install --no-cache-dir testrepository && \ pip install --no-cache-dir testrepository && \
pip install --no-cache-dir --upgrade -r requirements.txt && \ 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 # Remove temporary packages and files
RUN apk del .build-deps && \ apk del .build-deps && \
rm -rf ${APP_DIR}/src rm -rf ${APP_DIR}/src
# Default Extensions # 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
paster --plugin=ckan make-config ckan ${APP_DIR}/production.ini && \
paster --plugin=ckan config-tool ${APP_DIR}/production.ini "ckan.plugins = ${CKAN__PLUGINS}"
# Create and update CKAN config COPY setup ${APP_DIR}
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}"
EXPOSE 8000 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