From 299ff31781343ded73306b3e0955e07cb79a49f3 Mon Sep 17 00:00:00 2001 From: Disassembler Date: Sun, 6 Mar 2022 10:59:23 +0100 Subject: [PATCH] Initial commit --- .env | 3 ++ README.rst | 2 + docker-compose.yml | 40 ++++++++++++++++ sahana/Dockerfile | 46 +++++++++++++++++++ sahana/image.d/entrypoint.sh | 14 ++++++ sahana/image.d/etc/nginx/nginx.conf | 40 ++++++++++++++++ .../image.d/etc/services.d/.s6-svscan/finish | 5 ++ sahana/image.d/etc/services.d/atd/run | 3 ++ sahana/image.d/etc/services.d/nginx/run | 3 ++ .../image.d/etc/services.d/uwsgi/down-signal | 1 + sahana/image.d/etc/services.d/uwsgi/run | 3 ++ sahana/image.d/etc/sudoers.d/sahana | 1 + sahana/image.d/etc/uwsgi/uwsgi.conf | 22 +++++++++ sahana/image.d/srv/web2py/routes.py | 10 ++++ sahana/image.d/srv/web2py/run_scheduler.py | 6 +++ sahana/image.d/usr/local/bin/migrate | 15 ++++++ 16 files changed, 214 insertions(+) create mode 100644 .env create mode 100644 README.rst create mode 100644 docker-compose.yml create mode 100644 sahana/Dockerfile create mode 100755 sahana/image.d/entrypoint.sh create mode 100644 sahana/image.d/etc/nginx/nginx.conf create mode 100755 sahana/image.d/etc/services.d/.s6-svscan/finish create mode 100755 sahana/image.d/etc/services.d/atd/run create mode 100755 sahana/image.d/etc/services.d/nginx/run create mode 100644 sahana/image.d/etc/services.d/uwsgi/down-signal create mode 100755 sahana/image.d/etc/services.d/uwsgi/run create mode 100644 sahana/image.d/etc/sudoers.d/sahana create mode 100644 sahana/image.d/etc/uwsgi/uwsgi.conf create mode 100644 sahana/image.d/srv/web2py/routes.py create mode 100644 sahana/image.d/srv/web2py/run_scheduler.py create mode 100755 sahana/image.d/usr/local/bin/migrate diff --git a/.env b/.env new file mode 100644 index 0000000..3b9862c --- /dev/null +++ b/.env @@ -0,0 +1,3 @@ +POSTGRES_USER=sahana +POSTGRES_PASSWORD=sahana +POSTGRES_DB=sahana diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..e51acb4 --- /dev/null +++ b/README.rst @@ -0,0 +1,2 @@ +Sahana Eden +=========== diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..7670265 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,40 @@ +--- +version: '3' +services: + + sahana: + build: sahana + image: sahana:1.0.3-220306 + restart: unless-stopped + depends_on: + - sahana-postgres + env_file: + - .env + expose: + - 8080 + ports: + - 8080:8080 + volumes: + - sahana_models:/srv/web2py/applications/eden/models + - sahana_databases:/srv/web2py/applications/eden/databases + - sahana_errors:/srv/web2py/applications/eden/errors + - sahana_sessions:/srv/web2py/applications/eden/sessions + - sahana_uploads:/srv/web2py/applications/eden/uploads + + sahana-postgres: + image: postgis/postgis:13-3.2-alpine + restart: unless-stopped + environment: + - POSTGRES_USER + - POSTGRES_PASSWORD + - POSTGRES_DB + volumes: + - postgres_data:/var/lib/postgresql/data + +volumes: + sahana_models: + sahana_databases: + sahana_errors: + sahana_sessions: + sahana_uploads: + postgres_data: diff --git a/sahana/Dockerfile b/sahana/Dockerfile new file mode 100644 index 0000000..b348e9b --- /dev/null +++ b/sahana/Dockerfile @@ -0,0 +1,46 @@ +FROM docker.io/alpine:3.15 + +RUN \ + # Install OS and python runtime dependencies + apk --no-cache add curl libbz2 libgcc libstdc++ libxml2 libxslt ncurses-libs openssl pcre readline s6 xz-libs && \ + apk --no-cache add python3 py3-pip py3-wheel && \ + ln -s /usr/bin/python3 /usr/bin/python + +RUN \ + # Install Sahana Eden runtime dependencies + apk --no-cache add ansible at geos nginx py3-dateutil py3-gdal py3-lxml py3-numpy py3-pillow py3-psycopg2 py3-requests py3-sgmllib3k py3-yaml uwsgi-python3 sudo + +RUN \ + # Install build dependencies + apk --no-cache add --virtual .deps build-base freetype-dev git py3-numpy-dev python3-dev ttf-dejavu && \ + # Install web2py 2.21.2 + # See https://github.com/sahana/eden/blob/master/tests/travis/install_web2py.sh + git clone --recursive git://github.com/web2py/web2py.git /srv/web2py && \ + git -C /srv/web2py reset --hard 31905858b && \ + git -C /srv/web2py submodule update --recursive && \ + # Install Sahana + git clone --recursive --depth 1 https://github.com/sahana/eden.git /srv/web2py/applications/eden && \ + # Install python dependencies, exclude old or unnecessary ones + # - boto - Needed for setup on AWS + # - PyRTF - Ceased to exist + # - openstacksdk - Needed for setup on OpenStack + sed -i 's/^boto/#boto/' /srv/web2py/applications/eden/optional_requirements.txt && \ + sed -i 's/^PyRTF/#PyRTF/' /srv/web2py/applications/eden/optional_requirements.txt && \ + sed -i 's/^openstacksdk/#openstacksdk/' /srv/web2py/applications/eden/optional_requirements.txt && \ + pip3 install -r /srv/web2py/applications/eden/optional_requirements.txt && \ + # Hackfix paths for ansible and other modules with hardcoded paths + ln -s /srv/web2py /home/prod && \ + # Create OS user + addgroup -S -g 8080 sahana && \ + adduser -S -u 8080 -h /srv/web2py -s /bin/false -g sahana -G sahana sahana && \ + chown -R sahana:sahana /srv/web2py && \ + # Cleanup + apk --no-cache del .deps && \ + find /srv/web2py -name '.git*' -exec rm -rf {} + && \ + rm -r /root/.cache + +COPY image.d / + +ENTRYPOINT ["/entrypoint.sh"] +EXPOSE 8080 +VOLUME ["/srv/web2py/applications/eden/models", "/srv/web2py/applications/eden/databases", "/srv/web2py/applications/eden/errors", "/srv/web2py/applications/eden/sessions", "/srv/web2py/applications/eden/uploads"] diff --git a/sahana/image.d/entrypoint.sh b/sahana/image.d/entrypoint.sh new file mode 100755 index 0000000..2366229 --- /dev/null +++ b/sahana/image.d/entrypoint.sh @@ -0,0 +1,14 @@ +#!/bin/sh +set -e + +if [ "$*" != "" ]; then + exec $@ +fi + +echo "Starting Sahana Eden..." + +# Upgrade database +/usr/local/bin/migrate norestart + +# Exec into s6 supervisor +exec /bin/s6-svscan /etc/services.d diff --git a/sahana/image.d/etc/nginx/nginx.conf b/sahana/image.d/etc/nginx/nginx.conf new file mode 100644 index 0000000..9a3a7da --- /dev/null +++ b/sahana/image.d/etc/nginx/nginx.conf @@ -0,0 +1,40 @@ +user nginx; +pid /run/nginx.pid; +worker_processes 1; +error_log /dev/stderr warn; + +events { + worker_connections 1024; +} + +http { + include mime.types; + default_type application/octet-stream; + + access_log off; + server_tokens off; + client_max_body_size 100m; + sendfile on; + tcp_nodelay on; + send_timeout 300; + + server { + listen 8080; + + location /eden { + uwsgi_pass unix:///run/uwsgi; + uwsgi_read_timeout 300s; + uwsgi_send_timeout 300s; + include uwsgi_params; + } + + location ~* /eden/static { + root /srv/web2py/applications; + expires max; + } + + location = / { + return 301 https://$http_x_forwarded_host/eden/; + } + } +} diff --git a/sahana/image.d/etc/services.d/.s6-svscan/finish b/sahana/image.d/etc/services.d/.s6-svscan/finish new file mode 100755 index 0000000..71d6fbc --- /dev/null +++ b/sahana/image.d/etc/services.d/.s6-svscan/finish @@ -0,0 +1,5 @@ +#!/bin/execlineb -P + +foreground { s6-svwait -d -t 3000 atd } +foreground { s6-svwait -d -t 3000 nginx } +foreground { s6-svwait -d -t 3000 uwsgi } diff --git a/sahana/image.d/etc/services.d/atd/run b/sahana/image.d/etc/services.d/atd/run new file mode 100755 index 0000000..6142dae --- /dev/null +++ b/sahana/image.d/etc/services.d/atd/run @@ -0,0 +1,3 @@ +#!/bin/execlineb -P + +atd -f diff --git a/sahana/image.d/etc/services.d/nginx/run b/sahana/image.d/etc/services.d/nginx/run new file mode 100755 index 0000000..dff57ac --- /dev/null +++ b/sahana/image.d/etc/services.d/nginx/run @@ -0,0 +1,3 @@ +#!/bin/execlineb -P + +nginx -g "daemon off;" diff --git a/sahana/image.d/etc/services.d/uwsgi/down-signal b/sahana/image.d/etc/services.d/uwsgi/down-signal new file mode 100644 index 0000000..d751378 --- /dev/null +++ b/sahana/image.d/etc/services.d/uwsgi/down-signal @@ -0,0 +1 @@ +SIGINT diff --git a/sahana/image.d/etc/services.d/uwsgi/run b/sahana/image.d/etc/services.d/uwsgi/run new file mode 100755 index 0000000..1289260 --- /dev/null +++ b/sahana/image.d/etc/services.d/uwsgi/run @@ -0,0 +1,3 @@ +#!/bin/execlineb -P + +uwsgi --ini /etc/uwsgi/uwsgi.conf diff --git a/sahana/image.d/etc/sudoers.d/sahana b/sahana/image.d/etc/sudoers.d/sahana new file mode 100644 index 0000000..0617737 --- /dev/null +++ b/sahana/image.d/etc/sudoers.d/sahana @@ -0,0 +1 @@ +sahana ALL=(ALL) NOPASSWD:ALL diff --git a/sahana/image.d/etc/uwsgi/uwsgi.conf b/sahana/image.d/etc/uwsgi/uwsgi.conf new file mode 100644 index 0000000..48d6448 --- /dev/null +++ b/sahana/image.d/etc/uwsgi/uwsgi.conf @@ -0,0 +1,22 @@ +[uwsgi] +uid = sahana +gid = sahana +chdir = /srv/web2py + +plugin = python +enable-threads = 1 +module = wsgihandler + +socket = /run/uwsgi +chown-socket = nginx:nginx + +cheaper = 1 +cheaper-algo = spare +cheaper-initial = 1 +cheaper-step = 1 +workers = 8 +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 diff --git a/sahana/image.d/srv/web2py/routes.py b/sahana/image.d/srv/web2py/routes.py new file mode 100644 index 0000000..4c439ed --- /dev/null +++ b/sahana/image.d/srv/web2py/routes.py @@ -0,0 +1,10 @@ +#!/usr/bin/python +default_application = 'eden' +default_controller = 'default' +default_function = 'index' +routes_onerror = [ + ('eden/400', '!'), + ('eden/401', '!'), + ('eden/*', '/eden/errors/index'), + ('*/*', '/eden/errors/index'), +] diff --git a/sahana/image.d/srv/web2py/run_scheduler.py b/sahana/image.d/srv/web2py/run_scheduler.py new file mode 100644 index 0000000..d6e5dbd --- /dev/null +++ b/sahana/image.d/srv/web2py/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/image.d/usr/local/bin/migrate b/sahana/image.d/usr/local/bin/migrate new file mode 100755 index 0000000..aa1fed8 --- /dev/null +++ b/sahana/image.d/usr/local/bin/migrate @@ -0,0 +1,15 @@ +#!/bin/sh + +# This is a compatible rewrite of https://github.com/sahana/eden_deploy/blob/master/roles/common/files/migrate specific for Alpine s6-based container + +# Migrate database +cd /srv/web2py +sed -i 's/settings.base.migrate = False/settings.base.migrate = True/g' applications/eden/models/000_config.py +rm -rf applications/eden/compiled +sudo -u sahana -- ./web2py.py -S eden -M -R applications/eden/static/scripts/tools/noop.py +sed -i 's/settings.base.migrate = True/settings.base.migrate = False/g' applications/eden/models/000_config.py + +# Restart uwsgi +if [ "$1" != "norestart" ]; then + s6-svc -t /etc/services.d/uwsgi +fi