zoneminder/distros/debian8/zoneminder-database.postinst

143 lines
4.9 KiB
Plaintext
Raw Normal View History

#! /bin/sh
# postinst maintainer script for zoneminder-db package
2014-12-02 07:58:18 +08:00
2014-12-12 20:04:17 +08:00
set -e
2014-12-03 20:40:30 +08:00
# Source the debconf stuff
. /usr/share/debconf/confmodule
2014-12-12 20:04:17 +08:00
# Source the config file if exists
2014-12-03 20:40:30 +08:00
CONFIGFILE=/etc/zm/zm.conf
2014-12-12 20:04:17 +08:00
if [ -e $CONFIGFILE ]; then
. $CONFIGFILE
fi
mysql_update() {
2014-12-07 04:39:50 +08:00
2014-12-02 07:58:18 +08:00
# Source the dbconfig stuff
. /usr/share/dbconfig-common/internal/mysql
2014-12-01 00:55:26 +08:00
2014-12-08 23:57:37 +08:00
# Update the password of the hard-coded default 'admin' account
test -z $ADMIN_PASSWORD || dbc_mysql_exec_command "UPDATE Users SET Password = password('$ADMIN_PASSWORD') WHERE Username = 'admin';" || true
# Update the database version
dbc_mysql_exec_command "UPDATE Config SET Value = '$DB_VERSION' WHERE Name = 'ZM_DYN_DB_VERSION';" || true
2014-12-01 00:55:26 +08:00
}
2014-12-02 07:58:18 +08:00
if [ -f /usr/share/dbconfig-common/dpkg/postinst ]; then
# Set the first version in which dbconfig-common was introduced in the package
dbc_first_version="1.28.0"
# Set the database type
2014-12-12 20:04:17 +08:00
dbc_dbtypes="mysql"
2014-12-02 07:58:18 +08:00
# Source the dbconfig-common stuff
. /usr/share/dbconfig-common/dpkg/postinst
fi
2014-12-12 20:04:17 +08:00
# Do this when the package is installed, upgraded or reconfigured
if [ "$1" = "configure" ] || [ "$1" = "reconfigure" ]; then
2014-12-02 07:58:18 +08:00
# Install sql database create file for dbconfig
# (needed at first package installation)
if [ ! -f /usr/share/dbconfig-common/data/zoneminder/install/mysql ]; then
install -m 644 /usr/share/zoneminder/db/zm_create.sql \
/usr/share/dbconfig-common/data/zoneminder/install/mysql
# Remove unneeded sql requests
# dbconfig will create the underlying database
sed -i "/^ *CREATE DATABASE /d" \
/usr/share/dbconfig-common/data/zoneminder/install/mysql
sed -i "/^ *USE /d" \
/usr/share/dbconfig-common/data/zoneminder/install/mysql
fi
# Symlink sql update files for dbconfig (needed when upgrading the package)
for sqlfile in /usr/share/zoneminder/db/zm_update-*.sql; do
lnk=`echo $sqlfile | sed "s/^\/usr\/share\/zoneminder\/db\/zm_update-\(.*\)\.sql/\1/"`
if [ ! -L /usr/share/dbconfig-common/data/zoneminder/upgrade/mysql/$lnk ]; then
ln -sf $sqlfile \
/usr/share/dbconfig-common/data/zoneminder/upgrade/mysql/$lnk
fi
done || true
2014-12-02 07:58:18 +08:00
# Create the underlying database and populate it
# dbconfig will take care of applying any updates which are newer than the
# previously installed version
dbc_go zoneminder $@
2014-12-08 23:57:37 +08:00
# Get the password of ZoneMinder user 'admin' from debconf
2014-12-02 07:58:18 +08:00
db_get zoneminder/admin_password
2014-12-08 23:57:37 +08:00
ADMIN_PASSWORD=$RET
2014-12-12 20:04:17 +08:00
# Remove the password from debconf database
test -z $ADMIN_PASSWORD || db_reset zoneminder/admin_password || true
2014-12-02 07:58:18 +08:00
# Get the lastest database version from dbconfig upgrade folder
DB_VERSION=$(ls -rv /usr/share/dbconfig-common/data/zoneminder/upgrade/$dbc_dbtypes | head -1)
# Update the default admin account and database version
mysql_update
2014-12-02 07:58:18 +08:00
2014-12-08 23:57:37 +08:00
# Retrieve data from dbconfig (inputs from user)
. /etc/dbconfig-common/zoneminder.conf
# ZoneMinder config file handling
# Inspired by: http://manpages.debian.org/cgi-bin/man.cgi?query=debconf-devel&sektion=7
# Backup the config file
cp -a -f $CONFIGFILE ${CONFIGFILE}.postinst.bak
# Redeclare variables if missing in config file
test -z $dbc_dbserver || grep -Eq "^ *ZM_DB_HOST=" $CONFIGFILE \
|| echo "ZM_DB_HOST=" >> ${CONFIGFILE}.postinst.bak
test -z $dbc_dbname || grep -Eq "^ *ZM_DB_NAME=" $CONFIGFILE \
|| echo "ZM_DB_NAME=" >> ${CONFIGFILE}.postinst.bak
test -z $dbc_dbuser || grep -Eq "^ *ZM_DB_USER=" $CONFIGFILE \
|| echo "ZM_DB_USER=" >> ${CONFIGFILE}.postinst.bak
test -z $dbc_dbpass || grep -Eq "^ *ZM_DB_PASS=" $CONFIGFILE \
|| echo "ZM_DB_PASS=" >> ${CONFIGFILE}.postinst.bak
# Prevent ZM_DB_HOST to be empty if user selected the 'unix socket' method
if test -z $dbc_dbserver; then
dbc_dbserver_override="localhost"
else
dbc_dbserver_override=$dbc_dbserver
2014-12-07 04:39:50 +08:00
fi
2014-12-02 07:58:18 +08:00
2014-12-08 23:57:37 +08:00
# Update variables in config file
sed -i "s/^ *ZM_DB_HOST=.*/ZM_DB_HOST=$dbc_dbserver_override/" \
${CONFIGFILE}.postinst.bak
test -z $dbc_dbname || sed -i "s/^ *ZM_DB_NAME=.*/ZM_DB_NAME=$dbc_dbname/" \
${CONFIGFILE}.postinst.bak
test -z $dbc_dbuser || sed -i "s/^ *ZM_DB_USER=.*/ZM_DB_USER=$dbc_dbuser/" \
${CONFIGFILE}.postinst.bak
test -z $dbc_dbpass || sed -i "s/^ *ZM_DB_PASS=.*/ZM_DB_PASS=$dbc_dbpass/" \
${CONFIGFILE}.postinst.bak
# Clean-up backup file
mv -f ${CONFIGFILE}.postinst.bak $CONFIGFILE
2014-12-12 20:04:17 +08:00
# As requested by the Debian Webapps Policy Manual §3.2.1
chown root:$ZM_WEB_GROUP /etc/zm/zm.conf
chmod 640 /etc/zm/zm.conf
2014-12-02 07:58:18 +08:00
fi
2014-12-12 20:04:17 +08:00
# Do this every time the package is installed or upgraded
# Test for database presence to avoid failure of zmupdate.pl
if [ "$dbc_install" = "true" ] && [ "$1" = "configure" ]; then
2014-12-02 07:58:18 +08:00
# Ensure zoneminder is stopped
deb-systemd-invoke stop zoneminder.service || exit $?
2014-12-12 20:04:17 +08:00
# Run the ZoneMinder update tool
zmupdate.pl
fi
#DEBHELPER#
exit 0