From d5886aa7ae7adb9c36e8548a9b70738da70765e0 Mon Sep 17 00:00:00 2001
From: Eike Cochu <eike@cochu.com>
Date: Sat, 9 Apr 2016 02:21:17 +0200
Subject: [PATCH] updated docker config, fixed saving topic model to file on
 restore

---
 README.md                                     | 13 +++---
 build-config.sh                               | 10 +++++
 build.sh                                      | 29 +------------
 docker/Dockerfile                             | 12 +++---
 docker/docker-build.sh                        |  9 ----
 docker/docker-run.sh                          |  5 ++-
 docker/supervisord.conf                       |  7 +++-
 docker/webapps/.gitkeep                       |  0
 docker/webroot/.gitkeep                       |  0
 vipra-backend/src/main/webapp/WEB-INF/web.xml |  3 --
 vipra-cmd/runcfg/CMD.launch                   |  2 +-
 .../de/vipra/cmd/option/EditModelCommand.java | 13 +++++-
 vipra-ui/README.md                            | 41 ++++++++++++++++++-
 vipra-ui/app/index.html                       |  1 +
 vipra-ui/app/js/config.js                     | 11 ++++-
 vipra-ui/gulpfile.js                          | 10 +++--
 .../de/vipra/util/model/TopicModelConfig.java |  7 ++--
 17 files changed, 109 insertions(+), 64 deletions(-)
 create mode 100755 build-config.sh
 create mode 100644 docker/webapps/.gitkeep
 create mode 100644 docker/webroot/.gitkeep

diff --git a/README.md b/README.md
index a0ea5370..32e3e6a7 100644
--- a/README.md
+++ b/README.md
@@ -13,11 +13,14 @@ This application was created by Eike Cochu for his master's degree thesis in com
 
 ## Installation
 
-1. If MongoDB or ElasticSearch run on different servers that the JavaEE application server, then the configuration files need to be changed. Change `config.properties` file
-  1. in `vipra.war`: Open `vipra.war` in an archive program, navigate to `/WEB-INF/classes` and edit `config.properties` file appropriately.
-  2. in `vipra-cmd.jar`: Open `vipra-cmd.jar` and edit `config.properties` file appropriately.
-  3. Test connection by running `./vipra -t`
-2. Copy `vipra.war` to your JavaEE application server
+1. Deploy vipra.war or exploded vipra directory to a Java application server. Deploy to ROOT (/) or a different context path (/vipra, ...)
+2. Edit WEB-INF/classes/config.json if MongoDB or ElasticSearch do not run with default configuration (host/port)
+3. Deploy vipra-ui to a static webserver 
+4. Create rewrite rules to rewrite all non-matching files to /index.html
+5. If not deployed in root, change /index.html <base> tag to context path
+6. Change /js/config.js if the backend was not deployed on the same server, not in root context or if the server has a different port than 8080
+
+if everything is left default, application should be available under: http://someserver/ and backend: http://someserver:8080/rest/
 
 ## Development
 
diff --git a/build-config.sh b/build-config.sh
new file mode 100755
index 00000000..447cfd85
--- /dev/null
+++ b/build-config.sh
@@ -0,0 +1,10 @@
+#!/bin/bash
+
+INCLUDE_FRONTEND_IN_WAR=1
+
+# enable/disable (1/0) project building
+BUILD_DTM=1
+BUILD_VIPRA_UTIL=1
+BUILD_VIPRA_CMD=1
+BUILD_VIPRA_UI=1
+BUILD_VIPRA_BACKEND=1
\ No newline at end of file
diff --git a/build.sh b/build.sh
index 110b7c5f..7bf4ddf4 100755
--- a/build.sh
+++ b/build.sh
@@ -1,18 +1,6 @@
-#!/bin/sh
+#!/bin/bash
 
-# point this folder to the webapps dir of your tomcat server for auto deploy
-TOMCAT_WEBAPPS="vm/webapps"
-
-# enable/disable (1/0) project building
-BUILD_DTM=1
-BUILD_VIPRA_UTIL=1
-BUILD_VIPRA_CMD=1
-BUILD_VIPRA_UI=1
-BUILD_VIPRA_BACKEND=1
-
-# enable to deploy frontend assets in vipra.war
-# disable if you want to serve frontend assets from another server
-INCLUDE_FRONTEND_IN_WAR=1
+source ./build-config.sh
 
 # =========================================================
 # DO NOT EDIT AFTER THIS POINT
@@ -139,18 +127,5 @@ else
         fi
 fi
 
-# deploy backend war
-
-VIPRA_WAR="vipra-backend/target/vipra.war"
-
-echo "" >> $LOG
-echo "-------------------------------" >> $LOG
-if [ -d $TOMCAT_WEBAPPS ] && [ -f $VIPRA_WAR ]; then
-        echo "deploying vipra-backend" | tee -a $LOG
-        cp $VIPRA_WAR $TOMCAT_WEBAPPS
-else
-        echo "deploying vipra-backend > skipped" | tee -a $LOG
-fi
-
 echo "complete"
 exit $?
diff --git a/docker/Dockerfile b/docker/Dockerfile
index b9c0fbfa..cd360765 100644
--- a/docker/Dockerfile
+++ b/docker/Dockerfile
@@ -3,11 +3,13 @@ MAINTAINER Eike Cochu <eike@cochu.com>
 ENV ES_VERSION 2.3.1
 ENV TOMCAT_MAJOR 8
 ENV TOMCAT_VERSION 8.0.33
-RUN apt-get update && \
-    apt-get install -y wget nano openjdk-8-jdk mongodb supervisor && \
+RUN echo "deb http://ppa.launchpad.net/nginx/stable/ubuntu wily main" >> /etc/apt/sources.list && \
+		apt-key adv --keyserver keyserver.ubuntu.com --recv-keys C300EE8C && \
+		apt-get update && \
+    apt-get install -y --no-install-recommends wget nano openjdk-8-jdk mongodb supervisor nginx && \
     apt-get clean && \
     rm -rf /var/lib/apt/lists/* && \
-    mkdir -p /var/log/supervisor /data/db && \
+    mkdir -p /webroot /var/log/supervisor /data/db && \
     wget --no-check-certificate -O- https://download.elastic.co/elasticsearch/release/org/elasticsearch/distribution/tar/elasticsearch/$ES_VERSION/elasticsearch-$ES_VERSION.tar.gz | tar xvfz - && \
     mv elasticsearch-$ES_VERSION elasticsearch && \
     wget -O- http://apache.openmirror.de/tomcat/tomcat-$TOMCAT_MAJOR/v$TOMCAT_VERSION/bin/apache-tomcat-$TOMCAT_VERSION.tar.gz | tar xvfz - && \
@@ -15,6 +17,6 @@ RUN apt-get update && \
 COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
 COPY tomcat-users.xml /tomcat/conf/tomcat-users.xml
 COPY elasticsearch.yml /elasticsearch/config/elasticsearch.yml
-COPY vipra.war /tomcat/webapps/vipra.war
-EXPOSE 8080 9200 9300 27017
+COPY nginx.conf /etc/nginx/nginx.conf
+EXPOSE 80 8080 9200 9300 27017
 CMD ["/usr/bin/supervisord"]
\ No newline at end of file
diff --git a/docker/docker-build.sh b/docker/docker-build.sh
index fd1787c8..6b37c7c1 100755
--- a/docker/docker-build.sh
+++ b/docker/docker-build.sh
@@ -1,13 +1,4 @@
 #!/bin/sh
 
-VIPRA_WAR="../vipra-backend/target/vipra.war"
-
-if [ ! -f $VIPRA_WAR ]; then
-	echo "vipra.war not found. Did you run build.sh?"
-	exit 1
-fi
-
-cp $VIPRA_WAR .
 docker build -t eikecochu/vipra .
-rm vipra.war
 exit 0
\ No newline at end of file
diff --git a/docker/docker-run.sh b/docker/docker-run.sh
index d7680269..d241c725 100755
--- a/docker/docker-run.sh
+++ b/docker/docker-run.sh
@@ -1,8 +1,11 @@
 #!/bin/sh
 
+HOST_NGINX=80
 HOST_TOMCAT=8080
 HOST_ELASTICSEARCH_REST=9200
 HOST_ELASTICSEARCH_API=9300
 HOST_MONGODB=27017
 
-docker run -d -p $HOST_TOMCAT:8080 -p $HOST_ELASTICSEARCH_REST:9200 -p $HOST_ELASTICSEARCH_API:9300 -p $HOST_MONGODB:27017 eikecochu/vipra
\ No newline at end of file
+DIR="$(dirname "$(readlink -f "$0")")"
+
+docker run -d -p $HOST_NGINX:80 -p $HOST_TOMCAT:8080 -p $HOST_ELASTICSEARCH_REST:9200 -p $HOST_ELASTICSEARCH_API:9300 -p $HOST_MONGODB:27017 -v $DIR/webroot:/webroot -v $DIR/webapps:/tomcat/webapps eikecochu/vipra
\ No newline at end of file
diff --git a/docker/supervisord.conf b/docker/supervisord.conf
index d2cc713b..f46a37ab 100644
--- a/docker/supervisord.conf
+++ b/docker/supervisord.conf
@@ -16,4 +16,9 @@ environment=CATALINA_HOME="/tomcat",CATALINA_BASE="/tomcat",CATALINA_TMPDIR="/to
 command=/tomcat/bin/catalina.sh run
 autostart=true
 autorestart=true
-startsecs=10
\ No newline at end of file
+startsecs=10
+
+[program:nginx]
+command=/usr/sbin/nginx
+autostart=true
+autorestart=true
\ No newline at end of file
diff --git a/docker/webapps/.gitkeep b/docker/webapps/.gitkeep
new file mode 100644
index 00000000..e69de29b
diff --git a/docker/webroot/.gitkeep b/docker/webroot/.gitkeep
new file mode 100644
index 00000000..e69de29b
diff --git a/vipra-backend/src/main/webapp/WEB-INF/web.xml b/vipra-backend/src/main/webapp/WEB-INF/web.xml
index 90ba036b..da89b35d 100644
--- a/vipra-backend/src/main/webapp/WEB-INF/web.xml
+++ b/vipra-backend/src/main/webapp/WEB-INF/web.xml
@@ -3,9 +3,6 @@
 	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
 	version="3.1">
-	<welcome-file-list>
-		<welcome-file>public/index.html</welcome-file>
-	</welcome-file-list>
 	<servlet>
 		<servlet-name>jersey</servlet-name>
 		<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
diff --git a/vipra-cmd/runcfg/CMD.launch b/vipra-cmd/runcfg/CMD.launch
index 234953e3..b86e653f 100644
--- a/vipra-cmd/runcfg/CMD.launch
+++ b/vipra-cmd/runcfg/CMD.launch
@@ -11,7 +11,7 @@
 </listAttribute>
 <stringAttribute key="org.eclipse.jdt.launching.CLASSPATH_PROVIDER" value="org.eclipse.m2e.launchconfig.classpathProvider"/>
 <stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="de.vipra.cmd.Main"/>
-<stringAttribute key="org.eclipse.jdt.launching.PROGRAM_ARGUMENTS" value="-S test -M"/>
+<stringAttribute key="org.eclipse.jdt.launching.PROGRAM_ARGUMENTS" value="-C yearly"/>
 <stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="vipra-cmd"/>
 <stringAttribute key="org.eclipse.jdt.launching.SOURCE_PATH_PROVIDER" value="org.eclipse.m2e.launchconfig.sourcepathProvider"/>
 <stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-ea"/>
diff --git a/vipra-cmd/src/main/java/de/vipra/cmd/option/EditModelCommand.java b/vipra-cmd/src/main/java/de/vipra/cmd/option/EditModelCommand.java
index 44552463..11c2c87e 100644
--- a/vipra-cmd/src/main/java/de/vipra/cmd/option/EditModelCommand.java
+++ b/vipra-cmd/src/main/java/de/vipra/cmd/option/EditModelCommand.java
@@ -1,9 +1,15 @@
 package de.vipra.cmd.option;
 
+import java.io.IOException;
+
+import com.fasterxml.jackson.core.JsonGenerationException;
+import com.fasterxml.jackson.databind.JsonMappingException;
+
 import de.vipra.util.Config;
 import de.vipra.util.ConsoleUtils;
 import de.vipra.util.Constants.ProcessorMode;
 import de.vipra.util.Constants.WindowResolution;
+import de.vipra.util.ex.ConfigException;
 import de.vipra.util.ex.DatabaseException;
 import de.vipra.util.model.TopicModelConfig;
 import de.vipra.util.model.TopicModelFull;
@@ -12,13 +18,15 @@ import de.vipra.util.service.MongoService;
 public class EditModelCommand implements Command {
 
 	private final String[] names;
+	private Config config;
 	private MongoService<TopicModelFull, String> dbTopicModels;
 
 	public EditModelCommand(final String[] names) {
 		this.names = names;
 	}
 
-	private void editModel(final TopicModelConfig topicModelConfig) throws DatabaseException {
+	private void editModel(final TopicModelConfig topicModelConfig)
+			throws DatabaseException, JsonGenerationException, JsonMappingException, IOException, ConfigException {
 		ConsoleUtils.info("editing model: " + topicModelConfig.getName());
 		topicModelConfig.setDescription(ConsoleUtils.readString("description (↲ to skip)", topicModelConfig.getDescription(), true));
 		topicModelConfig.setkTopics(ConsoleUtils.readInt("k topics", topicModelConfig.getkTopics(), 1, null, true));
@@ -50,11 +58,12 @@ public class EditModelCommand implements Command {
 		final TopicModelFull topicModel = new TopicModelFull(topicModelConfig.getName());
 		topicModel.setModelConfig(topicModelConfig);
 		dbTopicModels.updateSingle(topicModel, "modelConfig");
+		topicModelConfig.saveToFile(topicModelConfig.getModelDir(config.getDataDirectory()));
 	}
 
 	@Override
 	public void run() throws Exception {
-		final Config config = Config.getConfig();
+		config = Config.getConfig();
 		dbTopicModels = MongoService.getDatabaseService(config, TopicModelFull.class);
 
 		for (final String name : names) {
diff --git a/vipra-ui/README.md b/vipra-ui/README.md
index 762d63e3..333bb4de 100644
--- a/vipra-ui/README.md
+++ b/vipra-ui/README.md
@@ -1,3 +1,42 @@
 # Vipra UI
 
-The frontend UI project.
\ No newline at end of file
+The frontend UI project. Built with AngularJS and many other Javascript and CSS frameworks and libraries:
+
+* [AngularJS](https://github.com/angular/angular.js) by Google (MIT)
+* [Angular Hotkeys](https://github.com/chieffancypants/angular-hotkeys) by Wes Cruver (MIT)
+* [AngularUI Router](https://github.com/angular-ui/ui-router) by The AngularUI Team, Karsten Sperling (MIT)
+* [jQuery](https://github.com/jquery/jquery) by the jQuery Foundation and other contributors (MIT)
+* [Bootstrap](https://github.com/twbs/bootstrap) by Twitter (MIT)
+* [bootstrap-datetimepicker](https://github.com/Eonasdan/bootstrap-datetimepicker) by Jonathan Peterson (MIT)
+* [nya-bootstrap-select](https://github.com/lordfriend/nya-bootstrap-select) by Nyasoft (MIT)
+* [Bootbox.js](https://github.com/makeusabrew/bootbox) by Nick Payne (MIT)
+* [Highcharts](http://www.highcharts.com/) by Highsoft (CC-BY-NC)
+* [vis.js](https://github.com/almende/vis) by Almende B.V. (MIT)
+* [Moment.js](https://github.com/moment/moment) by Tim Wood, Iskren Chernev, Moment.js contributors (MIT)
+* [randomColor](https://github.com/davidmerfield/randomColor) by David Merfield (CC0 1.0)
+* [FontAwesome](https://github.com/FortAwesome/Font-Awesome) by Dave Gandy (SIL OFL 1.1/MIT)
+* [Bower](https://github.com/bower/bower) by Twitter and other contributors (MIT)
+* [Gulp](https://github.com/gulpjs/gulp) by Fractal (MIT)
+* [gulp-less](https://github.com/plus3network/gulp-less) by Plus 3 Network (MIT)
+* [gulp-concat](https://github.com/contra/gulp-concat) by Fractal (MIT)
+* [gulp-uglify](https://github.com/terinjokes/gulp-uglify) by Terin Stock (MIT)
+* [gulp-clean-css](https://github.com/scniro/gulp-clean-css) by scniro (MIT)
+* [gulp-webserver](https://github.com/schickling/gulp-webserver) by Johannes Schickling (MIT)
+* [gulp-ng-annotate](https://github.com/Kagami/gulp-ng-annotate) by Kagami Hiiragi and contributors (CC0 1.0)
+* [gulp-templatecache](https://github.com/miickel/gulp-angular-templatecache) by Mickel (MIT)
+
+## Installation
+
+### Base path
+
+Simply put all files into a static web server document root.
+
+If the application is not deployed into a web root (/), change the base tag in the file `index.html`
+```html
+<base href="/somepath" />
+```
+the href value has to be the same under which the application should be available, e.g. http://someserver/somepath/index.html.
+
+### Backend connection
+
+The backend is expected to be deployed to a Java application server. To change the default REST URL, edit the file `js/config.js`
\ No newline at end of file
diff --git a/vipra-ui/app/index.html b/vipra-ui/app/index.html
index 925f65b3..caafc504 100644
--- a/vipra-ui/app/index.html
+++ b/vipra-ui/app/index.html
@@ -29,6 +29,7 @@
   <link href="css/app.css" rel="stylesheet">
   <!-- javascript -->
   <script src="js/vendor.js"></script>
+  <script src="js/config.js"></script>
   <script src="js/app.js"></script>
   <script src="js/templates.js"></script>
 </head>
diff --git a/vipra-ui/app/js/config.js b/vipra-ui/app/js/config.js
index f61fc16e..e3005dd5 100644
--- a/vipra-ui/app/js/config.js
+++ b/vipra-ui/app/js/config.js
@@ -10,7 +10,16 @@
   window.Vipra = window.Vipra || {};
 
   Vipra.config = {
-    restUrl: '/vipra/rest'
+	  /*
+	   * Point this URL to the backend REST servlet. The default is /rest, which expects the servlet
+	   * to be deployed in the root context. If the servlet is deployed under another context, prepend
+	   * it to this URL. Test the URL by entering this into a browser:
+	   *
+	   *    http://yourserver[/rest]/application.wadl
+	   *                      ^^^^^ 
+	   *                      this is the restUrl
+	   */
+    restUrl: '//' + location.hostname + ':8080/rest'
   };
 
 })();
\ No newline at end of file
diff --git a/vipra-ui/gulpfile.js b/vipra-ui/gulpfile.js
index 1ab3e2c9..9f8e6da5 100644
--- a/vipra-ui/gulpfile.js
+++ b/vipra-ui/gulpfile.js
@@ -52,18 +52,20 @@ gulp.task('less', function() {
 });
 
 gulp.task('js', function() {
-  gulp.src('app/js/**/*.js')
+  gulp.src(['app/js/**/*.js', '!app/js/config.js'])
     .pipe(concat('app.js'))
     .pipe(ngannotate())
-    //.pipe(uglify())
+    .pipe(gulp.dest('public/js'));
+  gulp.src('app/js/config.js')
     .pipe(gulp.dest('public/js'));
 });
 
 gulp.task('js-rel', function() {
-  gulp.src(['app/js/**/*.js', '!app/js/dev.js'])
+  gulp.src(['app/js/**/*.js', '!app/js/config.js', '!app/js/dev.js'])
     .pipe(concat('app.js'))
     .pipe(ngannotate())
-    //.pipe(uglify())
+    .pipe(gulp.dest('public/js'));
+  gulp.src('app/js/config.js')
     .pipe(gulp.dest('public/js'));
 });
 
diff --git a/vipra-util/src/main/java/de/vipra/util/model/TopicModelConfig.java b/vipra-util/src/main/java/de/vipra/util/model/TopicModelConfig.java
index 1f116b4f..7e842401 100644
--- a/vipra-util/src/main/java/de/vipra/util/model/TopicModelConfig.java
+++ b/vipra-util/src/main/java/de/vipra/util/model/TopicModelConfig.java
@@ -1,7 +1,6 @@
 package de.vipra.util.model;
 
 import java.io.File;
-import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.Serializable;
 
@@ -231,9 +230,9 @@ public class TopicModelConfig implements Serializable {
 
 	public static TopicModelConfig readFromFile(final File modelDir) throws JsonParseException, JsonMappingException, IOException {
 		final File file = new File(modelDir, FILE_NAME);
-		if (!file.exists())
-			throw new FileNotFoundException(file.getAbsolutePath());
-		return Config.mapper.readValue(file, TopicModelConfig.class);
+		if (file.exists())
+			return Config.mapper.readValue(file, TopicModelConfig.class);
+		return new TopicModelConfig();
 	}
 
 	@Override
-- 
GitLab