From c8aa5675abda716952a0ba9de8a4590284fa7859 Mon Sep 17 00:00:00 2001
From: Tor-Einar Skog <tor-einar.skog@nibio.no>
Date: Fri, 27 Sep 2019 07:28:49 +0000
Subject: [PATCH] Storing before reverting VirtualPC

---
 vips-backend-single-server.sh                 | 29 ++++++++++++++-----
 wildfly_config/init_standalone_xml.py         |  6 ++--
 wildfly_config/init_systemd.py                | 19 ++++++++++++
 wildfly_config/systemd_templates/launch.sh    | 12 ++++++++
 wildfly_config/systemd_templates/wildfly.conf |  8 +++++
 .../systemd_templates/wildfly.service         | 16 ++++++++++
 6 files changed, 79 insertions(+), 11 deletions(-)
 create mode 100644 wildfly_config/init_systemd.py
 create mode 100644 wildfly_config/systemd_templates/launch.sh
 create mode 100644 wildfly_config/systemd_templates/wildfly.conf
 create mode 100644 wildfly_config/systemd_templates/wildfly.service

diff --git a/vips-backend-single-server.sh b/vips-backend-single-server.sh
index 8f7e3b1..e4f15a7 100755
--- a/vips-backend-single-server.sh
+++ b/vips-backend-single-server.sh
@@ -4,6 +4,7 @@
 # Author Tor-Einar Skog <tor-einar.skog@nibio.no>
 
 CODE_USER=wildfly
+INITIAL_DIRECTORY=$(pwd)
 
 # Locale update
 # We want the server to user UTF-8 as standard
@@ -73,9 +74,11 @@ cd ../VIPSLogic
 sudo -H -u $CODE_USER bash -c "mvn install -DskipTests"
 
 # Download and unzip Wildfly 16
+cd ..
 WILDFLY_VERSION=16.0.0.Final
-sudo -H -u $CODE_USER bash -c "https://download.jboss.org/wildfly/$WILDFLY_VERSION/$WILDFLY_VERSION.tar.gz"
-sudo -H -u $CODE_USER bash -c "tar xzf $WILDFLY_VERSION.tar.gz"
+sudo -H -u $CODE_USER bash -c "wget https://download.jboss.org/wildfly/$WILDFLY_VERSION/wildfly-$WILDFLY_VERSION.tar.gz"
+sudo -H -u $CODE_USER bash -c "tar xzf wildfly-$WILDFLY_VERSION.tar.gz"
+sudo -H -u $CODE_USER bash -c "rm wildfly-$WILDFLY_VERSION.tar.gz"
 
 # Edit standalone.xml, the Wildfly config file
 printf "\nWILDFLY CONFIGURATION\n"
@@ -88,25 +91,33 @@ do
 	read -p "MD5 salt (to make the one-way encryption much harder to break. Type 10-20 random characters) [*]: " md5salt
 done
 
-WILDFLY_HOME=/home/$CODE_USER/$WILDFLY_VERSION
+WILDFLY_HOME=/home/$CODE_USER/wildfly-$WILDFLY_VERSION
 WILDFLY_CONFIG_PATH=$WILDFLY_HOME/standalone/configuration
 
-cd wildfly_config
+cd $INITIAL_DIRECTORY/wildfly_config
 sudo -H -u $CODE_USER bash -c "python3 init_standalone_xml.py --smtpserver $smtpserver --md5salt $md5salt --dbpassword $vipslogic_password --path $WILDFLY_CONFIG_PATH"
 
 # Add the required modules for VIPSLogic to Wildfly
 # PostgreSQL
 # etc
 # All modules needed to run VIPSLogic AND VIPSCore are added
-cp -r modules/* $WILDFLY_HOME/modules
+sudo -H -u $CODE_USER bash -c "cp -r modules/* $WILDFLY_HOME/modules"
 
 # Set up WildFly as a systemd service
+mkdir /etc/wildfly
+cp systemd_templates/wildfly.conf /etc/wildfly
+cp systemd_templates/wildfly.service /etc/systemd/system
+sed -i -e "s,WILDFLY_PATH_REPLACE_WITH_SED,$WILDFLY_HOME," /etc/systemd/system/wildfly.service
+cp systemd_templates/launch.sh $WILDFLY_HOME/bin
+sed -i -e "s,WILDFLY_PATH_REPLACE_WITH_SED,$WILDFLY_HOME," $WILDFLY_HOME/bin/launch.sh
 
+# Add VIPSLogic to the Wildfly deployments folder
+sudo -H -u $CODE_USER bash -c "ln -s /home/$CODE_USER/VIPSLogic/target/VIPSLogic-1.0-SNAPSHOT.war $WILDFLY_HOME/standalone/deployments/"
 
 
-# Install and configure Apache
-
 # Run (test?) WildFly with VIPSLogic deployed
+$WILDFLY_HOME/bin/standalone.sh
+
 # If successful, this will migrate the vipslogic database to its correct state
 # Next up is adding organization information
 
@@ -128,7 +139,7 @@ read -p "Web map default center longitude (WGS84):" longitude
 read -p "Web map default center latitude (WGS84):" latitude
 read -p "Default timezone (See https://docs.oracle.com/javase/8/docs/api/java/util/TimeZone.html): " timezone
 
-printf "\nINITIAL ADMIN USER INFO"
+printf "\nINITIAL ADMIN USER INFO\n"
 while [ "$user_email" == "" ]
 do
         read -p "Email [*]: " user_email
@@ -139,3 +150,5 @@ do
         read -p "Last name [*]: " last_name
 done
 
+
+# Install and configure Apache
diff --git a/wildfly_config/init_standalone_xml.py b/wildfly_config/init_standalone_xml.py
index c134ae9..37376d8 100755
--- a/wildfly_config/init_standalone_xml.py
+++ b/wildfly_config/init_standalone_xml.py
@@ -17,10 +17,10 @@ args = parser.parse_args()
 
 path = args.path
 # Make a copy of the original file
-copyfile(path + "standalone.xml", path + "standalone_original.xml")
+copyfile(path + "/standalone.xml", path + "/standalone_original.xml")
 
 # The destination document
-standalone_dom = parse(path + "standalone.xml")
+standalone_dom = parse(path + "/standalone.xml")
 
 # The system properties to add to destination (standalone.xml)
 vsp_dom = parse("vipslogic_system_properties.xml")
@@ -56,6 +56,6 @@ for driver in drivers_elm.getElementsByTagName("driver"):
 
 
 # Write to file
-outputfile = open(path + "standalone.xml", "w")
+outputfile = open(path + "/standalone.xml", "w")
 outputfile.write(standalone_dom.toxml())
 outputfile.close()
diff --git a/wildfly_config/init_systemd.py b/wildfly_config/init_systemd.py
new file mode 100644
index 0000000..da8bb2e
--- /dev/null
+++ b/wildfly_config/init_systemd.py
@@ -0,0 +1,19 @@
+#!/usr/bin/python3
+
+# Sets up Wildfly as a systemd service in Ubuntu
+# (c) 2019 NIBIO
+# Author: Tor-Einar Skog <tor-einar.skog@nibio.no>
+
+from shutil import copyfile
+import argparse
+import os
+
+parser = argparse.ArgumentParser()
+parser.add_argument("--path")
+args = parser.parse_args()
+
+path = args.path
+
+# Copy wildfly conf
+os.makedirs("/etc/wildfly")
+copyfile(wildfly.conf, path + "/
diff --git a/wildfly_config/systemd_templates/launch.sh b/wildfly_config/systemd_templates/launch.sh
new file mode 100644
index 0000000..0489d88
--- /dev/null
+++ b/wildfly_config/systemd_templates/launch.sh
@@ -0,0 +1,12 @@
+#!/bin/bash
+
+if [ "x$WILDFLY_HOME" = "x" ]; then
+    WILDFLY_HOME="WILDFLY_PATH_REPLACE_WITH_SED"
+fi
+
+if [[ "$1" == "domain" ]]; then
+    $WILDFLY_HOME/bin/domain.sh -c $2 -b $3
+else
+    $WILDFLY_HOME/bin/standalone.sh -c $2 -b $3
+fi
+
diff --git a/wildfly_config/systemd_templates/wildfly.conf b/wildfly_config/systemd_templates/wildfly.conf
new file mode 100644
index 0000000..4ff3293
--- /dev/null
+++ b/wildfly_config/systemd_templates/wildfly.conf
@@ -0,0 +1,8 @@
+# The configuration you want to run
+WILDFLY_CONFIG=standalone.xml
+
+# The mode you want to run
+WILDFLY_MODE=standalone
+
+# The address to bind to
+WILDFLY_BIND=0.0.0.0
diff --git a/wildfly_config/systemd_templates/wildfly.service b/wildfly_config/systemd_templates/wildfly.service
new file mode 100644
index 0000000..db0476a
--- /dev/null
+++ b/wildfly_config/systemd_templates/wildfly.service
@@ -0,0 +1,16 @@
+[Unit]
+Description=The WildFly Application Server
+After=syslog.target network.target
+Before=httpd.service
+
+[Service]
+Environment=LAUNCH_JBOSS_IN_BACKGROUND=1
+EnvironmentFile=-/etc/wildfly/wildfly.conf
+User=wildfly
+LimitNOFILE=102642
+PIDFile=/var/run/wildfly/wildfly.pid
+ExecStart=WILDFLY_PATH_REPLACE_WITH_SED/bin/launch.sh $WILDFLY_MODE $WILDFLY_CONFIG $WILDFLY_BIND
+StandardOutput=journal+console
+
+[Install]
+WantedBy=multi-user.target
-- 
GitLab