bugfix - rule creation failure on GUI
[holmes/rule-management.git] / rulemgt-standalone / src / main / assembly / bin / initDB.sh
index 60b1f77..86a2d80 100644 (file)
@@ -1,4 +1,4 @@
-#!/bin/bash
+#!/bin/sh
 #
 # Copyright 2017 ZTE Corporation.
 #
@@ -22,23 +22,41 @@ password=$2
 dbname=$3
 port=$4
 host=$5
-echo "start init holmes rulemgt db"
+echo "Initializing the holmes rule management database..."
 main_path=$HOME/..
+
+if [ -d /opt/hrmconfig ]; then
+    cp /opt/hrmconfig/onap-holmes_rulemgt-createobj.sql "$main_path/dbscripts/postgresql/onap-holmes_rulemgt-createobj.sql"
+else
+    sed -i "s|DBNAME|$dbname|g" "$main_path/dbscripts/postgresql/onap-holmes_rulemgt-createobj.sql"
+    sed -i "s|DBUSER|$user|g" "$main_path/dbscripts/postgresql/onap-holmes_rulemgt-createobj.sql"
+    sed -i "s|DBPWD|$password|g" "$main_path/dbscripts/postgresql/onap-holmes_rulemgt-createobj.sql"
+fi
+
 cat $main_path/dbscripts/postgresql/onap-holmes_rulemgt-createobj.sql
 echo "dbname=$dbname"
 echo "user=$user"
 echo "password=$password"
 echo "port=$port"
 echo "host=$host"
-export PGPASSWORD=$password
-psql -U $user -p $port -h $host -f $main_path/dbscripts/postgresql/onap-holmes_rulemgt-createobj.sql
-psql -U $user -p $port -h $host -d $dbname --command 'select * from aplus_rule;'
+
+if [ -z `env | grep PGPASSWORD` ]; then
+    export PGPASSWORD=$password
+    need_unset=1
+fi
+
+psql -U "$user" -p "$port" -h "$host" -d "$dbname" -f $main_path/dbscripts/postgresql/onap-holmes_rulemgt-createobj.sql
+psql -U "$user" -p "$port" -h "$host" -d "$dbname" --command 'select * from aplus_rule;'
 sql_result=$?
-unset PGPASSWORD
-cat "sql_result="$sql_result
+
+if [ "$need_unset"x = "1"x ]; then
+    unset PGPASSWORD
+fi
+
+echo "sql_result=$sql_result"
 if [ $sql_result != 0 ] ; then
-   echo "failed to init rulemgt database!"
+   echo "Failed to initialize the database!"
    exit 1
 fi
-echo "init rulemgt success!"
+echo "The database is initialized successfully!"
 exit 0