X-Git-Url: http://koha-dev.rot13.org:8081/gitweb/?a=blobdiff_plain;f=debian%2Fscripts%2Fkoha-create;h=bb2b642082749d79ca2cd495d3ae91653b241cbc;hb=297d22459f670212624d22a0e48447799004935b;hp=45f65e4465491cfd71ad06d4317471f94fede825;hpb=b25fa64cd6758e6173e3dcfcdcfa2b0a161fcb50;p=koha_gimpoz diff --git a/debian/scripts/koha-create b/debian/scripts/koha-create index 45f65e4465..bb2b642082 100755 --- a/debian/scripts/koha-create +++ b/debian/scripts/koha-create @@ -19,24 +19,34 @@ set -e +usage="Usage: $0 [--create-db|--request-db|--populate-db|--use-db] \ + [--marcflavor marc21|normarc|unimarc] \ + [--zebralang en|nb|fr] \ + [--defaultsql /path/to/some.sql] \ + [--configfile /path/to/config] [--passwdfile /path/to/passwd] \ + [--database database] [--adminuser n] instancename" die() { echo "$@" 1>&2 exit 1 } - +# UPPER CASE VARIABLES - from configfile or default value +# lower case variables - generated within this script 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/__OPACPORT__/$OPACPORT/g" \ -e "s/__INTRAPORT__/$INTRAPORT/g" \ - -e "s/__OPACSERVER__/$domain/g" \ + -e "s/__OPACSERVER__/$opacdomain/g" \ -e "s/__INTRASERVER__/$intradomain/g" \ -e "s/__ZEBRA_PASS__/$zebrapwd/g" \ + -e "s/__ZEBRA_MARC_FORMAT__/$ZEBRA_MARC_FORMAT/g" \ + -e "s/__ZEBRA_LANGUAGE__/$ZEBRA_LANGUAGE/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" \ @@ -44,121 +54,297 @@ generate_config_file() { "/etc/koha/$1" > "$2" } +getmysqlhost() { + awk ' + /^\[/ { inclient = 0 } + /^\[client\]/ { inclient = 1 } + inclient && /^ *host *=/ { print $3 }' \ + /etc/mysql/koha-common.cnf +} + +getinstancemysqlpassword() { + xmlstarlet sel -t -v 'yazgfs/config/pass' "/etc/koha/sites/$1/koha-conf.xml" +} + +getinstancemysqluser() { + xmlstarlet sel -t -v 'yazgfs/config/user' "/etc/koha/sites/$1/koha-conf.xml" +} + +getinstancemysqldatabase() { + xmlstarlet sel -t -v 'yazgfs/config/database' "/etc/koha/sites/$1/koha-conf.xml" +} # Set defaults and read config file, if it exists. DOMAIN="" +OPACPORT="80" +OPACPREFIX="" +OPACSUFFIX="" INTRAPORT="8080" INTRAPREFIX="" INTRASUFFIX="" DEFAULTSQL="" +ZEBRA_MARC_FORMAT="marc21" +ZEBRA_LANGUAGE="en" +ADMINUSER="1" +PASSWDFILE="/etc/koha/passwd" 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" = "" ] +[ $# -ge 2 ] && [ $# -le 16 ] || die $usage + +TEMP=`getopt -o crpm:l:d:f:a: -l create-db,request-db,populate-db,use-db,marcflavor:,zebralang:,defaultsql:,configfile:,passwdfile:,adminuser: \ + -n "$0" -- "$@"` + +# Note the quotes around `$TEMP': they are essential! +eval set -- "$TEMP" + +# Temporary variables for the command line options +CLO_ZEBRA_MARC_FORMAT="" +CLO_ZEBRA_LANGUAGE="" +CLO_DEFAULTSQL="" +CLO_ADMINUSER="" + +while true ; do + case "$1" in + -c|--create-db) op=create ; shift ;; + -r|--request-db) op=request ; shift ;; + -p|--populate-db) op=populate ; shift ;; + -u|--use-db) op=use ; shift ;; + -m|--marcflavor) CLO_ZEBRA_MARC_FORMAT="$2" ; shift 2 ;; + -l|--zebralang) CLO_ZEBRA_LANGUAGE="$2" ; shift 2 ;; + -d|--defaultsql) CLO_DEFAULTSQL="$2" ; shift 2 ;; + -f|--configfile) configfile="$2" ; shift 2 ;; + -s|--passwdfile) CLO_PASSWDFILE="$2" ; shift 2 ;; + -b|--database) CLO_DATABASE="$2" ; shift 2 ;; + -a|--adminuser) CLO_ADMINUSER="$2" ; shift 2 ;; + --) shift ; break ;; + *) die "Internal error processing command line arguments" ;; + esac +done + +# Load the configfile given on the command line +if [ "$configfile" != "" ] then - intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN" -else - intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN:$INTRAPORT" + if [ -e "$configfile" ] + then + . "$configfile" + else + die "$configfile does not exist."; + fi fi - -# Create new user and group. -username="$name-koha" -if getent passwd "$username" > /dev/null +# Make sure options from the command line get the highest precedence +if [ "$CLO_ZEBRA_MARC_FORMAT" != "" ] then - die "User $username already exists." + ZEBRA_MARC_FORMAT="$CLO_ZEBRA_MARC_FORMAT" fi -if getent group "$username" > /dev/null +if [ "$CLO_ZEBRA_LANGUAGE" != "" ] then - die "Group $username already exists." + ZEBRA_LANGUAGE="$CLO_ZEBRA_LANGUAGE" +fi +if [ "$CLO_DEFAULTSQL" != "" ] +then + DEFAULTSQL="$CLO_DEFAULTSQL" +fi +if [ "$CLO_ADMINUSER" != "" ] +then + ADMINUSER="$CLO_ADMINUSER" +fi +if [ "$CLO_PASSWDFILE" != "" ] +then + PASSWDFILE="$CLO_PASSWDFILE" fi -adduser --no-create-home --disabled-login --gecos "Koha instance $username" \ - --quiet "$username" +name="$1" -# Create the site-specific directories. -koha-create-dirs "$name" +opacdomain="$OPACPREFIX$name$OPACSUFFIX$DOMAIN" +intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN" -# Generate Zebra database password. -zebrapwd="$(pwgen -1)" +if [ `cat $PASSWDFILE | grep "^$name:"` ] +then + passwdline=`cat $PASSWDFILE | grep "^$name:"` + mysqluser=`echo $passwdline | cut -d ":" -f 2` + mysqlpwd=`echo $passwdline | cut -d ":" -f 3` + mysqldb=`echo $passwdline | cut -d ":" -f 4` +fi +# The order of precedence for MySQL database name is: +# default < passwd file < command line +if [ "$mysqldb" = "" ] +then + mysqldb="koha_$name" +fi +if [ "$CLO_DATABASE" != "" ] +then + mysqldb="$CLO_DATABASE" +fi -# Set up MySQL database for this instance. -mysqldb="koha_$name" -mysqluser="koha_$name" -mysqlpwd="$(pwgen -1)" -mysql --defaults-extra-file=/etc/mysql/debian.cnf < /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 -s 12 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 -# 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 + 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 - # 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 <) { print md5_base64($_), "\n"; }') + mysql --host="$mysqlhost" --user="$mysqluser" \ +--password="$mysqlpwd" <