X-Git-Url: https://gerrit.onap.org/r/gitweb?p=holmes%2Fengine-management.git;a=blobdiff_plain;f=engine-d-standalone%2Fsrc%2Fmain%2Fassembly%2Fbin%2FinitDB.sh;h=f0c3713623f97ef43ade28dd81467ffc0a59ba1a;hp=7f16c2a5f8b63c9fe6d42ccf333a0678c76d90d8;hb=5aac9dc424bf07b2d641667afae7c34c35505595;hpb=9c14e8bc2360c0098a23486ea71fc5c99e2fd64e diff --git a/engine-d-standalone/src/main/assembly/bin/initDB.sh b/engine-d-standalone/src/main/assembly/bin/initDB.sh index 7f16c2a..f0c3713 100644 --- a/engine-d-standalone/src/main/assembly/bin/initDB.sh +++ b/engine-d-standalone/src/main/assembly/bin/initDB.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/sh # # Copyright 2017 ZTE Corporation. # @@ -19,16 +19,53 @@ DIRNAME=`dirname $0` HOME=`cd $DIRNAME/; pwd` user=$1 password=$2 -port=$3 -host=$4 -echo "start init holmes engine-d db" -main_path=$HOME/../ -mysql -u$user -p$password -P$port -h$host <$main_path/dbscripts/mysql/openo-holmes_engine_d-createobj.sql +dbname=$3 +port=$4 +host=$5 +echo "Initializing the holmes engine management database..." +main_path=$HOME/.. + +# if deployed using helm, use the helm-generated configuration file. +if [ -d /opt/hemconfig ]; then + cp /opt/hemconfig/onap-holmes_engine-createobj.sql "$main_path/dbscripts/postgresql/onap-holmes_engine-createobj.sql" +else + sed -i "s|DBNAME|$dbname|g" "$main_path/dbscripts/postgresql/onap-holmes_engine-createobj.sql" + sed -i "s|DBUSER|$user|g" "$main_path/dbscripts/postgresql/onap-holmes_engine-createobj.sql" + sed -i "s|DBPWD|$password|g" "$main_path/dbscripts/postgresql/onap-holmes_engine-createobj.sql" +fi + +cat $main_path/dbscripts/postgresql/onap-holmes_engine-createobj.sql + +echo "dbname=$dbname" +echo "user=$user" +echo "password=$password" +echo "port=$port" +echo "host=$host" + +if [ -z `env | grep PGPASSWORD` ]; then + export PGPASSWORD=$password + need_unset=1 +fi + +# Escape the single quote to avoid cause errors when executing the scripts. +password=`sed -rn "s/.+ ('.+') .+/\1/p" $main_path/dbscripts/postgresql/onap-holmes_engine-createobj.sql` +password=${password:1:-1} +password_e=`echo $password | sed "s#'#''#g"` +sed -i "s#$password#$password_e#" $main_path/dbscripts/postgresql/onap-holmes_engine-createobj.sql + +psql -U "$user" -p "$port" -h "$host" -d "$dbname" -f $main_path/dbscripts/postgresql/onap-holmes_engine-createobj.sql +psql -U "$user" -p "$port" -h "$host" -d "$dbname" --command 'select * from alarm_info;' 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 engine-d database!" + echo "Failed to initialize the database!" exit 1 fi -echo "init engine-d success!" +echo "The database is initialized successfully!" exit 0