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