From 31179704b251d0f83734fed4d035fea4ca932482 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Tue, 25 May 2010 13:44:26 +1200 Subject: [PATCH] Make koha-create be able to handle manual database creation. This is necessary if we do not have access to DB server with sufficient permissions. The DB server is used for other things, that may well be the case. Signed-off-by: Galen Charlton --- debian/scripts/koha-create | 201 +++++++++++++++++++++++-------------- 1 file changed, 128 insertions(+), 73 deletions(-) diff --git a/debian/scripts/koha-create b/debian/scripts/koha-create index f360cf2c30..33edb94c25 100755 --- a/debian/scripts/koha-create +++ b/debian/scripts/koha-create @@ -53,6 +53,11 @@ getmysqlhost() { /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="" @@ -67,8 +72,16 @@ fi # Parse command line. -[ "$#" = 1 ] || die "Usage: $0 instancename" -name="$1" +[ "$#" = 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 @@ -78,97 +91,139 @@ else fi -# Create new user and group. -username="$name-koha" -if getent passwd "$username" > /dev/null +mysqldb="koha_$name" +mysqlhost="$(getmysqlhost)" +mysqluser="koha_$name" + +if [ "$op" = create ] || [ "$op" = request ] then - die "User $username already exists." + mysqlpwd="$(pwgen -1)" +else + mysqlpwd="$(getinstancemysqlpassword $name)" fi -if getent group "$username" > /dev/null + + +if [ "$op" = create ] || [ "$op" = request ] then - die "Group $username already exists." -fi -adduser --no-create-home --disabled-login --gecos "Koha instance $username" \ - --quiet "$username" + # 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. + 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 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 + -# Use the default database content if that exists. -if [ -e "$DEFAULTSQL" ] +if [ "$op" = create ] || [ "$op" = populate ] then - # Populate the database with default content. - zcat "$DEFAULTSQL" | - sed "s/__KOHASITE__/$name/g" | - mysql --defaults-extra-file=/etc/mysql/koha-common.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/koha-common.cnf <) { print md5_base64($_), "\n"; }') + mysql --host="$mysqlhost" --user="$mysqluser" \ +--password="$mysqlpwd" <