From 8461ab11ea2ebd2d422308bcf39a5330a0432e36 Mon Sep 17 00:00:00 2001
From: David Trattnig <david.trattnig@o94.at>
Date: Tue, 28 Apr 2020 15:52:46 +0200
Subject: [PATCH] Modularized installation for avoid issues.

---
 .gitignore                                    |  4 +-
 install.sh                                    | 86 +++++--------------
 script/build-web.sh                           |  1 -
 script/create-engineuser.sh                   |  9 ++
 script/initialize.sh                          | 17 ++++
 script/install-opam-packages.sh               |  0
 script/install-system-packages.sh             |  7 --
 ...init-db-mariadb.sh => setup-db-mariadb.sh} |  0
 script/setup-db.sh                            | 37 ++++++++
 9 files changed, 89 insertions(+), 72 deletions(-)
 create mode 100755 script/create-engineuser.sh
 create mode 100755 script/initialize.sh
 mode change 100644 => 100755 script/install-opam-packages.sh
 delete mode 100644 script/install-system-packages.sh
 rename script/{init-db-mariadb.sh => setup-db-mariadb.sh} (100%)
 mode change 100644 => 100755
 create mode 100755 script/setup-db.sh

diff --git a/.gitignore b/.gitignore
index 050e83c3..f508b39e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,6 +2,7 @@
 *.pyc
 *.log
 logs
+tmp
 .vscode/tags
 configuration/engine.ini
 web/css/aura-clock-bundle.css
@@ -9,4 +10,5 @@ web/css/aura-player-bundle.css
 web/js/aura-clock-bundle.js
 web/js/aura-player-bundle.js
 web/clock.html
-web/trackservice.html
\ No newline at end of file
+web/trackservice.html
+script/.engine.install-db.lock
\ No newline at end of file
diff --git a/install.sh b/install.sh
index c2d5807c..9daab708 100755
--- a/install.sh
+++ b/install.sh
@@ -13,14 +13,12 @@ if [ $mode == "prod" ]; then
     echo "[Installing AURA ENGINE for Production]"
 fi
 
-echo "Installation System Packages ..."
-bash script/install-system-packages.sh
-
-
-
 
 # Development and Production
 
+echo "Installation OPAM Packages ..."
+bash script/install-opam-packages.sh
+
 echo "Installing Web Application Packages ..."
 bash script/install-web.sh
 
@@ -31,81 +29,43 @@ python3.7 $(which pip3) install -r requirements.txt
 # Development 
 
 if [ $mode == "dev" ]; then
-    # Set LOCK file location
-    LOCKFILE_DB=configuration/.engine.install-db.lock
 
-    echo "Create local Logs Folder ..."
+    echo "Create local 'logs' Folder ..."
     mkdir -p logs
 
     echo "Copy configuration to './configuration/engine.ini'"
-    cp -n configuration/sample.engine.ini configuration/engine.ini
+    cp -n configuration/sample-development.engine.ini configuration/engine.ini
 fi
 
 
 # Production 
 
 if [ $mode == "prod" ]; then
-    # Set LOCK file location
-    LOCKFILE_DB=/etc/aura/.engine.install-db.lock
-
-    if getent passwd 'engineuser' > /dev/null 2>&1; then  
-        echo "User 'engineuser' exists already."; 
-    else     
-        echo "Creating Engine User ..."
-        adduser engineuser
-        adduser engineuser sudo
-    fi
-    
-    echo "Copy Supervisor Config to '/etc/supervisor/conf.d'"
-    cp configuration/supervisor/* /etc/supervisor/conf.d/
-    echo "Refresh Supervisor Config"
-    supervisorctl reread
-
-    echo "Create Log Directory '/var/log/aura/'"
-    mkdir -p /var/log/aura
 
-    echo "Create Configuration Directory '/etc/aura/'"
-    mkdir -p /etc/aura
+    echo "Create local 'tmp' Folder ..."
+    mkdir -p tmp
 
     echo "Copy configuration to '/etc/aura/engine.ini'"
-    cp -n configuration/sample.engine.ini /etc/aura/engine.ini
-    
-    echo "Set Ownership of '/opt/aura/engine', '/var/log/aura/' and '/etc/aura/engine.ini' to Engine User"
-    chown -R engineuser:engineuser .
-    chown -R engineuser:engineuser /var/log/aura/
-    chown -R engineuser:engineuser /etc/aura/engine.ini
-fi
+    cp -n configuration/sample-production.engine.ini /etc/aura/engine.ini
 
+    # echo "Copy Supervisor Config to '/etc/supervisor/conf.d'"
+    # cp configuration/supervisor/* /etc/supervisor/conf.d/
+
+    # echo "Refresh Supervisor Config"
+    # supervisord -c /opt/aura/engine/configuration/supervisor/supervisord.conf reread
+
+    # echo "Update Supervisor Configuration"
+    # supervisorctl -c /opt/aura/engine/configuration/supervisor/supervisord.conf update
+
+    echo "Refresh Supervisor Config"
+    supervisord reread
+
+    echo "Update Supervisor Configuration"
+    supervisorctl update
 
-# Setup Database
-# Check if databases are already set-up
-if test -f "$LOCKFILE_DB"; then
-  echo "Aura Engine Databases are already existing! Skipping..."
-else
-    echo "Setting up database ..."
-    echo 
-    echo "Which database system do you want to use? (Press '1' or '2')"
-    echo "  [1] MariaDB"
-    echo "  [2] Other / Manually"
-    echo
-
-    while true; do
-    read -rsn1 input
-    if [ "$input" = "1" ]; then
-        echo "Creating DB for MariaDB ..."
-        bash script/init-db-mariadb.sh
-        break
-    fi
-    if [ "$input" = "2" ]; then
-        echo "Manual database setup selected."
-        break
-    fi
-    done
-
-    # Create lockfile to avoid accidential re-creation of the database
-    touch $LOCKFILE_DB
 fi
 
+
 echo 
 echo "+++ Installation of AURA Engine finished! +++"
 echo
\ No newline at end of file
diff --git a/script/build-web.sh b/script/build-web.sh
index 500c16da..d42ecdbc 100755
--- a/script/build-web.sh
+++ b/script/build-web.sh
@@ -1,4 +1,3 @@
-
 #!/bin/bash
 
 echo "Building AURA Clock ..."
diff --git a/script/create-engineuser.sh b/script/create-engineuser.sh
new file mode 100755
index 00000000..d3a78944
--- /dev/null
+++ b/script/create-engineuser.sh
@@ -0,0 +1,9 @@
+#!/bin/bash
+
+if getent passwd 'engineuser' > /dev/null 2>&1; then  
+    echo "User 'engineuser' exists already."; 
+else     
+    echo "Creating Engine User ..."
+    adduser engineuser
+    adduser engineuser sudo
+fi
\ No newline at end of file
diff --git a/script/initialize.sh b/script/initialize.sh
new file mode 100755
index 00000000..b22dfb7e
--- /dev/null
+++ b/script/initialize.sh
@@ -0,0 +1,17 @@
+#!/bin/bash
+
+#
+# Prepare folders and permissions for installing engine on production.
+#
+
+echo "Create Log Directory '/var/log/aura/'"
+mkdir -p /var/log/aura
+
+echo "Create Configuration Directory '/etc/aura/'"
+mkdir -p /etc/aura
+
+echo "Set Ownership of '/opt/aura/engine', '/var/log/aura/' and '/etc/aura/engine.ini' to Engine User"
+chown -R engineuser:engineuser /opt/aura/
+chown -R engineuser:engineuser /etc/aura/
+chown -R engineuser:engineuser /var/log/aura/
+chown -R engineuser:engineuser /var/log/supervisor/
\ No newline at end of file
diff --git a/script/install-opam-packages.sh b/script/install-opam-packages.sh
old mode 100644
new mode 100755
diff --git a/script/install-system-packages.sh b/script/install-system-packages.sh
deleted file mode 100644
index 2b5c6491..00000000
--- a/script/install-system-packages.sh
+++ /dev/null
@@ -1,7 +0,0 @@
-#!/bin/bash
-
-sudo apt install -y \
-    supervisor \
-    redis-server \
-    libsndfile1 ffmpeg \
-    quelcom \
\ No newline at end of file
diff --git a/script/init-db-mariadb.sh b/script/setup-db-mariadb.sh
old mode 100644
new mode 100755
similarity index 100%
rename from script/init-db-mariadb.sh
rename to script/setup-db-mariadb.sh
diff --git a/script/setup-db.sh b/script/setup-db.sh
new file mode 100755
index 00000000..12e0f61f
--- /dev/null
+++ b/script/setup-db.sh
@@ -0,0 +1,37 @@
+#!/bin/bash
+
+#
+# Setup Database
+#
+
+# Set LOCK file location
+LOCKFILE_DB=.engine.install-db.lock
+
+# Check if databases are already set-up
+if test -f "$LOCKFILE_DB"; then
+  echo "Aura Engine Databases are already existing! Skipping..."
+else
+    echo "Setting up database ..."
+    echo 
+    echo "Which database system do you want to use? (Press '1' or '2')"
+    echo "  [1] MariaDB"
+    echo "  [2] Other / Manually"
+    echo
+
+    while true; do
+    read -rsn1 input
+
+    if [ "$input" = "1" ]; then
+        echo "Creating DB for MariaDB ..."
+        bash init-db-mariadb.sh
+        break
+    fi
+    if [ "$input" = "2" ]; then
+        echo "Manual database setup selected."
+        break
+    fi
+    done
+
+    # Create lockfile to avoid accidential re-creation of the database
+    touch $LOCKFILE_DB
+fi
-- 
GitLab