source: lliurex-analytics-server/trunk/fuentes/debian/postinst @ 4859

Last change on this file since 4859 was 4859, checked in by mabarracus, 2 years ago

fix postinst

File size: 7.3 KB
Line 
1#!/bin/sh
2# postinst script
3#
4# see: dh_installdeb(1)
5
6set -e
7# AVOID DECOMENT WITHOUT DEBCONF QUESTIONS
8#. /usr/share/debconf/confmodule
9
10# summary of how this script can be called:
11#        * <postinst> `configure' <most-recently-configured-version>
12#        * <old-postinst> `abort-upgrade' <new version>
13#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
14#          <new-version>
15#        * <postinst> `abort-remove'
16#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
17#          <failed-install-package> <version> `removing'
18#          <conflicting-package> <version>
19# for details, see http://www.debian.org/doc/debian-policy/ or
20# the debian-policy package
21
22do_dialog(){
23    input=$(dialog      \
24        --title "$1"    \
25        --inputbox "$2" 0 0     \
26        3>&1 1>&2 2>&3 3>&-)
27        clear
28}
29ask_question(){
30    dialog --yesno "$1" 0 0
31    if [ $? -eq 0 ]; then
32        input=yes
33    else
34        input=no
35    fi
36    clear
37}
38
39case "$1" in
40    configure)
41
42    ASK="no"
43   
44#CHECK OLD CONFIG
45    if [ ! -d "/etc/lliurex-analytics-server" ]; then
46        ASK="yes"
47        echo "creating config directory"
48        mkdir /etc/lliurex-analytics-server
49    else
50        if [ -f "/etc/lliurex-analytics-server/config_db" ]; then
51            USER=$(cat /etc/lliurex-analytics-server/config_db|perl -ne '/user=(.*)/i && print $1')
52            PASS=$(cat /etc/lliurex-analytics-server/config_db|perl -ne '/pass=(.*)/i && print $1')
53            if [ -n "${USER}" -a -n "${PASS}" ]; then
54                RES=$(echo 'show tables;'|mysql -u${USER} -p${PASS} -s analytics 2>/dev/null || true)
55                if [ -n "${RES}" ]; then
56                    user="${USER}"
57                    pass="${PASS}"
58                    ASK="no"
59                    echo "using user & pass from config_db file"
60                else
61                    echo "wrong user & pass into config_db file"
62                    ASK="yes"
63                fi
64            else
65                echo "user & pass not detected in config_db file"
66                ASK="yes"
67            fi
68        else
69            echo "config_db file not found"
70            ASK="yes"
71        fi
72    fi
73
74# ASK QUESTIONS
75    print_info="no"
76    ask_user="yes"
77    if [ "${ASK}" = "yes" ]; then
78        ask_question 'Desea configurar la base de datos analytics?'
79        if [ "x$input" = "xyes" ]; then
80            create_db=$input
81            ask_question 'Desea crear usuario para la tabla de la base de datos?'
82            create_user=$input
83            if [ "x$input" = "xyes" ]; then
84                do_dialog 'Database configuration' 'Nombre del usuario para crear:'
85                user=$input
86                do_dialog 'Database configuration' 'Password del usuario para crear:'
87                pass=$input
88            else
89                do_dialog 'Database configuration' 'Usuario con permiso en bd analytics?'
90                user=$input
91                do_dialog 'Database configuration' 'Password del usuario con permiso en bd analytics?'
92                pass=$input
93            fi
94            do_dialog 'Database configuration' 'Password root de mysql?'
95            rootpass=$input
96        else
97            print_info="yes"
98        fi
99    else
100    #ASK=no
101        create_db="no"
102        create_user="no"
103        ask_user="no"
104    fi
105
106
107# UPDATE DB FROM OLDER VERSIONS
108    dumpfile="/usr/lib/analytics-server/analytics-0_1_3-to-0_1_4.sql"
109
110    #UPDATE FROM 0.1.3 to 0.1.4
111
112    older_version="$2"
113    target_update="0.1.4"
114
115    need_update_to_0_1_4=no
116    dpkg --compare-versions "$older_version" lt "$target_update" && need_update_to_0_1_4=yes
117    if [ "x$need_update_to_0_1_4" = "xyes" ]; then
118        echo Updating to database version 0.1.4
119        mysql < $dumpfile
120    fi
121
122
123#INITIALIZE DB
124
125    if [ "x$create_db" = "xyes" ]; then
126        if [ "x$rootpass" = "x" ]; then
127            mysqlroot="mysql -uroot"
128            mysqluser="mysql -u$user -p$pass"
129        else
130            mysqlroot="mysql -uroot -p$rootpass"
131            mysqluser="mysql -u$user -p$pass"
132        fi
133        if [ "x$create_user" = "xyes" ]; then
134            echo "create user '$user'@'%' identified by '$pass';" | $mysqlroot
135            echo "grant all privileges on analytics.* to '$user'@'%';" | $mysqlroot
136            echo "flush privileges;" | $mysqlroot
137            echo "user=$user" > /etc/lliurex-analytics-server/config_db
138            echo "pass=$pass" >> /etc/lliurex-analytics-server/config_db
139            chmod 750 /etc/lliurex-analytics-server/config_db
140        fi
141        $mysqluser < $dumpfile 
142    else
143        if [ "x$create_user" != "xyes" -a "${ask_user}" != "no" ]; then
144            do_dialog 'Analytics configuration' 'Usuario con permiso en bd analytics?'
145            user=$input
146            do_dialog 'Analytics configuration' 'Password del usuario con permiso en bd analytics?'
147            pass=$input
148            echo "user=$user" > /etc/lliurex-analytics-server/config_db
149            echo "pass=$pass" >> /etc/lliurex-analytics-server/config_db
150            chmod 640 /etc/lliurex-analytics-server/config_db
151        fi
152    fi
153
154    RES1=$(grep -o '@phpuser@' /usr/lib/analytics-server/analytics/config.php)
155    RES2=$(grep -o '@phppass@' /usr/lib/analytics-server/analytics/config.php)
156
157    if [ -f "/usr/lib/analytics-server/analytics/config.php" -a -n "${RES1}" -a -n "${RES2}" ]; then
158        sed -i.old -e "s/@phpuser@/$user/g" -e  "s/@phppass@/$pass/g" /usr/lib/analytics-server/analytics/config.php
159        rm /usr/lib/analytics-server/analytics/config.php.old
160        chmod 640 /usr/lib/analytics-server/analytics/config.php
161        apachectl_cmd=$(which apachectl)
162        apache_group=$(${apachectl_cmd} -t -D DUMP_RUN_CFG 2>/dev/null|perl -ne '/group:[ ]+name=\"(.*)\"/i && print $1')
163        if [ -z "${apache_group}" ]; then
164            echo "Warning: apache group not detected, falling back to www-data"
165            apache_group="www-data"
166        fi
167        chgrp ${apache_group} /usr/lib/analytics-server/analytics/config.php
168    else
169        echo "BUG !! WARNING: replace tags @phpuser@ & @phppass@ not found in /usr/lib/analytics-server/analytics/config.php"
170        echo "Installation may not work succesfully, report bug to developers"
171    fi
172
173
174# CONFIG APACHE
175
176    if [ -e /usr/share/apache2/apache2-maintscript-helper ] ; then
177        . /usr/share/apache2/apache2-maintscript-helper
178        apache2_invoke enmod rewrite || exit $?
179        apache2_invoke ensite analytics || exit $?
180    fi
181
182    if [ "${print_info}" = "yes" ]; then
183            echo
184            echo "To import database schema use:"
185            echo ""
186            echo "echo \"create user '<user>'@'%' identified by '<password>';\" | mysql -u root -p<root_password>"
187            echo "echo \"grant all privileges on analytics.* to '<user>'@'%';\" | mysql -u root -p<root_password>"
188            echo "echo \"flush privileges; \" | mysql -u root -p<root_password>"
189            echo "mysql -u <user> -p<root_password> < /usr/lib/analytics-server/analytics.sql"
190            echo "After that, edit /usr/lib/analytics-server/analytics/config.php"
191            echo
192            echo
193    fi
194   
195
196    ;;
197
198    abort-upgrade|abort-remove|abort-deconfigure)
199    ;;
200
201    *)
202        echo "postinst called with unknown argument \`$1'" >&2
203        exit 1
204    ;;
205esac
206
207# dh_installdeb will replace this with shell code automatically
208# generated by other debhelper scripts.
209
210#DEBHELPER#
211
212exit 0
213
214
Note: See TracBrowser for help on using the repository browser.