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] \
23 [--marcflavor marc21|normarc|unimarc] \
24 [--zebralang en|nb|fr] \
25 [--defaultsql /path/to/some.sql] \
26 [--configfile /path/to/config] instancename"
33 generate_config_file() {
35 chown "root:$username" "$2"
37 sed -e "s/__KOHASITE__/$name/g" \
38 -e "s/__OPACPORT__/80/g" \
39 -e "s/__INTRAPORT__/$INTRAPORT/g" \
40 -e "s/__OPACSERVER__/$domain/g" \
41 -e "s/__INTRASERVER__/$intradomain/g" \
42 -e "s/__ZEBRA_PASS__/$zebrapwd/g" \
43 -e "s/__ZEBRA_MARC_FORMAT__/$ZEBRA_MARC_FORMAT/g" \
44 -e "s/__ZEBRA_LANGUAGE__/$ZEBRA_LANGUAGE/g" \
45 -e "s/__DB_NAME__/$mysqldb/g" \
46 -e "s/__DB_HOST__/$mysqlhost/g" \
47 -e "s/__DB_USER__/$mysqluser/g" \
48 -e "s/__DB_PASS__/$mysqlpwd/g" \
49 -e "s/__UNIXUSER__/$username/g" \
50 -e "s/__UNIXGROUP__/$username/g" \
56 /^\[/ { inclient = 0 }
57 /^\[client\]/ { inclient = 1 }
58 inclient && /^ *host *=/ { print $3 }' \
59 /etc/mysql/koha-common.cnf
62 getinstancemysqlpassword() {
63 sed -n '/<pass>/s:.*>\(.*\)</pass>.*:\1:p' \
64 "/etc/koha/sites/$1/koha-conf.xml"
67 # Set defaults and read config file, if it exists.
73 ZEBRA_MARC_FORMAT="marc21"
75 if [ -e /etc/koha/koha-sites.conf ]
77 . /etc/koha/koha-sites.conf
80 [ $# -ge 2 ] && [ $# -le 10 ] || die $usage
82 TEMP=`getopt -o crpm:l:d:f: -l create-db,request-db,populate-db,marcflavor:,zebralang:,defaultsql:,configfile: \
85 # Note the quotes around `$TEMP': they are essential!
88 # Temporary variables for the command line options
89 CLO_ZEBRA_MARC_FORMAT=""
95 -c|--create-db) op=create ; shift ;;
96 -r|--request-db) op=request ; shift ;;
97 -p|--populate-db) op=populate ; shift ;;
98 -m|--marcflavor) CLO_ZEBRA_MARC_FORMAT="$2" ; shift 2 ;;
99 -l|--zebralang) CLO_ZEBRA_LANGUAGE="$2" ; shift 2 ;;
100 -d|--defaultsql) CLO_DEFAULTSQL="$2" ; shift 2 ;;
101 -f|--configfile) configfile="$2" ; shift 2 ;;
103 *) die "Internal error processing command line arguments" ;;
107 # Load the configfile given on the command line
108 if [ -e "$configfile" ]
112 die "$configfile does not exist.";
115 # Make sure options from the command line get the highest precedence
116 if [ "$CLO_ZEBRA_MARC_FORMAT" != "" ]
118 ZEBRA_MARC_FORMAT="$CLO_ZEBRA_MARC_FORMAT"
120 if [ "$CLO_ZEBRA_LANGUAGE" != "" ]
122 ZEBRA_LANGUAGE="$CLO_ZEBRA_LANGUAGE"
124 if [ "$CLO_DEFAULTSQL" != "" ]
126 DEFAULTSQL="$CLO_DEFAULTSQL"
131 domain="$name$DOMAIN"
132 if [ "$INTRAPORT" = 80 ] || [ "$INTRAPORT" = "" ]
134 intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN"
136 intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN:$INTRAPORT"
141 mysqlhost="$(getmysqlhost)"
142 mysqluser="koha_$name"
144 if [ "$op" = create ] || [ "$op" = request ]
146 mysqlpwd="$(pwgen -1)"
148 mysqlpwd="$(getinstancemysqlpassword $name)"
152 if [ "$op" = create ] || [ "$op" = request ]
154 # Create new user and group.
155 username="$name-koha"
156 if getent passwd "$username" > /dev/null
158 die "User $username already exists."
160 if getent group "$username" > /dev/null
162 die "Group $username already exists."
164 adduser --no-create-home --disabled-login \
165 --gecos "Koha instance $username" \
166 --home "/var/lib/koha/$name" \
169 # Create the site-specific directories.
170 koha-create-dirs "$name"
172 # Generate Zebra database password.
173 zebrapwd="$(pwgen -1)"
174 # Set up MySQL database for this instance.
175 if [ "$op" = create ]
177 mysql --defaults-extra-file=/etc/mysql/koha-common.cnf <<eof
178 CREATE DATABASE \`$mysqldb\`;
179 CREATE USER \`$mysqluser\`@'%' IDENTIFIED BY '$mysqlpwd';
180 GRANT ALL PRIVILEGES ON \`$mysqldb\`.* TO \`$mysqluser\`;
185 # Generate and install Apache site-available file and log dir.
186 generate_config_file apache-site.conf.in \
187 "/etc/apache2/sites-available/$name"
188 mkdir "/var/log/koha/$name"
189 chown "$username:$username" "/var/log/koha/$name"
192 # Generate and install main Koha config file.
193 generate_config_file koha-conf-site.xml.in \
194 "/etc/koha/sites/$name/koha-conf.xml"
196 # Generate and install Zebra config files.
197 generate_config_file zebra-biblios-site.cfg.in \
198 "/etc/koha/sites/$name/zebra-biblios.cfg"
199 generate_config_file zebra-authorities-site.cfg.in \
200 "/etc/koha/sites/$name/zebra-authorities.cfg"
201 generate_config_file zebra-authorities-dom-site.cfg.in \
202 "/etc/koha/sites/$name/zebra-authorities-dom.cfg"
203 generate_config_file zebra.passwd.in \
204 "/etc/koha/sites/$name/zebra.passwd"
207 # Create a GPG-encrypted file for requesting a DB to be set up.
208 if [ "$op" = request ]
210 touch "$name-db-request.txt"
211 chmod 0600 "$name-db-request.txt"
212 cat > "$name-db-request.txt" << eof
213 Please create a MySQL database and user on $mysqlhost as follows:
215 database name: $mysqldb
216 database user: $mysqluser
222 echo "See $name-db-request.txt for database creation request."
223 echo "Please forward it to the right person, and then run"
224 echo "$0 --populate-db $name"
230 if [ "$op" = create ] || [ "$op" = populate ]
232 # Use the default database content if that exists.
233 if [ -e "$DEFAULTSQL" ]
235 # Populate the database with default content.
237 sed "s/__KOHASITE__/$name/g" |
238 mysql --host="$mysqlhost" --user="$mysqluser" --password="$mysqlpwd"
241 # Change the default user's password.
242 staffpass="$(pwgen -1)"
243 staffdigest=$(echo -n "$staffpass" |
245 use Digest::MD5 qw(md5_base64);
246 while (<>) { print md5_base64($_), "\n"; }')
247 mysql --host="$mysqlhost" --user="$mysqluser" \
248 --password="$mysqlpwd" <<eof
251 SET password = '$staffdigest'
252 WHERE borrowernumber = 3;
255 echo "staff user password is '$staffpass' but keep that secret"
257 # Upgrade the database schema, just in case the dump was from an
259 koha-upgrade-schema "$name"
261 echo "Koha instance is empty, no staff user created."
266 if [ "$op" = create ] || [ "$op" = populate ]
268 # Reconfigure Apache.
270 service apache2 restart
273 koha-start-zebra "$name"
277 if [ "$op" = request ]
284 Email for this instance is disabled. When you're ready to enable it, use:
285 koha-email-enable $name