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