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
56 getinstancemysqlpassword
() {
57 sed -n '/<pass>/s:.*>\(.*\)</pass>.*:\1:p' \
58 "/etc/koha/sites/$1/koha-conf.xml"
62 # Set defaults and read config file, if it exists.
68 if [ -e /etc
/koha
/koha-sites.conf
]
70 .
/etc
/koha
/koha-sites.conf
76 die
"Usage: $0 [--create-db|--request-db|--populate-db] instancename"
78 --create-db) op
=create
;;
79 --request-db) op
=request
;;
80 --populate-db) op
=populate
;;
81 *) die
"Usage: $0 [--create-db|--request-db|--populate-db] instancename" ;;
86 if [ "$INTRAPORT" = 80 ] ||
[ "$INTRAPORT" = "" ]
88 intradomain
="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN"
90 intradomain
="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN:$INTRAPORT"
95 mysqlhost
="$(getmysqlhost)"
96 mysqluser
="koha_$name"
98 if [ "$op" = create
] ||
[ "$op" = request
]
100 mysqlpwd
="$(pwgen -1)"
102 mysqlpwd
="$(getinstancemysqlpassword $name)"
106 if [ "$op" = create
] ||
[ "$op" = request
]
108 # Create new user and group.
109 username
="$name-koha"
110 if getent passwd
"$username" > /dev
/null
112 die
"User $username already exists."
114 if getent group
"$username" > /dev
/null
116 die
"Group $username already exists."
118 adduser
--no-create-home --disabled-login \
119 --gecos "Koha instance $username" \
122 # Create the site-specific directories.
123 koha-create-dirs
"$name"
125 # Generate Zebra database password.
126 zebrapwd
="$(pwgen -1)"
128 # Set up MySQL database for this instance.
129 if [ "$op" = create
]
131 mysql
--defaults-extra-file=/etc
/mysql
/koha-common.cnf
<<eof
132 CREATE DATABASE $mysqldb;
133 CREATE USER '$mysqluser'@'%' IDENTIFIED BY '$mysqlpwd';
134 GRANT ALL PRIVILEGES ON $mysqldb.* TO '$mysqluser';
140 # Generate and install Apache site-available file and log dir.
141 generate_config_file apache-site.conf.
in \
142 "/etc/apache2/sites-available/$name"
143 mkdir
"/var/log/koha/$name"
144 chown
"$username:$username" "/var/log/koha/$name"
147 # Generate and install main Koha config file.
148 generate_config_file koha-conf-site.xml.
in \
149 "/etc/koha/sites/$name/koha-conf.xml"
151 # Generate and install Zebra config files.
152 generate_config_file zebra-biblios-site.cfg.
in \
153 "/etc/koha/sites/$name/zebra-biblios.cfg"
154 generate_config_file zebra-authorities-site.cfg.
in \
155 "/etc/koha/sites/$name/zebra-authorities.cfg"
156 generate_config_file zebra-authorities-dom-site.cfg.
in \
157 "/etc/koha/sites/$name/zebra-authorities-dom.cfg"
158 generate_config_file zebra.passwd.
in \
159 "/etc/koha/sites/$name/zebra.passwd"
162 # Create a GPG-encrypted file for requesting a DB to be set up.
163 if [ "$op" = request
]
165 touch "$name-db-request.txt"
166 chmod 0600 "$name-db-request.txt"
167 cat > "$name-db-request.txt" << eof
168 Please create a MySQL database and user on $mysqlhost as follows:
170 database name: $mysqldb
171 database user: $mysqluser
177 echo "See $name-db-request.txt for database creation request."
178 echo "Please forward it to the right person, and then run"
179 echo "$0 --populate-db $name"
185 if [ "$op" = create
] ||
[ "$op" = populate
]
187 # Use the default database content if that exists.
188 if [ -e "$DEFAULTSQL" ]
190 # Populate the database with default content.
192 sed "s/__KOHASITE__/$name/g" |
193 mysql
--host="$mysqlhost" --user="$mysqluser" --password="$mysqlpwd"
196 # Change the default user's password.
197 staffpass
="$(pwgen -1)"
198 staffdigest
=$
(echo -n "$staffpass" |
200 use Digest::MD5 qw(md5_base64);
201 while (<>) { print md5_base64($_), "\n"; }')
202 mysql
--host="$mysqlhost" --user="$mysqluser" \
203 --password="$mysqlpwd" <<eof
206 SET password = '$staffdigest'
207 WHERE borrowernumber = 3;
210 echo "staff user password is '$staffpass' but keep that secret"
212 # Upgrade the database schema, just in case the dump was from an
214 koha-upgrade-schema
"$name"
216 echo "Koha instance is empty, no staff user created."
221 if [ "$op" = create
] ||
[ "$op" = populate
]
223 # Reconfigure Apache.
225 service apache2 restart
228 koha-start-zebra
"$name"
232 if [ "$op" = request
]