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