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" \
54 -e "s/__PLUGINS_DIR__/\/var\/lib\/koha\/$name\/plugins/g" \
60 /^\[/ { inclient = 0 }
61 /^\[client\]/ { inclient = 1 }
62 inclient && /^ *host *=/ { print $3 }' \
63 /etc/mysql/koha-common.cnf
66 getinstancemysqlpassword() {
67 xmlstarlet sel -t -v 'yazgfs/config/pass' "/etc/koha/sites/$1/koha-conf.xml"
70 getinstancemysqluser() {
71 xmlstarlet sel -t -v 'yazgfs/config/user' "/etc/koha/sites/$1/koha-conf.xml"
74 getinstancemysqldatabase() {
75 xmlstarlet sel -t -v 'yazgfs/config/database' "/etc/koha/sites/$1/koha-conf.xml"
78 # Set defaults and read config file, if it exists.
87 ZEBRA_MARC_FORMAT="marc21"
90 PASSWDFILE="/etc/koha/passwd"
91 if [ -e /etc/koha/koha-sites.conf ]
93 . /etc/koha/koha-sites.conf
96 [ $# -ge 2 ] && [ $# -le 16 ] || die $usage
98 TEMP=`getopt -o crpm:l:d:f:b:a: -l create-db,request-db,populate-db,use-db,marcflavor:,zebralang:,defaultsql:,configfile:,passwdfile:,database:,adminuser: \
101 # Note the quotes around `$TEMP': they are essential!
104 # Temporary variables for the command line options
105 CLO_ZEBRA_MARC_FORMAT=""
106 CLO_ZEBRA_LANGUAGE=""
112 -c|--create-db) op=create ; shift ;;
113 -r|--request-db) op=request ; shift ;;
114 -p|--populate-db) op=populate ; shift ;;
115 -u|--use-db) op=use ; shift ;;
116 -m|--marcflavor) CLO_ZEBRA_MARC_FORMAT="$2" ; shift 2 ;;
117 -l|--zebralang) CLO_ZEBRA_LANGUAGE="$2" ; shift 2 ;;
118 -d|--defaultsql) CLO_DEFAULTSQL="$2" ; shift 2 ;;
119 -f|--configfile) configfile="$2" ; shift 2 ;;
120 -s|--passwdfile) CLO_PASSWDFILE="$2" ; shift 2 ;;
121 -b|--database) CLO_DATABASE="$2" ; shift 2 ;;
122 -a|--adminuser) CLO_ADMINUSER="$2" ; shift 2 ;;
124 *) die "Internal error processing command line arguments" ;;
128 # Load the configfile given on the command line
129 if [ "$configfile" != "" ]
131 if [ -e "$configfile" ]
135 die "$configfile does not exist.";
139 # Make sure options from the command line get the highest precedence
140 if [ "$CLO_ZEBRA_MARC_FORMAT" != "" ]
142 ZEBRA_MARC_FORMAT="$CLO_ZEBRA_MARC_FORMAT"
144 if [ "$CLO_ZEBRA_LANGUAGE" != "" ]
146 ZEBRA_LANGUAGE="$CLO_ZEBRA_LANGUAGE"
148 if [ "$CLO_DEFAULTSQL" != "" ]
150 DEFAULTSQL="$CLO_DEFAULTSQL"
152 if [ "$CLO_ADMINUSER" != "" ]
154 ADMINUSER="$CLO_ADMINUSER"
156 if [ "$CLO_PASSWDFILE" != "" ]
158 PASSWDFILE="$CLO_PASSWDFILE"
163 opacdomain="$OPACPREFIX$name$OPACSUFFIX$DOMAIN"
164 intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN"
167 if [ -f $PASSWDFILE ] && [ `cat $PASSWDFILE | grep "^$name:"` ]
169 passwdline=`cat $PASSWDFILE | grep "^$name:"`
170 mysqluser=`echo $passwdline | cut -d ":" -f 2`
171 mysqlpwd=`echo $passwdline | cut -d ":" -f 3`
172 mysqldb=`echo $passwdline | cut -d ":" -f 4`
175 # The order of precedence for MySQL database name is:
176 # default < passwd file < command line
177 if [ "$mysqldb" = "" ]
181 if [ "$CLO_DATABASE" != "" ]
183 mysqldb="$CLO_DATABASE"
186 if [ "$mysqluser" = "" ]
188 mysqluser="koha_$name"
190 mysqlhost="$(getmysqlhost)"
192 if [ "$op" = create ] || [ "$op" = request ] || [ "$op" = use ]
194 if [ "$mysqlpwd" = "" ]
196 mysqlpwd="$(pwgen -s 16 1)"
199 mysqlpwd="$(getinstancemysqlpassword $name)"
203 if [ "$op" = create ] || [ "$op" = request ] || [ "$op" = use ]
205 # Create new user and group.
206 username="$name-koha"
207 if getent passwd "$username" > /dev/null
209 die "User $username already exists."
211 if getent group "$username" > /dev/null
213 die "Group $username already exists."
215 adduser --no-create-home --disabled-login \
216 --gecos "Koha instance $username" \
217 --home "/var/lib/koha/$name" \
220 # Create the site-specific directories.
221 koha-create-dirs "$name"
223 # Generate Zebra database password.
224 zebrapwd="$(pwgen -s 16 1)"
225 # Future enhancement: make this configurable for when your db is on
227 mysql_hostname="localhost"
228 # Set up MySQL database for this instance.
229 if [ "$op" = create ]
231 mysql --defaults-extra-file=/etc/mysql/koha-common.cnf <<eof
232 CREATE DATABASE \`$mysqldb\`;
233 CREATE USER \`$mysqluser\`@'$mysql_hostname' IDENTIFIED BY '$mysqlpwd';
234 CREATE USER \`$mysqluser\`@'%' IDENTIFIED BY '$mysqlpwd';
235 GRANT ALL PRIVILEGES ON \`$mysqldb\`.* TO \`$mysqluser\`;
242 mysql --defaults-extra-file=/etc/mysql/koha-common.cnf --force <<eof
243 CREATE USER \`$mysqluser\`@'$mysql_hostname' IDENTIFIED BY '$mysqlpwd';
244 CREATE USER \`$mysqluser\`@'%' IDENTIFIED BY '$mysqlpwd';
245 GRANT ALL PRIVILEGES ON \`$mysqldb\`.* TO \`$mysqluser\`;
250 # Generate and install Apache site-available file and log dir.
251 generate_config_file apache-site.conf.in \
252 "/etc/apache2/sites-available/$name"
253 mkdir "/var/log/koha/$name"
254 chown "$username:$username" "/var/log/koha/$name"
257 # Generate and install main Koha config file.
258 generate_config_file koha-conf-site.xml.in \
259 "/etc/koha/sites/$name/koha-conf.xml"
261 # Generate and install Zebra config files.
262 generate_config_file zebra-biblios-site.cfg.in \
263 "/etc/koha/sites/$name/zebra-biblios.cfg"
264 generate_config_file zebra-authorities-site.cfg.in \
265 "/etc/koha/sites/$name/zebra-authorities.cfg"
266 generate_config_file zebra-authorities-dom-site.cfg.in \
267 "/etc/koha/sites/$name/zebra-authorities-dom.cfg"
268 generate_config_file zebra.passwd.in \
269 "/etc/koha/sites/$name/zebra.passwd"
272 # Create a GPG-encrypted file for requesting a DB to be set up.
273 if [ "$op" = request ]
275 touch "$name-db-request.txt"
276 chmod 0600 "$name-db-request.txt"
277 cat > "$name-db-request.txt" << eof
278 Please create a MySQL database and user on $mysqlhost as follows:
280 database name: $mysqldb
281 database user: $mysqluser
287 echo "See $name-db-request.txt for database creation request."
288 echo "Please forward it to the right person, and then run"
289 echo "$0 --populate-db $name"
295 if [ "$op" = create ] || [ "$op" = populate ]
297 # Re-fetch the passwords from the config we've generated, allows it
298 # to be different from what we set, in case the user had to change
300 mysqluser=$(getinstancemysqluser $name)
301 mysqldb=$(getinstancemysqldatabase $name)
302 # Use the default database content if that exists.
303 if [ -e "$DEFAULTSQL" ]
305 # Populate the database with default content.
307 sed "s/__KOHASITE__/$name/g" |
308 mysql --host="$mysqlhost" --user="$mysqluser" --password="$mysqlpwd" "$mysqldb"
311 # Change the default user's password.
312 staffpass="$(pwgen 12 1)"
313 staffdigest=$(echo -n "$staffpass" |
315 use Digest::MD5 qw(md5_base64);
316 while (<>) { print md5_base64($_), "\n"; }')
317 mysql --host="$mysqlhost" --user="$mysqluser" \
318 --password="$mysqlpwd" <<eof
321 SET password = '$staffdigest'
322 WHERE borrowernumber = $ADMINUSER;
325 echo "staff user password is '$staffpass' but keep that secret"
327 # Upgrade the database schema, just in case the dump was from an
329 koha-upgrade-schema "$name"
331 echo "Koha instance is empty, no staff user created."
336 if [ "$op" = create ] || [ "$op" = populate ] || [ "$op" = use ]
338 # Reconfigure Apache.
340 service apache2 restart
343 koha-start-zebra "$name"
347 if [ "$op" = request ]
354 Email for this instance is disabled. When you're ready to enable it, use:
355 koha-email-enable $name