From b2d10a6dc49295406184973a3efb74c476f14545 Mon Sep 17 00:00:00 2001
From: Atte <attecscfi@gmail.com>
Date: Thu, 8 Nov 2018 18:08:42 +0000
Subject: [PATCH] Added a Dockerfile to create a container for running the
 webserver cgi-scripts for VR conversions

---
 Dockerfile                                 |   4 -
 README.md                                  |  10 +
 containerization/Dockerfile                |  19 ++
 containerization/README.md                 |  13 ++
 webserver/cgi-bin/NOMAD/configuredmaterial |   0
 webserver/cgi-bin/NOMAD/configurematerial  |   0
 webserver/cgi-bin/NOMAD/material           | 208 ++++++++++-----------
 webserver/cgi-bin/NOMAD/materialAnalytics  |   0
 webserver/pass                             |   2 +-
 9 files changed, 147 insertions(+), 109 deletions(-)
 delete mode 100644 Dockerfile
 create mode 100644 containerization/Dockerfile
 create mode 100644 containerization/README.md
 mode change 100644 => 100755 webserver/cgi-bin/NOMAD/configuredmaterial
 mode change 100644 => 100755 webserver/cgi-bin/NOMAD/configurematerial
 mode change 100644 => 100755 webserver/cgi-bin/NOMAD/material
 mode change 100644 => 100755 webserver/cgi-bin/NOMAD/materialAnalytics

diff --git a/Dockerfile b/Dockerfile
deleted file mode 100644
index ffd31ea..0000000
--- a/Dockerfile
+++ /dev/null
@@ -1,4 +0,0 @@
-FROM bitnami/apache:2.4
-COPY ./webserver/htdocs/NOMAD/  /opt/bitnami/apache/htdocs/NOMAD/
-COPY ./webserver/cgi-bin/NOMAD/ /opt/bitnami/apache/cgi-bin/NOMAD/
-
diff --git a/README.md b/README.md
index adb8485..802e736 100644
--- a/README.md
+++ b/README.md
@@ -43,6 +43,15 @@ Subdirectories:
 		htdocs: Web pages containing the VR software and documentation.
 		cgi-bin: cgi scripts to create suitable config files from a material number.
 	
+		Docker container running the VR webservices
+		# install docker
+		# add your NOMAD username and password in webserver/pass
+		# build container
+			sudo docker build -t nomadvr .
+		# run container
+			sudo docker run -d -p 8080:8080 nomadvr
+		# Test e.g. with you.rip.add.res:8080/cgi-bin/NOMAD/material?109702
+
 	RemoteVisualization: NOMAD2xyz app 
 		Transforms a json from encyclopedia or archive into an XYZ file	
 		Useful for the remote visualization infrastructure
@@ -54,6 +63,7 @@ Subdirectories:
 		MD-Driver/SimpleMove and MD-Driver/PeriodicTable: see MD-Driver/Readme
 			Support for atom drag-and-drop functionality in OpenVR NOMAD VR.
 	
+
 More platforms will be added in the future.
 
 The code is distributed under the Apache 2.0 License (See LICENSE file).
diff --git a/containerization/Dockerfile b/containerization/Dockerfile
new file mode 100644
index 0000000..f3552ba
--- /dev/null
+++ b/containerization/Dockerfile
@@ -0,0 +1,19 @@
+# note, copy this file to VR-demos folder first
+FROM bitnami/apache:2.4
+
+# copy NOMAD webservices
+COPY ./webserver/htdocs/NOMAD/  /opt/bitnami/apache/htdocs/NOMAD/
+COPY ./webserver/cgi-bin/NOMAD/ /opt/bitnami/apache/cgi-bin/NOMAD/
+COPY ./webserver/pass /opt/bitnami/apache/
+
+# allow cgi-bin
+RUN sed -i 's%#LoadModule cgid_module modules/mod_cgid.so%LoadModule cgid_module modules/mod_cgid.so%' /opt/bitnami/apache/conf/httpd.conf
+
+# add wget. gzip, curl, tar are there by default
+USER root
+RUN apt-get update \
+    && apt-get install -y wget \
+    && apt-get install -y zip \
+    && rm -rf /var/lib/apt/lists/*
+
+
diff --git a/containerization/README.md b/containerization/README.md
new file mode 100644
index 0000000..4a164b2
--- /dev/null
+++ b/containerization/README.md
@@ -0,0 +1,13 @@
+Docker file to build a container to perform the VR conversion webservices
+
+* copy the Dockerfile to .. (VR-demos -folder)
+* install docker
+* add your NOMAD username and password in webserver/pass
+* build container
+   sudo docker build -t nomadvr .
+* run container
+   sudo docker run -d -p 8080:8080 nomadvr
+* Test e.g. with you.rip.add.res:8080/cgi-bin/NOMAD/material?109702
+* stop container
+  sudo docker stop <container-name>
+
diff --git a/webserver/cgi-bin/NOMAD/configuredmaterial b/webserver/cgi-bin/NOMAD/configuredmaterial
old mode 100644
new mode 100755
diff --git a/webserver/cgi-bin/NOMAD/configurematerial b/webserver/cgi-bin/NOMAD/configurematerial
old mode 100644
new mode 100755
diff --git a/webserver/cgi-bin/NOMAD/material b/webserver/cgi-bin/NOMAD/material
old mode 100644
new mode 100755
index 14c55e1..d1e276e
--- a/webserver/cgi-bin/NOMAD/material
+++ b/webserver/cgi-bin/NOMAD/material
@@ -1,104 +1,104 @@
-#!/bin/bash
-
- # Copyright 2016-2018 Ruben Jesus Garcia Hernandez
- #
- # Licensed 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.
-
-source ../../pass
-
-if [[ "$QUERY_STRING" = "" ]]
-then
-	#echo Location: /NOMAD/
-	echo Location:https://www.nomad-coe.eu/the-project/graphics/VR-prototype
-	echo ''
-else
-#rgh: text/plain works in pc but android chrome saves it as .txt, removing the extension
-
-#	export TOKEN=`cat t.b64`
-	export MAT=$QUERY_STRING
-	export RAND=$RANDOM
-#rgh: before end of september 2017, use /var/tmp, after, use /tmp (apache update)
-	cd /tmp
-	/usr/bin/mkdir $RAND
-	cd $RAND
-
-#pre 2018: http://encyclopedia-api.nomad-coe.eu/v1.0/saml/
-#post 2018:
-SAMLURL=https://encyclopedia.nomad-coe.eu/api/v1.0/saml/
-
-wget --save-cookies cookies.txt \
-     --keep-session-cookies \
-        --no-check-certificate $SAMLURL -O login.html >/dev/null 2>&1
-URL=`grep "form action" login.html |cut -f 2 -d \" |head -n 1`
-wget --save-cookies cookies2.txt --keep-session-cookies --load-cookies cookies.txt --no-check-certificate \
-	"https://idp.nomad-coe.eu$URL&j_username=$USER&j_password=$PASS&_eventId_proceed" -O a.html >/dev/null 2>&1
-URL=`grep "form action" a.html |cut -f 2 -d \" |head -n 1`
-wget --save-cookies cookies3.txt --keep-session-cookies --load-cookies cookies2.txt --no-check-certificate \
-	"https://idp.nomad-coe.eu$URL&_shib_idp_consentOptions=_shib_idp_globalConsent&_eventId_proceed" \
-	-O b.html >/dev/null 2>&1
-
-#now continue, as we don't support javascript :o)
-URL2=`echo $URL |cut -f 1 -d \?`
-DEST=`grep action b.html |cut -f 2 -d \"`
-RELAY=`grep RelayState b.html |cut -f6 -d \"`
-SAML=`grep SAMLResponse b.html |cut -f6 -d \"`
-#rgh: since 25-09-2017, this line gives "invalid url" (possibly curl update)
-#RELAY="https%3A%2F%2Fencyclopedia-api.nomad-coe.eu%2Fv1.0%2Fsaml%2F"
-#pre-2018
-#RELAY="https://encyclopedia-api.nomad-coe.eu/v1.0/saml/"
-RELAY=$SAMLURL
-
-#echo relay is $RELAY
-
-
-MATERIALSURL=https://encyclopedia.nomad-coe.eu/api/v1.0/materials
-
-curl -L -c cookies3.txt -X GET -F "RelayState=$RELAY" -F "SAMLResponse=$SAML" "$SAMLURL?acs" -o resp.json >/dev/null 2>&1
-TOKEN=`grep data resp.json |cut -f 4 -d \"`
-
-
-	mkdir $MAT
-	cd $MAT
-	wget --no-check-certificate --user "$TOKEN" --password "" $MATERIALSURL/$MAT/cells?pagination=off -O material_cells.json >/dev/null 2>&1 
-	wget --no-check-certificate --user "$TOKEN" --password "" $MATERIALSURL/$MAT/elements?pagination=off -O material_elements.json >/dev/null 2>&1
-	echo \# Material $QUERY_STRING >> $MAT.ncfg
-	echo background 0 0 0 >> $MAT.ncfg
-	echo atomscaling 0.5 >> $MAT.ncfg
-	echo json material >> $MAT.ncfg
-	echo displaybonds >> $MAT.ncfg
-	echo displayunitcell >> $MAT.ncfg
-	echo showcontrollers >> $MAT.ncfg
-	cd ..
-
-	if grep html $MAT/material_cells.json > /dev/null ; then
-		echo "CGI backend for NOMAD VR: Encyclopedia API issue. Material $MAT"  | 
-			mailx -s 'CGI backend for NOMAD VR: Encyclopedia API issue' $EMAIL
-		echo "Content-type: text/html"
-		echo ''
-		echo "<html><head><title>"
-		echo "VR backend currently being updated"
-		echo "</title></head><body>"
-		echo "VR backend currently being updated."
-		echo "Please <a href=\"https://www.nomad-coe.eu/the-project/graphics/contact-2\">contact us</a> if the problem persists."
-		echo "</body></html>"
-	else
-		zip -r $MAT $MAT >/dev/null 2>&1 
-	
-		echo "Content-type: application/octet-stream"
-		echo "Content-disposition: attachment; filename=${QUERY_STRING}.zip"
-		echo ''
-		cat $MAT.zip
-	fi
-	cd ..
-	\rm -r $RAND
-fi
+#!/bin/bash
+
+ # Copyright 2016-2018 Ruben Jesus Garcia Hernandez
+ #
+ # Licensed 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.
+
+source ../../pass
+
+if [[ "$QUERY_STRING" = "" ]]
+then
+	#echo Location: /NOMAD/
+	echo Location:https://www.nomad-coe.eu/the-project/graphics/VR-prototype
+	echo ''
+else
+#rgh: text/plain works in pc but android chrome saves it as .txt, removing the extension
+
+#	export TOKEN=`cat t.b64`
+	export MAT=$QUERY_STRING
+	export RAND=$RANDOM
+#rgh: before end of september 2017, use /var/tmp, after, use /tmp (apache update)
+	cd /tmp
+	/usr/bin/mkdir $RAND
+	cd $RAND
+
+#pre 2018: http://encyclopedia-api.nomad-coe.eu/v1.0/saml/
+#post 2018:
+SAMLURL=https://encyclopedia.nomad-coe.eu/api/v1.0/saml/
+
+wget --save-cookies cookies.txt \
+     --keep-session-cookies \
+        --no-check-certificate $SAMLURL -O login.html >/dev/null 2>&1
+URL=`grep "form action" login.html |cut -f 2 -d \" |head -n 1`
+wget --save-cookies cookies2.txt --keep-session-cookies --load-cookies cookies.txt --no-check-certificate \
+	"https://idp.nomad-coe.eu$URL&j_username=$USER&j_password=$PASS&_eventId_proceed" -O a.html >/dev/null 2>&1
+URL=`grep "form action" a.html |cut -f 2 -d \" |head -n 1`
+wget --save-cookies cookies3.txt --keep-session-cookies --load-cookies cookies2.txt --no-check-certificate \
+	"https://idp.nomad-coe.eu$URL&_shib_idp_consentOptions=_shib_idp_globalConsent&_eventId_proceed" \
+	-O b.html >/dev/null 2>&1
+
+#now continue, as we don't support javascript :o)
+URL2=`echo $URL |cut -f 1 -d \?`
+DEST=`grep action b.html |cut -f 2 -d \"`
+RELAY=`grep RelayState b.html |cut -f6 -d \"`
+SAML=`grep SAMLResponse b.html |cut -f6 -d \"`
+#rgh: since 25-09-2017, this line gives "invalid url" (possibly curl update)
+#RELAY="https%3A%2F%2Fencyclopedia-api.nomad-coe.eu%2Fv1.0%2Fsaml%2F"
+#pre-2018
+#RELAY="https://encyclopedia-api.nomad-coe.eu/v1.0/saml/"
+RELAY=$SAMLURL
+
+#echo relay is $RELAY
+
+
+MATERIALSURL=https://encyclopedia.nomad-coe.eu/api/v1.0/materials
+
+curl -L -c cookies3.txt -X GET -F "RelayState=$RELAY" -F "SAMLResponse=$SAML" "$SAMLURL?acs" -o resp.json >/dev/null 2>&1
+TOKEN=`grep data resp.json |cut -f 4 -d \"`
+
+
+	mkdir $MAT
+	cd $MAT
+	wget --no-check-certificate --user "$TOKEN" --password "" $MATERIALSURL/$MAT/cells?pagination=off -O material_cells.json >/dev/null 2>&1 
+	wget --no-check-certificate --user "$TOKEN" --password "" $MATERIALSURL/$MAT/elements?pagination=off -O material_elements.json >/dev/null 2>&1
+	echo \# Material $QUERY_STRING >> $MAT.ncfg
+	echo background 0 0 0 >> $MAT.ncfg
+	echo atomscaling 0.5 >> $MAT.ncfg
+	echo json material >> $MAT.ncfg
+	echo displaybonds >> $MAT.ncfg
+	echo displayunitcell >> $MAT.ncfg
+	echo showcontrollers >> $MAT.ncfg
+	cd ..
+
+	if grep html $MAT/material_cells.json > /dev/null ; then
+		echo "CGI backend for NOMAD VR: Encyclopedia API issue. Material $MAT"  | 
+			mailx -s 'CGI backend for NOMAD VR: Encyclopedia API issue' $EMAIL
+		echo "Content-type: text/html"
+		echo ''
+		echo "<html><head><title>"
+		echo "VR backend currently being updated"
+		echo "</title></head><body>"
+		echo "VR backend currently being updated."
+		echo "Please <a href=\"https://www.nomad-coe.eu/the-project/graphics/contact-2\">contact us</a> if the problem persists."
+		echo "</body></html>"
+	else
+		zip -r $MAT $MAT >/dev/null 2>&1 
+	
+		echo "Content-type: application/octet-stream"
+		echo "Content-disposition: attachment; filename=${QUERY_STRING}.zip"
+		echo ''
+		cat $MAT.zip
+	fi
+	cd ..
+#	\rm -r $RAND
+fi
diff --git a/webserver/cgi-bin/NOMAD/materialAnalytics b/webserver/cgi-bin/NOMAD/materialAnalytics
old mode 100644
new mode 100755
diff --git a/webserver/pass b/webserver/pass
index 35953d7..541d45f 100644
--- a/webserver/pass
+++ b/webserver/pass
@@ -3,5 +3,5 @@ export USER=
 export PASS=
 
 #Notification email
-export EMAIL=
+export EMAIL
 
-- 
GitLab