Alpinize + Dockerize Sahana Eden

This commit is contained in:
Disassembler 2018-01-27 22:26:37 +01:00
parent d117d3d236
commit 5a67551dd4
No known key found for this signature in database
GPG Key ID: 524BD33A0EE29499
64 changed files with 135 additions and 115 deletions

108
sahana.sh
View File

@ -1,102 +1,52 @@
#!/bin/bash
#!/bin/sh
SOURCE_DIR=$(realpath $(dirname "${0}"))/sahana
# Install dependencies for Sahana
apt-get -y --no-install-recommends install python-dateutil python-imaging python-lxml python-matplotlib python-openid python-psycopg2 python-pyth python-reportlab python-requests python-shapely python-tweepy python-xlrd python-xlwt
# Check prerequisites
docker image ls | grep -q postgres || $(realpath $(dirname "${0}"))/postgres.sh
# Install web2py
git clone --recursive https://github.com/web2py/web2py.git /srv/sahana
git -C /srv/sahana checkout cda35fd
git -C /srv/sahana submodule update
# Install Sahana
git clone --depth 1 https://github.com/sahana/eden.git /srv/sahana/applications/eden
# Install TwitterSearch 0.78.6
git clone --depth 1 https://github.com/ckoepp/TwitterSearch.git /tmp/TwitterSearch
cd /tmp/TwitterSearch
python setup.py install
cd -
rm -rf /tmp/TwitterSearch
# Copy Czech language file
cp ${SOURCE_DIR}/srv/sahana/applications/eden/languages/cs.py /srv/sahana/applications/eden/languages/cs.py
# Symlink fonts with Czech glyphs
ln -s /usr/share/fonts/truetype/dejavu/DejaVuSerif-Bold.ttf /srv/sahana/applications/eden/static/fonts/DejaVuSerif-Bold.ttf
ln -s /usr/share/fonts/truetype/dejavu/DejaVuSerif.ttf /srv/sahana/applications/eden/static/fonts/DejaVuSerif.ttf
# Build Docker container
docker build -t sahana ${SOURCE_DIR}
# Create PostgreSQL user and database
export SAHANA_PWD=$(head -c 18 /dev/urandom | base64)
envsubst <${SOURCE_DIR}/tmp/sahana-createdb.sql >/tmp/sahana-createdb.sql
sudo -u postgres psql -f /tmp/sahana-createdb.sql
rm -f /tmp/sahana-createdb.sql
envsubst <${SOURCE_DIR}/createdb.sql | docker exec -i postgres psql
# Configure web2py
rm -rf /srv/sahana/applications/{admin,examples,welcome}
cp ${SOURCE_DIR}/srv/sahana/routes.py /srv/sahana/routes.py
cp ${SOURCE_DIR}/srv/sahana/run_scheduler.py /srv/sahana/run_scheduler.py
ln -s /srv/sahana/handlers/wsgihandler.py /srv/sahana/wsgihandler.py
# Prepare persistent directory structure
mkdir -p /srv/sahana/conf /srv/sahana/data/databases /srv/sahana/data/uploads
chown -R 8001:8001 /srv/sahana/conf
docker run --rm -v /srv/sahana/conf:/mnt/conf sahana cp -rp /srv/web2py/applications/eden/models/. /mnt/conf
# Copy Spotter template
cp -r ${SOURCE_DIR}/srv/sahana/data/Spotter /srv/sahana/data/
chown -R 8001:8001 /srv/sahana/data
# Configure Sahana
mkdir /srv/sahana/applications/eden/{databases,errors,sessions}
mkdir -p /srv/sahana/applications/eden/static/cache/chart
mkdir -p /srv/sahana/applications/eden/uploads/{gis_cache,images,tracks}
export SAHANA_HMAC=$(head -c 18 /dev/urandom | base64)
envsubst <${SOURCE_DIR}/srv/sahana/applications/eden/models/000_config.py >/srv/sahana/applications/eden/models/000_config.py
cp ${SOURCE_DIR}/srv/sahana/applications/eden/models/00_settings.py /srv/sahana/applications/eden/models/00_settings.py
envsubst <${SOURCE_DIR}/srv/sahana/conf/000_config.py >/srv/sahana/conf/000_config.py
cp ${SOURCE_DIR}/srv/sahana/conf/00_settings.py /srv/sahana/conf/00_settings.py
export SAHANA_ADMIN_USER=admin@example.com
export SAHANA_ADMIN_PWD=$(head -c 12 /dev/urandom | base64)
cp -r ${SOURCE_DIR}/srv/sahana/applications/eden/modules/templates/Spotter /srv/sahana/applications/eden/modules/templates/
envsubst <${SOURCE_DIR}/srv/sahana/applications/eden/modules/templates/Spotter/masterUsers.csv >/srv/sahana/applications/eden/modules/templates/Spotter/masterUsers.csv
# Create OS user
adduser --system --group --home /srv/sahana --shell /bin/false sahana
chown -R sahana:sahana /srv/sahana
envsubst <${SOURCE_DIR}/srv/sahana/data/Spotter/masterUsers.csv >/srv/sahana/data/Spotter/masterUsers.csv
# Populate database
cd /srv/sahana
sudo -u sahana python web2py.py -S eden -M -R applications/eden/static/scripts/tools/noop.py
docker run --rm -h sahana --link postgres -v /srv/sahana/conf:/srv/web2py/applications/eden/models -v /srv/sahana/data/Spotter:/srv/web2py/applications/eden/modules/templates/Spotter -v /srv/sahana/data/databases:/srv/web2py/applications/eden/databases sahana ./web2py.py -S eden -M -R applications/eden/static/scripts/tools/noop.py
# Set "production values" (increases performance) only if the DEBUG environment variable is not set
if [ ${DEBUG:-0} -eq 0 ]; then
sed -i 's/settings.base.migrate = True/settings.base.migrate = False/' /srv/sahana/applications/eden/models/000_config.py
sed -i 's/settings.base.debug = True/settings.base.debug = False/' /srv/sahana/applications/eden/models/000_config.py
sed -i 's/#settings.base.prepopulate = 0/settings.base.prepopulate = 0/' /srv/sahana/applications/eden/models/000_config.py
sudo -u sahana python web2py.py -S eden -M -R applications/eden/static/scripts/tools/compile.py
sed -i 's/settings.base.migrate = True/settings.base.migrate = False/' /srv/sahana/conf/000_config.py
sed -i 's/settings.base.debug = True/settings.base.debug = False/' /srv/sahana/conf/000_config.py
sed -i 's/#settings.base.prepopulate = 0/settings.base.prepopulate = 0/' /srv/sahana/conf/000_config.py
fi
# Create uwsgi and nginx app definition
cp ${SOURCE_DIR}/etc/uwsgi/apps-available/sahana.ini /etc/uwsgi/apps-available/sahana.ini
ln -s /etc/uwsgi/apps-available/sahana.ini /etc/uwsgi/apps-enabled/sahana.ini
cp ${SOURCE_DIR}/etc/nginx/apps-available/sahana /etc/nginx/apps-available/sahana
ln -s /etc/nginx/apps-available/sahana /etc/nginx/apps-enabled/sahana
# Create Sahana service
cp ${SOURCE_DIR}/etc/init.d/sahana /etc/init.d/sahana
rc-update add sahana boot
service sahana start
# Restart services
systemctl restart uwsgi
systemctl restart nginx
# Create nginx app definition
cp ${SOURCE_DIR}/etc/nginx/conf.d/sahana.conf /etc/nginx/conf.d/sahana.conf
service nginx reload
# Add portal application definition
portal-app-manager sahana-eden "/eden/" "${SAHANA_ADMIN_USER}" "${SAHANA_ADMIN_PWD}"
# TODO: Does matplotlib still require MPLCONFIGDIR directory?
# If so, /srv/sahana/wsgihandler.py needs to be modified
# TODO: Potentially needed dependencies
# WARNING: Setup unresolved dependency: boto required for Setup Module
# WARNING: S3Doc unresolved dependency: sunburnt required for Full-Text Search support
# WARNING: S3GIS unresolved dependency: selenium required for Map printing support
# WARNING: Setup unresolved dependency: PyYAML required for Setup Module
# WARNING: S3GIS unresolved dependency: GDAL required for Shapefile support
# WARNING: Setup unresolved dependency: ansible required for Setup Module
# WARNING: Survey unresolved dependency: PyRTF required if you want to export assessment/survey templates as a Word document
# WARNING: Unresolved optional dependency: pyserial required
# WARNING: Message Parsing unresolved dependency: TwitterSearch required for fetching results from twitter keyword queries
# TODO: Development notes
# vi /srv/sahana/applications/eden/modules/templates/default/config.py
# vi /srv/sahana/applications/eden/modules/s3cfg.py
# sudo -u sahana /srv/sahana/web2py.py -i 192.168.2.202 -a sahana --nogui
# sudo -u sahana openssl req -new -x509 -nodes -out /srv/sahana/cert.pem -keyout /srv/sahana/cert.key -subj "/C=CZ/CN=$(hostname -f)"
# sudo -u sahana /srv/sahana/web2py.py -i 192.168.2.202 -a sahana -c /srv/sahana/cert.pem -k /srv/sahana/cert.key --nogui
portal-app-manager sahana-eden "https://{host}:8401/eden/" "${SAHANA_ADMIN_USER}" "${SAHANA_ADMIN_PWD}"

60
sahana/Dockerfile Normal file
View File

@ -0,0 +1,60 @@
FROM alpine:3.7
MAINTAINER Disassembler <disassembler@dasm.cz>
RUN \
# Install Python2 runtime
apk --no-cache add python2
RUN \
# Install runtime XML dependencies
apk --no-cache add libxml2 libxslt
RUN \
# Install runtime dependencies
apk --no-cache add py2-dateutil py2-lxml py2-numpy py2-pillow py2-psycopg2 py2-requests \
&& echo 'http://dl-cdn.alpinelinux.org/alpine/edge/testing' >>/etc/apk/repositories \
&& apk --no-cache add geos py-gdal \
&& sed -i '$ d' /etc/apk/repositories
RUN \
# Install build dependencies
apk --no-cache add --virtual .deps build-base git freetype-dev libpng-dev py-numpy-dev py2-pip python2-dev ttf-dejavu \
# Hackfix for python find_library('c') call
&& ln -s /lib/ld-musl-x86_64.so.1 /lib/libc.so.1 \
# Install web2py
&& git clone --recursive https://github.com/web2py/web2py.git /srv/web2py \
&& git -C /srv/web2py checkout 7035398 \
&& git -C /srv/web2py submodule update \
# Fix Web2py urllib import (https://github.com/web2py/pydal/commit/9d2290c)
&& sed -i 's/import traceback/import traceback\nimport urllib/' /srv/web2py/gluon/packages/dal/pydal/base.py \
# Install Sahana
&& git clone --depth 1 https://github.com/sahana/eden.git /srv/web2py/applications/eden \
# Install python dependencies, exclude old or unnecessary ones
&& sed -i 's/^ansible/#ansible/' /srv/web2py/applications/eden/optional_requirements.txt \
&& sed -i 's/^boto/#boto/' /srv/web2py/applications/eden/optional_requirements.txt \
&& sed -i 's/^PIL/#PIL/' /srv/web2py/applications/eden/optional_requirements.txt \
&& sed -i 's/^PyRTF/#PyRTF/' /srv/web2py/applications/eden/optional_requirements.txt \
&& sed -i 's/^PyYAML/#PyYAML/' /srv/web2py/applications/eden/optional_requirements.txt \
&& pip install -r /srv/web2py/applications/eden/optional_requirements.txt \
# Copy fonts with Czech glyphs
&& cp /usr/share/fonts/ttf-dejavu/DejaVuSerif-Bold.ttf /srv/web2py/applications/eden/static/fonts/DejaVuSerif-Bold.ttf \
&& cp /usr/share/fonts/ttf-dejavu/DejaVuSerif.ttf /srv/web2py/applications/eden/static/fonts/DejaVuSerif.ttf \
# Create OS user
&& addgroup -S -g 8001 sahana \
&& adduser -S -u 8001 -h /srv/web2py -s /bin/false -g sahana -G sahana sahana \
&& chown -R sahana:sahana /srv/web2py \
# Cleanup
&& apk del .deps \
&& find /srv/web2py -name '.git*' -exec rm -rf {} + \
&& rm -r /srv/web2py/applications/admin /srv/web2py/applications/examples /srv/web2py/applications/welcome \
&& rm -r /root/.cache
COPY --chown=8001:8001 docker/srv/ /srv/
COPY docker/usr/ /usr/
VOLUME ["/srv/web2py/applications/eden/models", "/srv/web2py/applications/eden/databases", "/srv/web2py/applications/eden/uploads"]
EXPOSE 8001
USER sahana
WORKDIR /srv/web2py
CMD ["web2py_eden.sh"]

View File

@ -0,0 +1,4 @@
#!/bin/sh
cd /srv/web2py
./web2py.py -a $(head -c 18 /dev/urandom | base64) -K eden -X -i 0.0.0.0 -p 8001 --nogui

26
sahana/etc/init.d/sahana Executable file
View File

@ -0,0 +1,26 @@
#!/sbin/openrc-run
description="Sahana Eden docker container"
depend() {
need docker net
use dns logger netmount
after postgres
}
start() {
/usr/bin/docker run -d --rm \
--name sahana \
-h sahana \
--link postgres \
-p 127.0.0.1:9001:8001 \
-v /srv/sahana/conf:/srv/web2py/applications/eden/models \
-v /srv/sahana/data/Spotter:/srv/web2py/applications/eden/modules/templates/Spotter \
-v /srv/sahana/data/databases:/srv/web2py/applications/eden/databases \
-v /srv/sahana/data/uploads:/srv/web2py/applications/eden/uploads \
sahana
}
stop() {
/usr/bin/docker stop sahana
}

View File

@ -1,13 +0,0 @@
location /eden {
uwsgi_pass unix:///run/uwsgi/app/sahana/socket;
uwsgi_read_timeout 300s;
uwsgi_send_timeout 300s;
include uwsgi_params;
uwsgi_param UWSGI_SCHEME $scheme;
uwsgi_param SERVER_SOFTWARE nginx/$nginx_version;
}
location ~* /eden/static {
root /srv/sahana/applications;
expires max;
}

View File

@ -0,0 +1,14 @@
server {
listen [::]:8001 ipv6only=off;
listen [::]:8401 ssl http2 ipv6only=off;
access_log /var/log/nginx/sahana.access.log;
error_log /var/log/nginx/sahana.error.log;
location / {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Host $host:$server_port;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://127.0.0.1:9001;
}
}

View File

@ -1,15 +0,0 @@
[uwsgi]
uid = sahana
gid = sahana
chown-socket = www-data:www-data
chdir = /srv/sahana
module = wsgihandler
cheaper = 1
cheaper-algo = spare
cheaper-initial = 1
cheaper-step = 1
workers = 4
idle = 600
harakiri = 300
mule = run_scheduler.py
cron = 10 -1 -1 -1 -1 python web2py.py -Q -S eden -M -R scripts/sessions2trash.py -A -o

View File

@ -30,7 +30,7 @@ settings.base.template = "Spotter"
settings.database.db_type = "postgres"
#settings.database.db_type = "mysql"
# Uncomment to use a different host
#settings.database.host = "localhost"
settings.database.host = "postgres"
# Uncomment to use a different port
#settings.database.port = 3306
#settings.database.port = 5432
@ -92,7 +92,7 @@ settings.auth.hmac_key = "${SAHANA_HMAC}"
# Email settings
# Outbound server
settings.mail.server = "127.0.0.1:25"
settings.mail.server = "postfix:587"
#settings.mail.tls = True
# Useful for Windows Laptops:
# https://www.google.com/settings/security/lesssecureapps

View File

Can't render this file because it has a wrong number of fields in line 7.

View File

Can't render this file because it contains an unexpected character in line 2 and column 211.

View File

@ -1,6 +0,0 @@
#!/usr/bin/env python
from gluon.shell import run
if __name__ == '__main__':
run('eden', True, True, None, False, 'from gluon import current; current._scheduler.loop()')