From 8cc184c7e76e82e07ebe3e2fa82d6ee5173912fa Mon Sep 17 00:00:00 2001 From: Disassembler Date: Fri, 14 Sep 2018 08:53:27 +0200 Subject: [PATCH] LXCize SAMBRO setup --- sahana-demo/setup.sh | 3 +- sambro/etc/init.d/sambro | 32 ------------------- sambro/lxcfile | 12 +++++++ sambro/{build.sh => setup.sh} | 29 ++++++++--------- sambro/{ => setup}/createdb.sql | 0 sambro/setup/etc/init.d/sambro | 23 +++++++++++++ sambro/{ => setup}/masterUsers.csv | 0 .../{ => setup}/srv/sambro/conf/000_config.py | 0 .../srv/sambro/conf/00_settings.py | 0 .../srv/sambro/data/SAMBRO/config.py | 0 10 files changed, 49 insertions(+), 50 deletions(-) delete mode 100755 sambro/etc/init.d/sambro create mode 100644 sambro/lxcfile rename sambro/{build.sh => setup.sh} (54%) rename sambro/{ => setup}/createdb.sql (100%) create mode 100755 sambro/setup/etc/init.d/sambro rename sambro/{ => setup}/masterUsers.csv (100%) rename sambro/{ => setup}/srv/sambro/conf/000_config.py (100%) rename sambro/{ => setup}/srv/sambro/conf/00_settings.py (100%) rename sambro/{ => setup}/srv/sambro/data/SAMBRO/config.py (100%) diff --git a/sahana-demo/setup.sh b/sahana-demo/setup.sh index 77dd09c..cfa0c47 100755 --- a/sahana-demo/setup.sh +++ b/sahana-demo/setup.sh @@ -25,12 +25,11 @@ envsubst <${SOURCE_DIR}/srv/sahana-demo/conf/000_config.py >/srv/sahana-demo/con 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 +# Replace template in LXC container mount 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 /var/lib/lxc/sahana/sahana/tmp/masterUsers.csv # Install service diff --git a/sambro/etc/init.d/sambro b/sambro/etc/init.d/sambro deleted file mode 100755 index ad199ff..0000000 --- a/sambro/etc/init.d/sambro +++ /dev/null @@ -1,32 +0,0 @@ -#!/sbin/openrc-run - -description="SAMBRO docker container" - -depend() { - need docker postfix postgres -} - -start() { - /usr/bin/docker run -d --rm \ - --name sambro \ - -h sambro \ - --link postfix \ - --link postgres \ - -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 -} - -start_post() { - /usr/bin/vmmgr register-proxy sambro -} - -stop_pre() { - /usr/bin/vmmgr unregister-proxy sambro -} - -stop() { - /usr/bin/docker stop sambro -} diff --git a/sambro/lxcfile b/sambro/lxcfile new file mode 100644 index 0000000..6b1a048 --- /dev/null +++ b/sambro/lxcfile @@ -0,0 +1,12 @@ +IMAGE sambro +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/SAMBRO srv/web2py/applications/eden/modules/templates/SAMBRO +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/sambro/build.sh b/sambro/setup.sh similarity index 54% rename from sambro/build.sh rename to sambro/setup.sh index f8f61d9..9bd4038 100755 --- a/sambro/build.sh +++ b/sambro/setup.sh @@ -1,41 +1,34 @@ #!/bin/sh set -e -SOURCE_DIR=$(realpath $(dirname "${0}"))/sambro +SOURCE_DIR=$(realpath $(dirname "${0}"))/setup # 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 -cp ${SOURCE_DIR}/etc/init.d/sambro /etc/init.d/sambro -rc-update -u +[ ! -e /run/openrc/started/postgres ] && service postgres start && STOP_POSTGRES=1 # Create PostgreSQL user and database export SAMBRO_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/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 +cp -rp /var/lib/lxc/sahana/sahana/srv/web2py/applications/eden/models/. /srv/sahana-demo/conf +cp -rp /srv/web2py/applications/eden/modules/templates/SAMBRO/. /srv/sahana-demo/data/SAMBRO # Configure SAMBRO export SAMBRO_HMAC=$(head -c 18 /dev/urandom | base64) export SAMBRO_ADMIN_USER=admin@example.com export SAMBRO_ADMIN_PWD=$(head -c 12 /dev/urandom | base64) envsubst <${SOURCE_DIR}/srv/sambro/conf/000_config.py >/srv/sambro/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 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 vmmgr update-login sambro "${SAMBRO_ADMIN_USER}" "${SAMBRO_ADMIN_PWD}" # Populate database -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 su - sahana -c "./web2py.py -S eden -M -R applications/eden/static/scripts/tools/noop.py" -rm /tmp/masterUsers.csv +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' +rm /var/lib/lxc/sahana/sahana/tmp/masterUsers.csv # Set "production values" (increases performance) only if the DEBUG environment variable is not set if [ ${DEBUG:-0} -eq 0 ]; then @@ -44,5 +37,9 @@ if [ ${DEBUG:-0} -eq 0 ]; then sed -i 's/#settings.base.prepopulate = 0/settings.base.prepopulate = 0/' /srv/sambro/conf/000_config.py fi +# Install service +cp ${SOURCE_DIR}/etc/init.d/sambro /etc/init.d/sambro +rc-update -u + # Stop services required for build -service postgres stop +[ ! -z ${STOP_POSTGRES} ] && service postgres stop diff --git a/sambro/createdb.sql b/sambro/setup/createdb.sql similarity index 100% rename from sambro/createdb.sql rename to sambro/setup/createdb.sql diff --git a/sambro/setup/etc/init.d/sambro b/sambro/setup/etc/init.d/sambro new file mode 100755 index 0000000..4f0582a --- /dev/null +++ b/sambro/setup/etc/init.d/sambro @@ -0,0 +1,23 @@ +#!/sbin/openrc-run + +description="SAMBRO container" + +depend() { + need cgroups postgres +} + +start() { + lxc-start sambro +} + +start_post() { + /usr/bin/vmmgr register-proxy sambro +} + +stop_pre() { + /usr/bin/vmmgr unregister-proxy sambro +} + +stop() { + lxc-stop sambro +} diff --git a/sambro/masterUsers.csv b/sambro/setup/masterUsers.csv similarity index 100% rename from sambro/masterUsers.csv rename to sambro/setup/masterUsers.csv diff --git a/sambro/srv/sambro/conf/000_config.py b/sambro/setup/srv/sambro/conf/000_config.py similarity index 100% rename from sambro/srv/sambro/conf/000_config.py rename to sambro/setup/srv/sambro/conf/000_config.py diff --git a/sambro/srv/sambro/conf/00_settings.py b/sambro/setup/srv/sambro/conf/00_settings.py similarity index 100% rename from sambro/srv/sambro/conf/00_settings.py rename to sambro/setup/srv/sambro/conf/00_settings.py diff --git a/sambro/srv/sambro/data/SAMBRO/config.py b/sambro/setup/srv/sambro/data/SAMBRO/config.py similarity index 100% rename from sambro/srv/sambro/data/SAMBRO/config.py rename to sambro/setup/srv/sambro/data/SAMBRO/config.py