Alpinize + Dockerize SAMBRO

This commit is contained in:
Disassembler 2018-01-28 09:13:30 +01:00
parent f0ec1794ca
commit 6a605e169a
Signed by: Disassembler
GPG Key ID: 524BD33A0EE29499
13 changed files with 69 additions and 8369 deletions

107
sambro.sh
View File

@ -2,102 +2,49 @@
SOURCE_DIR=$(realpath $(dirname "${0}"))/sambro
# 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
# Install web2py
git clone --recursive https://github.com/web2py/web2py.git /srv/sambro
git -C /srv/sambro checkout cda35fd
git -C /srv/sambro submodule update
# Install Sahana
git clone --depth 1 https://github.com/sahana/eden.git /srv/sambro/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/sambro/applications/eden/languages/cs.py /srv/sambro/applications/eden/languages/cs.py
# Symlink fonts with Czech glyphs
ln -s /usr/share/fonts/truetype/dejavu/DejaVuSerif-Bold.ttf /srv/sambro/applications/eden/static/fonts/DejaVuSerif-Bold.ttf
ln -s /usr/share/fonts/truetype/dejavu/DejaVuSerif.ttf /srv/sambro/applications/eden/static/fonts/DejaVuSerif.ttf
# Check prerequisites
docker image ls | grep -q postgres || $(realpath $(dirname "${0}"))/postgres.sh
docker image ls | grep -q sahana || docker build -t sahana $(realpath $(dirname "${0}"))/sahana
# Create PostgreSQL user and database
export SAMBRO_PWD=$(head -c 18 /dev/urandom | base64)
envsubst <${SOURCE_DIR}/tmp/sambro-createdb.sql >/tmp/sambro-createdb.sql
sudo -u postgres psql -f /tmp/sambro-createdb.sql
rm -f /tmp/sambro-createdb.sql
envsubst <${SOURCE_DIR}/createdb.sql | docker exec -i postgres psql
# Configure web2py
rm -rf /srv/sambro/applications/{admin,examples,welcome}
cp ${SOURCE_DIR}/srv/sambro/routes.py /srv/sambro/routes.py
cp ${SOURCE_DIR}/srv/sambro/run_scheduler.py /srv/sambro/run_scheduler.py
ln -s /srv/sambro/handlers/wsgihandler.py /srv/sambro/wsgihandler.py
# Prepare persistent directory structure
mkdir -p /srv/sambro/conf /srv/sambro/data/databases /srv/sambro/data/uploads /srv/sambro/data/SAMBRO
chown -R 8001:8001 /srv/sambro/conf /srv/sambro/data
docker run --rm -v /srv/sambro/conf:/mnt/conf sahana cp -rp /srv/web2py/applications/eden/models/. /mnt/conf
docker run --rm -v /srv/sambro/data/SAMBRO:/mnt/data sahana cp -rp /srv/web2py/applications/eden/modules/templates/SAMBRO/. /mnt/data
# Configure Sahana
mkdir /srv/sambro/applications/eden/{databases,errors,sessions}
mkdir -p /srv/sambro/applications/eden/static/cache/chart
mkdir -p /srv/sambro/applications/eden/uploads/{gis_cache,images,tracks}
# Configure SAMBRO
export SAMBRO_HMAC=$(head -c 18 /dev/urandom | base64)
envsubst <${SOURCE_DIR}/srv/sambro/applications/eden/models/000_config.py >/srv/sambro/applications/eden/models/000_config.py
cp ${SOURCE_DIR}/srv/sambro/applications/eden/models/00_settings.py /srv/sambro/applications/eden/models/00_settings.py
envsubst <${SOURCE_DIR}/srv/sambro/conf/000_config.py >/srv/sambro/conf/000_config.py
cp ${SOURCE_DIR}/srv/sambro/conf/00_settings.py /srv/sambro/conf/00_settings.py
cp ${SOURCE_DIR}/srv/sambro/data/SAMBRO/config.py /srv/sambro/data/SAMBRO/config.py
export SAMBRO_ADMIN_USER=admin@example.com
export SAMBRO_ADMIN_PWD=$(head -c 12 /dev/urandom | base64)
cp ${SOURCE_DIR}/srv/sambro/applications/eden/modules/templates/SAMBRO/config.py /srv/sambro/applications/eden/modules/templates/SAMBRO/config.py
envsubst <${SOURCE_DIR}/srv/sambro/applications/eden/modules/templates/default/users/masterUsers.csv >/srv/sambro/applications/eden/modules/templates/default/users/masterUsers.csv
# Create OS user
adduser --system --group --home /srv/sambro --shell /bin/false sambro
chown -R sambro:sambro /srv/sambro
envsubst <${SOURCE_DIR}/masterUsers.csv >/tmp/masterUsers.csv
# Populate database
cd /srv/sambro
sudo -u sambro python web2py.py -S eden -M -R applications/eden/static/scripts/tools/noop.py
docker run --rm -h sambro --link postgres -v /srv/sambro/conf:/srv/web2py/applications/eden/models -v /srv/sambro/data/SAMBRO:/srv/web2py/applications/eden/modules/templates/SAMBRO -v /tmp/masterUsers.csv:/srv/web2py/applications/eden/modules/templates/default/users/masterUsers.csv -v /srv/sambro/data/databases:/srv/web2py/applications/eden/databases sahana ./web2py.py -S eden -M -R applications/eden/static/scripts/tools/noop.py
rm /tmp/masterUsers.csv
# 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/sambro/applications/eden/models/000_config.py
sed -i 's/settings.base.debug = True/settings.base.debug = False/' /srv/sambro/applications/eden/models/000_config.py
sed -i 's/#settings.base.prepopulate = 0/settings.base.prepopulate = 0/' /srv/sambro/applications/eden/models/000_config.py
sudo -u sambro 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/sambro/conf/000_config.py
sed -i 's/settings.base.debug = True/settings.base.debug = False/' /srv/sambro/conf/000_config.py
sed -i 's/#settings.base.prepopulate = 0/settings.base.prepopulate = 0/' /srv/sambro/conf/000_config.py
fi
# Create uwsgi and nginx app definition
cp ${SOURCE_DIR}/etc/uwsgi/apps-available/sambro.ini /etc/uwsgi/apps-available/sambro.ini
ln -s /etc/uwsgi/apps-available/sambro.ini /etc/uwsgi/apps-enabled/sambro.ini
cp ${SOURCE_DIR}/etc/nginx/sites-available/sambro /etc/nginx/sites-available/sambro
ln -s /etc/nginx/sites-available/sambro /etc/nginx/sites-enabled/sambro
# Create SAMBRO service
cp ${SOURCE_DIR}/etc/init.d/sambro /etc/init.d/sambro
rc-update add sambro boot
service sambro start
# Restart services
systemctl restart uwsgi
systemctl restart nginx
# Create nginx app definition
cp ${SOURCE_DIR}/etc/nginx/conf.d/sambro.conf /etc/nginx/conf.d/sambro.conf
service nginx reload
# Add portal application definition
portal-app-manager sambro "https://{host}:8099/eden/" "${SAMBRO_ADMIN_USER}" "${SAMBRO_ADMIN_PWD}"
portal-app-manager sambro "https://{host}:8499/eden/" "${SAMBRO_ADMIN_USER}" "${SAMBRO_ADMIN_PWD}"
portal-app-manager sambro-mobile
# TODO: Does matplotlib still require MPLCONFIGDIR directory?
# If so, /srv/sambro/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/sambro/applications/eden/modules/templates/default/config.py
# vi /srv/sambro/applications/eden/modules/s3cfg.py
# sudo -u sambro /srv/sambro/web2py.py -i 192.168.2.202 -a sambro --nogui
# sudo -u sambro openssl req -new -x509 -nodes -out /srv/sambro/cert.pem -keyout /srv/sambro/cert.key -subj "/C=CZ/CN=$(hostname -f)"
# sudo -u sambro /srv/sambro/web2py.py -i 192.168.2.202 -a sambro -c /srv/sambro/cert.pem -k /srv/sambro/cert.key --nogui

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

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

View File

@ -0,0 +1,14 @@
server {
listen [::]:8099 ipv6only=off;
listen [::]:8499 ssl http2 ipv6only=off;
access_log /var/log/nginx/sambro.access.log;
error_log /var/log/nginx/sambro.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:9099;
}
}

View File

@ -1,25 +0,0 @@
server {
listen 8099 ssl http2;
listen [::]:8099 ssl http2;
access_log /var/log/nginx/sambro.access.log;
error_log /var/log/nginx/sambro.error.log;
location / {
return 301 https://$http_host:8099/eden/;
}
location /eden {
uwsgi_pass unix:///run/uwsgi/app/sambro/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/sambro/applications;
expires max;
}
}

View File

@ -1,15 +0,0 @@
[uwsgi]
uid = sambro
gid = sambro
chown-socket = www-data:www-data
chdir = /srv/sambro
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

File diff suppressed because it is too large Load Diff

View File

@ -30,7 +30,7 @@ settings.base.template = "SAMBRO"
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 = "${SAMBRO_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

@ -1,11 +0,0 @@
#!/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'),
]

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()')