Add support for a remote mysql server.
[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
23 die() {
24     echo "$@" 1>&2
25     exit 1
26 }
27
28
29 generate_config_file() {
30     touch "$2"
31     chown "root:$username" "$2"
32     chmod 0640 "$2"
33     sed -e "s/__KOHASITE__/$name/g" \
34         -e "s/__OPACPORT__/80/g" \
35         -e "s/__INTRAPORT__/$INTRAPORT/g" \
36         -e "s/__OPACSERVER__/$domain/g" \
37         -e "s/__INTRASERVER__/$intradomain/g" \
38         -e "s/__ZEBRA_PASS__/$zebrapwd/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
57 # Set defaults and read config file, if it exists.
58 DOMAIN=""
59 INTRAPORT="8080"
60 INTRAPREFIX=""
61 INTRASUFFIX=""
62 DEFAULTSQL=""
63 if [ -e /etc/koha/koha-sites.conf ]
64 then
65     . /etc/koha/koha-sites.conf
66 fi
67
68
69 # Parse command line.
70 [ "$#" = 1 ] || die "Usage: $0 instancename"
71 name="$1"
72 domain="$name$DOMAIN"
73 if [ "$INTRAPORT" = 80 ] || [ "$INTRAPORT" = "" ]
74 then
75     intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN"
76 else
77     intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN:$INTRAPORT"
78 fi
79
80
81 # Create new user and group.
82 username="$name-koha"
83 if getent passwd "$username" > /dev/null
84 then
85     die "User $username already exists."
86 fi
87 if getent group "$username" > /dev/null
88 then
89     die "Group $username already exists."
90 fi
91 adduser --no-create-home --disabled-login --gecos "Koha instance $username" \
92     --quiet "$username"
93
94
95 # Create the site-specific directories.
96 koha-create-dirs "$name"
97
98
99 # Generate Zebra database password.
100 zebrapwd="$(pwgen -1)"
101
102
103 # Set up MySQL database for this instance.
104 mysqldb="koha_$name"
105 mysqlhost="$(getmysqlhost)"
106 mysqluser="koha_$name"
107 mysqlpwd="$(pwgen -1)"
108 mysql --defaults-extra-file=/etc/mysql/koha-common.cnf <<eof
109 CREATE DATABASE $mysqldb;
110 CREATE USER '$mysqluser'@'%' IDENTIFIED BY '$mysqlpwd';
111 GRANT ALL PRIVILEGES ON $mysqldb.* TO '$mysqluser';
112 FLUSH PRIVILEGES;
113 eof
114
115
116 # Use the default database content if that exists.
117 if [ -e "$DEFAULTSQL" ]
118 then
119     # Populate the database with default content.
120     zcat "$DEFAULTSQL" |
121     sed "s/__KOHASITE__/$name/g" |
122     mysql --defaults-extra-file=/etc/mysql/koha-common.cnf
123
124
125     # Change the default user's password.
126     staffpass="$(pwgen -1)"
127     staffdigest=$(echo -n "$staffpass" |
128                   perl -e '
129                         use Digest::MD5 qw(md5_base64); 
130                         while (<>) { print md5_base64($_), "\n"; }')
131     mysql --defaults-extra-file=/etc/mysql/koha-common.cnf <<eof
132 USE \`$mysqldb\`;
133 UPDATE borrowers 
134 SET password = '$staffdigest' 
135 WHERE borrowernumber = 3;
136 eof
137     echo "staff user password is '$staffpass' but keep that secret"
138 else
139     echo "Koha instance is empty, no staff user created."
140 fi
141
142
143 # Generate and install Apache site-available file and log dir.
144 generate_config_file apache-site.conf.in "/etc/apache2/sites-available/$name"
145 mkdir "/var/log/koha/$name"
146 chown "$username:$username" "/var/log/koha/$name"
147
148
149 # Generate and install main Koha config file.
150 generate_config_file koha-conf-site.xml.in \
151     "/etc/koha/sites/$name/koha-conf.xml"
152
153
154 # Generate and install Zebra config files.
155 generate_config_file zebra-biblios-site.cfg.in \
156     "/etc/koha/sites/$name/zebra-biblios.cfg"
157 generate_config_file zebra-authorities-site.cfg.in \
158     "/etc/koha/sites/$name/zebra-authorities.cfg"
159 generate_config_file zebra-authorities-dom-site.cfg.in \
160     "/etc/koha/sites/$name/zebra-authorities-dom.cfg"
161 generate_config_file zebra.passwd.in \
162     "/etc/koha/sites/$name/zebra.passwd"
163
164
165 # Upgrade the database schema, just in case the dump was from an old version.
166 koha-upgrade-schema "$name"
167
168
169 # Reconfigure Apache.
170 a2ensite "$name"
171 service apache2 restart
172
173 # Start Zebra.
174 koha-start-zebra "$name"