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_USER__/$mysqluser/g" \
41 -e "s/__DB_PASS__/$mysqlpwd/g" \
42 -e "s/__UNIXUSER__/$username/g" \
43 -e "s/__UNIXGROUP__/$username/g" \
48 # Set defaults and read config file, if it exists.
54 if [ -e /etc/koha/koha-sites.conf ]
56 . /etc/koha/koha-sites.conf
61 [ "$#" = 1 ] || die "Usage: $0 instancename"
64 if [ "$INTRAPORT" = 80 ] || [ "$INTRAPORT" = "" ]
66 intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN"
68 intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN:$INTRAPORT"
72 # Create new user and group.
74 if getent passwd "$username" > /dev/null
76 die "User $username already exists."
78 if getent group "$username" > /dev/null
80 die "Group $username already exists."
82 adduser --no-create-home --disabled-login --gecos "Koha instance $username" \
86 # Create the site-specific directories.
87 koha-create-dirs "$name"
90 # Generate Zebra database password.
91 zebrapwd="$(pwgen -1)"
94 # Set up MySQL database for this instance.
96 mysqluser="koha_$name"
97 mysqlpwd="$(pwgen -1)"
98 mysql --defaults-extra-file=/etc/mysql/koha-common.cnf <<eof
99 CREATE DATABASE $mysqldb;
100 CREATE USER '$mysqluser' IDENTIFIED BY '$mysqlpwd';
101 GRANT ALL PRIVILEGES ON $mysqldb.* TO '$mysqluser';
106 # Use the default database content if that exists.
107 if [ -e "$DEFAULTSQL" ]
109 # Populate the database with default content.
111 sed "s/__KOHASITE__/$name/g" |
112 mysql --defaults-extra-file=/etc/mysql/koha-common.cnf
115 # Change the default user's password.
116 staffpass="$(pwgen -1)"
117 staffdigest=$(echo -n "$staffpass" |
119 use Digest::MD5 qw(md5_base64);
120 while (<>) { print md5_base64($_), "\n"; }')
121 mysql --defaults-extra-file=/etc/mysql/koha-common.cnf <<eof
124 SET password = '$staffdigest'
125 WHERE borrowernumber = 3;
127 echo "staff user password is '$staffpass' but keep that secret"
129 echo "Koha instance is empty, no staff user created."
133 # Generate and install Apache site-available file and log dir.
134 generate_config_file apache-site.conf.in "/etc/apache2/sites-available/$name"
135 mkdir "/var/log/koha/$name"
136 chown "$username:$username" "/var/log/koha/$name"
139 # Generate and install main Koha config file.
140 generate_config_file koha-conf-site.xml.in \
141 "/etc/koha/sites/$name/koha-conf.xml"
144 # Generate and install Zebra config files.
145 generate_config_file zebra-biblios-site.cfg.in \
146 "/etc/koha/sites/$name/zebra-biblios.cfg"
147 generate_config_file zebra-authorities-site.cfg.in \
148 "/etc/koha/sites/$name/zebra-authorities.cfg"
149 generate_config_file zebra-authorities-dom-site.cfg.in \
150 "/etc/koha/sites/$name/zebra-authorities-dom.cfg"
151 generate_config_file zebra.passwd.in \
152 "/etc/koha/sites/$name/zebra.passwd"
155 # Upgrade the database schema, just in case the dump was from an old version.
156 koha-upgrade-schema "$name"
159 # Reconfigure Apache.
161 service apache2 restart
164 koha-start-zebra "$name"