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

Last change on this file since 4856 was 4856, 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_version" && need_update_to_0_1_4=yes
117
118    if [ "x$need_update_to_0_1_4" = "xyes" ]; then
119        echo Updating to database version 0.1.4
120        mysql -u root analytics < $dumpfile
121    fi
122
123
124#INITIALIZE DB
125
126    if [ "x$create_db" = "xyes" ]; then
127        if [ "x$rootpass" = "x" ]; then
128            mysqlroot="mysql -uroot"
129            mysqluser="mysql -u$user -p$pass"
130        else
131            mysqlroot="mysql -uroot -p$rootpass"
132            mysqluser="mysql -u$user -p$pass"
133        fi
134        if [ "x$create_user" = "xyes" ]; then
135            echo "create user '$user'@'%' identified by '$pass';" | $mysqlroot
136            echo "grant all privileges on analytics.* to '$user'@'%';" | $mysqlroot
137            echo "flush privileges;" | $mysqlroot
138            echo "user=$user" > /etc/lliurex-analytics-server/config_db
139            echo "pass=$pass" >> /etc/lliurex-analytics-server/config_db
140            chmod 750 /etc/lliurex-analytics-server/config_db
141        fi
142        $mysqluser < $dumpfile 
143    else
144        if [ "x$create_user" != "xyes" -a "${ask_user}" != "no" ]; then
145            do_dialog 'Analytics configuration' 'Usuario con permiso en bd analytics?'
146            user=$input
147            do_dialog 'Analytics configuration' 'Password del usuario con permiso en bd analytics?'
148            pass=$input
149            echo "user=$user" > /etc/lliurex-analytics-server/config_db
150            echo "pass=$pass" >> /etc/lliurex-analytics-server/config_db
151            chmod 640 /etc/lliurex-analytics-server/config_db
152        fi
153    fi
154
155    RES1=$(grep -o '@phpuser@' /usr/lib/analytics-server/analytics/config.php)
156    RES2=$(grep -o '@phppass@' /usr/lib/analytics-server/analytics/config.php)
157
158    if [ -f "/usr/lib/analytics-server/analytics/config.php" -a -n "${RES1}" -a -n "${RES2}" ]; then
159        sed -i.old -e "s/@phpuser@/$user/g" -e  "s/@phppass@/$pass/g" /usr/lib/analytics-server/analytics/config.php
160        rm /usr/lib/analytics-server/analytics/config.php.old
161        chmod 640 /usr/lib/analytics-server/analytics/config.php
162        apachectl_cmd=$(which apachectl)
163        apache_group=$(${apachectl_cmd} -t -D DUMP_RUN_CFG 2>/dev/null|perl -ne '/group:[ ]+name=\"(.*)\"/i && print $1')
164        if [ -z "${apache_group}" ]; then
165            echo "Warning: apache group not detected, falling back to www-data"
166            apache_group="www-data"
167        fi
168        chgrp ${apache_group} /usr/lib/analytics-server/analytics/config.php
169    else
170        echo "BUG !! WARNING: replace tags @phpuser@ & @phppass@ not found in /usr/lib/analytics-server/analytics/config.php"
171        echo "Installation may not work succesfully, report bug to developers"
172    fi
173
174
175# CONFIG APACHE
176
177    if [ -e /usr/share/apache2/apache2-maintscript-helper ] ; then
178        . /usr/share/apache2/apache2-maintscript-helper
179        apache2_invoke enmod rewrite || exit $?
180        apache2_invoke ensite analytics || exit $?
181    fi
182
183    if [ "${print_info}" = "yes" ]; then
184            echo
185            echo "To import database schema use:"
186            echo ""
187            echo "echo \"create user '<user>'@'%' identified by '<password>';\" | mysql -u root -p<root_password>"
188            echo "echo \"grant all privileges on analytics.* to '<user>'@'%';\" | mysql -u root -p<root_password>"
189            echo "echo \"flush privileges; \" | mysql -u root -p<root_password>"
190            echo "mysql -u <user> -p<root_password> < /usr/lib/analytics-server/analytics.sql"
191            echo "After that, edit /usr/lib/analytics-server/analytics/config.php"
192            echo 
193            echo
194    fi
195   
196
197    ;;
198
199    abort-upgrade|abort-remove|abort-deconfigure)
200    ;;
201
202    *)
203        echo "postinst called with unknown argument \`$1'" >&2
204        exit 1
205    ;;
206esac
207
208# dh_installdeb will replace this with shell code automatically
209# generated by other debhelper scripts.
210
211#DEBHELPER#
212
213exit 0
214
215
Note: See TracBrowser for help on using the repository browser.