Bug 6540 - Add defaultsql command line option
[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                 -d|--defaultsql) DEFAULTSQL="$2" ; shift 2 ;;
93                 --) shift ; break ;;
94                 *) die "Internal error! " ;;
95         esac
96 done
97
98 name="$1"
99
100 domain="$name$DOMAIN"
101 if [ "$INTRAPORT" = 80 ] || [ "$INTRAPORT" = "" ]
102 then
103     intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN"
104 else
105     intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN:$INTRAPORT"
106 fi
107
108
109 mysqldb="koha_$name"
110 mysqlhost="$(getmysqlhost)"
111 mysqluser="koha_$name"
112
113 if [ "$op" = create ] || [ "$op" = request ]
114 then
115     mysqlpwd="$(pwgen -1)"
116 else
117     mysqlpwd="$(getinstancemysqlpassword $name)"
118 fi
119
120
121 if [ "$op" = create ] || [ "$op" = request ]
122 then
123     # Create new user and group.
124     username="$name-koha"
125     if getent passwd "$username" > /dev/null
126     then
127         die "User $username already exists."
128     fi
129     if getent group "$username" > /dev/null
130     then
131         die "Group $username already exists."
132     fi
133     adduser --no-create-home --disabled-login \
134         --gecos "Koha instance $username" \
135         --home "/var/lib/koha/$name" \
136         --quiet "$username"
137
138     # Create the site-specific directories.
139     koha-create-dirs "$name"
140
141     # Generate Zebra database password.
142     zebrapwd="$(pwgen -1)"
143     # Set up MySQL database for this instance.
144     if [ "$op" = create ]
145     then
146         mysql --defaults-extra-file=/etc/mysql/koha-common.cnf <<eof
147 CREATE DATABASE \`$mysqldb\`;
148 CREATE USER \`$mysqluser\`@'%' IDENTIFIED BY '$mysqlpwd';
149 GRANT ALL PRIVILEGES ON \`$mysqldb\`.* TO \`$mysqluser\`;
150 FLUSH PRIVILEGES;
151 eof
152     fi
153
154     # Generate and install Apache site-available file and log dir.
155     generate_config_file apache-site.conf.in \
156         "/etc/apache2/sites-available/$name"
157     mkdir "/var/log/koha/$name"
158     chown "$username:$username" "/var/log/koha/$name"
159
160
161     # Generate and install main Koha config file.
162     generate_config_file koha-conf-site.xml.in \
163         "/etc/koha/sites/$name/koha-conf.xml"
164
165     # Generate and install Zebra config files.
166     generate_config_file zebra-biblios-site.cfg.in \
167         "/etc/koha/sites/$name/zebra-biblios.cfg"
168     generate_config_file zebra-authorities-site.cfg.in \
169         "/etc/koha/sites/$name/zebra-authorities.cfg"
170     generate_config_file zebra-authorities-dom-site.cfg.in \
171         "/etc/koha/sites/$name/zebra-authorities-dom.cfg"
172     generate_config_file zebra.passwd.in \
173         "/etc/koha/sites/$name/zebra.passwd"
174
175
176     # Create a GPG-encrypted file for requesting a DB to be set up.
177     if [ "$op" = request ]
178     then
179         touch "$name-db-request.txt"
180         chmod 0600 "$name-db-request.txt"
181         cat > "$name-db-request.txt" << eof
182 Please create a MySQL database and user on $mysqlhost as follows:
183
184 database name: $mysqldb
185 database user: $mysqluser
186      password: $mysqlpwd
187
188 Thank you.
189 eof
190
191         echo "See $name-db-request.txt for database creation request."
192         echo "Please forward it to the right person, and then run"
193         echo "$0 --populate-db $name"
194         echo "Thanks."
195     fi
196 fi
197
198
199 if [ "$op" = create ] || [ "$op" = populate ]
200 then
201     # Use the default database content if that exists.
202     if [ -e "$DEFAULTSQL" ]
203     then
204         # Populate the database with default content.
205         zcat "$DEFAULTSQL" |
206         sed "s/__KOHASITE__/$name/g" |
207         mysql --host="$mysqlhost" --user="$mysqluser" --password="$mysqlpwd"
208
209
210         # Change the default user's password.
211         staffpass="$(pwgen -1)"
212         staffdigest=$(echo -n "$staffpass" |
213                       perl -e '
214                             use Digest::MD5 qw(md5_base64); 
215                             while (<>) { print md5_base64($_), "\n"; }')
216         mysql --host="$mysqlhost" --user="$mysqluser" \
217 --password="$mysqlpwd" <<eof
218 USE \`$mysqldb\`;
219 UPDATE borrowers 
220 SET password = '$staffdigest' 
221 WHERE borrowernumber = 3;
222 eof
223
224         echo "staff user password is '$staffpass' but keep that secret"
225
226         # Upgrade the database schema, just in case the dump was from an 
227         # old version.
228         koha-upgrade-schema "$name"
229     else
230         echo "Koha instance is empty, no staff user created."
231     fi
232 fi
233
234
235 if [ "$op" = create ] || [ "$op" = populate ]
236 then
237     # Reconfigure Apache.
238     a2ensite "$name"
239     service apache2 restart
240
241     # Start Zebra.
242     koha-start-zebra "$name"
243 fi
244
245
246 if [ "$op" = request ]
247 then
248     koha-disable "$name"
249 fi
250
251 echo <<eoh
252
253 Email for this instance is disabled. When you're ready to enable it, use:
254 koha-email-enable $name
255 eoh