Merge branch 'bug_8641' into 3.12-master
[koha.git] / debian / scripts / koha-create
1 #!/bin/sh
2 #
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.
10
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.
15
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/>.
18
19
20 set -e
21
22 usage="Usage: $0 [--create-db|--request-db|--populate-db|--use-db] \
23     [--marcflavor marc21|normarc|unimarc] \
24     [--zebralang en|nb|fr] \
25     [--defaultsql /path/to/some.sql] \
26     [--configfile /path/to/config] [--passwdfile /path/to/passwd] \
27     [--database database] [--adminuser n] instancename"
28
29 die() {
30     echo "$@" 1>&2
31     exit 1
32 }
33
34 # UPPER CASE VARIABLES - from configfile or default value
35 # lower case variables - generated within this script
36 generate_config_file() {
37     touch "$2"
38     chown "root:$username" "$2"
39     chmod 0640 "$2"
40     sed -e "s/__KOHASITE__/$name/g" \
41         -e "s/__OPACPORT__/$OPACPORT/g" \
42         -e "s/__INTRAPORT__/$INTRAPORT/g" \
43         -e "s/__OPACSERVER__/$opacdomain/g" \
44         -e "s/__INTRASERVER__/$intradomain/g" \
45         -e "s/__ZEBRA_PASS__/$zebrapwd/g" \
46         -e "s/__ZEBRA_MARC_FORMAT__/$ZEBRA_MARC_FORMAT/g" \
47         -e "s/__ZEBRA_LANGUAGE__/$ZEBRA_LANGUAGE/g" \
48         -e "s/__DB_NAME__/$mysqldb/g" \
49         -e "s/__DB_HOST__/$mysqlhost/g" \
50         -e "s/__DB_USER__/$mysqluser/g" \
51         -e "s/__DB_PASS__/$mysqlpwd/g" \
52         -e "s/__UNIXUSER__/$username/g" \
53         -e "s/__UNIXGROUP__/$username/g" \
54         "/etc/koha/$1" > "$2"
55 }
56
57 getmysqlhost() {
58     awk '
59         /^\[/ { inclient = 0 }
60         /^\[client\]/ { inclient = 1 }
61         inclient && /^ *host *=/ { print $3 }' \
62         /etc/mysql/koha-common.cnf
63 }
64
65 getinstancemysqlpassword() {
66     xmlstarlet sel -t -v 'yazgfs/config/pass' "/etc/koha/sites/$1/koha-conf.xml"
67 }
68
69 getinstancemysqluser() {
70     xmlstarlet sel -t -v 'yazgfs/config/user' "/etc/koha/sites/$1/koha-conf.xml"
71 }
72
73 getinstancemysqldatabase() {
74     xmlstarlet sel -t -v 'yazgfs/config/database' "/etc/koha/sites/$1/koha-conf.xml"
75 }
76
77 # Set defaults and read config file, if it exists.
78 DOMAIN=""
79 OPACPORT="80"
80 OPACPREFIX=""
81 OPACSUFFIX=""
82 INTRAPORT="8080"
83 INTRAPREFIX=""
84 INTRASUFFIX=""
85 DEFAULTSQL=""
86 ZEBRA_MARC_FORMAT="marc21"
87 ZEBRA_LANGUAGE="en"
88 ADMINUSER="1"
89 PASSWDFILE="/etc/koha/passwd"
90 if [ -e /etc/koha/koha-sites.conf ]
91 then
92     . /etc/koha/koha-sites.conf
93 fi
94
95 [ $# -ge 2 ] && [ $# -le 16 ] || die $usage
96
97 TEMP=`getopt -o crpm:l:d:f:b:a: -l create-db,request-db,populate-db,use-db,marcflavor:,zebralang:,defaultsql:,configfile:,passwdfile:,database:,adminuser: \
98      -n "$0" -- "$@"`
99
100 # Note the quotes around `$TEMP': they are essential!
101 eval set -- "$TEMP"
102
103 # Temporary variables for the command line options
104 CLO_ZEBRA_MARC_FORMAT=""
105 CLO_ZEBRA_LANGUAGE=""
106 CLO_DEFAULTSQL=""
107 CLO_ADMINUSER=""
108
109 while true ; do
110         case "$1" in
111                 -c|--create-db) op=create ; shift ;;
112                 -r|--request-db) op=request ; shift ;;
113                 -p|--populate-db) op=populate ; shift ;;
114         -u|--use-db) op=use ; shift ;;
115                 -m|--marcflavor) CLO_ZEBRA_MARC_FORMAT="$2" ; shift 2 ;;
116                 -l|--zebralang) CLO_ZEBRA_LANGUAGE="$2" ; shift 2 ;;
117                 -d|--defaultsql) CLO_DEFAULTSQL="$2" ; shift 2 ;;
118                 -f|--configfile) configfile="$2" ; shift 2 ;;
119         -s|--passwdfile) CLO_PASSWDFILE="$2" ; shift 2 ;;
120         -b|--database) CLO_DATABASE="$2" ; shift 2 ;;
121                 -a|--adminuser) CLO_ADMINUSER="$2" ; shift 2 ;;
122                 --) shift ; break ;;
123                 *) die "Internal error processing command line arguments" ;;
124         esac
125 done
126
127 # Load the configfile given on the command line
128 if [ "$configfile" != "" ]
129 then
130     if [ -e "$configfile" ]
131     then
132         . "$configfile"
133     else
134         die "$configfile does not exist.";
135     fi
136 fi
137
138 # Make sure options from the command line get the highest precedence
139 if [ "$CLO_ZEBRA_MARC_FORMAT" != "" ]
140 then
141     ZEBRA_MARC_FORMAT="$CLO_ZEBRA_MARC_FORMAT"
142 fi
143 if [ "$CLO_ZEBRA_LANGUAGE" != "" ]
144 then
145     ZEBRA_LANGUAGE="$CLO_ZEBRA_LANGUAGE"
146 fi
147 if [ "$CLO_DEFAULTSQL" != "" ]
148 then
149     DEFAULTSQL="$CLO_DEFAULTSQL"
150 fi
151 if [ "$CLO_ADMINUSER" != "" ]
152 then
153     ADMINUSER="$CLO_ADMINUSER"
154 fi
155 if [ "$CLO_PASSWDFILE" != "" ]
156 then
157     PASSWDFILE="$CLO_PASSWDFILE"
158 fi
159
160 name="$1"
161
162 opacdomain="$OPACPREFIX$name$OPACSUFFIX$DOMAIN"
163 intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN"
164
165
166 if [ -f $PASSWDFILE ] && [ `cat $PASSWDFILE | grep "^$name:"` ]
167 then
168     passwdline=`cat $PASSWDFILE | grep "^$name:"`
169     mysqluser=`echo $passwdline | cut -d ":" -f 2`
170     mysqlpwd=`echo $passwdline | cut -d ":" -f 3`
171     mysqldb=`echo $passwdline | cut -d ":" -f 4`
172 fi
173
174 # The order of precedence for MySQL database name is:
175 # default < passwd file < command line
176 if [ "$mysqldb" = "" ]
177 then
178     mysqldb="koha_$name"
179 fi
180 if [ "$CLO_DATABASE" != "" ]
181 then
182     mysqldb="$CLO_DATABASE"
183 fi
184
185 if [ "$mysqluser" = "" ]
186 then
187     mysqluser="koha_$name"
188 fi
189 mysqlhost="$(getmysqlhost)"
190
191 if [ "$op" = create ] || [ "$op" = request ] || [ "$op" = use ]
192 then
193     if [ "$mysqlpwd" = "" ]
194     then
195         mysqlpwd="$(pwgen -1)"
196     fi
197 else
198     mysqlpwd="$(getinstancemysqlpassword $name)"
199 fi
200
201
202 if [ "$op" = create ] || [ "$op" = request ] || [ "$op" = use ]
203 then
204     # Create new user and group.
205     username="$name-koha"
206     if getent passwd "$username" > /dev/null
207     then
208         die "User $username already exists."
209     fi
210     if getent group "$username" > /dev/null
211     then
212         die "Group $username already exists."
213     fi
214     adduser --no-create-home --disabled-login \
215         --gecos "Koha instance $username" \
216         --home "/var/lib/koha/$name" \
217         --quiet "$username"
218
219     # Create the site-specific directories.
220     koha-create-dirs "$name"
221
222     # Generate Zebra database password.
223     zebrapwd="$(pwgen -s 12 1)"
224     # Future enhancement: make this configurable for when your db is on
225     # another server.
226     mysql_hostname="localhost"
227     # Set up MySQL database for this instance.
228     if [ "$op" = create ]
229     then
230         mysql --defaults-extra-file=/etc/mysql/koha-common.cnf <<eof
231 CREATE DATABASE \`$mysqldb\`;
232 CREATE USER \`$mysqluser\`@'$mysql_hostname' IDENTIFIED BY '$mysqlpwd';
233 CREATE USER \`$mysqluser\`@'%' IDENTIFIED BY '$mysqlpwd';
234 GRANT ALL PRIVILEGES ON \`$mysqldb\`.* TO \`$mysqluser\`;
235 FLUSH PRIVILEGES;
236 eof
237     fi #`
238
239     if [ "$op" = use ]
240     then
241         mysql --defaults-extra-file=/etc/mysql/koha-common.cnf --force <<eof
242 CREATE USER \`$mysqluser\`@'$mysql_hostname' IDENTIFIED BY '$mysqlpwd';
243 CREATE USER \`$mysqluser\`@'%' IDENTIFIED BY '$mysqlpwd';
244 GRANT ALL PRIVILEGES ON \`$mysqldb\`.* TO \`$mysqluser\`;
245 FLUSH PRIVILEGES;
246 eof
247     fi #`
248
249     # Generate and install Apache site-available file and log dir.
250     generate_config_file apache-site.conf.in \
251         "/etc/apache2/sites-available/$name"
252     mkdir "/var/log/koha/$name"
253     chown "$username:$username" "/var/log/koha/$name"
254
255
256     # Generate and install main Koha config file.
257     generate_config_file koha-conf-site.xml.in \
258         "/etc/koha/sites/$name/koha-conf.xml"
259
260     # Generate and install Zebra config files.
261     generate_config_file zebra-biblios-site.cfg.in \
262         "/etc/koha/sites/$name/zebra-biblios.cfg"
263     generate_config_file zebra-authorities-site.cfg.in \
264         "/etc/koha/sites/$name/zebra-authorities.cfg"
265     generate_config_file zebra-authorities-dom-site.cfg.in \
266         "/etc/koha/sites/$name/zebra-authorities-dom.cfg"
267     generate_config_file zebra.passwd.in \
268         "/etc/koha/sites/$name/zebra.passwd"
269
270
271     # Create a GPG-encrypted file for requesting a DB to be set up.
272     if [ "$op" = request ]
273     then
274         touch "$name-db-request.txt"
275         chmod 0600 "$name-db-request.txt"
276         cat > "$name-db-request.txt" << eof
277 Please create a MySQL database and user on $mysqlhost as follows:
278
279 database name: $mysqldb
280 database user: $mysqluser
281      password: $mysqlpwd
282
283 Thank you.
284 eof
285
286         echo "See $name-db-request.txt for database creation request."
287         echo "Please forward it to the right person, and then run"
288         echo "$0 --populate-db $name"
289         echo "Thanks."
290     fi
291 fi
292
293
294 if [ "$op" = create ] || [ "$op" = populate ]
295 then
296     # Re-fetch the passwords from the config we've generated, allows it
297     # to be different from what we set, in case the user had to change
298     # something.
299     mysqluser=$(getinstancemysqluser $name)
300     mysqldb=$(getinstancemysqldatabase $name)
301     # Use the default database content if that exists.
302     if [ -e "$DEFAULTSQL" ]
303     then
304         # Populate the database with default content.
305         zcat "$DEFAULTSQL" |
306         sed "s/__KOHASITE__/$name/g" |
307         mysql --host="$mysqlhost" --user="$mysqluser" --password="$mysqlpwd" "$mysqldb"
308
309
310         # Change the default user's password.
311         staffpass="$(pwgen -1)"
312         staffdigest=$(echo -n "$staffpass" |
313                       perl -e '
314                             use Digest::MD5 qw(md5_base64); 
315                             while (<>) { print md5_base64($_), "\n"; }')
316         mysql --host="$mysqlhost" --user="$mysqluser" \
317 --password="$mysqlpwd" <<eof
318 USE \`$mysqldb\`;
319 UPDATE borrowers 
320 SET password = '$staffdigest' 
321 WHERE borrowernumber = $ADMINUSER;
322 eof
323         #`
324         echo "staff user password is '$staffpass' but keep that secret"
325
326         # Upgrade the database schema, just in case the dump was from an 
327         # old version.
328         koha-upgrade-schema "$name"
329     else
330         echo "Koha instance is empty, no staff user created."
331     fi
332 fi
333
334
335 if [ "$op" = create ] || [ "$op" = populate ] || [ "$op" = use ]
336 then
337     # Reconfigure Apache.
338     a2ensite "$name"
339     service apache2 restart
340
341     # Start Zebra.
342     koha-start-zebra "$name"
343 fi
344
345
346 if [ "$op" = request ]
347 then
348     koha-disable "$name"
349 fi
350
351 echo <<eoh
352
353 Email for this instance is disabled. When you're ready to enable it, use:
354 koha-email-enable $name
355 eoh