From c719d43009264c6ded0dd47331a54bd355210249 Mon Sep 17 00:00:00 2001
From: Nils Christian <nils.christian@ittm-solutions.com>
Date: Wed, 1 Aug 2018 15:53:39 +0200
Subject: [PATCH] remove unneeded shell scripting from RPM; adjust RPM
 installation instruction

---
 rpm/INSTALL.rst     | 76 ++++++++++++++++++++++-----------------------
 rpm/buildrpm.sh     | 40 ++++++++++++------------
 rpm/minerva.spec.in | 57 +---------------------------------
 3 files changed, 60 insertions(+), 113 deletions(-)

diff --git a/rpm/INSTALL.rst b/rpm/INSTALL.rst
index 11116e0551..c42029bb26 100644
--- a/rpm/INSTALL.rst
+++ b/rpm/INSTALL.rst
@@ -3,15 +3,9 @@ MINERVA installation instructions using RPM Package Manager
 
 These instructions guide you through the installation process of
 `MINERVA <https://git-r3lab.uni.lu/piotr.gawron/minerva>`_ on Red Hat
-Enterprise Linux 7 or CentOS 7. The server will also host the database
-required for the functioning of MINERVA.
-
-.. note::
-
-   Content visualization of MINERVA platform is supported by Google
-   Maps API. Users of MINERVA platform are obliged to comply with the
-   `Google Maps/Google Earth APIs Terms of Service
-   <https://www.google.com/intl/en-US_US/help/terms_maps.html>`_.
+Enterprise Linux 7 or CentOS 7. In these instructions the server will
+also host the database required for the functioning of MINERVA, but
+using a different server is possible as well.
 
 
 Overview
@@ -32,12 +26,13 @@ Install PostgreSQL and initialise it with
 
 .. code:: shell
 
-    yum install -y postgresql-server
-    postgresql-setup initdb
+    yum install -y https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm
+    yum install -y postgresql96-server
+    /usr/pgsql-9.6/bin/postgresql96-setup initdb
 
 Ensure that the database authentication on IPv4 on ``localhost`` is
 done with md5-based passwords by adding the following line to
-``/var/lib/pgsql/data/pg_hba.conf``
+``/var/lib/pgsql/9.6/data/pg_hba.conf``
 
 .. code::
 
@@ -48,33 +43,26 @@ Enable and start postgresql
 
 .. code:: shell
 
-    systemctl enable postgresql
-    systemctl start postgresql
+    systemctl enable postgresql-9.6
+    systemctl start postgresql-9.6
 
 Create the MINERVA database user and the database
 
 .. code:: shell
 
     su - postgres -c "createuser -d -r -s map_viewer"
-    su - postgres -c "echo \"ALTER USER map_viewer WITH PASSWORD '123qweasdzxc';\"| psql"
+    su - postgres -c "echo \"ALTER USER map_viewer WITH PASSWORD 'yourSecretPasswordHere';\"| psql"
     su - postgres -c "createdb -O map_viewer map_viewer"
 
-.. warning::
-
-    Currently the password for the database user ``map_viewer`` is
-    hardcoded, therefore make sure the database cannot be accessed
-    from distrusted hosts.
-
 
 Apache Tomcat
 -------------
 
-Install and enable (don't start yet) Apache Tomcat with
+Install Apache Tomcat with
 
 .. code:: shell
 
     yum install -y tomcat
-    systemctl enable tomcat
 
 In ``/etc/sysconfig/tomcat``, adjust the memory settings, e.g.
 
@@ -91,30 +79,42 @@ connections, e.g. using ``firewalld`` this can be accomplished with
 
 .. warning::
 
-    Currently the administrator password of MINERVA is hardcoded,
-    therefore make sure the MINERVA cannot be accessed from distrusted
-    hosts until the password was changed.
+    Currently the initial administrator password of MINERVA is
+    hardcoded, therefore make sure the MINERVA cannot be accessed from
+    distrusted hosts until the password was changed.
+
+Enable and start Tomcat
+
+.. code:: shell
+
+    systemctl enable tomcat
+    systemctl start tomcat
+
 
 MINERVA
 -------
 
-Install MINERVA using ``yum``
+Create a configuration file at ``/etc/minerva/db.properties`` so
+MINERVA knows how to connect to the database:
 
-.. code:: shell
+.. code::
 
-    yum install -y minerva-X.Y.Z-1.el7.centos.noarch.rpm
+    database.uri=jdbc:postgresql://localhost:5432/map_viewer
+    database.username=map_viewer
+    database.password=yourSecretPasswordHere
 
-and start tomcat
+Install MINERVA with
 
 .. code:: shell
 
-    systemctl start tomcat
-
-Now point your browser to the newly installed service, e.g. on a local
-network this could be `<http://192.168.0.42:8080/minerva/>`_.
+    yum install -y minerva-X.Y.Z-1.el7.noarch.rpm
 
-.. warning::
+Tomcat will automatically deploy the MINERVA web archive, and after a
+short time it will have initialised the database and MINERVA will be
+running. Point point your browser to the newly installed service,
+e.g. on a local network this could be
+`<http://192.168.0.42:8080/minerva/>`_.
 
-    This way of installing MINERVA does **not** ensure that security
-    updates are installed with ``yum update``, therefore you have to
-    establish a process to install such updates manually.
+Login with the username ``admin`` and the password ``admin``. Click on
+the ``INFO`` tab in the left panel, and click on ``MANUAL`` to get
+more information about administrating and using MINERVA.
diff --git a/rpm/buildrpm.sh b/rpm/buildrpm.sh
index eb3166272a..e425be4ede 100755
--- a/rpm/buildrpm.sh
+++ b/rpm/buildrpm.sh
@@ -4,6 +4,27 @@ set -e
 
 MINERVA_SRC_DIR="$(dirname "$(dirname "$0")")"
 
+# file that should be deployed on tomcat; if an argument is given to this script, use it as war-file
+TOMCAT_FILE="${1:-$MINERVA_SRC_DIR/web/target/web-1.0.war}"
+
+endswith() { case $2 in *"$1") true;; *) false;; esac; }
+
+if ! endswith ".war" "$TOMCAT_FILE"; then
+    echo "first argument should be a file ending in '.war'"
+    exit -1
+fi
+if [ ! -f "$TOMCAT_FILE" ]; then
+    echo "file does not exist: $TOMCAT_FILE"
+    exit -1
+fi
+
+# get the version number from the changelog in war file
+current_version=$(unzip -p $TOMCAT_FILE CHANGELOG | head -1 | sed 's/^minerva (\(.*\)).*/\1/')
+if [ -z "$current_version" ]; then
+    echo "could not extract version number from CHANGELOG in $TOMCAT_FILE"
+    exit -1
+fi
+
 # where generated files will be written
 RPMBUILD_TEMP="${RPMBUILD_TEMP:-$MINERVA_SRC_DIR/rpm/rpmbuildtemp}"
 
@@ -17,18 +38,6 @@ fi
 # current date (for automatic changelog)
 CURDATE=$(date +"%a %b %d %Y")
 
-# file that should be deployed on tomcat
-TOMCAT_FILE="$MINERVA_SRC_DIR/web/target/web-1.0.war"
-
-# file with bash code generating DB schema diffs for different versions
-dbschemadiff="$MINERVA_SRC_DIR/management_scripts/db_schema_and_diff.sh"
-
-# directory with database schema
-DB_SCHEMA_DIR="$MINERVA_SRC_DIR/persist/src/db"
-
-# destination directory for database upgrade scripts
-DBSCRIPT_DEST_DIR="$RPMBUILD_TEMP/BUILD/sql"
-
 # clean build directories
 rm -rf "$RPMBUILD_TEMP"
 
@@ -39,13 +48,6 @@ mkdir -p "$RPMBUILD_TEMP/"{BUILD,SPECS}
 cp "$TOMCAT_FILE" "$RPMBUILD_TEMP/BUILD/minerva.war"
 cp "$MINERVA_SRC_DIR/README.md" "$MINERVA_SRC_DIR/CHANGELOG" "$MINERVA_SRC_DIR/rpm/INSTALL.rst" "$MINERVA_SRC_DIR/rpm/logrotate_minerva" "$RPMBUILD_TEMP/BUILD"
 
-mkdir -p "$DBSCRIPT_DEST_DIR"
-# copy base sql schema
-cp "$DB_SCHEMA_DIR/base.sql" "$DBSCRIPT_DEST_DIR/db_0.sql"
-
-# set $versions and $current_version; generate upgrade scripts in DBSCRIPT_DEST_DIR
-source "$dbschemadiff"
-
 # create RPM spec file
 cp "$MINERVA_SRC_DIR/rpm/minerva.spec.in" "$RPMBUILD_TEMP/SPECS/minerva.spec"
 sed -i "s/__CURRENT_VERSION__/$current_version/g" "$RPMBUILD_TEMP/SPECS/minerva.spec"
diff --git a/rpm/minerva.spec.in b/rpm/minerva.spec.in
index 99556fd49b..555ba6d81f 100644
--- a/rpm/minerva.spec.in
+++ b/rpm/minerva.spec.in
@@ -15,69 +15,15 @@ Web application for visualization, exploration and management of
 molecular networks encoded in SBGN-compliant format. The resource is
 used and managed via a webbrowser.
 
-%pre -p /bin/bash
-# check that postgres is started and tomcat is stopped, otherwise abort installation/upgrade
-if ! $(systemctl -q is-active postgresql); then
-    echo "postgresql not running, aborting installation/upgrade of minerva" >&2
-    exit -1
-fi
-if $(systemctl -q is-active tomcat); then
-    echo "tomcat is running, aborting installation/upgrade of minerva" >&2
-    exit -1
-fi
-# make sure the user `map_viewer` has the required permissions
-attributes_map_viewer=$(su - postgres -c "psql -F $'\t' --no-align -t -c '\du map_viewer'" | cut -f 2)
-if ! [[ $attributes_map_viewer == *"Superuser"* ]] || ! [[ $attributes_map_viewer == *"Create role"* ]] || ! [[ $attributes_map_viewer == *"Create DB"* ]]; then
-    echo "Postgres user map_viewer does not exist or does not have the required attributes." >&2
-    echo "Make sure to create map_viewer with the permission 'Superuser, Create role, Create DB'." >&2
-    echo "Attributes are: '$attributes_map_viewer'" >&2
-    echo "Aborting installation/upgrade of minerva." >&2
-    exit -1
-fi
-# make sure the database map_viewer exists
-if ! su - postgres -c "psql --dbname map_viewer -q -c '\q'" 2> /dev/null; then
-    echo "Database map_viewer does not exist, aborting installation/upgrade of minerva." >&2
-    exit -1
-fi
-
 %preun -p /bin/bash
 if [ $1 == 1 ]; then
-    # save version information of the old package to make sure we run the correct DB-update script after installation
-    mkdir -p %{_sharedstatedir}/rpm-state/%{name}
-    echo -n %{version} > %{_sharedstatedir}/rpm-state/%{name}/OLD_VERSION
     # remove old unpacked ("exploded") directory of war file
     rm -rf %{_sharedstatedir}/tomcat/webapps/%{name}
 fi
 
-%posttrans -p /bin/bash
-LOGFILE=%{_localstatedir}/log/minerva-install.log
-if [ -e %{_sharedstatedir}/rpm-state/%{name}/OLD_VERSION ]; then
-    OLD_VERSION=$(cat %{_sharedstatedir}/rpm-state/%{name}/OLD_VERSION)
-    echo $(date) "Upgrading minerva from $OLD_VERSION to %{version}" >> $LOGFILE
-    if [ ! -e %{_datadir}/%{name}/schema/db_${OLD_VERSION}_to_%{version}.sql ]; then
-        echo "ERROR: missing DB-update script %{_datadir}/%{name}/schema/db_${OLD_VERSION}_to_%{version}.sql" >&2
-        exit -1
-    fi
-    su - postgres -c "psql map_viewer -f %{_datadir}/%{name}/schema/db_${OLD_VERSION}_to_%{version}.sql" >> $LOGFILE 2>&1
-    rm -f %{_sharedstatedir}/rpm-state/%{name}/OLD_VERSION
-    rmdir %{_sharedstatedir}/rpm-state/%{name}
-else
-    echo $(date) "Installing minerva %{version}"  >> $LOGFILE
-    if [ $(su - postgres -c "psql --dbname map_viewer -F $'\t' --no-align -t -c '\dt'" | wc -l) -gt 51 ]; then
-        echo "The database map_viewer is not empty, aborting initialisation of database." >&2
-        echo "The database map_viewer is not empty, aborting initialisation of database." >> $LOGFILE
-        exit -1
-    fi
-    # install base version of the framework
-    su - postgres -c "psql map_viewer -f %{_datadir}/%{name}/schema/db_0.sql" >> $LOGFILE 2>&1
-    # install patch to current version
-    su - postgres -c "psql map_viewer -f %{_datadir}/%{name}/schema/db_0_to_%{version}.sql" >> $LOGFILE 2>&1
-fi
-
 %install
 rm -rf %{buildroot}
-mkdir -p %{buildroot}/%{_datadir}/%{name}/schema
-install -m 0644 sql/*.sql %{buildroot}/%{_datadir}/%{name}/schema
+mkdir -p %{buildroot}/%{_datadir}/%{name}
 install -m 0644 %{name}.war %{buildroot}/%{_datadir}/%{name}/%{name}.war
 mkdir -p %{buildroot}/%{_sysconfdir}/logrotate.d
 install -m 0644 logrotate_minerva %{buildroot}/%{_sysconfdir}/logrotate.d/minerva
@@ -88,7 +34,6 @@ ln -s %{_datadir}/%{name}/%{name}.war %{buildroot}/%{_sharedstatedir}/tomcat/web
 %defattr(-,root,root,-)
 %doc README.md CHANGELOG INSTALL.rst
 %{_datadir}/%{name}/%{name}.war
-%{_datadir}/%{name}/schema/*
 %config(noreplace) %{_sysconfdir}/logrotate.d/minerva
 %attr(-,tomcat,tomcat) %{_sharedstatedir}/tomcat/webapps/%{name}.war
 
-- 
GitLab