From 05320027254cffe41ed79fcf5c135f074bd36c53 Mon Sep 17 00:00:00 2001 From: Disassembler Date: Sun, 25 Jun 2017 20:49:41 +0200 Subject: [PATCH] Add script + configs for Sahana --- sahana.sh | 77 +++++++++++++++++++ sahana/etc/nginx/apps-available/sahana | 13 ++++ sahana/etc/uwsgi/apps-available/sahana.ini | 14 ++++ .../applications/eden/models/000_config.py | 14 ++++ sahana/srv/sahana/routes.py | 11 +++ sahana/srv/sahana/run_scheduler.py | 6 ++ sahana/tmp/createdb-sahana.sql | 8 ++ 7 files changed, 143 insertions(+) create mode 100644 sahana.sh create mode 100644 sahana/etc/nginx/apps-available/sahana create mode 100644 sahana/etc/uwsgi/apps-available/sahana.ini create mode 100644 sahana/srv/sahana/applications/eden/models/000_config.py create mode 100644 sahana/srv/sahana/routes.py create mode 100644 sahana/srv/sahana/run_scheduler.py create mode 100644 sahana/tmp/createdb-sahana.sql diff --git a/sahana.sh b/sahana.sh new file mode 100644 index 0000000..10572fa --- /dev/null +++ b/sahana.sh @@ -0,0 +1,77 @@ +#!/bin/bash + +SOURCE_DIR=$(realpath $(dirname "${0}")) + +# Install dependencies for Sahana +apt-get -y --no-install-recommends install python-dateutil python-imaging python-lxml python-matplotlib python-psycopg2 python-reportlab python-requests python-shapely python-xlrd python-xlwt + +# Install web2py +wget http://web2py.com/examples/static/web2py_src.zip -O /srv/web2py.zip +unzip -d /srv /srv/web2py.zip +mv /srv/web2py /srv/sahana +rm -f /srv/web2py.zip + +# Install Sahana +wget https://github.com/sahana/eden/archive/master.tar.gz -O /srv/sahana/applications/eden.tgz +tar xzf /srv/sahana/applications/eden.tgz -C /srv/sahana/applications/ +mv /srv/sahana/applications/eden-master /srv/sahana/applications/eden +rm -f /srv/sahana/applications/eden.tgz + +# Create PostgreSQL user and database +export SAHANA_PWD=$(head -c 260 /dev/urandom | tr -cd '[:alnum:]' | head -c 26) +envsubst <${SOURCE_DIR}/sahana/tmp/createdb-sahana.sql >/tmp/createdb-sahana.sql +sudo -u postgres psql -f /tmp/createdb-sahana.sql +rm -f /tmp/createdb-sahana.sql + +# Configure web2py +cp ${SOURCE_DIR}/sahana/srv/sahana/routes.py /srv/sahana/routes.py +cp ${SOURCE_DIR}/sahana/srv/sahana/run_scheduler.py /srv/sahana/run_scheduler.py +ln -s /srv/sahana/handlers/wsgihandler.py /srv/sahana/wsgihandler.py + +# 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 260 /dev/urandom | tr -cd '[:alnum:]' | head -c 26) +envsubst <${SOURCE_DIR}/sahana/srv/sahana/applications/eden/models/000_config.py >/srv/sahana/applications/eden/models/000_config.py + +# Create OS user +adduser --system --group --home /srv/sahana --shell /bin/false sahana +chown -R sahana:sahana /srv/sahana + +# Populate database +cd /srv/sahana +sudo -u sahana python web2py.py -S eden -M -R applications/eden/static/scripts/tools/noop.py + +# Set "production values" (increases performance) +sed -i 's/settings.base.migrate = True/settings.base.migrate = 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 + +# Create uwsgi and nginx app definition +cp ${SOURCE_DIR}/sahana/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}/sahana/etc/nginx/apps-available/sahana /etc/nginx/apps-available/sahana +ln -s /etc/nginx/apps-available/sahana /etc/nginx/apps-enabled/sahana + +# Restart services +systemctl restart uwsgi +systemctl restart nginx + +# TODO: Set cgroups + +# 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: pyth required for RTF document support in Full-Text Search +# 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: S3Msg unresolved dependency tweepy required for non-Tropo Twitter support +# WARNING: Message Parsing unresolved dependency: TwitterSearch required for fetching results from twitter keyword queries diff --git a/sahana/etc/nginx/apps-available/sahana b/sahana/etc/nginx/apps-available/sahana new file mode 100644 index 0000000..2f315d9 --- /dev/null +++ b/sahana/etc/nginx/apps-available/sahana @@ -0,0 +1,13 @@ +location /eden/ { + uwsgi_pass unix:///run/uwsgi/app/sahana/socket; + uwsgi_read_timeout 120s; + uwsgi_send_timeout 120s; + include uwsgi_params; + uwsgi_param UWSGI_SCHEME $scheme; + uwsgi_param SERVER_SOFTWARE nginx/$nginx_version; +} + +location ~* /eden/static/ { + root /srv/sahana/applications/; + expires max; +} diff --git a/sahana/etc/uwsgi/apps-available/sahana.ini b/sahana/etc/uwsgi/apps-available/sahana.ini new file mode 100644 index 0000000..0e9b473 --- /dev/null +++ b/sahana/etc/uwsgi/apps-available/sahana.ini @@ -0,0 +1,14 @@ +[uwsgi] +uid = sahana +gid = sahana +chown-socket = www-data:www-data +chdir = /srv/sahana +module = wsgihandler +mule = run_scheduler.py +cheaper = 1 +cheaper-algo = spare +cheaper-initial = 1 +cheaper-step = 1 +workers = 4 +idle = 600 +harakiri = 120 diff --git a/sahana/srv/sahana/applications/eden/models/000_config.py b/sahana/srv/sahana/applications/eden/models/000_config.py new file mode 100644 index 0000000..77ddbe6 --- /dev/null +++ b/sahana/srv/sahana/applications/eden/models/000_config.py @@ -0,0 +1,14 @@ +settings.base.template = "default" +settings.database.db_type = "postgres" +settings.database.password = "${SAHANA_PWD}" +settings.gis.spatialdb = True +settings.base.migrate = True +settings.base.debug = False +settings.auth.hmac_key = "${SAHANA_HMAC}" +settings.frontpage.rss = [ + {"title": "Eden", "url": "http://eden.sahanafoundation.org/timeline?ticket=on&changeset=on&milestone=on&wiki=on&max=50&daysback=90&format=rss"}, + {"title": "Twitter", "url": "http://www.rssitfor.me/getrss?name=@SahanaFOSS"} +] +settings.import_template() +# settings.base.prepopulate = 0 +VERSION = 1 diff --git a/sahana/srv/sahana/routes.py b/sahana/srv/sahana/routes.py new file mode 100644 index 0000000..1f2fe7e --- /dev/null +++ b/sahana/srv/sahana/routes.py @@ -0,0 +1,11 @@ +#!/usr/bin/python +default_application = 'eden' +default_controller = 'default' +default_function = 'index' +routes_onerror = [ + ('eden/400', '!'), + ('eden/401', '!'), + ('eden/509', '!'), + ('eden/*', '/eden/errors/index'), + ('*/*', '/eden/errors/index'), +] diff --git a/sahana/srv/sahana/run_scheduler.py b/sahana/srv/sahana/run_scheduler.py new file mode 100644 index 0000000..d6e5dbd --- /dev/null +++ b/sahana/srv/sahana/run_scheduler.py @@ -0,0 +1,6 @@ +#!/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()') diff --git a/sahana/tmp/createdb-sahana.sql b/sahana/tmp/createdb-sahana.sql new file mode 100644 index 0000000..df525eb --- /dev/null +++ b/sahana/tmp/createdb-sahana.sql @@ -0,0 +1,8 @@ +CREATE ROLE sahana NOSUPERUSER NOCREATEDB NOCREATEROLE NOINHERIT LOGIN ENCRYPTED PASSWORD '${SAHANA_PWD}'; +CREATE DATABASE sahana; +REVOKE ALL ON DATABASE sahana FROM public; +ALTER DATABASE sahana OWNER TO sahana; +\c sahana; +CREATE EXTENSION postgis; +GRANT ALL ON geometry_columns TO sahana; +GRANT ALL ON spatial_ref_sys TO sahana;