From 3ad745ed2ba56b9043a33c3913c8e33e02066223 Mon Sep 17 00:00:00 2001
From: Stefano Alberto Russo <stefano.russo@gmail.com>
Date: Mon, 11 Oct 2021 23:24:07 +0200
Subject: [PATCH] Added ESAP GUI and added it to the proxy.

---
 docker-compose-dev.yml             | 11 ++++
 esap/build                         |  1 +
 services/gui/Dockerfile            | 71 +++++++++++++++++++++++
 services/gui/entrypoint.sh         | 40 +++++++++++++
 services/gui/index.html            |  6 ++
 services/gui/run_gui.sh            | 13 +++++
 services/gui/sites_enabled_default | 91 ++++++++++++++++++++++++++++++
 services/proxy/000-default.conf    | 14 +++--
 services/proxy/default-ssl.conf    | 11 +++-
 services/proxy/run_Apache.sh       |  6 +-
 10 files changed, 252 insertions(+), 12 deletions(-)
 create mode 100644 services/gui/Dockerfile
 create mode 100644 services/gui/entrypoint.sh
 create mode 100644 services/gui/index.html
 create mode 100644 services/gui/run_gui.sh
 create mode 100644 services/gui/sites_enabled_default

diff --git a/docker-compose-dev.yml b/docker-compose-dev.yml
index baf7e59..539821c 100644
--- a/docker-compose-dev.yml
+++ b/docker-compose-dev.yml
@@ -15,6 +15,17 @@ services:
     #volumes:
     #   - ./services/api-gateway/opt/esap-api-gateway:/opt/opt/esap-api-gateway
 
+
+  gui:
+    image: "esap/gui"
+    container_name: gui
+    hostname: gui
+    ports:
+      - "8001:8001"
+    #volumes:
+    #   - ./services/gui/opt/esap-gui:/opt/opt/esap-gui
+
+
   proxy:
     image: "esap/proxy"
     container_name: proxy
diff --git a/esap/build b/esap/build
index 62cd0bd..fe48119 100755
--- a/esap/build
+++ b/esap/build
@@ -31,6 +31,7 @@ if [[ "x$SERVICE" == "x" ]] ; then
     # Build all services
     $BUILD_COMMAND services/api-gateway -t esap/api-gateway
     $BUILD_COMMAND services/proxy -t esap/proxy
+    $BUILD_COMMAND services/gui -t esap/gui
         
 else
 
diff --git a/services/gui/Dockerfile b/services/gui/Dockerfile
new file mode 100644
index 0000000..cbf78c3
--- /dev/null
+++ b/services/gui/Dockerfile
@@ -0,0 +1,71 @@
+FROM ubuntu:20.04
+MAINTAINER Stefano Alberto Russo <stefano.russo@gmail.com>
+
+#----------------------
+# Basics
+#----------------------
+
+# Set non-interactive
+ENV DEBIAN_FRONTEND noninteractive
+
+# Update
+RUN apt-get update
+
+# Utilities
+RUN apt-get install -y nano telnet unzip wget openssh-server sudo curl
+
+# Devel
+RUN apt-get install -y build-essential python-dev git-core
+
+# Node & nginx
+RUN apt-get install -y nodejs npm nginx
+
+
+#------------------------
+# Esap user
+#------------------------
+
+# Add group. We chose GID 65527 to try avoiding conflicts.
+RUN groupadd -g 65527 esap
+
+# Add user. We chose UID 65527 to try avoiding conflicts.
+RUN useradd esap -d /esap -u 65527 -g 65527 -m -s /bin/bash
+
+# Add esap user to sudoers
+RUN adduser esap sudo
+
+# No pass sudo (for everyone, actually)
+COPY sudoers /etc/sudoers
+
+
+#------------------------
+# Code
+#------------------------
+
+RUN cd /opt && git clone https://git.astron.nl/astron-sdc/esap-gui.git
+RUN cd /opt/esap-gui && git pull && git checkout 0be8125b478007c75b13002d4e5273af7e14c057
+RUN cd /opt/esap-gui && npm install
+RUN cd /opt/esap-gui && npm run build
+
+# Link static files
+RUN ln -s /opt/esap-gui/build /var/www/html/esap-gui
+RUN ln -s /opt/esap-gui/build/static/ /var/www/html/static
+
+# Change nginx conf to listen on 8001
+COPY sites_enabled_default /etc/nginx/sites-enabled/default
+
+# Add redirect index
+COPY index.html /var/www/html/index.html
+
+
+
+#----------------------
+# Entrypoint
+#----------------------
+
+COPY run_gui.sh /run_gui.sh
+COPY entrypoint.sh /entrypoint.sh
+RUN chmod 755 /entrypoint.sh /run_gui.sh
+
+USER esap
+ENTRYPOINT ["/entrypoint.sh"]
diff --git a/services/gui/entrypoint.sh b/services/gui/entrypoint.sh
new file mode 100644
index 0000000..ecd9309
--- /dev/null
+++ b/services/gui/entrypoint.sh
@@ -0,0 +1,40 @@
+#!/bin/bash
+
+# Exit on any error.
+set -e
+
+echo ""
+echo "[INFO] Executing entrypoint..."
+
+#---------------------
+#   Save env
+#---------------------
+echo "[INFO] Dumping env"
+
+# Save env vars for later usage (e.g. docker exec)
+
+env | \
+while read env_var; do
+  if [[ $env_var == HOME\=* ]]; then
+      : # Skip HOME var
+  elif [[ $env_var == PWD\=* ]]; then
+      : # Skip PWD var
+  else
+      echo "export $env_var" >> /tmp/env.sh
+  fi
+done
+
+#---------------------
+#  Entrypoint command
+#---------------------
+
+
+if [[ "x$@" == "x" ]] ; then
+    ENTRYPOINT_COMMAND="/run_gui.sh"
+else
+    ENTRYPOINT_COMMAND=$@
+fi
+
+echo -n "[INFO] Executing Docker entrypoint command: "
+echo $ENTRYPOINT_COMMAND
+exec "$ENTRYPOINT_COMMAND"
diff --git a/services/gui/index.html b/services/gui/index.html
new file mode 100644
index 0000000..4f0853f
--- /dev/null
+++ b/services/gui/index.html
@@ -0,0 +1,6 @@
+
+<html>
+<head>
+<meta http-equiv="refresh" content="0; URL=/esap-gui" />
+</head>
+</html>
\ No newline at end of file
diff --git a/services/gui/run_gui.sh b/services/gui/run_gui.sh
new file mode 100644
index 0000000..fa5872e
--- /dev/null
+++ b/services/gui/run_gui.sh
@@ -0,0 +1,13 @@
+#!/bin/bash
+
+DATE=$(date)
+
+echo ""
+echo "==================================================="
+echo "Starting GUI @ $DATE"
+echo "==================================================="
+echo ""
+
+
+sudo nginx -g 'daemon off;'
+
diff --git a/services/gui/sites_enabled_default b/services/gui/sites_enabled_default
new file mode 100644
index 0000000..ec3b3c5
--- /dev/null
+++ b/services/gui/sites_enabled_default
@@ -0,0 +1,91 @@
+##
+# You should look at the following URL's in order to grasp a solid understanding
+# of Nginx configuration files in order to fully unleash the power of Nginx.
+# https://www.nginx.com/resources/wiki/start/
+# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
+# https://wiki.debian.org/Nginx/DirectoryStructure
+#
+# In most cases, administrators will remove this file from sites-enabled/ and
+# leave it as reference inside of sites-available where it will continue to be
+# updated by the nginx packaging team.
+#
+# This file will automatically load configuration files provided by other
+# applications, such as Drupal or Wordpress. These applications will be made
+# available underneath a path with that package name, such as /drupal8.
+#
+# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
+##
+
+# Default server configuration
+#
+server {
+    listen 8001 default_server;
+    listen [::]:8001 default_server;
+
+    # SSL configuration
+    #
+    # listen 443 ssl default_server;
+    # listen [::]:443 ssl default_server;
+    #
+    # Note: You should disable gzip for SSL traffic.
+    # See: https://bugs.debian.org/773332
+    #
+    # Read up on ssl_ciphers to ensure a secure configuration.
+    # See: https://bugs.debian.org/765782
+    #
+    # Self signed certs generated by the ssl-cert package
+    # Don't use them in a production server!
+    #
+    # include snippets/snakeoil.conf;
+
+    root /var/www/html;
+
+    # Add index.php to the list if you are using PHP
+    index index.html index.htm index.nginx-debian.html;
+
+    server_name _;
+
+    location / {
+        # First attempt to serve request as file, then
+        # as directory, then fall back to displaying a 404.
+        try_files $uri $uri/ =404;
+    }
+
+    # pass PHP scripts to FastCGI server
+    #
+    #location ~ \.php$ {
+    #   include snippets/fastcgi-php.conf;
+    #
+    #   # With php-fpm (or other unix sockets):
+    #   fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
+    #   # With php-cgi (or other tcp sockets):
+    #   fastcgi_pass 127.0.0.1:9000;
+    #}
+
+    # deny access to .htaccess files, if Apache's document root
+    # concurs with nginx's one
+    #
+    #location ~ /\.ht {
+    #   deny all;
+    #}
+}
+
+
+# Virtual Host configuration for example.com
+#
+# You can move that to a different file under sites-available/ and symlink that
+# to sites-enabled/ to enable it.
+#
+#server {
+#   listen 80;
+#   listen [::]:80;
+#
+#   server_name example.com;
+#
+#   root /var/www/example.com;
+#   index index.html;
+#
+#   location / {
+#       try_files $uri $uri/ =404;
+#   }
+#}
diff --git a/services/proxy/000-default.conf b/services/proxy/000-default.conf
index bcddcb5..f282240 100644
--- a/services/proxy/000-default.conf
+++ b/services/proxy/000-default.conf
@@ -32,16 +32,22 @@
 
     # TODO: Re-evaluate, this is somehow a bad idea, as
     #  1) dev env is different than staging/production, and
-    #  2) other roules in 001-proxy.conf are never reached
+    #  2) other rules in 001-proxy.conf are never reached
 
     RewriteEngine On
     RewriteCond %{HTTPS} off
     RewriteCond %{HTTP_HOST} !=localhost
     RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
+        
+    # ESAP API Gateway
+    ProxyPass /esap-api http://api-gateway:8000/esap-api
+    ProxyPassReverse /esap-api http://api-gateway:8000/esap-api
+    
+    # ESAP GUI
+    ProxyPass / http://gui:8001/
+    ProxyPassReverse / http://gui:8001/
+    
     
-    ProxyPass / http://api-gateway:8000/
-    ProxyPassReverse / http://api-gateway:8000/
-
 </VirtualHost>
 
 
diff --git a/services/proxy/default-ssl.conf b/services/proxy/default-ssl.conf
index 66b2f9b..658fa10 100644
--- a/services/proxy/default-ssl.conf
+++ b/services/proxy/default-ssl.conf
@@ -3,9 +3,14 @@
     <VirtualHost _default_:443>
         ServerAdmin webmaster@esap
 
-        ProxyPass / http://api-gateway:8000/
-        ProxyPassReverse / http://api-gateway:8000/
-
+        # ESAP API Gateway
+        ProxyPass /esap-api http://api-gateway:8000/esap-api
+        ProxyPassReverse /esap-api http://api-gateway:8000/esap-api
+    
+        # ESAP GUI
+        ProxyPass / http://gui:8001/
+        ProxyPassReverse / http://gui:8001/
+    
         # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
         # error, crit, alert, emerg.
         # It is also possible to configure the loglevel for particular
diff --git a/services/proxy/run_Apache.sh b/services/proxy/run_Apache.sh
index 8febb11..acb336d 100644
--- a/services/proxy/run_Apache.sh
+++ b/services/proxy/run_Apache.sh
@@ -1,11 +1,7 @@
 #!/bin/bash
 
 # Source env
-source /env.sh
+source /tmp/env.sh
 
 # Exec Apache in foreground
 exec /usr/sbin/apache2ctl -DFOREGROUND
-
-# Or just use in supervisord:
-#/bin/bash -c "source /etc/apache2/envvars && source /env.sh && exec /usr/sbin/apache2 -DFOREGROUND"
-
-- 
GitLab