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/>.
27 generate_config_file() {
29 chown "root:$username" "$2"
31 sed -e "s/__KOHASITE__/$name/g" \
32 -e "s/__OPACPORT__/80/g" \
33 -e "s/__INTRAPORT__/$INTRAPORT/g" \
34 -e "s/__OPACSERVER__/$domain/g" \
35 -e "s/__INTRASERVER__/$intradomain/g" \
36 -e "s/__ZEBRA_PASS__/$zebrapwd/g" \
37 -e "s/__ZEBRA_MARC_FORMAT__/$ZEBRA_MARC_FORMAT/g" \
38 -e "s/__ZEBRA_LANGUAGE__/$ZEBRA_LANGUAGE/g" \
39 -e "s/__DB_NAME__/$mysqldb/g" \
40 -e "s/__DB_HOST__/$mysqlhost/g" \
41 -e "s/__DB_USER__/$mysqluser/g" \
42 -e "s/__DB_PASS__/$mysqlpwd/g" \
43 -e "s/__UNIXUSER__/$username/g" \
44 -e "s/__UNIXGROUP__/$username/g" \
50 /^\[/ { inclient = 0 }
51 /^\[client\]/ { inclient = 1 }
52 inclient && /^ *host *=/ { print $3 }' \
53 /etc/mysql/koha-common.cnf
56 getinstancemysqlpassword() {
57 sed -n '/<pass>/s:.*>\(.*\)</pass>.*:\1:p' \
58 "/etc/koha/sites/$1/koha-conf.xml"
61 # Set defaults and read config file, if it exists.
67 ZEBRA_MARC_FORMAT="marc21"
69 if [ -e /etc/koha/koha-sites.conf ]
71 . /etc/koha/koha-sites.conf
74 [ $# -ge 2 ] && [ $# -le 6 ] ||
75 die "Usage: $0 [--create-db|--request-db|--populate-db] \
76 [--marcflavor marc21|normarc|unimarc] \
77 [--zebralang en|fr|nb] instancename"
79 TEMP=`getopt -o crpm:l: -l create-db,request-db,populate-db,marcflavor:,zebralang: \
82 # Note the quotes around `$TEMP': they are essential!
87 -c|--create-db) op=create ; shift ;;
88 -r|--request-db) op=request ; shift ;;
89 -p|--populate-db) op=populate ; shift ;;
90 -m|--marcflavor) ZEBRA_MARC_FORMAT="$2" ; shift 2 ;;
91 -l|--zebralang) ZEBRA_LANGUAGE="$2" ; shift 2 ;;
93 *) die "Internal error! " ;;
100 if [ "$INTRAPORT" = 80 ] || [ "$INTRAPORT" = "" ]
102 intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN"
104 intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN:$INTRAPORT"
109 mysqlhost="$(getmysqlhost)"
110 mysqluser="koha_$name"
112 if [ "$op" = create ] || [ "$op" = request ]
114 mysqlpwd="$(pwgen -1)"
116 mysqlpwd="$(getinstancemysqlpassword $name)"
120 if [ "$op" = create ] || [ "$op" = request ]
122 # Create new user and group.
123 username="$name-koha"
124 if getent passwd "$username" > /dev/null
126 die "User $username already exists."
128 if getent group "$username" > /dev/null
130 die "Group $username already exists."
132 adduser --no-create-home --disabled-login \
133 --gecos "Koha instance $username" \
134 --home "/var/lib/koha/$name" \
137 # Create the site-specific directories.
138 koha-create-dirs "$name"
140 # Generate Zebra database password.
141 zebrapwd="$(pwgen -1)"
142 # Set up MySQL database for this instance.
143 if [ "$op" = create ]
145 mysql --defaults-extra-file=/etc/mysql/koha-common.cnf <<eof
146 CREATE DATABASE \`$mysqldb\`;
147 CREATE USER \`$mysqluser\`@'%' IDENTIFIED BY '$mysqlpwd';
148 GRANT ALL PRIVILEGES ON \`$mysqldb\`.* TO \`$mysqluser\`;
153 # Generate and install Apache site-available file and log dir.
154 generate_config_file apache-site.conf.in \
155 "/etc/apache2/sites-available/$name"
156 mkdir "/var/log/koha/$name"
157 chown "$username:$username" "/var/log/koha/$name"
160 # Generate and install main Koha config file.
161 generate_config_file koha-conf-site.xml.in \
162 "/etc/koha/sites/$name/koha-conf.xml"
164 # Generate and install Zebra config files.
165 generate_config_file zebra-biblios-site.cfg.in \
166 "/etc/koha/sites/$name/zebra-biblios.cfg"
167 generate_config_file zebra-authorities-site.cfg.in \
168 "/etc/koha/sites/$name/zebra-authorities.cfg"
169 generate_config_file zebra-authorities-dom-site.cfg.in \
170 "/etc/koha/sites/$name/zebra-authorities-dom.cfg"
171 generate_config_file zebra.passwd.in \
172 "/etc/koha/sites/$name/zebra.passwd"
175 # Create a GPG-encrypted file for requesting a DB to be set up.
176 if [ "$op" = request ]
178 touch "$name-db-request.txt"
179 chmod 0600 "$name-db-request.txt"
180 cat > "$name-db-request.txt" << eof
181 Please create a MySQL database and user on $mysqlhost as follows:
183 database name: $mysqldb
184 database user: $mysqluser
190 echo "See $name-db-request.txt for database creation request."
191 echo "Please forward it to the right person, and then run"
192 echo "$0 --populate-db $name"
198 if [ "$op" = create ] || [ "$op" = populate ]
200 # Use the default database content if that exists.
201 if [ -e "$DEFAULTSQL" ]
203 # Populate the database with default content.
205 sed "s/__KOHASITE__/$name/g" |
206 mysql --host="$mysqlhost" --user="$mysqluser" --password="$mysqlpwd"
209 # Change the default user's password.
210 staffpass="$(pwgen -1)"
211 staffdigest=$(echo -n "$staffpass" |
213 use Digest::MD5 qw(md5_base64);
214 while (<>) { print md5_base64($_), "\n"; }')
215 mysql --host="$mysqlhost" --user="$mysqluser" \
216 --password="$mysqlpwd" <<eof
219 SET password = '$staffdigest'
220 WHERE borrowernumber = 3;
223 echo "staff user password is '$staffpass' but keep that secret"
225 # Upgrade the database schema, just in case the dump was from an
227 koha-upgrade-schema "$name"
229 echo "Koha instance is empty, no staff user created."
234 if [ "$op" = create ] || [ "$op" = populate ]
236 # Reconfigure Apache.
238 service apache2 restart
241 koha-start-zebra "$name"
245 if [ "$op" = request ]
252 Email for this instance is disabled. When you're ready to enable it, use:
253 koha-email-enable $name