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