prepping ubuntu packages
[openemr.git] / contrib / util / ubuntu_package_scripts / development / postinst
blobdaf142b9261fec46144497f4c658cea9baf5cb0a
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 2011-2014
9 # authors: Brady Miller <brady@sparmy.com>
10 # Amalu Obinna <amaluobinna@aol.com>
12 # Debian package post installation script steps:
13 # 1) Collect mysql root password (optional)
14 # 2) Download OpenEMR from official github repository
15 # 3) Install OpenEMR
16 # 4) Configure OpenEMR (if applicable)
18 # summary of how this script can be called:
19 # * <postinst> `configure' <most-recently-configured-version>
20 # * <old-postinst> `abort-upgrade' <new version>
21 # * <conflictor's-postinst> `abort-remove' `in-favour' <package>
22 # <new-version>
23 # * <postinst> `abort-remove'
24 # * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
25 # <failed-install-package> <version> `removing'
26 # <conflicting-package> <version>
27 # for details, see http://www.debian.org/doc/debian-policy/ or
28 # the debian-policy package
30 # Source debconf library.
31 . /usr/share/debconf/confmodule
33 case "$1" in
34 configure)
36 #constants and paths
37 LOGDIR=/var/log/git-openemr
38 LOG=$LOGDIR/install
39 CONFIGDIR=/etc/git-openemr
40 CONFIG=$CONFIGDIR/git-openemr.conf
41 WEB=/var/www
42 OPENEMR=$WEB/git-openemr
43 SITEDIR=$OPENEMR/sites/default
44 INSTALL_USER=gitopenemr
45 INSTALL_DATABASE=gitopenemr
46 #web user and group
47 WEB_GROUP=www-data
48 WEB_USER=www-data
49 #auto installer location
50 INST=$OPENEMR/contrib/util/installScripts/InstallerAuto.php
51 INSTTEMP=$OPENEMR/contrib/util/installScripts/InstallerAutoTemp.php
53 #Standardized echo function to send to only log file
54 # requires one parameter (string)
55 log_only () {
56 echo "`date`: $1" >> $LOG
59 #Standardized exit functions to be used
60 # requires one parameter (string with reason for exiting)
61 unable_exit () {
62 echo "`date`: $1" >> $LOG
63 echo "`date`: EXITING.........." >> $LOG
64 exit 1
67 #function to check mysql for selected databases
68 # 1st param is password, 2nd param database, 3rd param is host (optional), 4th param is user (optional)
69 check_mysql () {
70 if [ -n "$3" ]; then
71 HOST=$3
72 else
73 HOST=localhost
75 if [ -n "$4" ]; then
76 USE=$4
77 else
78 USE=root
81 if [ "`mysql -u "$USE" -h "$HOST" --password="$1" -e 'show databases' 2>/dev/null | awk '{ print $1}' | grep "^$2$"`" == "$2" ]; then
82 return 0
83 else
84 return 1
88 #function to collect variables from config files
89 # 1st param is variable name, 2nd param is filename
90 collect_var () {
91 echo `grep -i "^[[:space:]]*$1[[:space:]=]" $2 | cut -d \= -f 2 | cut -d \; -f 1 | sed "s/[ '\"]//gi"`
94 #function to insert variables into config files
95 # 1st param is variable name, 2nd param is variable, 3rd param is filename
96 insert_var () {
97 sed -i 's@^[ ]*'"$1"'[ =].*$@'"$1"' = '"$2"'@' "$3"
100 #function to prompt for input
101 # 1st param is name, 2nd param is priority, 3rd param is where result gets sent back in
102 # return the input
103 prompt_input () {
104 db_set "$1" ""
105 db_fset "$1" seen false
106 db_input "$2" "$1" || true
107 db_go || true
108 db_get "$1"
109 local input_value="$RET"
110 db_set "$1" ""
111 db_fset "$1" seen false
112 local __result=$3
113 eval $__result="'$input_value'"
116 #collect scripting information from config file
117 PROCESS=$(collect_var process $CONFIG)
118 PLAN=$(collect_var plan $CONFIG)
120 #Don't allow re-configuration
121 if [ "$PROCESS" == "complete" ] ; then
122 unable_exit "OpenEMR has already been configured."
123 elif [ "$PROCESS" == "pending" ] ; then
124 #continue with configuration
125 log_only "Configuring git-openemr package..."
126 else
127 unable_exit "Error reading process variable in configuration file."
130 if [ "$PLAN" == "upgrade" ] ; then
131 #upgrade is not available with the git-openemr package
132 log_only "Upgrading is not available with the git-openemr package"
133 log_only "This package is for testing most recent development (unstable) OpenEMR version"
134 prompt_input git-openemr/upgrade_not_supported critical ret_result
135 unable_exit "To upgrade to most recent git version, recommend removing/reinstalling package"
137 elif [ "$PLAN" == "install" ] ; then
138 #continue with installation
139 log_only "Installing git-openemr package..."
140 else
141 unable_exit "Error reading plan variable in configuration file."
144 #collect the mysql root password (if applicable)
145 MPASS=""
146 if check_mysql "$MPASS" "mysql"; then
147 log_only "Passed the mysql check loop"
148 else
149 #the blank initial mysql password didn't work, so prompt for password
150 # (will give 3 chances to provide correct password)
151 COUNTDOWN=1
152 while true; do
153 prompt_input git-openemr/mysql_p_install_${COUNTDOWN} critical ret_result
154 MPASS="$ret_result"
155 if check_mysql "$MPASS" "mysql"; then
156 #the mysql root password works, so can exit loop
157 log_only "Passed the mysql check loop"
158 break
159 else
160 #the mysql root password did not work
161 if [ "$COUNTDOWN" -ge "3" ]; then
162 prompt_input git-openemr/no_configure_mysql_root high ret_result
163 log_only "Will install OpenEMR, however will not configure OpenEMR. (unable to provide root password)"
164 break
167 let "COUNTDOWN += 1"
168 done
171 #decide whether to configure OpenEMR after it is installed
172 configure_flag=true
173 if check_mysql "$MPASS" "mysql"; then
174 #before auto configuration, ensure the openemr user and database do not exist
175 # Check for openemr database in mysql, if exist then will not configure
176 if check_mysql "$MPASS" "$INSTALL_DATABASE"; then
177 prompt_input git-openemr/no_configure_mysql_database high ret_result
178 log_only "Will install OpenEMR, however will not automatically configure OpenEMR. (MySQL database already exists)"
179 configure_flag=false;
181 # Check for OpenEMR user in mysql.user, if exist then will not configure
182 USER=$(mysql -s -u root -h localhost --password="$MPASS" -e "SELECT User from mysql.user where User='$INSTALL_USER'")
183 if [ "$USER" == "$INSTALL_USER" ]; then
184 prompt_input git-openemr/no_configure_mysql_user high ret_result
185 log_only "Will install OpenEMR, however will not automatically configure OpenEMR. (MySQL user already exists)"
186 configure_flag=false;
188 else
189 #the mysql root password didn't work, so do not configure OpenEMR
190 log_only "Will install OpenEMR, however will not automatically configure OpenEMR. (root password did not work)"
191 configure_flag=false;
194 #Create directory
195 TMPDIR=(`mktemp -d`)
196 cd $TMPDIR
198 # Download development version of openemr
199 log_only "Downloading official OpenEMR development version (please be patient)"
200 if !(git clone git://github.com/openemr/openemr.git >> $LOG 2>&1); then
201 # unable to download OpenEMR
202 prompt_input git-openemr/unable_download critical ret_result
203 unable_exit "Unable to download OpenEMR development version"
204 exit 1
207 # Remove .git directory
208 rm -rf `find $TMPDIR -name .git`
210 # Install openemr
211 if $configure_flag; then
212 log_only "Installing/Configuring OpenEMR development version..."
213 else
214 log_only "Installing OpenEMR development version..."
216 mv $TMPDIR/openemr $OPENEMR
217 cd $OPENEMR
218 rmdir $TMPDIR
220 #secure openemr
221 chown -Rf root:root $OPENEMR
223 # Set file and directory permissions
224 chmod 666 $SITEDIR/sqlconf.php
225 chown $WEB_GROUP.$WEB_USER $OPENEMR/interface/modules/zend_modules/config/application.config.php
226 chown -R $WEB_GROUP.$WEB_USER $SITEDIR/documents
227 chown -R $WEB_GROUP.$WEB_USER $SITEDIR/edi
228 chown -R $WEB_GROUP.$WEB_USER $SITEDIR/era
229 chown -R $WEB_GROUP.$WEB_USER $OPENEMR/library/freeb
230 chown -R $WEB_GROUP.$WEB_USER $SITEDIR/letter_templates
231 chown -R $WEB_GROUP.$WEB_USER $OPENEMR/interface/main/calendar/modules/PostCalendar/pntemplates/cache
232 chown -R $WEB_GROUP.$WEB_USER $OPENEMR/interface/main/calendar/modules/PostCalendar/pntemplates/compiled
233 chown -R $WEB_GROUP.$WEB_USER $OPENEMR/gacl/admin/templates_c
235 # NEED TO CONFIGURE APACHE BEFORE CONFIGURATION SINCE ZEND SUPPORT NEEDS PROPER CONFIGURATION
236 # Activate the OpenEMR conf file for apache
237 log_only "Activate OpenEMR config file for Apache"
238 a2ensite git-openemr.conf
239 # Ensure the apache rewrite module is turned on
240 a2enmod rewrite
241 # Restart the apache server
242 log_only "Restarting Apache service..."
243 invoke-rc.d apache2 restart >> $LOG 2>&1
245 #CONFIGURE OPENEMR
246 if $configure_flag; then
247 # Create a random password for the openemr mysql user
248 password=$(makepasswd --char=12)
250 # openemr installation VARIABLES
251 if [ "$MPASS" == "" ] ; then
252 rootpass="rootpass=BLANK" #MySQL server root password
253 else
254 rootpass="rootpass=$MPASS" #MySQL server root password
256 login="login=$INSTALL_USER" #username to MySQL openemr database
257 pass="pass=$password" #password to MySQL openemr database
258 dbname="dbname=$INSTALL_DATABASE" #MySQL openemr database name
261 # Run Auto Installer
263 sed -e 's@^exit;@ @' <$INST >$INSTTEMP
264 php -f $INSTTEMP $rootpass $login $pass $dbname >> $LOG 2>&1
265 rm -f $INSTTEMP
267 # NO NEED to secure files since this is a development/testing version
269 log_only "Done configuring OpenEMR"
272 #update config file, change process to complete and remove plan and pass
273 sed -i "s/^[ ]*process[ =].*$/process=complete/" $CONFIG
274 sed -i "/^[ ]*plan[ =].*$/d" $CONFIG
275 sed -i "/^[ ]*pass[ =].*$/d" $CONFIG
277 log_only "You can now use OpenEMR development version by browsing to:"
278 log_only "http://localhost/git-openemr"
279 if $configure_flag; then
280 prompt_input git-openemr/success_install_config high ret_result
281 log_only "user is 'admin' and password is 'pass'"
282 else
283 prompt_input git-openemr/success_install high ret_result
285 log_only "See the openemr man page for further instructions:"
286 log_only "type 'man git-openemr' at command line"
288 #stop db
289 db_stop
291 exit 0
293 abort-upgrade|abort-remove|abort-deconfigure)
295 echo "postinst asked to do $1"
296 exit 0
299 echo "postinst called with unknown argument \`$1'" >&2
300 exit 1
302 esac
304 sleep 5
305 exit 0