From 30447f818f6a6b2860935659318c13b3569fea7c Mon Sep 17 00:00:00 2001
From: Eike Cochu <eike@cochu.com>
Date: Thu, 10 Dec 2015 13:05:08 +0100
Subject: [PATCH] added tomcat to vm

---
 .gitignore                  |   1 +
 Vagrantfile                 |   1 +
 tmbs-frontend               |   1 +
 vm/bootstrap.sh             |  48 +++++++++---
 vm/config/environment       |   2 +-
 vm/config/initd-tomcat      |  26 +++++++
 vm/config/tomcat-server.xml | 142 ++++++++++++++++++++++++++++++++++++
 vm/config/tomcat-users.xml  |  39 ++++++++++
 8 files changed, 248 insertions(+), 12 deletions(-)
 create mode 160000 tmbs-frontend
 create mode 100644 vm/config/initd-tomcat
 create mode 100644 vm/config/tomcat-server.xml
 create mode 100644 vm/config/tomcat-users.xml

diff --git a/.gitignore b/.gitignore
index a977916f..f7500c5f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
 .vagrant/
+vm/webapps/
\ No newline at end of file
diff --git a/Vagrantfile b/Vagrantfile
index 76d5bead..807500ff 100644
--- a/Vagrantfile
+++ b/Vagrantfile
@@ -15,6 +15,7 @@ Vagrant.configure(2) do |config|
     master.vm.network :forwarded_port, guest: 8001,  host: 8001  # Spark MasterUI
     master.vm.network :forwarded_port, guest: 8002,  host: 8002  # Spark WorkerUI
     master.vm.network :forwarded_port, guest: 7077,  host: 7077  # Spark
+    master.vm.network :forwarded_port, guest: 8080,  host: 8000  # Tomcat
     master.vm.network :forwarded_port, guest: 9200,  host: 9200  # ElasticSearch REST API
     master.vm.provision :shell, path: 'vm/bootstrap.sh'
   end
diff --git a/tmbs-frontend b/tmbs-frontend
new file mode 160000
index 00000000..9030211f
--- /dev/null
+++ b/tmbs-frontend
@@ -0,0 +1 @@
+Subproject commit 9030211fb41c825b175a45d77131b669f2acc963
diff --git a/vm/bootstrap.sh b/vm/bootstrap.sh
index fb631ebc..138de264 100644
--- a/vm/bootstrap.sh
+++ b/vm/bootstrap.sh
@@ -1,5 +1,9 @@
 #!/bin/sh
 
+VMDIR=/vagrant/vm
+DATA=$VMDIR/data
+CONFIG=$VMDIR/config
+
 # -----------------------------------------------------------------------------
 # add repos
 apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
@@ -26,17 +30,17 @@ wget http://mirror.netcologne.de/apache.org/spark/spark-1.5.2/spark-1.5.2-bin-ha
 tar zxf spark-1.5.2-bin-hadoop2.6.tgz
 rm spark-1.5.2-bin-hadoop2.6.tgz
 mv spark-1.5.2-bin-hadoop2.6 spark
-ln -sf /vagrant/vm/config/spark-env.sh /home/vagrant/spark/conf/spark-env.sh
+ln -sf $CONFIG/spark-env.sh /home/vagrant/spark/conf/spark-env.sh
 
 # -----------------------------------------------------------------------------
 # install mongodb
 apt-get install -y mongodb-org
 
 # copy configuration
-ln -sf /vagrant/vm/config/master-mongod.conf /etc/mongod.conf
+ln -sf $CONFIG/master-mongod.conf /etc/mongod.conf
 
 # disable hugepages (https://docs.mongodb.org/manual/tutorial/transparent-huge-pages/)
-cp /vagrant/vm/config/disable-transparent-hugepages /etc/init.d/
+cp $CONFIG/disable-transparent-hugepages /etc/init.d/
 chmod 755 /etc/init.d/disable-transparent-hugepages
 update-rc.d disable-transparent-hugepages defaults
 echo 'never' > /sys/kernel/mm/transparent_hugepage/enabled 
@@ -46,14 +50,37 @@ echo 'never' > /sys/kernel/mm/transparent_hugepage/defrag
 service mongod restart
 
 # import mongodb data
-mongoimport --db test --collection articles --file /vagrant/vm/data/data.json --jsonArray
+mongoimport --db test --collection articles --file $DATA/data.json --jsonArray
 
 # -----------------------------------------------------------------------------
 # install mongo express
 apt-get install npm libkrb5-dev
 ln -sf $(which nodejs) /bin/node
 npm install -g mongo-express forever
-ln -sf /vagrant/vm/config/mongo-express.config.js /usr/local/lib/node_modules/mongo-express/config.js
+ln -sf $CONFIG/mongo-express.config.js /usr/local/lib/node_modules/mongo-express/config.js
+
+# -----------------------------------------------------------------------------
+# install tomcat
+wget http://mirror.netcologne.de/apache.org/tomcat/tomcat-8/v8.0.30/bin/apache-tomcat-8.0.30.tar.gz
+tar zxf apache-tomcat-8.0.30.tar.gz
+mv apache-tomcat-8.0.30 /usr/share/tomcat8
+ln -s /usr/share/tomcat /usr/share/tomcat8
+# setup server config
+rm /usr/share/tomcat/conf/server.xml /usr/share/tomcat/conf/tomcat-users.xml
+ln -s $CONFIG/tomcat-server.xml /usr/share/tomcat/conf/server.xml
+ln -s $CONFIG/tomcat-users.xml /usr/share/tomcat/conf/tomcat-users.xml
+# set permissions
+chown -R vagrant:vagrant /usr/share/tomcat8
+chmod +x /usr/share/tomcat/bin/*.sh
+# setup startup script
+cp $CONFIG/initd-tomcat /etc/init.d/tomcat
+chmod 755 /etc/init.d/tomcat
+update-rc.d tomcat defaults
+# move webapps dir
+mv /usr/share/tomcat/webapps $VMDIR/webapps
+ln -s $VMDIR/webapps /usr/share/tomcat/webapps
+# start tomcat
+service tomcat start
 
 # -----------------------------------------------------------------------------
 # install maven
@@ -62,20 +89,19 @@ tar zxf apache-maven-3.3.9-bin.tar.gz
 mv apache-maven-3.3.9-bin maven
 rm apache-maven-3.3.9-bin.tar.gz
 
-# -----------------------------------------------------------------------------
-# set environment
-cat /vagrant/vm/config/environment >> /etc/environment
-
 # -----------------------------------------------------------------------------
 # install mahout
 git clone https://github.com/apache/mahout.git mahout
-cd mahout
-mvn -DskipTests -X clean install
+mvn -DskipTests -X clean -f ./mahout install
 
 # -----------------------------------------------------------------------------
 # disable firewall
 ufw disable
 
+# -----------------------------------------------------------------------------
+# set environment
+cat $CONFIG/environment >> /etc/environment
+
 # -----------------------------------------------------------------------------
 # cleanup
 apt-get autoremove -y
diff --git a/vm/config/environment b/vm/config/environment
index 46bfb91d..4a081191 100644
--- a/vm/config/environment
+++ b/vm/config/environment
@@ -1,5 +1,5 @@
 # Java
-JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
+JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64/jre
 JAVA_TOOL_OPTIONS="-Xmx2048m -XX:MaxPermSize=1024m -Xms1024m"
 
 # Spark
diff --git a/vm/config/initd-tomcat b/vm/config/initd-tomcat
new file mode 100644
index 00000000..35facc43
--- /dev/null
+++ b/vm/config/initd-tomcat
@@ -0,0 +1,26 @@
+#!/bin/bash
+
+### BEGIN INIT INFO
+# Provides:        tomcat
+# Required-Start:  $network
+# Required-Stop:   $network
+# Default-Start:   2 3 4 5
+# Default-Stop:    0 1 6
+# Short-Description: Start/Stop Tomcat server
+### END INIT INFO
+
+PATH=/sbin:/bin:/usr/sbin:/usr/bin
+
+start() {
+ /bin/su - tomcat -c /usr/share/tomcat/bin/startup.sh
+}
+
+stop() {
+ /bin/su - tomcat -c /usr/share/tomcat/bin/shutdown.sh 
+}
+
+case $1 in
+  start|stop) $1;;
+  restart) stop; start;;
+  *) echo "Run as $0 <start|stop|restart>"; exit 1;;
+esac
\ No newline at end of file
diff --git a/vm/config/tomcat-server.xml b/vm/config/tomcat-server.xml
new file mode 100644
index 00000000..eb7dad74
--- /dev/null
+++ b/vm/config/tomcat-server.xml
@@ -0,0 +1,142 @@
+<?xml version='1.0' encoding='utf-8'?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You under the Apache License, Version 2.0
+  (the "License"); you may not use this file except in compliance with
+  the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+-->
+<!-- Note:  A "Server" is not itself a "Container", so you may not
+     define subcomponents such as "Valves" at this level.
+     Documentation at /docs/config/server.html
+ -->
+<Server port="8005" shutdown="SHUTDOWN">
+  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
+  <!-- Security listener. Documentation at /docs/config/listeners.html
+  <Listener className="org.apache.catalina.security.SecurityListener" />
+  -->
+  <!--APR library loader. Documentation at /docs/apr.html -->
+  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
+  <!-- Prevent memory leaks due to use of particular java/javax APIs-->
+  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
+  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
+  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
+
+  <!-- Global JNDI resources
+       Documentation at /docs/jndi-resources-howto.html
+  -->
+  <GlobalNamingResources>
+    <!-- Editable user database that can also be used by
+         UserDatabaseRealm to authenticate users
+    -->
+    <Resource name="UserDatabase" auth="Container"
+              type="org.apache.catalina.UserDatabase"
+              description="User database that can be updated and saved"
+              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
+              pathname="conf/tomcat-users.xml" />
+  </GlobalNamingResources>
+
+  <!-- A "Service" is a collection of one or more "Connectors" that share
+       a single "Container" Note:  A "Service" is not itself a "Container",
+       so you may not define subcomponents such as "Valves" at this level.
+       Documentation at /docs/config/service.html
+   -->
+  <Service name="Catalina">
+
+    <!--The connectors can use a shared executor, you can define one or more named thread pools-->
+    <!--
+    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
+        maxThreads="150" minSpareThreads="4"/>
+    -->
+
+
+    <!-- A "Connector" represents an endpoint by which requests are received
+         and responses are returned. Documentation at :
+         Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
+         Java AJP  Connector: /docs/config/ajp.html
+         APR (HTTP/AJP) Connector: /docs/apr.html
+         Define a non-SSL/TLS HTTP/1.1 Connector on port 8080
+    -->
+    <Connector port="8080" protocol="HTTP/1.1"
+               connectionTimeout="20000"
+               redirectPort="8443" />
+    <!-- A "Connector" using the shared thread pool-->
+    <!--
+    <Connector executor="tomcatThreadPool"
+               port="8080" protocol="HTTP/1.1"
+               connectionTimeout="20000"
+               redirectPort="8443" />
+    -->
+    <!-- Define a SSL/TLS HTTP/1.1 Connector on port 8443
+         This connector uses the NIO implementation that requires the JSSE
+         style configuration. When using the APR/native implementation, the
+         OpenSSL style configuration is required as described in the APR/native
+         documentation -->
+    <!--
+    <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
+               maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
+               clientAuth="false" sslProtocol="TLS" />
+    -->
+
+    <!-- Define an AJP 1.3 Connector on port 8009 -->
+    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
+
+
+    <!-- An Engine represents the entry point (within Catalina) that processes
+         every request.  The Engine implementation for Tomcat stand alone
+         analyzes the HTTP headers included with the request, and passes them
+         on to the appropriate Host (virtual host).
+         Documentation at /docs/config/engine.html -->
+
+    <!-- You should set jvmRoute to support load-balancing via AJP ie :
+    <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
+    -->
+    <Engine name="Catalina" defaultHost="localhost">
+
+      <!--For clustering, please take a look at documentation at:
+          /docs/cluster-howto.html  (simple how to)
+          /docs/config/cluster.html (reference documentation) -->
+      <!--
+      <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
+      -->
+
+      <!-- Use the LockOutRealm to prevent attempts to guess user passwords
+           via a brute-force attack -->
+      <Realm className="org.apache.catalina.realm.LockOutRealm">
+        <!-- This Realm uses the UserDatabase configured in the global JNDI
+             resources under the key "UserDatabase".  Any edits
+             that are performed against this UserDatabase are immediately
+             available for use by the Realm.  -->
+        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
+               resourceName="UserDatabase"/>
+      </Realm>
+
+      <Host name="localhost"  appBase="webapps"
+            unpackWARs="true" autoDeploy="true">
+
+        <!-- SingleSignOn valve, share authentication between web applications
+             Documentation at: /docs/config/valve.html -->
+        <!--
+        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
+        -->
+
+        <!-- Access log processes all example.
+             Documentation at: /docs/config/valve.html
+             Note: The pattern used is equivalent to using pattern="common" -->
+        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
+               prefix="localhost_access_log" suffix=".txt"
+               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
+
+      </Host>
+    </Engine>
+  </Service>
+</Server>
diff --git a/vm/config/tomcat-users.xml b/vm/config/tomcat-users.xml
new file mode 100644
index 00000000..4dc60e63
--- /dev/null
+++ b/vm/config/tomcat-users.xml
@@ -0,0 +1,39 @@
+<?xml version='1.0' encoding='utf-8'?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You under the Apache License, Version 2.0
+  (the "License"); you may not use this file except in compliance with
+  the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+-->
+<tomcat-users xmlns="http://tomcat.apache.org/xml"
+              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+              xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"
+              version="1.0">
+<!--
+  NOTE:  By default, no user is included in the "manager-gui" role required
+  to operate the "/manager/html" web application.  If you wish to use this app,
+  you must define such a user - the username and password are arbitrary.
+-->
+<!--
+  NOTE:  The sample user and role entries below are wrapped in a comment
+  and thus are ignored when reading this file. Do not forget to remove
+  <!.. ..> that surrounds them.
+-->
+<!--
+  <role rolename="tomcat"/>
+  <role rolename="role1"/>
+  <user username="tomcat" password="tomcat" roles="tomcat"/>
+  <user username="both" password="tomcat" roles="tomcat,role1"/>
+  <user username="role1" password="tomcat" roles="role1"/>
+-->
+</tomcat-users>
-- 
GitLab