diff --git a/motech.sh b/motech.sh index 260f868..8bdc6be 100755 --- a/motech.sh +++ b/motech.sh @@ -1,71 +1,42 @@ -#!/bin/bash +#!/bin/sh SOURCE_DIR=$(realpath $(dirname "${0}"))/motech -# Install dependencies -apt-get -y --no-install-recommends install activemq openjdk-8-jre-headless - -# Download Tomcat 8.0 -wget http://mirror.dkm.cz/apache/tomcat/tomcat-8/v8.0.47/bin/apache-tomcat-8.0.47.tar.gz -O /tmp/apache-tomcat-8.tgz -tar xf /tmp/apache-tomcat-8.tgz -C /srv -mv /srv/apache-tomcat-8.0.47 /srv/motech -rm -f /tmp/apache-tomcat-8.tgz - -# Download Motech -wget 'http://nexus.motechproject.org/service/local/artifact/maven/redirect?r=releases&g=org.motechproject&a=motech-platform-server&v=RELEASE&e=war' -O /tmp/motech.war -unzip /tmp/motech.war -d /srv/motech/webapps/motech -rm -f /tmp/motech.war - -# Update Postgres JDBC driver -rm -f /srv/motech/webapps/motech/WEB-INF/lib/postgresql-9.1-901.jdbc4.jar -rm -f /srv/motech/webapps/motech/WEB-INF/bundles/postgresql-9.1-901.jdbc4.jar -wget https://jdbc.postgresql.org/download/postgresql-42.1.4.jar -O /srv/motech/webapps/motech/WEB-INF/lib/postgresql-42.1.4.jar -cp /srv/motech/webapps/motech/WEB-INF/lib/postgresql-42.1.4.jar /srv/motech/webapps/motech/WEB-INF/bundles/postgresql-42.1.4.jar +# Build Docker container +docker build -t motech ${SOURCE_DIR} # Create database export MOTECH_PWD=$(head -c 18 /dev/urandom | base64) -envsubst <${SOURCE_DIR}/tmp/motech-createdb.sql >/tmp/motech-createdb.sql -sudo -u postgres psql -f /tmp/motech-createdb.sql -rm -f /tmp/motech-createdb.sql +envsubst <${SOURCE_DIR}/createdb.sql | docker exec -i postgres psql -# Configure ActiveMQ -cp ${SOURCE_DIR}/etc/activemq/instances-available/main/activemq.xml /etc/activemq/instances-available/main/activemq.xml -ln -s /etc/activemq/instances-available/main /etc/activemq/instances-enabled/main -systemctl restart activemq +# Configure Motech +mkdir -p /srv/motech/conf/config/org.motechproject.motech-platform-email +envsubst <${SOURCE_DIR}/srv/motech/conf/config/bootstrap.properties >/srv/motech/conf/config/bootstrap.properties +cp ${SOURCE_DIR}/srv/motech/conf/config-locations.properties /srv/motech/conf/config-locations.properties +cp ${SOURCE_DIR}/srv/motech/conf/config/motech-settings.properties /srv/motech/conf/config/motech-settings.properties +cp ${SOURCE_DIR}/srv/motech/conf/config/org.motechproject.motech-platform-email/motech-email.properties /srv/motech/conf/config/org.motechproject.motech-platform-email/motech-email.properties +chown -R 8013:8013 /srv/motech/conf +cp ${SOURCE_DIR}/srv/motech/update-ip.sh /srv/motech/update-ip.sh +/srv/motech/update-ip.sh -# Configure Motech bootstrap -mkdir -p /srv/motech/.motech/config/org.motechproject.motech-platform-email -envsubst <${SOURCE_DIR}/srv/motech/.motech/config/bootstrap.properties >/srv/motech/.motech/config/bootstrap.properties -cp ${SOURCE_DIR}/srv/motech/.motech/config-locations.properties /srv/motech/.motech/config-locations.properties -cp ${SOURCE_DIR}/srv/motech/.motech/config/motech-settings.properties /srv/motech/.motech/config/motech-settings.properties -cp ${SOURCE_DIR}/srv/motech/.motech/config/org.motechproject.motech-platform-email/motech-email.properties /srv/motech/.motech/config/org.motechproject.motech-platform-email/motech-email.properties - -# Create Motech OS user -adduser --system --group --home /srv/motech --shell /bin/false motech -chown -R motech:motech /srv/motech/ - -# Configure Tomcat -cp ${SOURCE_DIR}/srv/motech/conf/server.xml /srv/motech/conf/server.xml -cp ${SOURCE_DIR}/lib/systemd/system/motech.service /lib/systemd/system/motech.service -systemctl daemon-reload -systemctl enable motech -systemctl start motech +# Create Motech service +cp ${SOURCE_DIR}/etc/init.d/motech /etc/init.d/motech +rc-update add motech boot +service motech start # Configure Motech admin +echo `date` '- Waiting for database to be populated. This should take about a minute.' +until curl -s http://127.0.0.1:9013/motech/module/server/startup/ | grep -q adminLogin; do + sleep 1 +done export MOTECH_ADMIN_USER="admin" export MOTECH_ADMIN_EMAIL="admin@example.com" export MOTECH_ADMIN_PWD=$(head -c 12 /dev/urandom | base64) -until $(curl -s http://127.0.0.1:9081/motech/module/server/startup/ | grep -q adminLogin); do - sleep 1 -done -curl -H "Content-Type: application/json" -X POST -d "{\"adminLogin\":\"${MOTECH_ADMIN_USER}\",\"adminEmail\":\"${MOTECH_ADMIN_EMAIL}\",\"adminPassword\":\"${MOTECH_ADMIN_PWD}\",\"adminConfirmPassword\":\"${MOTECH_ADMIN_PWD}\",\"language\":\"cs\",\"providerName\":\"\",\"providerUrl\":\"\",\"schedulerUrl\":\"\"}" http://127.0.0.1:9081/motech/module/server/startup/ +curl -H "Content-Type: application/json" -X POST -d "{\"adminLogin\":\"${MOTECH_ADMIN_USER}\",\"adminEmail\":\"${MOTECH_ADMIN_EMAIL}\",\"adminPassword\":\"${MOTECH_ADMIN_PWD}\",\"adminConfirmPassword\":\"${MOTECH_ADMIN_PWD}\",\"language\":\"cs\",\"providerName\":\"\",\"providerUrl\":\"\",\"schedulerUrl\":\"\"}" http://127.0.0.1:9013/motech/module/server/startup/ -# Create nginx site definition -cp ${SOURCE_DIR}/etc/nginx/apps-available/motech /etc/nginx/apps-available/motech -ln -s /etc/nginx/apps-available/motech /etc/nginx/apps-enabled/motech - -# Restart services -systemctl restart nginx +# Create nginx app definition +cp ${SOURCE_DIR}/etc/nginx/conf.d/motech.conf /etc/nginx/conf.d/motech.conf +service nginx reload # Add portal application definition -portal-app-manager motech "/motech/" "${MOTECH_ADMIN_USER}" "${MOTECH_ADMIN_PWD}" +portal-app-manager motech "https://{host}:8413/motech/" "${MOTECH_ADMIN_USER}" "${MOTECH_ADMIN_PWD}" diff --git a/motech/Dockerfile b/motech/Dockerfile new file mode 100644 index 0000000..54e5265 --- /dev/null +++ b/motech/Dockerfile @@ -0,0 +1,46 @@ +FROM alpine:3.7 +MAINTAINER Disassembler + +RUN \ + # Install Java 1.8 JRE + apk --no-cache add openjdk8-jre-base paxctl \ + # Fix grsec attributes to loosen memory protection restrictions + && paxctl -cm /usr/lib/jvm/java-1.8-openjdk/jre/bin/java \ + && paxctl -cm /usr/lib/jvm/java-1.8-openjdk/bin/java \ + # Cleanup + && apk del paxctl + +RUN \ + # Install Tomcat 8 + wget http://mirror.dkm.cz/apache/tomcat/tomcat-8/v8.0.48/bin/apache-tomcat-8.0.48.tar.gz -O /tmp/apache-tomcat-8.tgz \ + && tar xf /tmp/apache-tomcat-8.tgz -C /srv \ + && mv /srv/apache-tomcat-8.0.48 /srv/tomcat \ + # Make catalina.sh available globally + && ln -s /srv/tomcat/bin/catalina.sh /usr/bin/catalina.sh \ + # Cleanup + && rm -rf /srv/tomcat/webapps/ROOT /srv/tomcat/webapps/docs /srv/tomcat/webapps/examples /srv/tomcat/webapps/host-manager /srv/tomcat/webapps/manager \ + && rm -f /tmp/apache-tomcat-8.tgz + +RUN \ + # Download Motech + wget http://nexus.motechproject.org/service/local/repositories/releases/content/org/motechproject/motech-platform-server/1.0/motech-platform-server-1.0.war -O /tmp/motech.war \ + && mkdir /srv/tomcat/webapps/motech \ + && unzip /tmp/motech.war -d /srv/tomcat/webapps/motech \ + # Update Postgres JDBC driver + && rm -f /srv/tomcat/webapps/motech/WEB-INF/lib/postgresql-9.1-901.jdbc4.jar /srv/tomcat/webapps/motech/WEB-INF/bundles/postgresql-9.1-901.jdbc4.jar \ + && wget https://jdbc.postgresql.org/download/postgresql-42.2.0.jar -O /srv/tomcat/webapps/motech/WEB-INF/lib/postgresql-42.2.0.jar \ + && cp /srv/tomcat/webapps/motech/WEB-INF/lib/postgresql-42.2.0.jar /srv/tomcat/webapps/motech/WEB-INF/bundles/postgresql-42.2.0.jar \ + # Configure Tomcat port + && sed -i 's/port="8080"/port="8013"/g' /srv/tomcat/conf/server.xml \ + # Create OS user + && addgroup -S -g 8013 motech \ + && adduser -S -u 8013 -h /srv/tomcat -s /bin/false -g motech -G motech motech \ + && chown -R motech:motech /srv/tomcat \ + # Cleanup + && rm -f /tmp/motech.war + +VOLUME ["/srv/tomcat/.motech"] +EXPOSE 8013 + +USER motech +CMD ["catalina.sh", "run"] diff --git a/motech/tmp/motech-createdb.sql b/motech/createdb.sql similarity index 100% rename from motech/tmp/motech-createdb.sql rename to motech/createdb.sql diff --git a/motech/etc/activemq/instances-available/main/activemq.xml b/motech/etc/activemq/instances-available/main/activemq.xml deleted file mode 100644 index 32286ee..0000000 --- a/motech/etc/activemq/instances-available/main/activemq.xml +++ /dev/null @@ -1,56 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - diff --git a/motech/etc/init.d/motech b/motech/etc/init.d/motech new file mode 100644 index 0000000..cdd9023 --- /dev/null +++ b/motech/etc/init.d/motech @@ -0,0 +1,17 @@ +#!/sbin/openrc-run + +description="Motech docker container" + +depend() { + need docker net + use dns logger netmount + after activemq postgres +} + +start() { + /usr/bin/docker run -d --rm --name motech -h motech --link activemq --link postgres -p 127.0.0.1:9013:8013 -v /srv/motech/conf:/srv/tomcat/.motech motech +} + +stop() { + /usr/bin/docker stop motech +} diff --git a/motech/etc/nginx/apps-available/motech b/motech/etc/nginx/apps-available/motech deleted file mode 100644 index 6afb3a4..0000000 --- a/motech/etc/nginx/apps-available/motech +++ /dev/null @@ -1,11 +0,0 @@ -location /motech { - alias /srv/motech/webapps/motech; - try_files $uri @motech; -} - -location @motech { - proxy_set_header X-Forwarded-For $remote_addr; - proxy_set_header X-Forwarded-Host $host:$server_port; - proxy_set_header X-Forwarded-Proto https; - proxy_pass http://127.0.0.1:9081; -} diff --git a/motech/etc/nginx/conf.d/motech.conf b/motech/etc/nginx/conf.d/motech.conf new file mode 100644 index 0000000..981c515 --- /dev/null +++ b/motech/etc/nginx/conf.d/motech.conf @@ -0,0 +1,14 @@ +server { + listen [::]:8013 ipv6only=off; + listen [::]:8413 ssl http2 ipv6only=off; + + access_log /var/log/nginx/motech.access.log; + error_log /var/log/nginx/motech.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:9013; + } +} diff --git a/motech/lib/systemd/system/motech.service b/motech/lib/systemd/system/motech.service deleted file mode 100644 index f62de67..0000000 --- a/motech/lib/systemd/system/motech.service +++ /dev/null @@ -1,14 +0,0 @@ -[Unit] -Description=Motech Tomcat 8.0 -After=network.target - -[Service] -Type=forking -User=motech -WorkingDirectory=/srv/motech -ExecStartPre=/srv/motech/update-ip.sh -ExecStart=/srv/motech/bin/startup.sh -ExecStop=/srv/motech/bin/shutdown.sh - -[Install] -WantedBy=multi-user.target diff --git a/motech/srv/motech/.motech/config-locations.properties b/motech/srv/motech/.motech/config-locations.properties deleted file mode 100644 index 9871a51..0000000 --- a/motech/srv/motech/.motech/config-locations.properties +++ /dev/null @@ -1 +0,0 @@ -config.location = /srv/motech/.motech/config diff --git a/motech/srv/motech/conf/config-locations.properties b/motech/srv/motech/conf/config-locations.properties new file mode 100644 index 0000000..2399775 --- /dev/null +++ b/motech/srv/motech/conf/config-locations.properties @@ -0,0 +1 @@ +config.location = /srv/tomcat/.motech/config diff --git a/motech/srv/motech/.motech/config/bootstrap.properties b/motech/srv/motech/conf/config/bootstrap.properties similarity index 70% rename from motech/srv/motech/.motech/config/bootstrap.properties rename to motech/srv/motech/conf/config/bootstrap.properties index 8836f06..42918dd 100644 --- a/motech/srv/motech/.motech/config/bootstrap.properties +++ b/motech/srv/motech/conf/config/bootstrap.properties @@ -1,19 +1,19 @@ jms.cache.producers=false jms.queue.for.scheduler=QueueForScheduler -sql.url=jdbc\:postgresql\://localhost\:5432/ +sql.url=jdbc\:postgresql\://postgres\:5432/ jms.maxConcurrentConsumers=10 sql.user=motech jms.queue.for.events=QueueForEvents jms.password= jms.session.cache.size=10 -org.osgi.framework.storage=/srv/motech/.motech/felix-cache +org.osgi.framework.storage=/srv/tomcat/.motech/felix-cache motech.message.redelivery.delay=1 jms.username= config.source=FILE -motech.dir=/srv/motech/.motech +motech.dir=/srv/tomcat/.motech sql.password=${MOTECH_PWD} sql.driver=org.postgresql.Driver jms.concurrentConsumers=1 -jms.broker.url=tcp\://localhost\:61616 +jms.broker.url=tcp\://activemq\:61616 motech.message.max.redelivery.count=3 jms.topic.for.events=TopicForEvents diff --git a/motech/srv/motech/.motech/config/motech-settings.properties b/motech/srv/motech/conf/config/motech-settings.properties similarity index 100% rename from motech/srv/motech/.motech/config/motech-settings.properties rename to motech/srv/motech/conf/config/motech-settings.properties diff --git a/motech/srv/motech/.motech/config/org.motechproject.motech-platform-email/motech-email.properties b/motech/srv/motech/conf/config/org.motechproject.motech-platform-email/motech-email.properties similarity index 100% rename from motech/srv/motech/.motech/config/org.motechproject.motech-platform-email/motech-email.properties rename to motech/srv/motech/conf/config/org.motechproject.motech-platform-email/motech-email.properties diff --git a/motech/srv/motech/conf/server.xml b/motech/srv/motech/conf/server.xml deleted file mode 100644 index 50d2a3a..0000000 --- a/motech/srv/motech/conf/server.xml +++ /dev/null @@ -1,142 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/motech/srv/motech/update-ip.sh b/motech/srv/motech/update-ip.sh index c0a2399..5588ab9 100755 --- a/motech/srv/motech/update-ip.sh +++ b/motech/srv/motech/update-ip.sh @@ -1,4 +1,4 @@ -#!/bin/sh - -URL=$(ip route get 1 | awk '{print $NF;exit}') -sed -i "s|^server\.url.*|server.url=https://${URL}/motech|" /srv/motech/.motech/config/motech-settings.properties +#!/bin/sh + +URL=$(ip route get 1 | awk '{print $NF;exit}') +sed -i "s|^server\.url.*|server.url=https://${URL}:8413/motech|" /srv/motech/conf/config/motech-settings.properties