diff --git a/sahana-demo/etc/init.d/sahana-demo b/sahana-demo/etc/init.d/sahana-demo deleted file mode 100755 index d22bc8f..0000000 --- a/sahana-demo/etc/init.d/sahana-demo +++ /dev/null @@ -1,32 +0,0 @@ -#!/sbin/openrc-run - -description="Sahana Eden Demo docker container" - -depend() { - need docker postfix postgres -} - -start() { - /usr/bin/docker run -d --rm \ - --name sahana-demo \ - -h sahana-demo \ - --link postfix \ - --link postgres \ - -v /srv/sahana-demo/conf:/srv/web2py/applications/eden/models \ - -v /srv/sahana-demo/data/${TEMPLATE}:/srv/web2py/applications/eden/modules/templates/${TEMPLATE} \ - -v /srv/sahana-demo/data/databases:/srv/web2py/applications/eden/databases \ - -v /srv/sahana-demo/data/uploads:/srv/web2py/applications/eden/uploads \ - sahana -} - -start_post() { - /usr/bin/vmmgr register-proxy sahana-demo -} - -stop_pre() { - /usr/bin/vmmgr unregister-proxy sahana-demo -} - -stop() { - /usr/bin/docker stop sahana-demo -} diff --git a/sahana-demo/lxcfile b/sahana-demo/lxcfile new file mode 100644 index 0000000..a87b945 --- /dev/null +++ b/sahana-demo/lxcfile @@ -0,0 +1,12 @@ +IMAGE sahana-demo +LAYER shared/alpine +LAYER shared/libxml +LAYER shared/python2 +LAYER sahana/sahana + +MOUNT /srv/sahana/conf srv/web2py/applications/eden/models +MOUNT /srv/sahana/data/default srv/web2py/applications/eden/modules/templates/default +MOUNT /srv/sahana/data/databases srv/web2py/applications/eden/databases +MOUNT /srv/sahana/data/uploads srv/web2py/applications/eden/uploads + +CMD s6-svscan /etc/services.d diff --git a/sahana-demo/build.sh b/sahana-demo/setup.sh similarity index 59% rename from sahana-demo/build.sh rename to sahana-demo/setup.sh index 64d31ae..77dd09c 100755 --- a/sahana-demo/build.sh +++ b/sahana-demo/setup.sh @@ -1,40 +1,41 @@ #!/bin/sh set -e -SOURCE_DIR=$(realpath $(dirname "${0}"))/sahana-demo +SOURCE_DIR=$(realpath $(dirname "${0}"))/setup export TEMPLATE=${TEMPLATE:-"default"} # Check prerequisites -lxc-ls | grep -q postfix || $(realpath $(dirname "${0}"))/postfix.sh -lxc-ls | grep -q postgres || $(realpath $(dirname "${0}"))/postgres.sh -service postgres start - -# Build Docker container -docker build -t sahana $(realpath $(dirname "${0}"))/sahana -envsubst <${SOURCE_DIR}/etc/init.d/sahana-demo >/etc/init.d/sahana-demo -chmod +x /etc/init.d/sahana-demo +[ ! -e /run/openrc/started/postgres ] && service postgres start && STOP_POSTGRES=1 # Create PostgreSQL user and database export SAHANADEMO_PWD=$(head -c 18 /dev/urandom | base64) -envsubst <${SOURCE_DIR}/createdb.sql | docker exec -i postgres psql +envsubst <${SOURCE_DIR}/createdb.sql | lxc-attach -u 5432 -g 5432 postgres -- psql # Prepare persistent directory structure mkdir -p /srv/sahana-demo/conf /srv/sahana-demo/data/databases /srv/sahana-demo/data/uploads /srv/sahana-demo/data/${TEMPLATE} chown -R 8001:8001 /srv/sahana-demo/conf /srv/sahana-demo/data -docker run --rm -v /srv/sahana-demo/conf:/mnt/conf sahana cp -rp /srv/web2py/applications/eden/models/. /mnt/conf -docker run --rm -v /srv/sahana-demo/data/${TEMPLATE}:/mnt/data sahana cp -rp /srv/web2py/applications/eden/modules/templates/${TEMPLATE}/. /mnt/data +cp -rp /var/lib/lxc/sahana/sahana/srv/web2py/applications/eden/models/. /srv/sahana-demo/conf +cp -rp /srv/web2py/applications/eden/modules/templates/${TEMPLATE}/. /srv/sahana-demo/data/${TEMPLATE} # Configure Sahana export SAHANADEMO_HMAC=$(head -c 18 /dev/urandom | base64) export SAHANADEMO_ADMIN_USER=admin@example.com export SAHANADEMO_ADMIN_PWD=$(head -c 12 /dev/urandom | base64) envsubst <${SOURCE_DIR}/srv/sahana-demo/conf/000_config.py >/srv/sahana-demo/conf/000_config.py -envsubst <${SOURCE_DIR}/masterUsers.csv >/tmp/masterUsers.csv +envsubst <${SOURCE_DIR}/masterUsers.csv >/var/lib/lxc/sahana/sahana/tmp/masterUsers.csv vmmgr update-login sahana-demo "${SAHANADEMO_ADMIN_USER}" "${SAHANADEMO_ADMIN_PWD}" +# Replace template in LXC container definition +sed -i "s/default/${TEMPLATE}/g" /var/lib/lxc/sahana-demo/config + # Populate database +lxc-execute -u 8001 -g 8001 sahana -- sh -c 'cd /srv/web2py; cp /tmp/masterUsers.csv applications/eden/modules/templates/default/users/masterUsers.csv; ./web2py.py -S eden -M -R applications/eden/static/scripts/tools/noop.py' docker run --rm -h sahana-demo --link postgres -v /srv/sahana-demo/conf:/srv/web2py/applications/eden/models -v /srv/sahana-demo/data/${TEMPLATE}:/srv/web2py/applications/eden/modules/templates/${TEMPLATE} -v /srv/sahana-demo/data/databases:/srv/web2py/applications/eden/databases -v /tmp/masterUsers.csv:/srv/web2py/applications/eden/modules/templates/default/users/masterUsers.csv sahana su - sahana -c "./web2py.py -S eden -M -R applications/eden/static/scripts/tools/noop.py" -rm /tmp/masterUsers.csv +rm /var/lib/lxc/sahana/sahana/tmp/masterUsers.csv + +# Install service +envsubst <${SOURCE_DIR}/etc/init.d/sahana-demo >/etc/init.d/sahana-demo +chmod +x /etc/init.d/sahana-demo # Stop services required for build -service postgres stop +[ ! -z ${STOP_POSTGRES} ] && service postgres stop diff --git a/sahana-demo/createdb.sql b/sahana-demo/setup/createdb.sql similarity index 100% rename from sahana-demo/createdb.sql rename to sahana-demo/setup/createdb.sql diff --git a/sahana-demo/setup/etc/init.d/sahana-demo b/sahana-demo/setup/etc/init.d/sahana-demo new file mode 100755 index 0000000..b4ef4e6 --- /dev/null +++ b/sahana-demo/setup/etc/init.d/sahana-demo @@ -0,0 +1,23 @@ +#!/sbin/openrc-run + +description="Sahana Eden Demo container" + +depend() { + need cgroups postgres +} + +start() { + lxc-start sahana-demo +} + +start_post() { + /usr/bin/vmmgr register-proxy sahana-demo +} + +stop_pre() { + /usr/bin/vmmgr unregister-proxy sahana-demo +} + +stop() { + lxc-stop sahana-demo +} diff --git a/sahana-demo/masterUsers.csv b/sahana-demo/setup/masterUsers.csv similarity index 100% rename from sahana-demo/masterUsers.csv rename to sahana-demo/setup/masterUsers.csv diff --git a/sahana-demo/srv/sahana-demo/conf/000_config.py b/sahana-demo/setup/srv/sahana-demo/conf/000_config.py similarity index 100% rename from sahana-demo/srv/sahana-demo/conf/000_config.py rename to sahana-demo/setup/srv/sahana-demo/conf/000_config.py diff --git a/sahana/setup.sh b/sahana/setup.sh index e648d2e..de14d3e 100755 --- a/sahana/setup.sh +++ b/sahana/setup.sh @@ -13,7 +13,7 @@ envsubst <${SOURCE_DIR}/createdb.sql | lxc-attach -u 5432 -g 5432 postgres -- ps # 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 +cp -rp /var/lib/lxc/sahana/sahana/srv/web2py/applications/eden/models/. /srv/sahana/conf # Copy Spotter template cp -r ${SOURCE_DIR}/srv/sahana/data/Spotter /srv/sahana/data/