3 # koha-create -- Create a new Koha instance.
4 # Copyright 2010 Catalyst IT, Ltd
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 usage="Usage: $0 [--create-db|--request-db|--populate-db|--use-db] \
23 [--marcflavor marc21|normarc|unimarc] \
24 [--zebralang en|nb|fr] \
25 [--defaultsql /path/to/some.sql] \
26 [--configfile /path/to/config] [--passwdfile /path/to/passwd] \
27 [--database database] [--adminuser n] instancename"
34 # UPPER CASE VARIABLES - from configfile or default value
35 # lower case variables - generated within this script
36 generate_config_file() {
38 chown "root:$username" "$2"
40 sed -e "s/__KOHASITE__/$name/g" \
41 -e "s/__OPACPORT__/$OPACPORT/g" \
42 -e "s/__INTRAPORT__/$INTRAPORT/g" \
43 -e "s/__OPACSERVER__/$opacdomain/g" \
44 -e "s/__INTRASERVER__/$intradomain/g" \
45 -e "s/__ZEBRA_PASS__/$zebrapwd/g" \
46 -e "s/__ZEBRA_MARC_FORMAT__/$ZEBRA_MARC_FORMAT/g" \
47 -e "s/__ZEBRA_LANGUAGE__/$ZEBRA_LANGUAGE/g" \
48 -e "s/__DB_NAME__/$mysqldb/g" \
49 -e "s/__DB_HOST__/$mysqlhost/g" \
50 -e "s/__DB_USER__/$mysqluser/g" \
51 -e "s/__DB_PASS__/$mysqlpwd/g" \
52 -e "s/__UNIXUSER__/$username/g" \
53 -e "s/__UNIXGROUP__/$username/g" \
59 /^\[/ { inclient = 0 }
60 /^\[client\]/ { inclient = 1 }
61 inclient && /^ *host *=/ { print $3 }' \
62 /etc/mysql/koha-common.cnf
65 getinstancemysqlpassword() {
66 xmlstarlet sel -t -v 'yazgfs/config/pass' "/etc/koha/sites/$1/koha-conf.xml"
69 getinstancemysqluser() {
70 xmlstarlet sel -t -v 'yazgfs/config/user' "/etc/koha/sites/$1/koha-conf.xml"
73 getinstancemysqldatabase() {
74 xmlstarlet sel -t -v 'yazgfs/config/database' "/etc/koha/sites/$1/koha-conf.xml"
77 # Set defaults and read config file, if it exists.
86 ZEBRA_MARC_FORMAT="marc21"
89 PASSWDFILE="/etc/koha/passwd"
90 if [ -e /etc/koha/koha-sites.conf ]
92 . /etc/koha/koha-sites.conf
95 [ $# -ge 2 ] && [ $# -le 16 ] || die $usage
97 TEMP=`getopt -o crpm:l:d:f:a: -l create-db,request-db,populate-db,use-db,marcflavor:,zebralang:,defaultsql:,configfile:,passwdfile:,adminuser: \
100 # Note the quotes around `$TEMP': they are essential!
103 # Temporary variables for the command line options
104 CLO_ZEBRA_MARC_FORMAT=""
105 CLO_ZEBRA_LANGUAGE=""
111 -c|--create-db) op=create ; shift ;;
112 -r|--request-db) op=request ; shift ;;
113 -p|--populate-db) op=populate ; shift ;;
114 -u|--use-db) op=use ; shift ;;
115 -m|--marcflavor) CLO_ZEBRA_MARC_FORMAT="$2" ; shift 2 ;;
116 -l|--zebralang) CLO_ZEBRA_LANGUAGE="$2" ; shift 2 ;;
117 -d|--defaultsql) CLO_DEFAULTSQL="$2" ; shift 2 ;;
118 -f|--configfile) configfile="$2" ; shift 2 ;;
119 -s|--passwdfile) CLO_PASSWDFILE="$2" ; shift 2 ;;
120 -b|--database) CLO_DATABASE="$2" ; shift 2 ;;
121 -a|--adminuser) CLO_ADMINUSER="$2" ; shift 2 ;;
123 *) die "Internal error processing command line arguments" ;;
127 # Load the configfile given on the command line
128 if [ "$configfile" != "" ]
130 if [ -e "$configfile" ]
134 die "$configfile does not exist.";
138 # Make sure options from the command line get the highest precedence
139 if [ "$CLO_ZEBRA_MARC_FORMAT" != "" ]
141 ZEBRA_MARC_FORMAT="$CLO_ZEBRA_MARC_FORMAT"
143 if [ "$CLO_ZEBRA_LANGUAGE" != "" ]
145 ZEBRA_LANGUAGE="$CLO_ZEBRA_LANGUAGE"
147 if [ "$CLO_DEFAULTSQL" != "" ]
149 DEFAULTSQL="$CLO_DEFAULTSQL"
151 if [ "$CLO_ADMINUSER" != "" ]
153 ADMINUSER="$CLO_ADMINUSER"
155 if [ "$CLO_PASSWDFILE" != "" ]
157 PASSWDFILE="$CLO_PASSWDFILE"
162 opacdomain="$OPACPREFIX$name$OPACSUFFIX$DOMAIN"
163 intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN"
166 if [ -f $PASSWDFILE ] && [ `cat $PASSWDFILE | grep "^$name:"` ]
168 passwdline=`cat $PASSWDFILE | grep "^$name:"`
169 mysqluser=`echo $passwdline | cut -d ":" -f 2`
170 mysqlpwd=`echo $passwdline | cut -d ":" -f 3`
171 mysqldb=`echo $passwdline | cut -d ":" -f 4`
174 # The order of precedence for MySQL database name is:
175 # default < passwd file < command line
176 if [ "$mysqldb" = "" ]
180 if [ "$CLO_DATABASE" != "" ]
182 mysqldb="$CLO_DATABASE"
185 if [ "$mysqluser" = "" ]
187 mysqluser="koha_$name"
189 mysqlhost="$(getmysqlhost)"
191 if [ "$op" = create ] || [ "$op" = request ] || [ "$op" = use ]
193 if [ "$mysqlpwd" = "" ]
195 mysqlpwd="$(pwgen -1)"
198 mysqlpwd="$(getinstancemysqlpassword $name)"
202 if [ "$op" = create ] || [ "$op" = request ] || [ "$op" = use ]
204 # Create new user and group.
205 username="$name-koha"
206 if getent passwd "$username" > /dev/null
208 die "User $username already exists."
210 if getent group "$username" > /dev/null
212 die "Group $username already exists."
214 adduser --no-create-home --disabled-login \
215 --gecos "Koha instance $username" \
216 --home "/var/lib/koha/$name" \
219 # Create the site-specific directories.
220 koha-create-dirs "$name"
222 # Generate Zebra database password.
223 zebrapwd="$(pwgen -s 12 1)"
224 # Future enhancement: make this configurable for when your db is on
226 mysql_hostname="localhost"
227 # Set up MySQL database for this instance.
228 if [ "$op" = create ]
230 mysql --defaults-extra-file=/etc/mysql/koha-common.cnf <<eof
231 CREATE DATABASE \`$mysqldb\`;
232 CREATE USER \`$mysqluser\`@'$mysql_hostname' IDENTIFIED BY '$mysqlpwd';
233 CREATE USER \`$mysqluser\`@'%' IDENTIFIED BY '$mysqlpwd';
234 GRANT ALL PRIVILEGES ON \`$mysqldb\`.* TO \`$mysqluser\`;
241 mysql --defaults-extra-file=/etc/mysql/koha-common.cnf --force <<eof
242 CREATE USER \`$mysqluser\`@'$mysql_hostname' IDENTIFIED BY '$mysqlpwd';
243 CREATE USER \`$mysqluser\`@'%' IDENTIFIED BY '$mysqlpwd';
244 GRANT ALL PRIVILEGES ON \`$mysqldb\`.* TO \`$mysqluser\`;
249 # Generate and install Apache site-available file and log dir.
250 generate_config_file apache-site.conf.in \
251 "/etc/apache2/sites-available/$name"
252 mkdir "/var/log/koha/$name"
253 chown "$username:$username" "/var/log/koha/$name"
256 # Generate and install main Koha config file.
257 generate_config_file koha-conf-site.xml.in \
258 "/etc/koha/sites/$name/koha-conf.xml"
260 # Generate and install Zebra config files.
261 generate_config_file zebra-biblios-site.cfg.in \
262 "/etc/koha/sites/$name/zebra-biblios.cfg"
263 generate_config_file zebra-authorities-site.cfg.in \
264 "/etc/koha/sites/$name/zebra-authorities.cfg"
265 generate_config_file zebra-authorities-dom-site.cfg.in \
266 "/etc/koha/sites/$name/zebra-authorities-dom.cfg"
267 generate_config_file zebra.passwd.in \
268 "/etc/koha/sites/$name/zebra.passwd"
271 # Create a GPG-encrypted file for requesting a DB to be set up.
272 if [ "$op" = request ]
274 touch "$name-db-request.txt"
275 chmod 0600 "$name-db-request.txt"
276 cat > "$name-db-request.txt" << eof
277 Please create a MySQL database and user on $mysqlhost as follows:
279 database name: $mysqldb
280 database user: $mysqluser
286 echo "See $name-db-request.txt for database creation request."
287 echo "Please forward it to the right person, and then run"
288 echo "$0 --populate-db $name"
294 if [ "$op" = create ] || [ "$op" = populate ]
296 # Re-fetch the passwords from the config we've generated, allows it
297 # to be different from what we set, in case the user had to change
299 mysqluser=$(getinstancemysqluser $name)
300 mysqldb=$(getinstancemysqldatabase $name)
301 # Use the default database content if that exists.
302 if [ -e "$DEFAULTSQL" ]
304 # Populate the database with default content.
306 sed "s/__KOHASITE__/$name/g" |
307 mysql --host="$mysqlhost" --user="$mysqluser" --password="$mysqlpwd" "$mysqldb"
310 # Change the default user's password.
311 staffpass="$(pwgen -1)"
312 staffdigest=$(echo -n "$staffpass" |
314 use Digest::MD5 qw(md5_base64);
315 while (<>) { print md5_base64($_), "\n"; }')
316 mysql --host="$mysqlhost" --user="$mysqluser" \
317 --password="$mysqlpwd" <<eof
320 SET password = '$staffdigest'
321 WHERE borrowernumber = $ADMINUSER;
324 echo "staff user password is '$staffpass' but keep that secret"
326 # Upgrade the database schema, just in case the dump was from an
328 koha-upgrade-schema "$name"
330 echo "Koha instance is empty, no staff user created."
335 if [ "$op" = create ] || [ "$op" = populate ] || [ "$op" = use ]
337 # Reconfigure Apache.
339 service apache2 restart
342 koha-start-zebra "$name"
346 if [ "$op" = request ]
353 Email for this instance is disabled. When you're ready to enable it, use:
354 koha-email-enable $name