From 147c24e0a38b5a9d80a56bddf2ad5fb0f4623e4b Mon Sep 17 00:00:00 2001
From: Tor-Einar Skog <tor-einar.skog@nibio.no>
Date: Thu, 24 Oct 2019 13:15:46 +0000
Subject: [PATCH] Storing, storing...

---
 db/vipscoremanager_init_1.sql                 | 18 ++++++
 db/vipslogic_init_2.sql                       | 54 ----------------
 vips-backend-single-server.sh                 | 32 +++++++++-
 ...ne_xml_for_vipscoremanager_and_vipscore.py | 62 +++++++++++++++++++
 wildfly_config/vipscoremanager_datasource.xml | 19 ++++++
 .../vipscoremanager_system_properties.xml     |  5 ++
 6 files changed, 135 insertions(+), 55 deletions(-)
 create mode 100644 db/vipscoremanager_init_1.sql
 delete mode 100644 db/vipslogic_init_2.sql
 create mode 100755 wildfly_config/init_standalone_xml_for_vipscoremanager_and_vipscore.py
 create mode 100644 wildfly_config/vipscoremanager_datasource.xml
 create mode 100644 wildfly_config/vipscoremanager_system_properties.xml

diff --git a/db/vipscoremanager_init_1.sql b/db/vipscoremanager_init_1.sql
new file mode 100644
index 0000000..20e5755
--- /dev/null
+++ b/db/vipscoremanager_init_1.sql
@@ -0,0 +1,18 @@
+SET statement_timeout = 0;
+SET lock_timeout = 0;
+SET client_encoding = 'UTF8';
+SET standard_conforming_strings = on;
+SET check_function_bodies = false;
+SET client_min_messages = warning;
+SET row_security = off;
+
+--
+-- TOC entry 2152 (class 1262 OID 16386)
+-- Name: vipscoremanager; Type: DATABASE; Schema: -; Owner: vipscoremanager
+--
+
+CREATE USER vipscoremanager WITH PASSWORD :vipscoremanager_password;
+
+CREATE DATABASE vipscoremanager WITH TEMPLATE = template0 ENCODING = 'UTF8' LC_COLLATE = 'en_US.UTF-8' LC_CTYPE = 'en_US.UTF-8';
+
+ALTER DATABASE vipscoremanager OWNER TO vipscoremanager;
diff --git a/db/vipslogic_init_2.sql b/db/vipslogic_init_2.sql
deleted file mode 100644
index 1a41e93..0000000
--- a/db/vipslogic_init_2.sql
+++ /dev/null
@@ -1,54 +0,0 @@
--- This script should be run as superuser (postgres) AFTER the first successful deployment of VIPSLogic 
--- DON'T FORGET TO EDIT WITH YOUR OWN INFO
--- psql -f init_org_and_user.sql vipslogic
-
--- In case you forgot until now...
-ALTER ROLE vipslogic NOSUPERUSER;
-
--- SETUP organization AND first admin user
-
-INSERT INTO organization (organization_id, organization_name, parent_organization_id, address1, address2, postal_code, country_code, default_locale, default_map_center, default_map_zoom, default_time_zone, city, default_vips_core_user_id, vipsweb_url) 
-VALUES (
-	1, -- organization_id
-	'VIPS Norge', -- organization_name
-	NULL, -- parent_organization_id (normally not in use, consider deprecated)
-	'Postboks 115', -- address1
-	NULL, -- address2
-	'1431', -- postal_code (ZIP)
-	'NO', -- country_code Ref. table public.country
-	'nb', -- default_locale Ref. https://docs.oracle.com/javase/8/docs/api/java/util/Locale.html 
-	ST_GeomFromText('POINT(10.00015 68.432044)', 4326), -- default_map_center 
-	4, -- default_map_zoom (OpenLayers zoom level)
-	'Europe/Oslo', -- default_time_zone Ref. https://docs.oracle.com/javase/8/docs/api/java/util/TimeZone.html
-	'Ås', -- city
-	1, -- default_vips_core_user_id
-	'http://www.vips-landbruk.no/' -- vipsweb_url URL to the public website
-);
-
-
-INSERT INTO vips_logic_user (user_id, email, first_name, last_name, organization_id, remarks, user_status_id, vips_core_user_id, preferred_locale, phone, approves_sms_billing, phone_country_code, free_sms) 
-VALUES (
-	1, -- user_id
-	'foo.bar@foobar.com', -- user_email 
-	'Foo', -- first_name
-	'Bar', -- last_name
-	1, -- organization_id
-	'Created at application initialization', -- General remarks about user 
-	4, -- user_status_id 4 = approved
-	1, -- vips_core_user_id = Which user id you have in the VIPSCoreManager auth system
-	'en', -- preferred_locale Ref. https://docs.oracle.com/javase/8/docs/api/java/util/Locale.html 
-	'21324354', -- phone (without country code) 
-	true, -- approves_sms_billing If true = Approves to the SMS service provider that billing is OK
-	'47', -- phone_country_code
-	true -- free_sms If true = receive free SMSs from the SMS service provider
-);
-
-INSERT INTO user_vips_logic_role (vips_logic_role_id, user_id) VALUES (1,1); -- super user
-
-INSERT INTO user_authentication (user_id, user_authentication_type_id, username, password) 
-VALUES (
-	1, -- user_id
-	1, -- authentication_type_id Ref public.user_authentication_type (Password in this example)
-	'foobar', -- username
-	'XXXXXXXXXXXXXXXX' -- password, MD5 encrypted with the no.nibio.vips.logic.MD5_SALT (see the appserver configuration settings)
-);
diff --git a/vips-backend-single-server.sh b/vips-backend-single-server.sh
index e1f0cf8..5d848a9 100755
--- a/vips-backend-single-server.sh
+++ b/vips-backend-single-server.sh
@@ -247,8 +247,38 @@ sudo -H -u $CODE_USER bash -c "mvn install -DskipTests"
 sudo -H -u $CODE_USER bash -c "ln -s /home/$CODE_USER/VIPSCore/target/VIPSCore-1.0-SNAPSHOT.war $WILDFLY_HOME/standalone/deployments/"
 sudo -H -u $CODE_USER bash -c "ln -s /home/$CODE_USER/VIPSCore/target/VIPSCoreManager-1.0-SNAPSHOT.war $WILDFLY_HOME/standalone/deployments/"
 
-# Add configuration properties to Wildfly's standalone.xml
+# Initializing the database for vipscoremanager
+printf "\nDATABASE USER INFORMATION for vipscoremanager\n"
+printf "We will create a postgresql user 'vipscoremanager' which will own the 'vipscoremanager' database\n"
+while [ "$vipscoremanager_password" == "" ]
+do
+        read -sp "Password for vipscoremanager [*]: " vipscoremanager_password
+done
 
+sudo -H -u postgres bash -c "psql -v vipscoremanager_password=\"'$vipscoremanager_password'\" -f db/vipscoremanager_init_1.sql"
+
+# Edit standalone.xml, the Wildfly config file, for VIPSCoreManager
+printf "\nWILDFLY CONFIGURATION for VIPSCoreManager\n"
+while [ "$smtpserver" == "" ]
+do
+        read -p "SMTP servername [*]: " smtpserver
+done
+while [ "$md5salt_2" == "" ]
+do
+        read -p "MD5 salt (to make the one-way encryption much harder to break. Type 10-20 random characters) [*]: " md5salt_2
+done
+while [ "$corebatch_username" == "" ]
+do
+	read -p "Core batch username (Allowing VIPSLogic to run models in VIPSCore) " corebatch_username
+done
+while [ "$corebatch_password" == "" ]
+do
+        read -p "Core batch password (Allowing VIPSLogic to run models in VIPSCore) " corebatch_password
+done
+
+
+cd $INITIAL_DIRECTORY/wildfly_config
+sudo -H -u $CODE_USER bash -c "python3 init_standalone_xml_for_vipscoremanager_and_vipscore.py --md5salt $md5salt_2 --dbpassword $vipscoremanager_password --corebatch_username $corebatch_username --corebatch_password $corebatch_password --path $WILDFLY_CONFIG_PATH"
 
 
 echo "-----------------------------------"
diff --git a/wildfly_config/init_standalone_xml_for_vipscoremanager_and_vipscore.py b/wildfly_config/init_standalone_xml_for_vipscoremanager_and_vipscore.py
new file mode 100755
index 0000000..2ad3ef0
--- /dev/null
+++ b/wildfly_config/init_standalone_xml_for_vipscoremanager_and_vipscore.py
@@ -0,0 +1,62 @@
+#!/usr/bin/python3
+
+# Adds VIPSCore and VIPSCoreManager specific config to [WILDFLY_HOME]/standalone/configuration/standalone.xml
+# (c) 2019 NIBIO
+# Author Tor-Einar Skog <tor-einar.skog@nibio.no>
+
+from shutil import copyfile
+from xml.dom.minidom import parse
+import argparse
+
+parser = argparse.ArgumentParser()
+parser.add_argument("--md5salt")
+parser.add_argument("--dbpassword")
+parser.add_argument("--corebatch_username")
+parser.add_argument("--corebatch_password")
+parser.add_argument("--path")
+args = parser.parse_args()
+
+path = args.path
+# Make a copy of the original file
+copyfile(path + "/standalone.xml", path + "/standalone_original.xml")
+
+# The destination document
+standalone_dom = parse(path + "/standalone.xml")
+
+# The system properties to add to destination (standalone.xml)
+vsp_dom = parse("vipscoremanager_system_properties.xml")
+
+system_properties = standalone_dom.getElementsByTagName("system-properties")
+if len(system_properties) == 0:
+    system_properties = standalone_dom.createElement("system-properties")
+    standalone_dom.getElementsByTagName("server")[0].insertBefore(system_properties, standalone_dom.getElementsByTagName("management")[0])
+else:
+    system_properties = system_properties[0]
+
+# Transfer system properties to standalone.xml
+for property in vsp_dom.getElementsByTagName("property"):
+    # We use script input parameters to set system property values
+    if property.getAttribute("name") == "no.nibio.vips.coremanager.MD5_SALT":
+        property.setAttribute("value",args.md5salt)
+    if property.getAttribute("name") == "no.nibio.vips.logic.CORE_BATCH_USERNAME":
+        property.setAttribute("value",args.corebatch_username)
+    if property.getAttribute("name") == "no.nibio.vips.logic.CORE_BATCH_PASSWORD":
+        property.setAttribute("value",args.corebatch_password)
+    system_properties.appendChild(property)
+
+
+# Transfer the data source and driver info to standalone.xml
+vipscoremanager_datasource_dom = parse("vipscoremanager_datasource.xml")
+datasources_elm = vipscoremanager_datasource_dom.getElementsByTagName("datasources")[0]
+standalone_datasources_elm = standalone_dom.getElementsByTagName("datasources")[0]
+vipscoremanager_datasource = None
+for datasource in datasources_elm.getElementsByTagName("datasource"):
+    if datasource.getAttribute("jndi-name") == "java:jboss/datasources/vipscoremanager":
+        datasource.getElementsByTagName("security")[0].getElementsByTagName("password")[0].firstChild.replaceWholeText(args.dbpassword)
+        standalone_datasources_elm.appendChild(datasource)
+# Assuming PostgreSQL driver has been added by init_standalone.py for VIPSLogic
+
+# Write to file
+outputfile = open(path + "/standalone.xml", "w")
+outputfile.write(standalone_dom.toxml())
+outputfile.close()
diff --git a/wildfly_config/vipscoremanager_datasource.xml b/wildfly_config/vipscoremanager_datasource.xml
new file mode 100644
index 0000000..f4cbc0c
--- /dev/null
+++ b/wildfly_config/vipscoremanager_datasource.xml
@@ -0,0 +1,19 @@
+<datasources>
+                <datasource jta="true" jndi-name="java:jboss/datasources/vipscoremanager" pool-name="Postgres-vipscoremanager-pool" enabled="true" use-java-context="true" use-ccm="true">
+                    <connection-url>jdbc:postgresql://localhost:5432/vipscoremanager</connection-url>
+                    <driver>postgresql</driver>
+                <pool>
+                        <min-pool-size>10</min-pool-size>
+                        <max-pool-size>90</max-pool-size>
+                        <prefill>true</prefill>
+                </pool>
+                    <security>
+                        <user-name>coremanager</user-name>
+                        <password>XXXXXXXXXX</password>
+                    </security>
+
+                </datasource>
+
+
+            </datasources>
+
diff --git a/wildfly_config/vipscoremanager_system_properties.xml b/wildfly_config/vipscoremanager_system_properties.xml
new file mode 100644
index 0000000..125aa50
--- /dev/null
+++ b/wildfly_config/vipscoremanager_system_properties.xml
@@ -0,0 +1,5 @@
+<system-properties>
+	<property name="no.nibio.vips.coremanager.VIPSCORE_URL" value="http://localhost:8080/VIPSCore"/>
+	<property name="no.nibio.vips.coremanager.VIPSBATCH_ALLOWED_IPS" value="127.0.0.1"/>
+        <property name="no.nibio.vips.coremanager.MD5_SALT" value="XXXX"/>
+</system-properties>
-- 
GitLab