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