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/__ZEBRA_MARC_FORMAT__/$ZEBRA_MARC_FORMAT/g" \
40 -e "s/__ZEBRA_LANGUAGE__/$ZEBRA_LANGUAGE/g" \
41 -e "s/__DB_NAME__/$mysqldb/g" \
42 -e "s/__DB_HOST__/$mysqlhost/g" \
43 -e "s/__DB_USER__/$mysqluser/g" \
44 -e "s/__DB_PASS__/$mysqlpwd/g" \
45 -e "s/__UNIXUSER__/$username/g" \
46 -e "s/__UNIXGROUP__/$username/g" \
52 /^\[/ { inclient = 0 }
53 /^\[client\]/ { inclient = 1 }
54 inclient && /^ *host *=/ { print $3 }' \
55 /etc/mysql/koha-common.cnf
58 getinstancemysqlpassword() {
59 sed -n '/<pass>/s:.*>\(.*\)</pass>.*:\1:p' \
60 "/etc/koha/sites/$1/koha-conf.xml"
64 # Set defaults and read config file, if it exists.
70 ZEBRA_MARC_FORMAT="marc21"
72 if [ -e /etc/koha/koha-sites.conf ]
74 . /etc/koha/koha-sites.conf
80 die "Usage: $0 [--create-db|--request-db|--populate-db] instancename"
82 --create-db) op=create ;;
83 --request-db) op=request ;;
84 --populate-db) op=populate ;;
85 *) die "Usage: $0 [--create-db|--request-db|--populate-db] instancename" ;;
90 if [ "$INTRAPORT" = 80 ] || [ "$INTRAPORT" = "" ]
92 intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN"
94 intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN:$INTRAPORT"
99 mysqlhost="$(getmysqlhost)"
100 mysqluser="koha_$name"
102 if [ "$op" = create ] || [ "$op" = request ]
104 mysqlpwd="$(pwgen -1)"
106 mysqlpwd="$(getinstancemysqlpassword $name)"
110 if [ "$op" = create ] || [ "$op" = request ]
112 # Create new user and group.
113 username="$name-koha"
114 if getent passwd "$username" > /dev/null
116 die "User $username already exists."
118 if getent group "$username" > /dev/null
120 die "Group $username already exists."
122 adduser --no-create-home --disabled-login \
123 --gecos "Koha instance $username" \
124 --home "/var/lib/koha/$name" \
127 # Create the site-specific directories.
128 koha-create-dirs "$name"
130 # Generate Zebra database password.
131 zebrapwd="$(pwgen -1)"
132 # Set up MySQL database for this instance.
133 if [ "$op" = create ]
135 mysql --defaults-extra-file=/etc/mysql/koha-common.cnf <<eof
136 CREATE DATABASE \`$mysqldb\`;
137 CREATE USER \`$mysqluser\`@'%' IDENTIFIED BY '$mysqlpwd';
138 GRANT ALL PRIVILEGES ON \`$mysqldb\`.* TO \`$mysqluser\`;
143 # Generate and install Apache site-available file and log dir.
144 generate_config_file apache-site.conf.in \
145 "/etc/apache2/sites-available/$name"
146 mkdir "/var/log/koha/$name"
147 chown "$username:$username" "/var/log/koha/$name"
150 # Generate and install main Koha config file.
151 generate_config_file koha-conf-site.xml.in \
152 "/etc/koha/sites/$name/koha-conf.xml"
154 # Generate and install Zebra config files.
155 generate_config_file zebra-biblios-site.cfg.in \
156 "/etc/koha/sites/$name/zebra-biblios.cfg"
157 generate_config_file zebra-authorities-site.cfg.in \
158 "/etc/koha/sites/$name/zebra-authorities.cfg"
159 generate_config_file zebra-authorities-dom-site.cfg.in \
160 "/etc/koha/sites/$name/zebra-authorities-dom.cfg"
161 generate_config_file zebra.passwd.in \
162 "/etc/koha/sites/$name/zebra.passwd"
165 # Create a GPG-encrypted file for requesting a DB to be set up.
166 if [ "$op" = request ]
168 touch "$name-db-request.txt"
169 chmod 0600 "$name-db-request.txt"
170 cat > "$name-db-request.txt" << eof
171 Please create a MySQL database and user on $mysqlhost as follows:
173 database name: $mysqldb
174 database user: $mysqluser
180 echo "See $name-db-request.txt for database creation request."
181 echo "Please forward it to the right person, and then run"
182 echo "$0 --populate-db $name"
188 if [ "$op" = create ] || [ "$op" = populate ]
190 # Use the default database content if that exists.
191 if [ -e "$DEFAULTSQL" ]
193 # Populate the database with default content.
195 sed "s/__KOHASITE__/$name/g" |
196 mysql --host="$mysqlhost" --user="$mysqluser" --password="$mysqlpwd"
199 # Change the default user's password.
200 staffpass="$(pwgen -1)"
201 staffdigest=$(echo -n "$staffpass" |
203 use Digest::MD5 qw(md5_base64);
204 while (<>) { print md5_base64($_), "\n"; }')
205 mysql --host="$mysqlhost" --user="$mysqluser" \
206 --password="$mysqlpwd" <<eof
209 SET password = '$staffdigest'
210 WHERE borrowernumber = 3;
213 echo "staff user password is '$staffpass' but keep that secret"
215 # Upgrade the database schema, just in case the dump was from an
217 koha-upgrade-schema "$name"
219 echo "Koha instance is empty, no staff user created."
224 if [ "$op" = create ] || [ "$op" = populate ]
226 # Reconfigure Apache.
228 service apache2 restart
231 koha-start-zebra "$name"
235 if [ "$op" = request ]
242 Email for this instance is disabled. When you're ready to enable it, use:
243 koha-email-enable $name