Add koha-foreach, a script to run a command for each 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 koha-create-dirs "$name"
88
89
90 # Generate Zebra database password.
91 zebrapwd="$(pwgen -1)"
92
93
94 # Set up MySQL database for this instance.
95 mysqldb="koha_$name"
96 mysqluser="koha_$name"
97 mysqlpwd="$(pwgen -1)"
98 mysql --defaults-extra-file=/etc/mysql/debian.cnf <<eof
99 CREATE DATABASE $mysqldb;
100 CREATE USER '$mysqluser' IDENTIFIED BY '$mysqlpwd';
101 GRANT ALL PRIVILEGES ON $mysqldb.* TO '$mysqluser';
102 FLUSH PRIVILEGES;
103 eof
104
105
106 # Use the default database content if that exists.
107 if [ -e "$DEFAULTSQL" ]
108 then
109     # Populate the database with default content.
110     zcat "$DEFAULTSQL" |
111     sed "s/__KOHASITE__/$name/g" |
112     mysql --defaults-extra-file=/etc/mysql/debian.cnf
113
114
115     # Change the default user's password.
116     staffpass="$(pwgen -1)"
117     staffdigest=$(echo -n "$staffpass" |
118                   perl -e '
119                         use Digest::MD5 qw(md5_base64); 
120                         while (<>) { print md5_base64($_), "\n"; }')
121     mysql --defaults-extra-file=/etc/mysql/debian.cnf <<eof
122 USE \`$mysqldb\`;
123 UPDATE borrowers 
124 SET password = '$staffdigest' 
125 WHERE borrowernumber = 3;
126 eof
127     echo "staff user password is '$staffpass' but keep that secret"
128 else
129     echo "Koha instance is empty, no staff user created."
130 fi
131
132
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"
137
138
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"
142
143
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"
153
154
155 # Upgrade the database schema, just in case the dump was from an old version.
156 koha-upgrade-schema "$name"
157
158
159 # Reconfigure Apache.
160 a2ensite "$name"
161 service apache2 restart
162
163 # Start Zebra.
164 koha-start-zebra "$name"