3 # koha-create -- Create a new Koha instance.
4 # Copyright 2010 Catalyst IT, Ltd
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.
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.
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/>.
29 generate_config_file() {
31 chown "root:$username" "$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" \
50 /^\[/ { inclient = 0 }
51 /^\[client\]/ { inclient = 1 }
52 inclient && /^ *host *=/ { print $3 }' \
53 /etc/mysql/koha-common.cnf
57 # Set defaults and read config file, if it exists.
63 if [ -e /etc/koha/koha-sites.conf ]
65 . /etc/koha/koha-sites.conf
70 [ "$#" = 1 ] || die "Usage: $0 instancename"
73 if [ "$INTRAPORT" = 80 ] || [ "$INTRAPORT" = "" ]
75 intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN"
77 intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN:$INTRAPORT"
81 # Create new user and group.
83 if getent passwd "$username" > /dev/null
85 die "User $username already exists."
87 if getent group "$username" > /dev/null
89 die "Group $username already exists."
91 adduser --no-create-home --disabled-login --gecos "Koha instance $username" \
95 # Create the site-specific directories.
96 koha-create-dirs "$name"
99 # Generate Zebra database password.
100 zebrapwd="$(pwgen -1)"
103 # Set up MySQL database for this instance.
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';
116 # Use the default database content if that exists.
117 if [ -e "$DEFAULTSQL" ]
119 # Populate the database with default content.
121 sed "s/__KOHASITE__/$name/g" |
122 mysql --defaults-extra-file=/etc/mysql/koha-common.cnf
125 # Change the default user's password.
126 staffpass="$(pwgen -1)"
127 staffdigest=$(echo -n "$staffpass" |
129 use Digest::MD5 qw(md5_base64);
130 while (<>) { print md5_base64($_), "\n"; }')
131 mysql --defaults-extra-file=/etc/mysql/koha-common.cnf <<eof
134 SET password = '$staffdigest'
135 WHERE borrowernumber = 3;
137 echo "staff user password is '$staffpass' but keep that secret"
139 echo "Koha instance is empty, no staff user created."
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"
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"
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"
165 # Upgrade the database schema, just in case the dump was from an old version.
166 koha-upgrade-schema "$name"
169 # Reconfigure Apache.
171 service apache2 restart
174 koha-start-zebra "$name"