1 | #!/bin/sh |
---|
2 | # postinst script |
---|
3 | # |
---|
4 | # see: dh_installdeb(1) |
---|
5 | |
---|
6 | set -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 | |
---|
22 | do_dialog(){ |
---|
23 | input=$(dialog \ |
---|
24 | --title "$1" \ |
---|
25 | --inputbox "$2" 0 0 \ |
---|
26 | 3>&1 1>&2 2>&3 3>&-) |
---|
27 | clear |
---|
28 | } |
---|
29 | ask_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 | |
---|
39 | case "$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=$(mysql -u${USER} -p${PASS} -N -s -D analytics -e "show tables" 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 | ask_root="yes" |
---|
95 | if [ -f /etc/mysql/debian.cnf ]; then |
---|
96 | root_test=$(mysql -u root -s -N -e "show databases" 2>/dev/null || true) |
---|
97 | if [ -z "$root_test" ]; then |
---|
98 | rootuser=$(cat /etc/mysql/debian.cnf |grep -A 4 '\[client\]'|perl -ne '/user[ \t]*=[ \t]*(.*)/i && print $1') |
---|
99 | rootpass=$(cat /etc/mysql/debian.cnf |grep -A 4 '\[client\]'|perl -ne '/password[ \t]*=[ \t]*(.*)/i && print $1') |
---|
100 | if [ -n "$root_user" -a -n "$root_pass" ]; then |
---|
101 | root_test=$(mysql -u $rootuser -p$rootpass -s -N -e "show databases" 2>/dev/null || true) |
---|
102 | if [ -z "$root_test" ]; then |
---|
103 | ask_root="no" |
---|
104 | rootuser="-u$rootuser" |
---|
105 | rootpass="-p$rootpass" |
---|
106 | fi |
---|
107 | fi |
---|
108 | else |
---|
109 | ask_root="no" |
---|
110 | rootuser="-uroot" |
---|
111 | rootpass="" |
---|
112 | fi |
---|
113 | fi |
---|
114 | if [ "x$ask_root" = "xyes" ]; then |
---|
115 | do_dialog 'Database configuration' 'Mysql root user? (empty is root)' |
---|
116 | rootuser=$input |
---|
117 | do_dialog 'Database configuration' 'Password root de mysql?' |
---|
118 | rootpass=$input |
---|
119 | root_test=$(mysql -u $rootuser -p$rootpass -s -N -e "show databases" 2>/dev/null || true) |
---|
120 | if [ -z "$root_test" ]; then |
---|
121 | echo "Wrong password, leaving all unconfigured and aborting...." |
---|
122 | exit 0 |
---|
123 | else |
---|
124 | if [ -z "$rootuser" ]; then |
---|
125 | rootuser="root" |
---|
126 | fi |
---|
127 | rootuser="-u$rootuser" |
---|
128 | if [ -z "$rootpass" ];then |
---|
129 | rootpass="" |
---|
130 | else |
---|
131 | rootpass="-p$rootpass" |
---|
132 | fi |
---|
133 | fi |
---|
134 | fi |
---|
135 | else |
---|
136 | print_info="yes" |
---|
137 | fi |
---|
138 | else |
---|
139 | #ASK=no |
---|
140 | create_db="no" |
---|
141 | create_user="no" |
---|
142 | ask_user="no" |
---|
143 | fi |
---|
144 | |
---|
145 | |
---|
146 | # UPDATE DB FROM OLDER VERSIONS |
---|
147 | tables=$(mysql $rootuser $rootpass -s -N -D analytics -e "show tables" 2>/dev/null|grep historico_clients) |
---|
148 | keys=$(mysqldump $rootuser $rootpass analytics|egrep '^[[:space:]]*KEY'|wc -l) |
---|
149 | dumpfile="/usr/lib/analytics-server/analytics-0_1_3-to-0_1_4.sql" |
---|
150 | |
---|
151 | #UPDATE FROM 0.1.3 to 0.1.4 |
---|
152 | |
---|
153 | older_version="$2" |
---|
154 | target_update="0.1.4" |
---|
155 | |
---|
156 | need_update_to_0_1_4=no |
---|
157 | dpkg --compare-versions "$older_version" lt "$target_update" && need_update_to_0_1_4=yes |
---|
158 | if [ -z "$tables" -o $keys -ne 12 ]; then |
---|
159 | need_update_to_0_1_4=yes |
---|
160 | echo "failed database test for table historico_clients or keys" |
---|
161 | fi |
---|
162 | if [ "x$need_update_to_0_1_4" = "xyes" ]; then |
---|
163 | echo Updating to database version upper than 0.1.4 |
---|
164 | mysqldump $rootuser $rootpass --no-create-info analytics > /tmp/__analytics_tmp__ |
---|
165 | mysql $rootuser $rootpass -s -N -e "drop database analytics" |
---|
166 | mysql $rootuser $rootpass < $dumpfile |
---|
167 | mysql $rootuser $rootpass analytics < /tmp/__analytics_tmp__ |
---|
168 | else |
---|
169 | echo "Seems that database is newer than 0.1.4, skipping database migration" |
---|
170 | fi |
---|
171 | |
---|
172 | |
---|
173 | #INITIALIZE DB |
---|
174 | |
---|
175 | if [ "x$create_db" = "xyes" ]; then |
---|
176 | if [ "x$create_user" = "xyes" ]; then |
---|
177 | mysql $rootuser $rootpass -s -N -e "create user '$user'@'%' identified by '$pass';" |
---|
178 | mysql $rootuser $rootpass -s -N -e "grant all privileges on analytics.* to '$user'@'%';" |
---|
179 | mysql $rootuser $rootpass -s -N -e "flush privileges;" |
---|
180 | echo "user=$user" > /etc/lliurex-analytics-server/config_db |
---|
181 | echo "pass=$pass" >> /etc/lliurex-analytics-server/config_db |
---|
182 | chmod 750 /etc/lliurex-analytics-server/config_db |
---|
183 | fi |
---|
184 | mysql $rootuser $rootpass < $dumpfile |
---|
185 | else |
---|
186 | if [ "x$create_user" != "xyes" -a "${ask_user}" != "no" ]; then |
---|
187 | do_dialog 'Analytics configuration' 'Usuario con permiso en bd analytics?' |
---|
188 | user=$input |
---|
189 | do_dialog 'Analytics configuration' 'Password del usuario con permiso en bd analytics?' |
---|
190 | pass=$input |
---|
191 | echo "user=$user" > /etc/lliurex-analytics-server/config_db |
---|
192 | echo "pass=$pass" >> /etc/lliurex-analytics-server/config_db |
---|
193 | chmod 640 /etc/lliurex-analytics-server/config_db |
---|
194 | fi |
---|
195 | fi |
---|
196 | |
---|
197 | RES1=$(grep -o '@phpuser@' /usr/lib/analytics-server/analytics/config.php) |
---|
198 | RES2=$(grep -o '@phppass@' /usr/lib/analytics-server/analytics/config.php) |
---|
199 | |
---|
200 | if [ -f "/usr/lib/analytics-server/analytics/config.php" -a -n "${RES1}" -a -n "${RES2}" ]; then |
---|
201 | sed -i.old -e "s/@phpuser@/$user/g" -e "s/@phppass@/$pass/g" /usr/lib/analytics-server/analytics/config.php |
---|
202 | rm /usr/lib/analytics-server/analytics/config.php.old |
---|
203 | chmod 640 /usr/lib/analytics-server/analytics/config.php |
---|
204 | apachectl_cmd=$(which apachectl) |
---|
205 | apache_group=$(${apachectl_cmd} -t -D DUMP_RUN_CFG 2>/dev/null|perl -ne '/group:[ ]+name=\"(.*)\"/i && print $1') |
---|
206 | if [ -z "${apache_group}" ]; then |
---|
207 | echo "Warning: apache group not detected, falling back to www-data" |
---|
208 | apache_group="www-data" |
---|
209 | fi |
---|
210 | chgrp ${apache_group} /usr/lib/analytics-server/analytics/config.php |
---|
211 | else |
---|
212 | echo "BUG !! WARNING: replace tags @phpuser@ & @phppass@ not found in /usr/lib/analytics-server/analytics/config.php" |
---|
213 | echo "Installation may not work succesfully, report bug to developers" |
---|
214 | fi |
---|
215 | |
---|
216 | |
---|
217 | # CONFIG APACHE |
---|
218 | |
---|
219 | if [ -e /usr/share/apache2/apache2-maintscript-helper ] ; then |
---|
220 | . /usr/share/apache2/apache2-maintscript-helper |
---|
221 | apache2_invoke enmod rewrite || exit $? |
---|
222 | apache2_invoke ensite analytics || exit $? |
---|
223 | fi |
---|
224 | |
---|
225 | if [ "${print_info}" = "yes" ]; then |
---|
226 | echo |
---|
227 | echo "To import database schema use:" |
---|
228 | echo "" |
---|
229 | echo "echo \"create user '<user>'@'%' identified by '<password>';\" | mysql -u root -p<root_password>" |
---|
230 | echo "echo \"grant all privileges on analytics.* to '<user>'@'%';\" | mysql -u root -p<root_password>" |
---|
231 | echo "echo \"flush privileges; \" | mysql -u root -p<root_password>" |
---|
232 | echo "mysql -u <user> -p<root_password> < /usr/lib/analytics-server/analytics.sql" |
---|
233 | echo "After that, edit /usr/lib/analytics-server/analytics/config.php" |
---|
234 | echo |
---|
235 | echo |
---|
236 | fi |
---|
237 | |
---|
238 | |
---|
239 | ;; |
---|
240 | |
---|
241 | abort-upgrade|abort-remove|abort-deconfigure) |
---|
242 | ;; |
---|
243 | |
---|
244 | *) |
---|
245 | echo "postinst called with unknown argument \`$1'" >&2 |
---|
246 | exit 1 |
---|
247 | ;; |
---|
248 | esac |
---|
249 | |
---|
250 | # dh_installdeb will replace this with shell code automatically |
---|
251 | # generated by other debhelper scripts. |
---|
252 | |
---|
253 | #DEBHELPER# |
---|
254 | |
---|
255 | exit 0 |
---|
256 | |
---|
257 | |
---|