/
preinst
312 lines (273 loc) · 10.7 KB
/
preinst
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#!/bin/bash
#
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.
#
# authors: Amalu Obinna <amaluobinna@aol.com>
# Brady Miller <brady@sparmy.com>
#
# date: 05/15/10
#
# Debian package pre-installation script steps:
# 1) Create log file
# 2) New install or Upgrade
# -Install
# a) If OpenEMR web directory already exist, then attempt an upgrade.
# b) If MySQL is already installed:
# -Collect the MySQL root password
# -Ensure no openemr mysql database or mysql user
# -Upgrade
# a) Ensure OpenEMR web directory does exist
# b) Collect package version and ensure appropriate upgrade
# c) Collect webpath and sql settings from current version
# d) Verify the mysql user/database exist
# e) Backup the current version web folder and mysql database
# f) Clear the cache directories in current version
# 3) Create config file
#
# Output log file:
# /var/log/openemr/install
#
#
# summary of how this script can be called:
# * <new-preinst> `install'
# * <new-preinst> `install' <old-version>
# * <new-preinst> `upgrade' <old-version>
# * <old-preinst> `abort-upgrade' <new-version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package
#constants and paths
LOGDIR=/var/log/openemr
LOG=$LOGDIR/install
CONFIGDIR=/etc/openemr
CONFIG=$CONFIGDIR/openemr.conf
TMPDIR=/tmp/openemr-tmp
WEB=/var/www
OPENEMR=$WEB/openemr
#hardcoded mysql user and database for install (not pertinent for upgrading)
# upgrading can use whatever is found in openemr/library/sqlconf.php
INSTALL_USER=openemr
INSTALL_DATABASE=openemr
#Standardized echo function to send to both echo and to log file
# requires one parameter (string)
output_both () {
echo $1
echo "`date`: $1" >> $LOG
}
#Standardized echo function to send to only log file
# requires one parameter (string)
log_only () {
echo "`date`: $1" >> $LOG
}
#Standardized exit function to be used when unable to install the openemr package
# requires one parameter (string with reason for exiting)
unable_exit () {
echo $1
echo "`date`: $1" >> $LOG
echo "EXITING.........."
echo "`date`: EXITING.........." >> $LOG
sleep 5
exit 1
}
#function to check mysql for selected databases
# 1st param is password, 2nd param database, 3rd param is host (optional), 4th param is user (optional)
check_mysql () {
if [ -n "$3" ]; then
HOST=$3
else
HOST=localhost
fi
if [ -n "$4" ]; then
USE=$4
else
USE=root
fi
echo `mysql -u "$USE" -h "$HOST" --password="$1" -e 'show databases' 2>/dev/null | awk '{ print $1}' | grep "^$2$"`
}
#function to collect variables from config files
# 1st param is variable name, 2nd param is filename
collect_var () {
echo `grep -i "^[[:space:]]*$1[[:space:]=]" $2 | cut -d \= -f 2 | cut -d \; -f 1 | sed "s/[ '\"]//gi"`
}
#function to hold the upgrade code (allow more flexible upgrading)
# 1st param is the previous version,
# 2nd param is whether raw or package upgrade
upgrade_function () {
#Check for /var/www/openemr. If does not exist, then exit.
if ! [ -d "$OPENEMR" ]; then
unable_exit "OpenEMR is not installed ($OPENEMR), so can not upgrade the OpenEMR Package."
fi
#collect previous version
OLD_VERSION=$1
#collect type of upgrade (either package or raw)
UPGRADETYPE=$2
#ensure version is appropriate for upgrade (if not, then exit!!)
#
# ONLY acceptable for raw is 3.0.0 or 3.0.0.1 or 3.1.0 (need to convert these)
#
if [ "$UPGRADETYPE" == "raw" ]; then
if [ "$OLD_VERSION" == "3.0.0" ]; then
OLD_VERSION="3.0.1-1"
elif [ "$OLD_VERSION" == "3.0.0.1" ]; then
OLD_VERSION="3.0.1-1"
elif [ "$OLD_VERSION" == "3.0.1" ]; then
OLD_VERSION="3.0.1-1"
elif [ "$OLD_VERSION" == "3.1.0" ]; then
OLD_VERSION="3.1.0-1"
elif [ "$OLD_VERSION" == "3.2.0" ]; then
OLD_VERSION="3.2.0-1"
else
unable_exit "Unable to upgrade from $OLD_VERSION version package with $UPGRADETYPE method, so can not upgrade the OpenEMR Package."
fi
fi
#
# ONLY acceptable package and converted raw is 3.0.1-1 or 3.1.0-1
#
if [ "$OLD_VERSION" != "3.0.1-1" ] && [ "$OLD_VERSION" != "3.1.0-1" ] && [ "$OLD_VERSION" != "3.2.0-1" ]; then
unable_exit "Unable to upgrade from $OLD_VERSION version package with $UPGRADETYPE method, so can not upgrade the OpenEMR Package."
fi
#collect openemr mysql data
SQLLOCATION=$(collect_var \$host $OPENEMR/library/sqlconf.php)
SQLUSER=$(collect_var \$login $OPENEMR/library/sqlconf.php)
SQLPASSWORD=$(collect_var \$pass $OPENEMR/library/sqlconf.php)
SQLDATABASE=$(collect_var \$dbase $OPENEMR/library/sqlconf.php)
SQLUTFFLAG=$(collect_var \$disable_utf8_flag $OPENEMR/library/sqlconf.php)
#if SQLUTFFLAG variable is empty, then make it false
if [ "$SQLUTFFLAG" == "" ]; then
SQLUTFFLAG="false"
fi
#ensure the mysql database and user exist
if ! [ "`check_mysql "$SQLPASSWORD" "$SQLDATABASE" "$SQLLOCATION" "$SQLUSER"`" == "$SQLDATABASE" ]; then
unable_exit "MySQL '$SQLDATABASE' database does not exist, unable to upgrade."
fi
#create the tmp directories
mkdir -p $TMPDIR/openemr_web_$OLD_VERSION
mkdir -p $TMPDIR/openemr_mysql_$OLD_VERSION
#backup web directory to tmp
cp -fr $OPENEMR/* $TMPDIR/openemr_web_$OLD_VERSION/
#backup mysql database to tmp
mysqldump -u "$SQLUSER" -h "$SQLLOCATION" --password="$SQLPASSWORD" "$SQLDATABASE" > $TMPDIR/openemr_mysql_$OLD_VERSION/openemr_mysql_$OLD_VERSION.sql
#clear the temporary openemr cache directories (no need to keep these during upgrade)
rm -fr $OPENEMR/interface/main/calendar/modules/PostCalendar/pntemplates/compiled/*
rm -fr $OPENEMR/interface/main/calendar/modules/PostCalendar/pntemplates/cache/*
rm -fr $OPENEMR/gacl/admin/templates_c/*
output_both "Upgrading OpenEMR from $OLD_VERSION..."
#Create the config file string, which will pass info to postinst script
#to continue upgrade process.
SETTING="#Optional settings\n\
#(currently empty, plan to use in subsequent versions of OpenEMR)\n\
\n\
#Installation settings\n\
# (DO NOT EDIT below!!!)\n\
process=pending\n\
plan=upgrade\n\
pass=\n\
previous_version=$OLD_VERSION\n\
sqllocation=$SQLLOCATION\n\
sqluser=$SQLUSER\n\
sqlpassword=$SQLPASSWORD\n\
sqldatabase=$SQLDATABASE\n\
sqlutfflag=$SQLUTFFLAG"
mkdir -p $CONFIGDIR
echo -e $SETTING > $CONFIG
}
case "$1" in
install)
#create the log file directory
mkdir -p $LOGDIR
#Check for /var/www/openemr, if exist then see if upgrade is possible
if [ -d "$OPENEMR" ]; then
#collect current version
v1=$(collect_var \$v_major $OPENEMR/version.php)
v2=$(collect_var \$v_minor $OPENEMR/version.php)
v3=$(collect_var \$v_patch $OPENEMR/version.php)
v4=$(collect_var \$v_tag $OPENEMR/version.php)
RAWVERSION="$v1.$v2.$v3$v4"
#attempt upgrade, if user desires
echo ""
echo -n "Found a version of openemr at $OPENEMR. Do you want to attempt an upgrade (ensure you have a backup before proceeding)? [y/N]"
read CONFIRM
if ! [[ $CONFIRM =~ [Yy] ]]; then
unable_exit "You have chosen to not install the OpenEMR package."
fi
upgrade_function $RAWVERSION "raw"
exit 0
fi
#Begin the config file string
# this is so the postinst script can follow installation
# variables:
# process states whether installation/upgrade is pending or complete
# plan states whether install or upgrade
# pass temporarily holds the mysql
SETTING="#Optional settings\n\
#(currently empty, plan to use in subsequent versions of OpenEMR)\n\
\n\
#Installation settings\n\
# (DO NOT EDIT below!!!)\n\
process=pending\n\
plan=install"
#If mysql is installed, then figure out root password
MYSQL=$(dpkg -l |grep mysql-server)
if [ -n "$MYSQL" ]; then
#set pass to blank as default
MPASS=""
## BEGIN MYSQL ROOT PASSWORD GRAB
if [ "`check_mysql "$MPASS" "mysql"`" != "mysql" ]; then
#the initial mysql password didn't work, so ask for password
COUNTDOWN=1
while true; do
echo ""
echo -n "Please enter your MySQL root password:"
read MPASS
echo ""
if [ "`check_mysql "$MPASS" "mysql"`" == "mysql" ]; then
#the mysql root password works, so can exit loop
break
else
#the mysql root password did not work
if [ "$COUNTDOWN" -ge "5" ]; then
output_both "5 attempts to enter your mysql root password have failed"
output_both "Recommend repeating OpenEMR installation when you know your mysql root password"
unable_exit "Giving up on OpenEMR package installation."
fi
echo "The entered MySQL root password did not work."
echo "$COUNTDOWN of 5 total attempts."
echo "PLEASE TRY AGAIN..."
fi
let "COUNTDOWN += 1"
done
fi
## END MYSQL ROOT PASSWORD GRAB
#successfully obtained mysql root password, so place it in the temp config file
SETTING="$SETTING\npass=$MPASS"
#now ensure the openemr user and database do not exist, if so then exit
# Check for openemr database in mysql, if exist then exit
if [ "`check_mysql "$MPASS" "$INSTALL_DATABASE"`" == "$INSTALL_DATABASE" ]; then
unable_exit "MySQL '$INSTALL_DATABASE' database already exists"
fi
# Check for user openemr in mysql.user, if exist then exit
USER=$(mysql -s -u root -h localhost --password="$MPASS" -e "SELECT User from mysql.user where User='$INSTALL_USER'")
if [ "$USER" == "$INSTALL_USER" ]; then
unable_exit "MySQl user '$INSTALL_USER' already exists"
fi
fi
#ready to install package, so create the config directory and files
mkdir -p $CONFIGDIR
echo -e $SETTING > $CONFIG
exit 0
;;
upgrade)
upgrade_function $2 "package"
exit 0
;;
abort-upgrade)
echo "preinst asked to do abort-upgrade"
exit 0
;;
*)
echo "preinst called with unknown argument \`$1'" >&2
exit 1
;;
esac