Ubuntu/debian package updates:
[openemr.git] / contrib / util / ubuntu_package_scripts / production / preinst
blobbe8c63b48eccbe70cc972ca0064ffac70e2aa6e0
1 #!/bin/bash -e
3 #This program is free software; you can redistribute it and/or modify
4 #it under the terms of the GNU General Public License as published by
5 #the Free Software Foundation; either version 2 of the License, or
6 #(at your option) any later version.
8 # Copyright 2012
9 # authors: Amalu Obinna <amaluobinna@aol.com>
10 # Brady Miller <brady@sparmy.com>
12 # Debian package pre-installation script steps:
13 # 1) Create log file
14 # 2) New install or Upgrade
15 # -Install
16 # a) If OpenEMR web directory already exist, then attempt an upgrade.
17 # -Upgrade (also works for multisite module, so can be mutliple databases)
18 # a) Ensure OpenEMR web directory does exist
19 # b) Collect package version and ensure appropriate upgrade
20 # c) Collect webpath and sql settings from current version
21 # d) Verify the mysql user(s)/database(s) exist
22 # e) Backup the current version mysql database(s)
23 # f) Backup the current version web folder
24 # g) Clear the cache directories in current version
25 # 3) Create config file
27 # Output log file:
28 # /var/log/openemr/install
31 # summary of how this script can be called:
32 # * <new-preinst> `install'
33 # * <new-preinst> `install' <old-version>
34 # * <new-preinst> `upgrade' <old-version>
35 # * <old-preinst> `abort-upgrade' <new-version>
36 # for details, see http://www.debian.org/doc/debian-policy/ or
37 # the debian-policy package
39 # Source debconf library.
40 . /usr/share/debconf/confmodule
42 #constants and paths
43 LOGDIR=/var/log/openemr
44 LOG=$LOGDIR/install
45 CONFIGDIR=/etc/openemr
46 CONFIG=$CONFIGDIR/openemr.conf
47 TMPDIR=/tmp/openemr-tmp
48 WEB=/var/www
49 OPENEMR=$WEB/openemr
50 SITEDIR=$OPENEMR/sites
51 #hardcoded mysql user and database for install (not pertinent for upgrading)
52 # upgrading can use whatever is found in openemr/library/sqlconf.php
53 INSTALL_USER=openemr
54 INSTALL_DATABASE=openemr
56 #Standardized echo function to send to only log file
57 # requires one parameter (string)
58 log_only () {
59 echo "`date`: $1" >> $LOG
62 #Standardized exit function to be used when unable to install the openemr package
63 # requires one parameter (string with reason for exiting)
64 unable_exit () {
65 echo "`date`: $1" >> $LOG
66 echo "`date`: EXITING.........." >> $LOG
67 exit 1
70 #function to prompt for input
71 # 1st param is name, 2nd param is priority, 3rd param is where result gets sent back in
72 # return the input
73 prompt_input () {
74 db_set "$1" ""
75 db_fset "$1" seen false
76 db_input "$2" "$1" || true
77 db_go || true
78 db_get "$1"
79 local input_value="$RET"
80 db_set "$1" ""
81 db_fset "$1" seen false
82 local __result=$3
83 eval $__result="'$input_value'"
86 #function to check mysql for selected databases
87 # 1st param is password, 2nd param database, 3rd param is host (optional), 4th param is user (optional)
88 check_mysql () {
89 if [ -n "$3" ]; then
90 HOST=$3
91 else
92 HOST=localhost
94 if [ -n "$4" ]; then
95 USE=$4
96 else
97 USE=root
100 if [ "`mysql -u "$USE" -h "$HOST" --password="$1" -e 'show databases' 2>/dev/null | awk '{ print $1}' | grep "^$2$"`" == "$2" ]; then
101 return 0
102 else
103 return 1
107 #function to collect variables from config files
108 # 1st param is variable name, 2nd param is filename
109 collect_var () {
110 echo `grep -i "^[[:space:]]*$1[[:space:]=]" $2 | cut -d \= -f 2 | cut -d \; -f 1 | sed "s/[ '\"]//gi"`
113 #function to check database before preparing to upgrade
114 # 1st param is the directory of database configuration script
115 # no return, but will exit the upgrade if database check fails
116 upgrade_check_database () {
118 #Collect the path of database configuration script
119 DATABASE_PATH="$1"
121 #Collect variables
122 SQL_LOCATION=$(collect_var \$host $DATABASE_PATH/sqlconf.php)
123 SQL_USER=$(collect_var \$login $DATABASE_PATH/sqlconf.php)
124 SQL_PASSWORD=$(collect_var \$pass $DATABASE_PATH/sqlconf.php)
125 SQL_DATABASE=$(collect_var \$dbase $DATABASE_PATH/sqlconf.php)
127 #ensure the mysql database and user exist
128 if check_mysql "$SQL_PASSWORD" "$SQL_DATABASE" "$SQL_LOCATION" "$SQL_USER"; then
129 log_only "For upgrade, confirmed that the mysql database ($SQL_DATABASE) and mysql user ($SQL_USER) exist"
130 else
131 prompt_input openemr/upgrade_not_database critical ret_result
132 unable_exit "MySQL '$SQL_DATABASE' database does not exist (or can't be accessed), unable to upgrade."
136 #function to process database to prepare for upgrade
137 # 1st param is the directory of database configuration script
138 # 2nd param is the sitename
139 # returns(via echo) pertinent configuration file information
140 upgrade_prepare_database () {
142 #Collect the path of database configuration script
143 DATABASE_PATH="$1"
145 #Collect the site name
146 SITE_NAME="$2"
148 #Collect variables
149 SQL_LOCATION=$(collect_var \$host $DATABASE_PATH/sqlconf.php)
150 SQL_USER=$(collect_var \$login $DATABASE_PATH/sqlconf.php)
151 SQL_PASSWORD=$(collect_var \$pass $DATABASE_PATH/sqlconf.php)
152 SQL_DATABASE=$(collect_var \$dbase $DATABASE_PATH/sqlconf.php)
153 SQL_UTFFLAG=$(collect_var \$disable_utf8_flag $DATABASE_PATH/sqlconf.php)
155 #if SQLUTFFLAG variable is empty, then make it false
156 if [ "$SQL_UTFFLAG" == "" ]; then
157 SQL_UTFFLAG="false"
160 #write variables to config file string
161 TEMP_SETTING=""
162 TEMP_SETTING=$TEMP_SETTING$SITE_NAME"_sqllocation=$SQL_LOCATION\n"
163 TEMP_SETTING=$TEMP_SETTING$SITE_NAME"_sqluser=$SQL_USER\n"
164 TEMP_SETTING=$TEMP_SETTING$SITE_NAME"_sqlpassword=$SQL_PASSWORD\n"
165 TEMP_SETTING=$TEMP_SETTING$SITE_NAME"_sqldatabase=$SQL_DATABASE\n"
166 TEMP_SETTING=$TEMP_SETTING$SITE_NAME"_sqlutfflag=$SQL_UTFFLAG\n"
168 #create the tmp sql directory
169 mkdir -p "$TMPDIR/openemr_mysql_${OLD_VERSION}_${SITE_NAME}"
171 #backup mysql database to tmp
172 mysqldump -u "$SQL_USER" -h "$SQL_LOCATION" --password="$SQL_PASSWORD" "$SQL_DATABASE" > "$TMPDIR/openemr_mysql_${OLD_VERSION}_${SITE_NAME}/openemr_mysql_${OLD_VERSION}_${SITE_NAME}.sql"
174 #return the settings (to be used for the config file passed to postinst
175 echo $TEMP_SETTING
178 #function to hold the upgrade code (allow more flexible upgrading)
179 # 1st param is the previous version,
180 # 2nd param is whether raw or package upgrade
181 upgrade_function () {
183 #Check for /var/www/openemr. If does not exist, then exit.
184 if ! [ -d "$OPENEMR" ]; then
185 prompt_input openemr/upgrade_not_installed critical ret_result
186 unable_exit "OpenEMR is not installed ($OPENEMR), so can not upgrade the OpenEMR Package."
189 #collect previous version
190 OLD_VERSION=$1
192 #collect type of upgrade (either package or raw)
193 UPGRADETYPE=$2
195 #ensure version is appropriate for upgrade (if not, then exit!!)
197 # ONLY acceptable for raw is 3.0.0 or 3.0.0.1 or 3.1.0 (need to convert these)
199 if [ "$UPGRADETYPE" == "raw" ]; then
200 if [ "$OLD_VERSION" == "3.0.0" ]; then
201 OLD_VERSION="3.0.1-1"
202 elif [ "$OLD_VERSION" == "3.0.0.1" ]; then
203 OLD_VERSION="3.0.1-1"
204 elif [ "$OLD_VERSION" == "3.0.1" ]; then
205 OLD_VERSION="3.0.1-1"
206 elif [ "$OLD_VERSION" == "3.1.0" ]; then
207 OLD_VERSION="3.1.0-1"
208 elif [ "$OLD_VERSION" == "3.2.0" ]; then
209 OLD_VERSION="3.2.0-1"
210 elif [ "$OLD_VERSION" == "3.2.0.1" ]; then
211 OLD_VERSION="3.2.0-1"
212 elif [ "$OLD_VERSION" == "4.0.0" ]; then
213 OLD_VERSION="4.0.0-1"
214 elif [ "$OLD_VERSION" == "4.1.0" ]; then
215 OLD_VERSION="4.1.0-1"
216 else
217 prompt_input openemr/upgrade_not_supported critical ret_result
218 unable_exit "Unable to upgrade from $OLD_VERSION version package with $UPGRADETYPE method, so can not upgrade the OpenEMR Package."
222 # ONLY acceptable package and converted raw is 3.0.1-1 or 3.1.0-1 or 3.2.0-1 or 4.0.0-1 or 4.1.0-1
224 if [ "$OLD_VERSION" != "3.0.1-1" ] && [ "$OLD_VERSION" != "3.1.0-1" ] && [ "$OLD_VERSION" != "3.2.0-1" ]&& [ "$OLD_VERSION" != "4.0.0-1" ] && [ "$OLD_VERSION" != "4.1.0-1" ]; then
225 prompt_input openemr/upgrade_not_supported critical ret_result
226 unable_exit "Unable to upgrade from $OLD_VERSION version package with $UPGRADETYPE method, so can not upgrade the OpenEMR Package."
229 #Initiate the config file string, which will pass info to postinst script
230 #to continue upgrade process.
231 SETTING="#Optional settings\n\
232 #(currently empty, plan to use in subsequent versions of OpenEMR)\n\
234 #Installation settings\n\
235 # (DO NOT EDIT below!!!)\n\
236 process=pending\n\
237 plan=upgrade\n\
238 previous_version=$OLD_VERSION\n"
240 #collect openemr mysql data
241 #(will need to look in sites directory if 4.0 or later)
242 #Also supports multisite module, which can have multiple separate databases
243 if [ "$OLD_VERSION" == "3.0.1-1" ] || [ "$OLD_VERSION" == "3.1.0-1" ] || [ "$OLD_VERSION" == "3.2.0-1" ]; then
245 #check database (will exit upgrade if check fails)
246 upgrade_check_database $OPENEMR/library
248 #Prepare uprade of database and collect settings that will placed in the config file string
249 DATABASE_CONFIG=$(upgrade_prepare_database $OPENEMR/library default)
250 SETTING=$SETTING$DATABASE_CONFIG
252 else
253 for dir in $(find $SITEDIR/* -maxdepth 0 -type d ); do
254 #collect sitename
255 SITENAME=$(basename "$dir")
257 #check database (will exit upgrade if check fails)
258 upgrade_check_database $SITEDIR/$SITENAME
260 #Prepare uprade of database and collect settings that will placed in the config file string
261 DATABASE_CONFIG=$(upgrade_prepare_database $SITEDIR/$SITENAME $SITENAME)
262 SETTING=$SETTING$DATABASE_CONFIG
263 done
266 #create the tmp web directory
267 mkdir -p $TMPDIR/openemr_web_$OLD_VERSION
269 #backup web directory to tmp
270 cp -fr $OPENEMR/* $TMPDIR/openemr_web_$OLD_VERSION/
272 #clear the temporary openemr cache directories (no need to keep these during upgrade)
273 rm -fr $OPENEMR/interface/main/calendar/modules/PostCalendar/pntemplates/compiled/*
274 rm -fr $OPENEMR/interface/main/calendar/modules/PostCalendar/pntemplates/cache/*
275 rm -fr $OPENEMR/gacl/admin/templates_c/*
277 log_only "Upgrading OpenEMR from $OLD_VERSION..."
279 #Create the config file, which will pass info to postinst script
280 #to continue upgrade process.
281 mkdir -p $CONFIGDIR
282 echo -e $SETTING > $CONFIG
286 case "$1" in
287 install)
289 #create the log file directory
290 mkdir -p $LOGDIR
292 #Check for /var/www/openemr, if exist then see if upgrade is possible
293 if [ -d "$OPENEMR" ]; then
295 #collect current version
296 #(for post-4.0 release will need to look in version.php file for version)
297 #(for post-4.0 release will need to use v_tag rather than tag)
298 #(if version.php exists then look there, otherwise get from globals)
299 if [ -e "$OPENEMR/version.php" ]; then
300 v1=$(collect_var \$v_major $OPENEMR/version.php)
301 v2=$(collect_var \$v_minor $OPENEMR/version.php)
302 v3=$(collect_var \$v_patch $OPENEMR/version.php)
303 v4=$(collect_var \$v_tag $OPENEMR//version.php)
304 else
305 v1=$(collect_var \$v_major $OPENEMR/interface/globals.php)
306 v2=$(collect_var \$v_minor $OPENEMR/interface/globals.php)
307 v3=$(collect_var \$v_patch $OPENEMR/interface/globals.php)
308 v4=$(collect_var \$tag $OPENEMR/interface/globals.php)
310 RAWVERSION="$v1.$v2.$v3$v4"
312 #attempt upgrade, if user desires
313 prompt_input openemr/upgrade_confirm critical ret_result
314 if [ "$ret_result" == "no" ]; then
315 prompt_input openemr/upgrade_no critical ret_result
316 unable_exit "You have chosen to not install the OpenEMR package."
318 upgrade_function $RAWVERSION "raw"
320 exit 0
323 #Create the config file to pass to the postinst script
324 # to signal that this is a new install
325 SETTING="#Optional settings\n\
326 #(currently empty, plan to use in subsequent versions of OpenEMR)\n\
328 #Installation settings\n\
329 # (DO NOT EDIT below!!!)\n\
330 process=pending\n\
331 plan=install"
332 mkdir -p $CONFIGDIR
333 echo -e $SETTING > $CONFIG
335 exit 0
338 upgrade)
340 upgrade_function $2 "package"
342 exit 0
345 abort-upgrade)
346 echo "preinst asked to do abort-upgrade"
347 exit 0
351 echo "preinst called with unknown argument \`$1'" >&2
352 exit 1
354 esac