(bug #4084) fix offline circ
[koha.git] / debian / scripts / koha-create
blob43e7c4ed3bcb54291ab112c078b7b9c01c80d55a
1 #!/bin/sh
3 # koha-create -- Create a new Koha instance.
4 # Copyright 2010 Catalyst IT, Ltd
5 #
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/>.
20 set -e
23 die() {
24 echo "$@" 1>&2
25 exit 1
29 generate_config_file() {
30 touch "$2"
31 chown "root:$username" "$2"
32 chmod 0640 "$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" \
45 "/etc/koha/$1" > "$2"
48 getmysqlhost() {
49 awk '
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.
63 DOMAIN=""
64 INTRAPORT="8080"
65 INTRAPREFIX=""
66 INTRASUFFIX=""
67 DEFAULTSQL=""
68 if [ -e /etc/koha/koha-sites.conf ]
69 then
70 . /etc/koha/koha-sites.conf
74 # Parse command line.
75 [ "$#" = 2 ] ||
76 die "Usage: $0 [--create-db|--request-db|--populate-db] instancename"
77 case "$1" in
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" ;;
82 esac
84 name="$2"
85 domain="$name$DOMAIN"
86 if [ "$INTRAPORT" = 80 ] || [ "$INTRAPORT" = "" ]
87 then
88 intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN"
89 else
90 intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN:$INTRAPORT"
94 mysqldb="koha_$name"
95 mysqlhost="$(getmysqlhost)"
96 mysqluser="koha_$name"
98 if [ "$op" = create ] || [ "$op" = request ]
99 then
100 mysqlpwd="$(pwgen -1)"
101 else
102 mysqlpwd="$(getinstancemysqlpassword $name)"
106 if [ "$op" = create ] || [ "$op" = request ]
107 then
108 # Create new user and group.
109 username="$name-koha"
110 if getent passwd "$username" > /dev/null
111 then
112 die "User $username already exists."
114 if getent group "$username" > /dev/null
115 then
116 die "Group $username already exists."
118 adduser --no-create-home --disabled-login \
119 --gecos "Koha instance $username" \
120 --quiet "$username"
122 # Create the site-specific directories.
123 koha-create-dirs "$name"
125 # Generate Zebra database password.
126 zebrapwd="$(pwgen -1)"
128 # Set up MySQL database for this instance.
129 if [ "$op" = create ]
130 then
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';
135 FLUSH PRIVILEGES;
140 # Generate and install Apache site-available file and log dir.
141 generate_config_file apache-site.conf.in \
142 "/etc/apache2/sites-available/$name"
143 mkdir "/var/log/koha/$name"
144 chown "$username:$username" "/var/log/koha/$name"
147 # Generate and install main Koha config file.
148 generate_config_file koha-conf-site.xml.in \
149 "/etc/koha/sites/$name/koha-conf.xml"
151 # Generate and install Zebra config files.
152 generate_config_file zebra-biblios-site.cfg.in \
153 "/etc/koha/sites/$name/zebra-biblios.cfg"
154 generate_config_file zebra-authorities-site.cfg.in \
155 "/etc/koha/sites/$name/zebra-authorities.cfg"
156 generate_config_file zebra-authorities-dom-site.cfg.in \
157 "/etc/koha/sites/$name/zebra-authorities-dom.cfg"
158 generate_config_file zebra.passwd.in \
159 "/etc/koha/sites/$name/zebra.passwd"
162 # Create a GPG-encrypted file for requesting a DB to be set up.
163 if [ "$op" = request ]
164 then
165 touch "$name-db-request.txt"
166 chmod 0600 "$name-db-request.txt"
167 cat > "$name-db-request.txt" << eof
168 Please create a MySQL database and user on $mysqlhost as follows:
170 database name: $mysqldb
171 database user: $mysqluser
172 password: $mysqlpwd
174 Thank you.
177 echo "See $name-db-request.txt for database creation request."
178 echo "Please forward it to the right person, and then run"
179 echo "$0 --populate-db $name"
180 echo "Thanks."
185 if [ "$op" = create ] || [ "$op" = populate ]
186 then
187 # Use the default database content if that exists.
188 if [ -e "$DEFAULTSQL" ]
189 then
190 # Populate the database with default content.
191 zcat "$DEFAULTSQL" |
192 sed "s/__KOHASITE__/$name/g" |
193 mysql --host="$mysqlhost" --user="$mysqluser" --password="$mysqlpwd"
196 # Change the default user's password.
197 staffpass="$(pwgen -1)"
198 staffdigest=$(echo -n "$staffpass" |
199 perl -e '
200 use Digest::MD5 qw(md5_base64);
201 while (<>) { print md5_base64($_), "\n"; }')
202 mysql --host="$mysqlhost" --user="$mysqluser" \
203 --password="$mysqlpwd" <<eof
204 USE \`$mysqldb\`;
205 UPDATE borrowers
206 SET password = '$staffdigest'
207 WHERE borrowernumber = 3;
210 echo "staff user password is '$staffpass' but keep that secret"
212 # Upgrade the database schema, just in case the dump was from an
213 # old version.
214 koha-upgrade-schema "$name"
215 else
216 echo "Koha instance is empty, no staff user created."
221 if [ "$op" = create ] || [ "$op" = populate ]
222 then
223 # Reconfigure Apache.
224 a2ensite "$name"
225 service apache2 restart
227 # Start Zebra.
228 koha-start-zebra "$name"
232 if [ "$op" = request ]
233 then
234 koha-disable "$name"