Koha/debian/scripts/koha-create
Lars Wirzenius 439989924d Add koha-upgrade-schema, a script to upgrade the database schema.
Signed-off-by: Galen Charlton <gmcharlt@gmail.com>
2010-05-25 07:59:35 -04:00

179 lines
5 KiB
Bash
Executable file

#!/bin/sh
#
# koha-create -- Create a new Koha instance.
# Copyright 2010 Catalyst IT, Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
set -e
die() {
echo "$@" 1>&2
exit 1
}
generate_config_file() {
touch "$2"
chown "root:$username" "$2"
chmod 0640 "$2"
sed -e "s/__KOHASITE__/$name/g" \
-e "s/__OPACPORT__/80/g" \
-e "s/__INTRAPORT__/$INTRAPORT/g" \
-e "s/__OPACSERVER__/$domain/g" \
-e "s/__INTRASERVER__/$intradomain/g" \
-e "s/__ZEBRA_PASS__/$zebrapwd/g" \
-e "s/__DB_NAME__/$mysqldb/g" \
-e "s/__DB_USER__/$mysqluser/g" \
-e "s/__DB_PASS__/$mysqlpwd/g" \
-e "s/__UNIXUSER__/$username/g" \
-e "s/__UNIXGROUP__/$username/g" \
"/etc/koha/$1" > "$2"
}
# Set defaults and read config file, if it exists.
DOMAIN=""
INTRAPORT="8080"
INTRAPREFIX=""
INTRASUFFIX=""
DEFAULTSQL=""
if [ -e /etc/koha/koha-sites.conf ]
then
. /etc/koha/koha-sites.conf
fi
# Parse command line.
[ "$#" = 1 ] || die "Usage: $0 instancename"
name="$1"
domain="$name$DOMAIN"
if [ "$INTRAPORT" = 80 ] || [ "$INTRAPORT" = "" ]
then
intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN"
else
intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN:$INTRAPORT"
fi
# Create new user and group.
username="$name-koha"
if getent passwd "$username" > /dev/null
then
die "User $username already exists."
fi
if getent group "$username" > /dev/null
then
die "Group $username already exists."
fi
adduser --no-create-home --disabled-login --gecos "Koha instance $username" \
--quiet "$username"
# Create the site-specific directories.
mkdir "/etc/koha/sites/$name"
mkdir "/var/lock/koha/$name"
mkdir "/var/lock/koha/$name/authorities"
mkdir "/var/lock/koha/$name/biblios"
chown -R "root:$username" "/var/lock/koha/$name"
chmod -R g+w "/var/lock/koha/$name"
[ -d "/var/spool/koha/$name" ] || mkdir "/var/spool/koha/$name"
install -d -o "$username" -g "$username" "/var/lib/koha/$name"
install -d -o "$username" -g "$username" "/var/lib/koha/$name/authorities"
install -d -o "$username" -g "$username" "/var/lib/koha/$name/biblios"
install -d -o "$username" -g "$username" "/var/lib/koha/$name/biblios/key"
install -d -o "$username" -g "$username" "/var/lib/koha/$name/biblios/register"
install -d -o "$username" -g "$username" "/var/lib/koha/$name/biblios/shadow"
install -d "/var/run/koha/$name/authorities"
install -d "/var/run/koha/$name/biblios"
# Generate Zebra database password.
zebrapwd="$(pwgen -1)"
# Set up MySQL database for this instance.
mysqldb="koha_$name"
mysqluser="koha_$name"
mysqlpwd="$(pwgen -1)"
mysql --defaults-extra-file=/etc/mysql/debian.cnf <<eof
CREATE DATABASE $mysqldb;
CREATE USER '$mysqluser' IDENTIFIED BY '$mysqlpwd';
GRANT ALL PRIVILEGES ON $mysqldb.* TO '$mysqluser';
FLUSH PRIVILEGES;
eof
# Use the default database content if that exists.
if [ -e "$DEFAULTSQL" ]
then
# Populate the database with default content.
zcat "$DEFAULTSQL" |
sed "s/__KOHASITE__/$name/g" |
mysql --defaults-extra-file=/etc/mysql/debian.cnf
# Change the default user's password.
staffpass="$(pwgen -1)"
staffdigest=$(echo -n "$staffpass" |
perl -e '
use Digest::MD5 qw(md5_base64);
while (<>) { print md5_base64($_), "\n"; }')
mysql --defaults-extra-file=/etc/mysql/debian.cnf <<eof
USE \`$mysqldb\`;
UPDATE borrowers
SET password = '$staffdigest'
WHERE borrowernumber = 3;
eof
echo "staff user password is '$staffpass' but keep that secret"
else
echo "Koha instance is empty, no staff user created."
fi
# Generate and install Apache site-available file and log dir.
generate_config_file apache-site.conf.in "/etc/apache2/sites-available/$name"
mkdir "/var/log/koha/$name"
chown "$username:$username" "/var/log/koha/$name"
# Generate and install main Koha config file.
generate_config_file koha-conf-site.xml.in \
"/etc/koha/sites/$name/koha-conf.xml"
# Generate and install Zebra config files.
generate_config_file zebra-biblios-site.cfg.in \
"/etc/koha/sites/$name/zebra-biblios.cfg"
generate_config_file zebra-authorities-site.cfg.in \
"/etc/koha/sites/$name/zebra-authorities.cfg"
generate_config_file zebra-authorities-dom-site.cfg.in \
"/etc/koha/sites/$name/zebra-authorities-dom.cfg"
# Upgrade the database schema, just in case the dump was from an old version.
koha-upgrade-schema "$name"
# Reconfigure Apache.
a2ensite "$name"
service apache2 restart
# Start Zebra.
koha-start-zebra "$name"