Add koha-create, a script to create a new Koha instance.
[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_USER__/$mysqluser/g" \
41         -e "s/__DB_PASS__/$mysqlpwd/g" \
42         -e "s/__UNIXUSER__/$username/g" \
43         -e "s/__UNIXGROUP__/$username/g" \
44         "/etc/koha/$1" > "$2"
45 }
46
47
48 # Set defaults and read config file, if it exists.
49 DOMAIN=""
50 INTRAPORT="8080"
51 INTRAPREFIX=""
52 INTRASUFFIX=""
53 DEFAULTSQL=""
54 if [ -e /etc/koha/koha-sites.conf ]
55 then
56     . /etc/koha/koha-sites.conf
57 fi
58
59
60 # Parse command line.
61 [ "$#" = 1 ] || die "Usage: $0 instancename"
62 name="$1"
63 domain="$name$DOMAIN"
64 if [ "$INTRAPORT" = 80 ] || [ "$INTRAPORT" = "" ]
65 then
66     intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN"
67 else
68     intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN:$INTRAPORT"
69 fi
70
71
72 # Create new user and group.
73 username="$name-koha"
74 if getent passwd "$username" > /dev/null
75 then
76     die "User $username already exists."
77 fi
78 if getent group "$username" > /dev/null
79 then
80     die "Group $username already exists."
81 fi
82 adduser --no-create-home --disabled-login --gecos "Koha instance $username" \
83     --quiet "$username"
84
85
86 # Create the site-specific directories.
87 mkdir "/etc/koha/sites/$name"
88
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"
95
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"
102
103 install -d "/var/run/koha/$name/authorities"
104 install -d "/var/run/koha/$name/biblios"
105
106
107 # Generate Zebra database password.
108 zebrapwd="$(pwgen -1)"
109
110
111 # Set up MySQL database for this instance.
112 mysqldb="koha_$name"
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';
119 FLUSH PRIVILEGES;
120 eof
121
122
123 # Use the default database content if that exists.
124 if [ -e "$DEFAULTSQL" ]
125 then
126     # Populate the database with default content.
127     zcat "$DEFAULTSQL" |
128     sed "s/__KOHASITE__/$name/g" |
129     mysql --defaults-extra-file=/etc/mysql/debian.cnf
130
131
132     # Change the default user's password.
133     staffpass="$(pwgen -1)"
134     staffdigest=$(echo -n "$staffpass" |
135                   perl -e '
136                         use Digest::MD5 qw(md5_base64); 
137                         while (<>) { print md5_base64($_), "\n"; }')
138     mysql --defaults-extra-file=/etc/mysql/debian.cnf <<eof
139 USE \`$mysqldb\`;
140 UPDATE borrowers 
141 SET password = '$staffdigest' 
142 WHERE borrowernumber = 3;
143 eof
144     echo "staff user password is '$staffpass' but keep that secret"
145 else
146     echo "Koha instance is empty, no staff user created."
147 fi
148
149
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"
154
155
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"
159
160
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"
168
169
170 # Reconfigure Apache.
171 a2ensite "$name"
172 service apache2 restart
173
174 # Start Zebra.
175 koha-start-zebra "$name"