ubuntu debian package fix for 4.0 - fix upgrading with site stuff
[openemr.git] / contrib / util / ubuntu_package_scripts / production / postinst
blob587e540b71e60edd04f3389f5ae15ea1a913e50f
1 #!/bin/bash
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 # authors: Amalu Obinna <amaluobinna@aol.com>
9 # Brady Miller <brady@sparmy.com>
11 # date: 10/05/10
13 # Debian package post installation script steps:
14 # 1) Collect setting from package configuration file
15 # 2) Install or Upgrade
16 # -Install
17 # a) Ensure OpenEMR MySQL database and user do not exist.
18 # b) If MySQL is already installed:
19 # -Collect the MySQL root password
20 # -ensure openemr mysql database/user does not exist
21 # c) Configure OpenEMR
22 # d) Configure Apache
23 # e) Configure PHP
24 # -Upgrade
25 # a) Modify new OpenEMR version configuration files
26 # b) Upgrade MySQL database
27 # c) Upgrade Access Controls
28 # d) Copy over old configuration files
29 # (Copy to files with .OLD extension to allow manual comparisons by user)
30 # e) Update PHP settings with new recommendations (not needed yet)
31 # f) Modify permissions for writable directories
32 # g) Secure the php installation/upgrading scripts
33 # 3) Modify the package configuration file
34 # 4) Echo instructions on starting openemr
36 # summary of how this script can be called:
37 # * <postinst> `configure' <most-recently-configured-version>
38 # * <old-postinst> `abort-upgrade' <new version>
39 # * <conflictor's-postinst> `abort-remove' `in-favour' <package>
40 # <new-version>
41 # * <postinst> `abort-remove'
42 # * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
43 # <failed-install-package> <version> `removing'
44 # <conflicting-package> <version>
45 # for details, see http://www.debian.org/doc/debian-policy/ or
46 # the debian-policy package
48 case "$1" in
49 configure)
51 #constants and paths
52 LOGDIR=/var/log/openemr
53 LOG=$LOGDIR/install
54 CONFIGDIR=/etc/openemr
55 CONFIG=$CONFIGDIR/openemr.conf
56 TMPDIR=/tmp/openemr-tmp
57 WEB=/var/www
58 OPENEMR=$WEB/openemr
59 SITEDIR=$OPENEMR/sites/default
60 #hardcoded mysql user and database for install (not pertinent for upgrading)
61 # upgrading can use whatever is found in openemr/library/sqlconf.php
62 INSTALL_USER=openemr
63 INSTALL_DATABASE=openemr
64 #auto install scripts
65 INST=$OPENEMR/contrib/util/installScripts/InstallerAuto.php
66 INSTTEMP=$OPENEMR/contrib/util/installScripts/InstallerAutoTemp.php
67 #php and apache files
68 PHP=/etc/php5/apache2/php.ini
69 APACHE=/etc/apache2/httpd.conf
70 #web user and group
71 WEB_GROUP=www-data
72 WEB_USER=www-data
74 #Standardized echo function to send to both echo and to log file
75 # requires one parameter (string)
76 output_both () {
77 echo $1
78 echo "`date`: $1" >> $LOG
81 #Standardized echo function to send to only log file
82 # requires one parameter (string)
83 log_only () {
84 echo "`date`: $1" >> $LOG
87 #Standardized exit functions to be used
88 # requires one parameter (string with reason for exiting)
89 unable_exit () {
90 echo $1
91 echo "`date`: $1" >> $LOG
92 echo "EXITING.........."
93 echo "`date`: EXITING.........." >> $LOG
94 sleep 5
95 exit 1
98 #function to check mysql for selected databases
99 # 1st param is password, 2nd param database, 3rd param is host (optional), 4th param is user (optional)
100 check_mysql () {
101 if [ -n "$3" ]; then
102 HOST=$3
103 else
104 HOST=localhost
106 if [ -n "$4" ]; then
107 USE=$4
108 else
109 USE=root
111 echo `mysql -u "$USE" -h "$HOST" --password="$1" -e 'show databases' 2>/dev/null | awk '{ print $1}' | grep "^$2$"`
114 #function to collect variables from config files
115 # 1st param is variable name, 2nd param is filename
116 collect_var () {
117 echo `grep -i "^[[:space:]]*$1[[:space:]=]" $2 | cut -d \= -f 2 | cut -d \; -f 1 | sed "s/[ '\"]//gi"`
120 #function to insert variables into config files
121 # 1st param is variable name, 2nd param is variable, 3rd param is filename
122 insert_var () {
123 sed -i 's@^[ ]*'"$1"'[ =].*$@'"$1"' = '"$2"'@' "$3"
126 #collect scripting information from config file
127 PROCESS=$(collect_var process $CONFIG)
128 PLAN=$(collect_var plan $CONFIG)
129 MPASS=$(collect_var pass $CONFIG)
131 #Don't allow re-configuration
132 if [ "$PROCESS" == "complete" ] ; then
133 unable_exit "OpenEMR has already been configured."
134 elif [ "$PROCESS" == "pending" ] ; then
135 #continue with configuration
136 log_only "Configuring package..."
137 else
138 unable_exit "Error reading process variable in configuration file."
141 if [ "$PLAN" == "upgrade" ] ; then
142 #continue with upgrade
144 #collect more information from config file
145 OLD_VERSION=$(collect_var previous_version $CONFIG)
146 SQLLOCATION=$(collect_var sqllocation $CONFIG)
147 SQLUSER=$(collect_var sqluser $CONFIG)
148 SQLPASSWORD=$(collect_var sqlpassword $CONFIG)
149 SQLDATABASE=$(collect_var sqldatabase $CONFIG)
150 SQLUTFFLAG=$(collect_var sqlutfflag $CONFIG)
152 #configure openemr/sites/default/sqlconf.php
153 insert_var "\$host" "\'$SQLLOCATION\';" $SITEDIR/sqlconf.php
154 insert_var "\$login" "\'$SQLUSER\';" $SITEDIR/sqlconf.php
155 insert_var "\$pass" "\'$SQLPASSWORD\';" $SITEDIR/sqlconf.php
156 insert_var "\$dbase" "\'$SQLDATABASE\';" $SITEDIR/sqlconf.php
157 insert_var "\$disable_utf8_flag" "$SQLUTFFLAG;" $SITEDIR/sqlconf.php
158 sed -i "s/^[ ]*\$config[ =].*0/\$config = 1/" $SITEDIR/sqlconf.php
160 #before run scripts, go to openemr directory
161 cd $OPENEMR
163 #upgrade the sql database
164 CONC_VERSION=$(echo $OLD_VERSION | cut -d \- -f 1)
165 echo "<?php \$_GET['site'] = 'default'; ?>" > $OPENEMR/TEMPsql_upgrade.php
166 cat $OPENEMR/sql_upgrade.php >> $OPENEMR/TEMPsql_upgrade.php
167 sed -i "/input type='submit'/d" $OPENEMR/TEMPsql_upgrade.php
168 sed -i "s/!empty(\$_POST\['form_submit'\])/empty(\$_POST\['form_submit'\])/" $OPENEMR/TEMPsql_upgrade.php
169 sed -i "s/^[ ]*\$form_old_version[ =].*$/\$form_old_version = \"$CONC_VERSION\";/" $OPENEMR/TEMPsql_upgrade.php
170 php -f $OPENEMR/TEMPsql_upgrade.php >> $LOG
171 rm -f $OPENEMR/TEMPsql_upgrade.php
173 #upgrade the gacl controls
174 echo "<?php \$_GET['site'] = 'default'; ?>" > $OPENEMR/TEMPacl_upgrade.php
175 cat $OPENEMR/acl_upgrade.php >> $OPENEMR/TEMPacl_upgrade.php
176 php -f $OPENEMR/TEMPacl_upgrade.php >> $LOG
177 rm -f $OPENEMR/TEMPacl_upgrade.php
179 #copy the old config file into new with the OLD at end to allow manual configuration of old
180 # optional settings.
181 if [ -d $TMPDIR/openemr_web_$OLD_VERSION/sites/default ]; then
182 cp -f $TMPDIR/openemr_web_$OLD_VERSION/sites/default/config.php $SITEDIR/config.php.OLD
183 else
184 cp -f $TMPDIR/openemr_web_$OLD_VERSION/includes/config.php $SITEDIR/config.php.OLD
187 # if site-specific directories are in the old locations, move them.
188 if [ -d $OPENEMR/documents ]; then
189 mv -f $OPENEMR/documents/* $SITEDIR/documents/
190 rm -rf $OPENEMR/documents
192 if [ -d $OPENEMR/era ]; then
193 mv -f $OPENEMR/era/* $SITEDIR/era/
194 rm -rf $OPENEMR/era
196 if [ -d $OPENEMR/edi ]; then
197 mv -f $OPENEMR/edi/* $SITEDIR/edi/
198 rm -rf $OPENEMR/edi
200 if [ -d $OPENEMR/custom/letter_templates ]; then
201 mv -f $OPENEMR/custom/letter_templates/* $SITEDIR/letter_templates/
202 rm -rf $OPENEMR/custom/letter_templates
205 #upgrade php settings if change or have new recs in future (none yet)
207 #secure openemr
208 chown -Rf root:root $OPENEMR
209 chmod 600 $OPENEMR/acl_setup.php
210 chmod 600 $OPENEMR/acl_upgrade.php
211 chmod 600 $OPENEMR/sl_convert.php
212 chmod 600 $OPENEMR/setup.php
213 chmod 600 $OPENEMR/sql_upgrade.php
214 chmod 600 $OPENEMR/ippf_upgrade.php
215 chmod 600 $OPENEMR/gacl/setup.php
216 chmod 600 $OPENEMR/admin.php
218 #set writable directories
219 chown -R $WEB_GROUP.$WEB_USER $SITEDIR/documents
220 chown -R $WEB_GROUP.$WEB_USER $SITEDIR/edi
221 chown -R $WEB_GROUP.$WEB_USER $SITEDIR/era
222 chown -R $WEB_GROUP.$WEB_USER $OPENEMR/library/freeb
223 chown -R $WEB_GROUP.$WEB_USER $SITEDIR/letter_templates
224 chown -R $WEB_GROUP.$WEB_USER $OPENEMR/interface/main/calendar/modules/PostCalendar/pntemplates/cache
225 chown -R $WEB_GROUP.$WEB_USER $OPENEMR/interface/main/calendar/modules/PostCalendar/pntemplates/compiled
226 chown -R $WEB_GROUP.$WEB_USER $OPENEMR/gacl/admin/templates_c
228 #update config file, change process to complete and remove others
229 sed -i "s/^[ ]*process[ =].*$/process=complete/" $CONFIG
230 sed -i "/^[ ]*plan[ =].*$/d" $CONFIG
231 sed -i "/^[ ]*pass[ =].*$/d" $CONFIG
232 sed -i "/^[ ]*previous_version[ =].*$/d" $CONFIG
233 sed -i "/^[ ]*sqllocation[ =].*$/d" $CONFIG
234 sed -i "/^[ ]*sqluser[ =].*$/d" $CONFIG
235 sed -i "/^[ ]*sqlpassword[ =].*$/d" $CONFIG
236 sed -i "/^[ ]*sqldatabase[ =].*$/d" $CONFIG
237 sed -i "/^[ ]*sqlutfflag[ =].*$/d" $CONFIG
239 #done upgrading
240 echo ""
241 echo "-----------------------------------------------------"
242 echo ""
243 output_both "OpenEMR upgrade is complete."
244 echo ""
245 output_both "Recommend setting optional configuration settings in:"
246 output_both "$SITEDIR/config.php"
247 output_both "(We have renamed your old configuration files to *.OLD)"
248 output_both "(We recommend you delete the *.OLD files when done)"
249 echo ""
250 output_both "We have placed backup of your old OpenEMR in $TMPDIR"
251 output_both "(We recommend you copy this somewhere protected since it"
252 output_both "contains confidential patient information)"
253 echo ""
254 echo "-----------------------------------------------------"
256 sleep 5
257 exit 0
259 elif [ "$PLAN" == "install" ] ; then
260 #continue with installation
261 log_only "Installing OpenEMR"
262 else
263 unable_exit "Error reading plan variable in configuration file."
266 ## BEGIN MYSQL ROOT PASSWORD GRAB
267 if [ "`check_mysql "$MPASS" "mysql"`" != "mysql" ]; then
268 #the initial mysql password didn't work, so ask for password
269 COUNTDOWN=1
270 while true; do
271 echo ""
272 echo -n "Please enter your MySQL root password:"
273 read MPASS
274 echo ""
275 if [ "`check_mysql "$MPASS" "mysql"`" == "mysql" ]; then
276 #the mysql root password works, so can exit loop
277 break
278 else
279 #the mysql root password did not work
280 if [ "$COUNTDOWN" -ge "5" ]; then
281 output_both "5 attempts to enter your mysql root password have failed"
282 output_both "Recommend repeating OpenEMR installation when you know your mysql root password"
283 unable_exit "Giving up on OpenEMR package installation."
285 echo "The entered MySQL root password did not work."
286 echo "$COUNTDOWN of 5 total attempts."
287 echo "PLEASE TRY AGAIN..."
289 let "COUNTDOWN += 1"
290 done
292 ## END MYSQL ROOT PASSWORD GRAB
294 #now ensure the openemr user and database do not exist, if so then exit
295 # Check for openemr database in mysql, if exist then exit
296 if [ "`check_mysql "$MPASS" "$INSTALL_DATABASE"`" == "$INSTALL_DATABASE" ]; then
297 unable_exit "MySQL '$INSTALL_DATABASE' database already exists"
299 # Check for OpenEMR user in mysql.user, if exist then exit
300 USER=$(mysql -s -u root -h localhost --password="$MPASS" -e "SELECT User from mysql.user where User='$INSTALL_USER'")
301 if [ "$USER" == "$INSTALL_USER" ]; then
302 unable_exit "MySQl user '$INSTALL_USER' already exists"
305 #go to openemr directory
306 cd $OPENEMR
308 #secure openemr
309 chown -Rf root:root $OPENEMR
311 #INSTALL AND CONFIGURE OPENEMR
312 output_both "Configuring OpenEMR"
314 # Create a random password for the openemr mysql user
315 password=$(makepasswd --char=12)
317 # openemr installation VARIABLES
318 if [ "$MPASS" == "" ] ; then
319 rootpass="rootpass=BLANK" #MySQL server root password
320 else
321 rootpass="rootpass=$MPASS" #MySQL server root password
323 login="login=$INSTALL_USER" #username to MySQL openemr database
324 pass="pass=$password" #password to MySQL openemr database
325 dbname="dbname=$INSTALL_DATABASE" #MySQL openemr database name
327 # Set file and directory permissions
328 chmod 666 $SITEDIR/sqlconf.php
329 chown -R $WEB_GROUP.$WEB_USER $SITEDIR/documents
330 chown -R $WEB_GROUP.$WEB_USER $SITEDIR/edi
331 chown -R $WEB_GROUP.$WEB_USER $SITEDIR/era
332 chown -R $WEB_GROUP.$WEB_USER $OPENEMR/library/freeb
333 chown -R $WEB_GROUP.$WEB_USER $SITEDIR/letter_templates
334 chown -R $WEB_GROUP.$WEB_USER $OPENEMR/interface/main/calendar/modules/PostCalendar/pntemplates/cache
335 chown -R $WEB_GROUP.$WEB_USER $OPENEMR/interface/main/calendar/modules/PostCalendar/pntemplates/compiled
336 chown -R $WEB_GROUP.$WEB_USER $OPENEMR/gacl/admin/templates_c
338 # Run Auto Installer
340 sed -e 's@^exit;@ @' <$INST >$INSTTEMP
341 php -f $INSTTEMP $rootpass $login $pass $dbname >> $LOG
342 rm -f $INSTTEMP
344 #remove global permission to all setup scripts
345 chmod 600 $OPENEMR/acl_setup.php
346 chmod 600 $OPENEMR/acl_upgrade.php
347 chmod 600 $OPENEMR/sl_convert.php
348 chmod 600 $OPENEMR/setup.php
349 chmod 600 $OPENEMR/sql_upgrade.php
350 chmod 600 $OPENEMR/ippf_upgrade.php
351 chmod 600 $OPENEMR/gacl/setup.php
353 log_only "Done configuring OpenEMR"
355 #This section configures Apache for OpenEMR
356 output_both "Configuring Apache for OpenEMR"
358 #Check to ensure the apache configuration files exists
359 if [ -f $APACHE ]; then
361 # First, backup the httpd.conf file before modifying
362 cp -f $APACHE $APACHE.BAK
364 # Second, append information to secure selected directories in OpenEMR
365 echo "#This is the start of the Apache configuration for OpenEMR." >> $APACHE
366 echo "#Below will secure directories with patient information." >> $APACHE
367 echo "<Directory \"$SITEDIR/documents\">" >> $APACHE
368 echo " order deny,allow" >> $APACHE
369 echo " Deny from all" >> $APACHE
370 echo "</Directory>" >> $APACHE
371 echo "<Directory \"$SITEDIR/edi\">" >> $APACHE
372 echo " order deny,allow" >> $APACHE
373 echo " Deny from all" >> $APACHE
374 echo "</Directory>" >> $APACHE
375 echo "<Directory \"$SITEDIR/era\">" >> $APACHE
376 echo " order deny,allow" >> $APACHE
377 echo " Deny from all" >> $APACHE
378 echo "</Directory>" >> $APACHE
379 echo "#This is the end of the Apache configuration for OpenEMR." >> $APACHE
381 #let user know the plan
382 output_both "Added entries to apache configuration to secure directories with patient information."
383 output_both "Placed backup of your original apache configuration file to $APACHE.BAK"
385 else
386 #can't find apache config file, so just echo instructions
387 echo ""
388 output_both "We recommend placing below lines into your apache configuration file:"
389 output_both "#This is the start of the Apache configuration for OpenEMR."
390 output_both "#Below will secure directories with patient information."
391 output_both "<Directory \"$SITEDIR/documents\">"
392 output_both " order deny,allow"
393 output_both " Deny from all"
394 output_both "</Directory>"
395 output_both "<Directory \"$SITEDIR/edi\">"
396 output_both " order deny,allow"
397 output_both " Deny from all"
398 output_both "</Directory>"
399 output_both "<Directory \"$SITEDIR/era\">"
400 output_both " order deny,allow"
401 output_both " Deny from all"
402 output_both "</Directory>"
403 output_both "#This is the end of the Apache configuration for OpenEMR."
404 echo ""
407 log_only "Done configuring Apache"
409 #This Section edits the php.ini file to accomodate the proper functioning of OpenEMR using php
410 output_both "Configuring PHP for OpenEMR"
412 #check to ensure the php configuration file exists
413 if [ -f $PHP ]; then
414 # First, collect php variables
415 collect_php () {
416 echo `grep -i "^[[:space:]]*$1[[:space:]=]" $PHP | cut -d \= -f 2 | cut -d \; -f 1 | sed 's/[ M]//gi'`
418 TAG_TEXT="short_open_tag"
419 TAG=$(collect_php "$TAG_TEXT")
420 EXEC_TEXT="max_execution_time"
421 EXEC=$(collect_php "$EXEC_TEXT")
422 INPUT_TEXT="max_input_time"
423 INPUT=$(collect_php "$INPUT_TEXT")
424 MEM_TEXT="memory_limit"
425 MEM=$(collect_php "$MEM_TEXT")
426 DISP_TEXT="display_errors"
427 DISP=$(collect_php "$DISP_TEXT")
428 LOGG_TEXT="log_errors"
429 LOGG=$(collect_php "$LOGG_TEXT")
430 GLOB_TEXT="register_globals"
431 GLOB=$(collect_php "$GLOB_TEXT")
432 POST_TEXT="post_max_size"
433 POST=$(collect_php "$POST_TEXT")
434 MAGIC_TEXT="magic_quotes_gpc"
435 MAGIC=$(collect_php "$MAGIC_TEXT")
436 UPLOAD_TEXT="file_uploads"
437 UPLOAD=$(collect_php "$UPLOAD_TEXT")
438 FILESIZE_TEXT="upload_max_filesize"
439 FILESIZE=$(collect_php "$FILESIZE_TEXT")
441 # Second, backup the php.ini file before modifying
442 cp $PHP $PHP.BAK
444 # Third, edit the required entries
445 # Do this in a for loop.
446 # First iteration will discover the recommended changes
447 # Second iteration will make the changes (if user request this)
448 FLAG_ON=0
449 process_php () {
450 if [ "$3" -eq "1" ]; then
451 # make rec to php.ini
452 if [ "$FLAG_ON" -eq "0" ]; then
453 output_both "We changed the following setting(s) in your php configuration file at $PHP :"
455 FLAG_ON=1
456 else
457 # modify php.ini
458 sed -i "s/^[ ]*$1[ =].*$/$1 = $2/" $PHP
459 output_both "Successfully set $1 = $2"
462 for i in `seq 1 2`; do
463 if [ "$TAG" != "On" ]; then
464 process_php "$TAG_TEXT" "On" $i
466 if [ "$EXEC" -lt "60" ]; then
467 process_php "$EXEC_TEXT" "60" $i
469 if [ "$INPUT" -lt "90" ]; then
470 process_php "$INPUT_TEXT" "90" $i
472 if [ "$MEM" -lt "128" ]; then
473 process_php "$MEM_TEXT" "128M" $i
475 if [ "$DISP" != "Off" ]; then
476 process_php "$DISP_TEXT" "Off" $i
478 if [ "$LOGG" != "On" ]; then
479 process_php "$LOGG_TEXT" "On" $i
481 if [ "$GLOB" != "Off" ]; then
482 process_php "$GLOB_TEXT" "Off" $i
484 if [ "$POST" -lt "30" ]; then
485 process_php "$POST_TEXT" "30M" $i
487 if [ "$MAGIC" != "On" ]; then
488 process_php "$MAGIC_TEXT" "On" $i
490 if [ "$UPLOAD" != "On" ]; then
491 process_php "$UPLOAD_TEXT" "On" $i
493 if [ "$FILESIZE" -lt "30" ]; then
494 process_php "$FILESIZE_TEXT" "30M" $i
496 if [ "$FLAG_ON" -eq "0" ]; then
497 output_both "Your PHP configuration is perfect for OpenEMR."
498 break
500 if [ "$i" -eq "1" ]; then
501 output_both "(We have placed a backup of your php configuration at $PHP.BAK)"
503 done
504 else
505 #can't find php config file, so just echo instructions
506 echo ""
507 output_both "We recommend ensuring you have below settings in your php configuration file:"
508 output_both "short_open_tag = On"
509 output_both "max_execution_time = 60"
510 output_both "max_input_time = 90"
511 output_both "memory_limit = 128M"
512 output_both "display_errors = Off"
513 output_both "log_errors = On"
514 output_both "register_globals = Off"
515 output_both "post_max_size = 30M"
516 output_both "magic_quotes_gpc = On"
517 output_both "file_uploads = On"
518 output_both "upload_max_filesize = 30M"
519 echo ""
522 log_only "Done configuring PHP"
524 output_both "Restarting Apache service"
525 invoke-rc.d apache2 restart >> $LOG
527 echo "--------------------------------------------------"
528 echo ""
529 output_both "You can now use OpenEMR by browsing to:"
530 output_both "http://localhost/openemr"
531 output_both "user is 'admin' and password is 'pass'"
532 echo ""
533 output_both "See the openemr man page for further instructions:"
534 output_both "type 'man openemr' at command line"
535 echo ""
536 echo "--------------------------------------------------"
538 #update config file, change process to complete and remove plan and pass
539 sed -i "s/^[ ]*process[ =].*$/process=complete/" $CONFIG
540 sed -i "/^[ ]*plan[ =].*$/d" $CONFIG
541 sed -i "/^[ ]*pass[ =].*$/d" $CONFIG
543 sleep 5
544 exit 0
546 abort-upgrade|abort-remove|abort-deconfigure)
548 echo "postinst asked to do $1"
549 exit 0
552 echo "postinst called with unknown argument \`$1'" >&2
553 exit 1
555 esac
557 sleep 5
558 exit 0