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 ;;
92 -d|--defaultsql) DEFAULTSQL="$2" ; shift 2 ;;
94 *) die "Internal error! " ;;
100 domain="$name$DOMAIN"
101 if [ "$INTRAPORT" = 80 ] || [ "$INTRAPORT" = "" ]
103 intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN"
105 intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN:$INTRAPORT"
110 mysqlhost="$(getmysqlhost)"
111 mysqluser="koha_$name"
113 if [ "$op" = create ] || [ "$op" = request ]
115 mysqlpwd="$(pwgen -1)"
117 mysqlpwd="$(getinstancemysqlpassword $name)"
121 if [ "$op" = create ] || [ "$op" = request ]
123 # Create new user and group.
124 username="$name-koha"
125 if getent passwd "$username" > /dev/null
127 die "User $username already exists."
129 if getent group "$username" > /dev/null
131 die "Group $username already exists."
133 adduser --no-create-home --disabled-login \
134 --gecos "Koha instance $username" \
135 --home "/var/lib/koha/$name" \
138 # Create the site-specific directories.
139 koha-create-dirs "$name"
141 # Generate Zebra database password.
142 zebrapwd="$(pwgen -1)"
143 # Set up MySQL database for this instance.
144 if [ "$op" = create ]
146 mysql --defaults-extra-file=/etc/mysql/koha-common.cnf <<eof
147 CREATE DATABASE \`$mysqldb\`;
148 CREATE USER \`$mysqluser\`@'%' IDENTIFIED BY '$mysqlpwd';
149 GRANT ALL PRIVILEGES ON \`$mysqldb\`.* TO \`$mysqluser\`;
154 # Generate and install Apache site-available file and log dir.
155 generate_config_file apache-site.conf.in \
156 "/etc/apache2/sites-available/$name"
157 mkdir "/var/log/koha/$name"
158 chown "$username:$username" "/var/log/koha/$name"
161 # Generate and install main Koha config file.
162 generate_config_file koha-conf-site.xml.in \
163 "/etc/koha/sites/$name/koha-conf.xml"
165 # Generate and install Zebra config files.
166 generate_config_file zebra-biblios-site.cfg.in \
167 "/etc/koha/sites/$name/zebra-biblios.cfg"
168 generate_config_file zebra-authorities-site.cfg.in \
169 "/etc/koha/sites/$name/zebra-authorities.cfg"
170 generate_config_file zebra-authorities-dom-site.cfg.in \
171 "/etc/koha/sites/$name/zebra-authorities-dom.cfg"
172 generate_config_file zebra.passwd.in \
173 "/etc/koha/sites/$name/zebra.passwd"
176 # Create a GPG-encrypted file for requesting a DB to be set up.
177 if [ "$op" = request ]
179 touch "$name-db-request.txt"
180 chmod 0600 "$name-db-request.txt"
181 cat > "$name-db-request.txt" << eof
182 Please create a MySQL database and user on $mysqlhost as follows:
184 database name: $mysqldb
185 database user: $mysqluser
191 echo "See $name-db-request.txt for database creation request."
192 echo "Please forward it to the right person, and then run"
193 echo "$0 --populate-db $name"
199 if [ "$op" = create ] || [ "$op" = populate ]
201 # Use the default database content if that exists.
202 if [ -e "$DEFAULTSQL" ]
204 # Populate the database with default content.
206 sed "s/__KOHASITE__/$name/g" |
207 mysql --host="$mysqlhost" --user="$mysqluser" --password="$mysqlpwd"
210 # Change the default user's password.
211 staffpass="$(pwgen -1)"
212 staffdigest=$(echo -n "$staffpass" |
214 use Digest::MD5 qw(md5_base64);
215 while (<>) { print md5_base64($_), "\n"; }')
216 mysql --host="$mysqlhost" --user="$mysqluser" \
217 --password="$mysqlpwd" <<eof
220 SET password = '$staffdigest'
221 WHERE borrowernumber = 3;
224 echo "staff user password is '$staffpass' but keep that secret"
226 # Upgrade the database schema, just in case the dump was from an
228 koha-upgrade-schema "$name"
230 echo "Koha instance is empty, no staff user created."
235 if [ "$op" = create ] || [ "$op" = populate ]
237 # Reconfigure Apache.
239 service apache2 restart
242 koha-start-zebra "$name"
246 if [ "$op" = request ]
253 Email for this instance is disabled. When you're ready to enable it, use:
254 koha-email-enable $name