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 mkdir "/etc/koha/sites/$name"
89 mkdir "/var/lock/koha/$name"
90 mkdir "/var/lock/koha/$name/authorities"
91 mkdir "/var/lock/koha/$name/biblios"
92 chown -R "root:$username" "/var/lock/koha/$name"
93 chmod -R g+w "/var/lock/koha/$name"
94 [ -d "/var/spool/koha/$name" ] || mkdir "/var/spool/koha/$name"
96 install -d -o "$username" -g "$username" "/var/lib/koha/$name"
97 install -d -o "$username" -g "$username" "/var/lib/koha/$name/authorities"
98 install -d -o "$username" -g "$username" "/var/lib/koha/$name/biblios"
99 install -d -o "$username" -g "$username" "/var/lib/koha/$name/biblios/key"
100 install -d -o "$username" -g "$username" "/var/lib/koha/$name/biblios/register"
101 install -d -o "$username" -g "$username" "/var/lib/koha/$name/biblios/shadow"
103 install -d "/var/run/koha/$name/authorities"
104 install -d "/var/run/koha/$name/biblios"
107 # Generate Zebra database password.
108 zebrapwd="$(pwgen -1)"
111 # Set up MySQL database for this instance.
113 mysqluser="koha_$name"
114 mysqlpwd="$(pwgen -1)"
115 mysql --defaults-extra-file=/etc/mysql/debian.cnf <<eof
116 CREATE DATABASE $mysqldb;
117 CREATE USER '$mysqluser' IDENTIFIED BY '$mysqlpwd';
118 GRANT ALL PRIVILEGES ON $mysqldb.* TO '$mysqluser';
123 # Use the default database content if that exists.
124 if [ -e "$DEFAULTSQL" ]
126 # Populate the database with default content.
128 sed "s/__KOHASITE__/$name/g" |
129 mysql --defaults-extra-file=/etc/mysql/debian.cnf
132 # Change the default user's password.
133 staffpass="$(pwgen -1)"
134 staffdigest=$(echo -n "$staffpass" |
136 use Digest::MD5 qw(md5_base64);
137 while (<>) { print md5_base64($_), "\n"; }')
138 mysql --defaults-extra-file=/etc/mysql/debian.cnf <<eof
141 SET password = '$staffdigest'
142 WHERE borrowernumber = 3;
144 echo "staff user password is '$staffpass' but keep that secret"
146 echo "Koha instance is empty, no staff user created."
150 # Generate and install Apache site-available file and log dir.
151 generate_config_file apache-site.conf.in "/etc/apache2/sites-available/$name"
152 mkdir "/var/log/koha/$name"
153 chown "$username:$username" "/var/log/koha/$name"
156 # Generate and install main Koha config file.
157 generate_config_file koha-conf-site.xml.in \
158 "/etc/koha/sites/$name/koha-conf.xml"
161 # Generate and install Zebra config files.
162 generate_config_file zebra-biblios-site.cfg.in \
163 "/etc/koha/sites/$name/zebra-biblios.cfg"
164 generate_config_file zebra-authorities-site.cfg.in \
165 "/etc/koha/sites/$name/zebra-authorities.cfg"
166 generate_config_file zebra-authorities-dom-site.cfg.in \
167 "/etc/koha/sites/$name/zebra-authorities-dom.cfg"
170 # Reconfigure Apache.
172 service apache2 restart
175 koha-start-zebra "$name"