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/>.
29 generate_config_file() {
31 chown "root:$username" "$2"
33 sed -e "s/__KOHASITE__/$name/g" \
34 -e "s/__OPACPORT__/80/g" \
35 -e "s/__INTRAPORT__/$INTRAPORT/g" \
36 -e "s/__OPACSERVER__/$domain/g" \
37 -e "s/__INTRASERVER__/$intradomain/g" \
38 -e "s/__ZEBRA_PASS__/$zebrapwd/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"
62 # Set defaults and read config file, if it exists.
68 if [ -e /etc/koha/koha-sites.conf ]
70 . /etc/koha/koha-sites.conf
76 die "Usage: $0 [--create-db|--request-db|--populate-db] instancename"
78 --create-db) op=create ;;
79 --request-db) op=request ;;
80 --populate-db) op=populate ;;
81 *) die "Usage: $0 [--create-db|--request-db|--populate-db] instancename" ;;
86 if [ "$INTRAPORT" = 80 ] || [ "$INTRAPORT" = "" ]
88 intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN"
90 intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN:$INTRAPORT"
95 mysqlhost="$(getmysqlhost)"
96 mysqluser="koha_$name"
98 if [ "$op" = create ] || [ "$op" = request ]
100 mysqlpwd="$(pwgen -1)"
102 mysqlpwd="$(getinstancemysqlpassword $name)"
106 if [ "$op" = create ] || [ "$op" = request ]
108 # Create new user and group.
109 username="$name-koha"
110 if getent passwd "$username" > /dev/null
112 die "User $username already exists."
114 if getent group "$username" > /dev/null
116 die "Group $username already exists."
118 adduser --no-create-home --disabled-login \
119 --gecos "Koha instance $username" \
120 --home "/var/lib/koha/$name" \
123 # Create the site-specific directories.
124 koha-create-dirs "$name"
126 # Generate Zebra database password.
127 zebrapwd="$(pwgen -1)"
128 # Set up MySQL database for this instance.
129 if [ "$op" = create ]
131 mysql --defaults-extra-file=/etc/mysql/koha-common.cnf <<eof
132 CREATE DATABASE \`$mysqldb\`;
133 CREATE USER \`$mysqluser\`@'%' IDENTIFIED BY '$mysqlpwd';
134 GRANT ALL PRIVILEGES ON \`$mysqldb\`.* TO \`$mysqluser\`;
139 # Generate and install Apache site-available file and log dir.
140 generate_config_file apache-site.conf.in \
141 "/etc/apache2/sites-available/$name"
142 mkdir "/var/log/koha/$name"
143 chown "$username:$username" "/var/log/koha/$name"
146 # Generate and install main Koha config file.
147 generate_config_file koha-conf-site.xml.in \
148 "/etc/koha/sites/$name/koha-conf.xml"
150 # Generate and install Zebra config files.
151 generate_config_file zebra-biblios-site.cfg.in \
152 "/etc/koha/sites/$name/zebra-biblios.cfg"
153 generate_config_file zebra-authorities-site.cfg.in \
154 "/etc/koha/sites/$name/zebra-authorities.cfg"
155 generate_config_file zebra-authorities-dom-site.cfg.in \
156 "/etc/koha/sites/$name/zebra-authorities-dom.cfg"
157 generate_config_file zebra.passwd.in \
158 "/etc/koha/sites/$name/zebra.passwd"
161 # Create a GPG-encrypted file for requesting a DB to be set up.
162 if [ "$op" = request ]
164 touch "$name-db-request.txt"
165 chmod 0600 "$name-db-request.txt"
166 cat > "$name-db-request.txt" << eof
167 Please create a MySQL database and user on $mysqlhost as follows:
169 database name: $mysqldb
170 database user: $mysqluser
176 echo "See $name-db-request.txt for database creation request."
177 echo "Please forward it to the right person, and then run"
178 echo "$0 --populate-db $name"
184 if [ "$op" = create ] || [ "$op" = populate ]
186 # Use the default database content if that exists.
187 if [ -e "$DEFAULTSQL" ]
189 # Populate the database with default content.
191 sed "s/__KOHASITE__/$name/g" |
192 mysql --host="$mysqlhost" --user="$mysqluser" --password="$mysqlpwd"
195 # Change the default user's password.
196 staffpass="$(pwgen -1)"
197 staffdigest=$(echo -n "$staffpass" |
199 use Digest::MD5 qw(md5_base64);
200 while (<>) { print md5_base64($_), "\n"; }')
201 mysql --host="$mysqlhost" --user="$mysqluser" \
202 --password="$mysqlpwd" <<eof
205 SET password = '$staffdigest'
206 WHERE borrowernumber = 3;
209 echo "staff user password is '$staffpass' but keep that secret"
211 # Upgrade the database schema, just in case the dump was from an
213 koha-upgrade-schema "$name"
215 echo "Koha instance is empty, no staff user created."
220 if [ "$op" = create ] || [ "$op" = populate ]
222 # Reconfigure Apache.
224 service apache2 restart
227 koha-start-zebra "$name"
231 if [ "$op" = request ]
238 Email for this instance is disabled. When you're ready to enable it, use:
239 koha-email-enable $name