Commit 7459a04a authored by Piotr Gawron's avatar Piotr Gawron
Browse files

debian installer uses dbconfig for database configuration

parent b449bbc7
......@@ -2,6 +2,8 @@ minerva (14.0.0~alpha.0) unstable; urgency=low
* Feature: log4j is replaced with log4j2 logging mechanism (#291)
* Feature removal: BioCompendium annotator removed (#32)
-- Piotr Gawron <piotr.gawron@uni.lu> Thu, 16 May 2019 15:00:00 +0200
minerva (13.1.0~alpha.0) unstable; urgency=low
* Feature: annotators are more flexible - you can define set of input and
outputs used by annotator (#617)
......
......@@ -45,6 +45,8 @@ gzip -n $SRC_DIR/minerva.txt
#copy configuration of logrotate
cp debian/logrotate/* $SRC_DIR/
touch $SRC_DIR/pgsql
cp debian/scripts/common.sh $SRC_DIR/
#copy changelog file
......@@ -60,13 +62,6 @@ current_version=`cat CHANGELOG |grep minerva |head -1 | cut -f2 -d'(' | cut -f1
echo "Current version: " $current_version
echo "Flyway was introduced in version: " $MAX_DB_VERSION_FOR_MIGRTION
#default connection params (if /etc/minerva/db.properties is not defined)
DB_HOST=`cat persist/src/main/resources/db.properties |grep "uri" |cut -f3 -d"/" |cut -f1 -d":"`
DB_PORT=`cat persist/src/main/resources/db.properties |grep "uri" |cut -f3 -d"/" |cut -f2 -d":"`
DB_DATABASE_NAME=`cat persist/src/main/resources/db.properties |grep "uri" |cut -f4 -d"/"`
DB_USERNAME=`cat persist/src/main/resources/db.properties |grep "username" |cut -f2 -d"="`
DB_PASSWORD=`cat persist/src/main/resources/db.properties |grep "password" |cut -f2 -d"="`
#------------------------------------------
# Now we have all db schema diff files. Let's start preparing
# debian package
......@@ -101,11 +96,6 @@ sed -i "s/__LOG_FILE__/$LOG_FILE/g" common.sh
#in the filesystem where package is installed
sed -i "s/__DB_SCRIPT_DIR__/$DB_SCRIPT_DIR/g" common.sh
sed -i "s/__DB_HOST__/$DB_HOST/g" common.sh
sed -i "s/__DB_PORT__/$DB_PORT/g" common.sh
sed -i "s/__DB_DATABASE_NAME__/$DB_DATABASE_NAME/g" common.sh
sed -i "s/__DB_USERNAME__/$DB_USERNAME/g" common.sh
sed -i "s/__DB_PASSWORD__/$DB_PASSWORD/g" common.sh
sed -i "s/__MAX_DB_VERSION_FOR_MIGRTION__/$MAX_DB_VERSION_FOR_MIGRTION/g" common.sh
sed -i -e "1r common.sh" debian/postinst
......
......@@ -31,74 +31,3 @@ fi
POSTGRES_OK=$(dpkg-query -W --showformat='${Status}\n' postgresql|grep "install ok installed")
DB_HOST="__DB_HOST__"
DB_PORT="__DB_PORT__"
DB_DATABASE_NAME="__DB_DATABASE_NAME__"
DB_USERNAME="__DB_USERNAME__"
DB_PASSWORD="__DB_PASSWORD__"
if [ -f /etc/minerva/db.properties ]; then
DB_PROPERTIES_FILE="/etc/minerva/db.properties"
log "$DB_PROPERTIES_FILE file found"
DB_HOST=`cat $DB_PROPERTIES_FILE |grep "uri" |cut -f3 -d"/" |cut -f1 -d":"`
DB_PORT=`cat $DB_PROPERTIES_FILE |grep "uri" |cut -f3 -d"/" |cut -f2 -d":"`
DB_DATABASE_NAME=`cat $DB_PROPERTIES_FILE |grep "uri" |cut -f4 -d"/"`
DB_USERNAME=`cat $DB_PROPERTIES_FILE |grep "username" |cut -f2 -d"="`
DB_PASSWORD=`cat $DB_PROPERTIES_FILE |grep "password" |cut -f2 -d"="`
fi
#if we connect to something that is not in localhost then we need to provide login and password
#because we won't have access to it as postgres user
if [ "$DB_HOST" != "localhost" ] && [ "$DB_HOST" != "127.0.0.1" ]
then
log "DB is located on the remote server: $DB_HOST"
IS_REMOTE_DB=true
else
log "DB is at localhost: $DB_HOST"
IS_REMOTE_DB=false
fi
exec_sql(){
log "Execute SQL : '$1'"
echo "$1" |PGPASSWORD=$DB_PASSWORD psql -h $DB_HOST -p $DB_PORT -U $DB_USERNAME $DB_DATABASE_NAME >>$LOG_FILE 2>>$LOG_FILE
}
exec_sql_file(){
log "Execute SQL file: $1"
PGPASSWORD=$DB_PASSWORD psql -h $DB_HOST -p $DB_PORT -U $DB_USERNAME $DB_DATABASE_NAME -f $1 >>$LOG_FILE 2>>$LOG_FILE
}
create_db(){
log "Create db: (remote=$IS_REMOTE_DB)"
if [ $IS_REMOTE_DB = false ]
then
su - postgres -c "createuser -d -r -s -p $DB_PORT $DB_USERNAME" >>$LOG_FILE 2>>$LOG_FILE
log "User created"
su - postgres -c "echo \"ALTER USER $DB_USERNAME WITH PASSWORD '$DB_PASSWORD';\"| psql -p $DB_PORT " >>$LOG_FILE 2>>$LOG_FILE
log "User credentials updated"
su - postgres -c "createdb -p $DB_PORT -O $DB_USERNAME $DB_DATABASE_NAME" >>$LOG_FILE 2>>$LOG_FILE
log "Db created"
hba_conf=`su - postgres -c "psql -t -P format=unaligned -c 'show hba_file';"`;
cp $hba_conf $hba_conf".bac"
cat $hba_conf".bac" |grep -v "all[ \t]*peer" >$hba_conf
printf "local all all md5\n" >>$hba_conf
invoke-rc.d postgresql restart
else
log "HOST is remote, assuming db and user are already there"
fi
}
stop_postgres() {
log "Stop postgres: (remote=$IS_REMOTE_DB)"
if [ $IS_REMOTE_DB = false ]
then
invoke-rc.d postgresql stop || true
fi
}
start_postgres() {
log "Start postgres: (remote=$IS_REMOTE_DB)"
if [ $IS_REMOTE_DB = false ]
then
invoke-rc.d postgresql restart
fi
}
#!/bin/sh
set -e
. /usr/share/debconf/confmodule
db_version 2.0
if [ -f /usr/share/dbconfig-common/dpkg/config.pgsql ]; then
. /usr/share/dbconfig-common/dpkg/config.pgsql
dbc_go minerva $@
fi
......@@ -13,4 +13,4 @@ Description: Standalone web application for visualization, exploration and
Tomcat server later to change the address.
Section: science
Homepage: http://r3lab.uni.lu/web/minerva-website/
Depends: ${misc:Depends}, tomcat7 | tomcat8, postgresql (>=9.3)
Depends: ${misc:Depends}, dbconfig-pgsql, tomcat7 | tomcat8, postgresql (>=9.3)
minerva.war /usr/share/minerva/
pgsql /usr/share/dbconfig-common/data/minerva/install/
......@@ -16,29 +16,52 @@
log "Running postinst" $1 $2;
invoke-rc.d $TOMCAT_PACKAGE status >> $LOG_FILE
mkdir /etc/minerva 2>/dev/null
. /usr/share/debconf/confmodule
db_version 2.0
# we execute it here because if some packages are missing then dpkg will return non-zero exit code
set -e
case "$1" in
configure)
start_postgres
#if we install
if [ "$OLD_VERSION" = "" ]
then
create_db
#if we update the package
if [ -f $DB_SCRIPT_DIR/db_${OLD_VERSION}_to_${MAX_DB_VERSION_FOR_MIGRTION}.sql ]; then
cp $DB_SCRIPT_DIR/db_${OLD_VERSION}_to_${MAX_DB_VERSION_FOR_MIGRTION}.sql /usr/share/dbconfig-common/data/minerva/install/pgsql
else
#if we update the package
if [ -f $DB_SCRIPT_DIR/db_${OLD_VERSION}_to_${MAX_DB_VERSION_FOR_MIGRTION}.sql ]; then
exec_sql_file "$DB_SCRIPT_DIR/db_${OLD_VERSION}_to_${MAX_DB_VERSION_FOR_MIGRTION}.sql"
else
log "INFO: File doesn't exist: $DB_SCRIPT_DIR/db_${OLD_VERSION}_to_${MAX_DB_VERSION_FOR_MIGRTION}.sql"
fi
log "INFO: File doesn't exist: $DB_SCRIPT_DIR/db_${OLD_VERSION}_to_${MAX_DB_VERSION_FOR_MIGRTION}.sql"
fi
#we have to restart tomcat, because application was started on the old database and it might crash during deplyment
#for instance in the new war file there was init script that uses some new database features, but they are not
#availble when new war is deployed from debian package, so tomcat will catch SQL exception and stop deployment
. /usr/share/dbconfig-common/dpkg/postinst.pgsql
dbc_generate_include_owner="root:root"
dbc_generate_include_perms="0640"
dbc_generate_include=sh:/etc/minerva/db.sh
# Need to start postgresql, so it doesn't fail on the installer
invoke-rc.d --force postgresql restart || true
dbc_go minerva $@
. /etc/minerva/db.sh
if [ -z "$dbport" ]
then
dbport=5432
fi
#create db configuration file
echo "database.uri=jdbc:postgresql://$dbserver:$dbport/$dbname" > /etc/minerva/db.properties
echo "database.username=$dbuser" >> /etc/minerva/db.properties
echo "database.password=$dbpass" >> /etc/minerva/db.properties
rm /etc/minerva/db.sh
#we have to restart tomcat, because application was started on the old database and it might crash during deployment
#for instance in the new war file there was init script that uses some new database features, but they are not
#available when new war is deployed from debian package, so tomcat will catch SQL exception and stop deployment
invoke-rc.d $TOMCAT_PACKAGE stop || true
rm -rf /var/lib/$TOMCAT_PACKAGE/webapps/minerva
ln -sf /usr/share/minerva/minerva.war /var/lib/$TOMCAT_PACKAGE/webapps/minerva.war
......
......@@ -13,6 +13,12 @@ log "Running postrm" $1 $2;
# we execute it here because if some packages are missing then dpkg will return non-zero exit code
set -e
if [ -f /usr/share/dbconfig-common/dpkg/postrm.pgsql ]; then
. /usr/share/dbconfig-common/dpkg/postrm.pgsql
dbc_go minerva $@
fi
case "$1" in
upgrade)
rm -rf /var/lib/$TOMCAT_PACKAGE/webapps/minerva
......@@ -22,35 +28,9 @@ case "$1" in
rm -rf /var/lib/$TOMCAT_PACKAGE/webapps/minerva
rm -rf /var/lib/$TOMCAT_PACKAGE/webapps/minerva.war
invoke-rc.d $TOMCAT_PACKAGE start || true
if [ $IS_REMOTE_DB = true ]
then
exec_sql "drop owned by $DB_USERNAME;"
else
if [ "" = "$POSTGRES_OK" ]
then
log "No postgresql package found.";
else
start_postgres
su - postgres -c "dropdb -p $DB_PORT $DB_DATABASE_NAME"
su - postgres -c "dropuser -p $DB_PORT $DB_USERNAME"
fi
fi
;;
abort-install)
invoke-rc.d $TOMCAT_PACKAGE start || true
if [ $IS_REMOTE_DB = true ]
then
exec_sql "drop owned by $DB_USER"
else
if [ "" = "$POSTGRES_OK" ];
then
log "No postgresql package found.";
else
start_postgres
su - postgres -c "dropdb -p $DB_PORT $DB_DATABASE_NAME"
su - postgres -c "dropuser -p $DB_PORT $DB_USERNAME"
fi
fi
;;
*)
echo "postrm called with unknown argument \`$1'" >&2
......
......@@ -17,11 +17,9 @@ set -e
case "$1" in
install|upgrade)
invoke-rc.d $TOMCAT_PACKAGE stop || true
stop_postgres
;;
abort-upgrade)
invoke-rc.d $TOMCAT_PACKAGE stop || true
stop_postgres
;;
*)
echo "preinst called with unknown argument \`$1'" >&2
......
......@@ -11,26 +11,20 @@ set -e
log "Running preinst" $1 $2;
# we execute it here because if some packages are missing then dpkg will return non-zero exit code
set -e
log Running prerm $1 $2;
invoke-rc.d postgresql start || true
TIMESTAMP=$(date +"%F_%H%M%S")
DUMP_FILE=$DB_SCRIPT_DIR/dump_$TIMESTAMP
log "dump $DB_DATABASE_NAME database to file $DUMP_FILE"
PGPASSWORD=$DB_PASSWORD pg_dump -h $DB_HOST -p $DB_PORT -U $DB_USERNAME $DB_DATABASE_NAME | gzip > "$DUMP_FILE".gz
. /usr/share/debconf/confmodule
. /usr/share/dbconfig-common/dpkg/prerm.pgsql
dbc_go minerva $@
case "$1" in
upgrade)
invoke-rc.d $TOMCAT_PACKAGE stop || true
stop_postgres
;;
remove)
invoke-rc.d $TOMCAT_PACKAGE stop || true
stop_postgres
;;
*)
echo "prerm called with unknown argument \`$1'" >&2
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment