#!/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 . 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_HOST__/$mysqlhost/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" } getmysqlhost() { awk ' /^\[/ { inclient = 0 } /^\[client\]/ { inclient = 1 } inclient && /^ *host *=/ { print $3 }' \ /etc/mysql/koha-common.cnf } getinstancemysqlpassword() { sed -n '//s:.*>\(.*\).*:\1:p' \ "/etc/koha/sites/$1/koha-conf.xml" } # 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. [ "$#" = 2 ] || die "Usage: $0 [--create-db|--request-db|--populate-db] instancename" case "$1" in --create-db) op=create ;; --request-db) op=request ;; --populate-db) op=populate ;; *) die "Usage: $0 [--create-db|--request-db|--populate-db] instancename" ;; esac name="$2" domain="$name$DOMAIN" if [ "$INTRAPORT" = 80 ] || [ "$INTRAPORT" = "" ] then intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN" else intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN:$INTRAPORT" fi mysqldb="koha_$name" mysqlhost="$(getmysqlhost)" mysqluser="koha_$name" if [ "$op" = create ] || [ "$op" = request ] then mysqlpwd="$(pwgen -1)" else mysqlpwd="$(getinstancemysqlpassword $name)" fi if [ "$op" = create ] || [ "$op" = request ] then # 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" \ --home "/var/lib/koha/$name" \ --quiet "$username" # Create the site-specific directories. koha-create-dirs "$name" # Generate Zebra database password. zebrapwd="$(pwgen -1)" # Set up MySQL database for this instance. if [ "$op" = create ] then mysql --defaults-extra-file=/etc/mysql/koha-common.cnf < "$name-db-request.txt" << eof Please create a MySQL database and user on $mysqlhost as follows: database name: $mysqldb database user: $mysqluser password: $mysqlpwd Thank you. eof echo "See $name-db-request.txt for database creation request." echo "Please forward it to the right person, and then run" echo "$0 --populate-db $name" echo "Thanks." fi fi if [ "$op" = create ] || [ "$op" = populate ] then # 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 --host="$mysqlhost" --user="$mysqluser" --password="$mysqlpwd" # 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 --host="$mysqlhost" --user="$mysqluser" \ --password="$mysqlpwd" <