-
Notifications
You must be signed in to change notification settings - Fork 0
/
koha-install-instance
executable file
·254 lines (217 loc) · 7.18 KB
/
koha-install-instance
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/bin/bash
# ------------------------------------------------------------------
# Author: Lennon Mazonde
# GitHub: @grandmaestr
# Title: koha-install
# Description:
# This script installs a Koha instance(s) with a local or remote database.
# You can copy and paste this script onto your server and make it executable by running "chmod a+x /path/to/file_name"
# To run the script, simply go to "/path/to/file_name" or if it's in your home directory, run "~/file_name" in the CLI.
# This script is interactive, so you'll be prompted for input at various stages.
# You can modify or use this script however you like but I'd really appreciate it if you give me a shout out or attribution if you post it elsewhere :)
# ------------------------------------------------------------------
set -euo pipefail
# Script version
VERSION=0.1.0
# Set the name of the script to variable
SCRIPT_NAME="$(basename ${0})"
# ------Help--------------------------------------------------------
Help(){
# Display Help
cat <<EOF
Usage: $SCRIPT_NAME [options]
Options:
a - The hostname, IP address or URL of the local or remote database. Defaults to localhost.
c - The IP or URL address of the client (running the Koha web server). If using a VPC in AWS, Azure, or GCP enter the **Private** IP of your EC2 instance. Defaults to localhost.
h - help. Print this help.
i - InstanceId. The name of the Koha instance.
l - options are local or remote. Defaults to local.
o - OPAC URL. The domain or subdomain to use for the OPAC page.
s - Staff URL. The domain or subdomain to use for the Staff page.
u - The username of the database root user.
v - version. Print the script version.
Example:
To create a Koha instance called "library" with a local database, run:
$SCRIPT_NAME -i library -u rootuser -p dbpassword -o opac.example.com -s staff.example.com
To create a Koha instance called "library" with a remote database, run:
$SCRIPT_NAME -i library -l remote -a mydb.123456789012.us-east-1.rds.amazonaws.com -c 172.16.78.121 -u rootuser -p dbpassword -o opac.example.com -s staff.example.com
EOF
}
# --- Options default values -------------------------------------------
# Default hostname value
hostname="localhost"
# Default client IP
clientipaddr="localhost"
# Default database location
dblocation="local"
# --- Options processing -------------------------------------------
if [ $# == 0 ] ; then
Help
exit 1;
fi
while getopts ":a:c:i:h:l:u:o:s:v" optname; do
case "$optname" in
a)
hostname=("$OPTARG")
;;
c)
clientipaddr=("$OPTARG")
;;
h)
Help
exit 0;
;;
i)
instanceid=("$OPTARG")
;;
l)
dblocation=("$OPTARG")
;;
o)
opacurl=("$OPTARG")
;;
s)
staffurl=("$OPTARG")
;;
u)
dbuser=("$OPTARG")
;;
v)
echo "Version $VERSION"
exit 0;
;;
\?)
echo "Unknown option $OPTARG"
Help
exit 0;
;;
:)
echo "Error: you must provide an argument for option -$OPTARG"
exit 0;
;;
*)
echo "Unknown error while processing options"
Help
exit 0;
;;
esac
done
shift $(($OPTIND - 1))
param1=$1
param2=$2
# --- Start instance------------------------------------------------
Start(){
# Enable instance
sudo koha-enable $val
# Enable koha-worker
sudo koha-worker --start $val
# Start Zebra
sudo koha-zebra --start $val
# Enable Plack
sudo koha-plack --enable $val
sudo koha-plack --start $val
}
# --- Install remote db --------------------------------------------
InstallRemoteDB(){
#create Koha database and user on remote server. Works on MariaDB. May need to be modified for a MySQL server.
mysql -h ${hostname} -u ${dbuser} -p${dbpass} -e "
CREATE DATABASE koha_${val};
CREATE USER 'koha_${val}'@'${clientipaddr}' IDENTIFIED BY '${remotekohadbpwd}';
GRANT ALL PRIVILEGES ON koha_${val}.* to 'koha_${val}'@'${clientipaddr}';
FLUSH PRIVILEGES;"
}
# --- Install remote db aurora--------------------------------------
InstallRemoteDBAurora(){
mysql -h ${hostname} -u ${dbuser} -p${dbpass} -e "
CREATE DATABASE koha_${val};
CREATE USER 'koha_${val}'@'${clientipaddr}' IDENTIFIED BY '${remotekohadbpwd}';
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES,
CREATE VIEW, EVENT, TRIGGER, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, EXECUTE ON koha_${val}.* to 'koha_${val}'@'${clientipaddr}'; FLUSH PRIVILEGES;"
}
# --- Create koha-common.cnf --------------------------------------
CreateKohaCommon(){
echo "
[client]
host = ${hostname}
user = ${dbuser}
password = ${dbpass}
socket = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host = ${hostname}
user = ${dbuser}
password = ${dbpass}
socket = /var/run/mysqld/mysqld.sock
" | sudo tee /etc/mysql/koha-common.cnf >/dev/null
}
# --- Locks -------------------------------------------------------
LOCK_FILE=/tmp/$SCRIPT_NAME.lock
if [ -f "$LOCK_FILE" ]; then
echo "Script is already running"
exit
fi
trap "rm -f $LOCK_FILE" EXIT
touch $LOCK_FILE
# --- Body --------------------------------------------------------
for val in "${instanceid}"; do
# Prompt for the mysql root password
echo "Enter the password for the database root user:"
read -s dbpass
# Prompt user for local or remote db install
while true; do
case $dblocation in
[lL][oO][cC][aA][lL]|[lL] )
# Create the instance
sudo koha-create --create-db $val
break;;
[rR][eE][mM][oO][tT][eE] | [rR] )
# Create the instance
sudo koha-create --request-db $val
# Get and set password for the remote Koha database
remotekohadbpwd=$(sudo xmlstarlet sel -t -v 'yazgfs/config/pass' /etc/koha/sites/$val/koha-conf.xml)
# Install a remote Koha instance database
InstallRemoteDB
# If using Amazon Aurora MySQL, comment the MySQL script above and uncomment the following line.
# InstallRemoteDBAurora
# Create koha-common.cnf
CreateKohaCommon
# remove remote db config files
sudo rm $val-db-request.txt
break;;
* )
echo "Invalid value for database location (-l). Option only accepts remote or local as valid inputs. Exiting."
exit;;
esac
done
# Set path to Apache config
configpath=/etc/apache2/sites-enabled/$val.conf
# Enable your Koha instance apache config
sudo a2ensite $val
# Strip out leading http(s):// and set to variable
staffdomain=$(echo "$staffurl" | sed "s|.*://||; s|/.*||" )
# Strip out leading http(s):// and set to variable
opacdomain=$(echo "$opacurl" | sed "s|.*://||; s|/.*||" )
# Replace ServerName with Staff url
if [[ -z ${staffdomain} ]];
then
echo "ServerName not specified, not setting"
else
sudo sed -i "/ServerName*/c\ ServerName $staffdomain" $configpath
fi
# Replace ServerName with OPAC url
if [[ -z ${opacdomain} ]];
then
echo "ServerName not specified, not setting"
else
sudo sed -i "0,/ServerName $staffdomain/s//ServerName $opacdomain/" $configpath
fi
# End loop
done
# Enable and start Koha instances
for val in "${instanceid[@]}"; do
Start
done
# Check Apache for configuration errors
echo "If you come across any errors, check your instance Apache config at /etc/apache2/sites-enabled/$val.conf"
sudo apachectl configtest
# Restart Apache
sudo systemctl restart apache2