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 | do_dialog(){ |
---|
22 | input=$(dialog \ |
---|
23 | --title "$1" \ |
---|
24 | --inputbox "$2" 0 0 \ |
---|
25 | 3>&1 1>&2 2>&3 3>&-) |
---|
26 | clear |
---|
27 | } |
---|
28 | ask_question(){ |
---|
29 | dialog --yesno "$1" 0 0 |
---|
30 | if [ $? -eq 0 ]; then |
---|
31 | input=yes |
---|
32 | else |
---|
33 | input=no |
---|
34 | fi |
---|
35 | clear |
---|
36 | } |
---|
37 | |
---|
38 | case "$1" in |
---|
39 | configure) |
---|
40 | mysql_alive="" |
---|
41 | retry=3 |
---|
42 | if [ -z "$mysql_alive" -a $retry -gt 0 ];then |
---|
43 | mysql_alive="$(pidof mysqld || true)" |
---|
44 | if [ -z "$mysql_alive" ]; then |
---|
45 | echo "Trying to start mysql rc.d " |
---|
46 | systemctl restart mysql || true |
---|
47 | sleep 5 |
---|
48 | fi |
---|
49 | retry=$(($retry -1)) |
---|
50 | fi |
---|
51 | if [ $retry -eq 0 ];then |
---|
52 | echo MySQL not running, no configuration possible, aborting... |
---|
53 | exit 1 |
---|
54 | fi |
---|
55 | ASK="no" |
---|
56 | |
---|
57 | #CHECK OLD CONFIG |
---|
58 | if [ ! -d "/etc/lliurex-analytics-server" ]; then |
---|
59 | ASK="yes" |
---|
60 | echo "creating config directory" |
---|
61 | mkdir /etc/lliurex-analytics-server |
---|
62 | else |
---|
63 | if [ -f "/etc/lliurex-analytics-server/config_db" ]; then |
---|
64 | USER=$(cat /etc/lliurex-analytics-server/config_db|perl -ne '/user=(.*)/i && print $1') |
---|
65 | PASS=$(cat /etc/lliurex-analytics-server/config_db|perl -ne '/pass=(.*)/i && print $1') |
---|
66 | if [ -n "${USER}" -a -n "${PASS}" ]; then |
---|
67 | RES=$(mysql -u${USER} -p${PASS} -N -s -Danalytics -e "show tables" 2>/dev/null || true) |
---|
68 | if [ -n "${RES}" ]; then |
---|
69 | user="${USER}" |
---|
70 | pass="${PASS}" |
---|
71 | rootuser="-u${USER}" |
---|
72 | rootpass="-p${PASS}" |
---|
73 | ASK="no" |
---|
74 | echo "using user & pass from config_db file" |
---|
75 | else |
---|
76 | echo "wrong user & pass into config_db file" |
---|
77 | ASK="yes" |
---|
78 | fi |
---|
79 | else |
---|
80 | echo "user & pass not detected in config_db file" |
---|
81 | ASK="yes" |
---|
82 | fi |
---|
83 | else |
---|
84 | echo "config_db file not found" |
---|
85 | ASK="yes" |
---|
86 | fi |
---|
87 | fi |
---|
88 | |
---|
89 | # ASK QUESTIONS |
---|
90 | print_info="no" |
---|
91 | ask_user="yes" |
---|
92 | if [ "${ASK}" = "yes" ]; then |
---|
93 | ask_question 'Desea configurar la base de datos analytics?' |
---|
94 | if [ "x$input" = "xyes" ]; then |
---|
95 | create_db=$input |
---|
96 | ask_question 'Desea crear usuario para la tabla de la base de datos?' |
---|
97 | create_user=$input |
---|
98 | if [ "x$input" = "xyes" ]; then |
---|
99 | do_dialog 'Database configuration' 'Nombre del usuario para crear:' |
---|
100 | user=$input |
---|
101 | do_dialog 'Database configuration' 'Password del usuario para crear:' |
---|
102 | pass=$input |
---|
103 | else |
---|
104 | do_dialog 'Database configuration' 'Usuario con permiso en bd analytics?' |
---|
105 | user=$input |
---|
106 | do_dialog 'Database configuration' 'Password del usuario con permiso en bd analytics?' |
---|
107 | pass=$input |
---|
108 | fi |
---|
109 | ask_root="yes" |
---|
110 | if [ -f /etc/mysql/debian.cnf ]; then |
---|
111 | root_test=$(mysql -u root -s -N -e "show databases" 2>/dev/null || true) |
---|
112 | if [ -z "$root_test" ]; then |
---|
113 | rootuser=$(cat /etc/mysql/debian.cnf |grep -A 4 '\[client\]'|perl -ne '/user[ \t]*=[ \t]*(.*)/i && print $1') |
---|
114 | rootpass=$(cat /etc/mysql/debian.cnf |grep -A 4 '\[client\]'|perl -ne '/password[ \t]*=[ \t]*(.*)/i && print $1') |
---|
115 | if [ -n "$rootuser" -a -n "$rootpass" ]; then |
---|
116 | root_test=$(mysql -u $rootuser -p$rootpass -s -N -e "show databases" 2>/dev/null || true) |
---|
117 | if [ -n "$root_test" ]; then |
---|
118 | ask_root="no" |
---|
119 | rootuser="-u$rootuser" |
---|
120 | rootpass="-p$rootpass" |
---|
121 | fi |
---|
122 | fi |
---|
123 | else |
---|
124 | ask_root="no" |
---|
125 | rootuser="-uroot" |
---|
126 | rootpass="" |
---|
127 | fi |
---|
128 | fi |
---|
129 | if [ "x$ask_root" = "xyes" ]; then |
---|
130 | do_dialog 'Database configuration' 'Mysql root user? (empty is root)' |
---|
131 | if [ -z "$input" ]; then |
---|
132 | rootuser="root" |
---|
133 | else |
---|
134 | rootuser=$input |
---|
135 | fi |
---|
136 | do_dialog 'Database configuration' 'Password root de mysql?' |
---|
137 | rootpass=$input |
---|
138 | root_test=$(mysql -u $rootuser -p$rootpass -s -N -e "show databases" 2>/dev/null || true) |
---|
139 | if [ -z "$root_test" ]; then |
---|
140 | echo "Wrong password, leaving all unconfigured and aborting...." |
---|
141 | exit 0 |
---|
142 | else |
---|
143 | if [ -z "$rootuser" ]; then |
---|
144 | rootuser="-uroot" |
---|
145 | fi |
---|
146 | rootuser="-u$rootuser" |
---|
147 | if [ -z "$rootpass" ];then |
---|
148 | rootpass="" |
---|
149 | else |
---|
150 | rootpass="-p$rootpass" |
---|
151 | fi |
---|
152 | fi |
---|
153 | fi |
---|
154 | else |
---|
155 | print_info="yes" |
---|
156 | fi |
---|
157 | else |
---|
158 | #ASK=no |
---|
159 | create_db="no" |
---|
160 | create_user="no" |
---|
161 | ask_user="no" |
---|
162 | fi |
---|
163 | |
---|
164 | # UPDATE DB FROM OLDER VERSIONS |
---|
165 | is_version_1=0 |
---|
166 | is_version_1_4=0 |
---|
167 | |
---|
168 | tables=$(mysql $rootuser $rootpass -s -N -Danalytics -e "show tables" 2>/dev/null || true) |
---|
169 | if [ -n "$tables" ]; then |
---|
170 | tables=$(echo $tables|grep historico_clients || true) |
---|
171 | if [ -n "$tables" ]; then |
---|
172 | is_version_1_4=1 |
---|
173 | else |
---|
174 | tables=$(echo $tables|grep historico || true) |
---|
175 | if [ -n "$tables" ]; then |
---|
176 | is_version_1=1 |
---|
177 | fi |
---|
178 | fi |
---|
179 | fi |
---|
180 | if [ "$is_version_1" = "1" ]; then |
---|
181 | echo "Updating database from version 1" |
---|
182 | mysql $rootuser $rootpass < /usr/lib/analytics-server/migrate_from_1.sql || true |
---|
183 | create_db="no" |
---|
184 | fi |
---|
185 | if [ "$is_version_1_4" = "1" ]; then |
---|
186 | echo "Updating database from version 1.4" |
---|
187 | mysql $rootuser $rootpass < /usr/lib/analytics-server/migrate_from_1_4.sql || true |
---|
188 | create_db="no" |
---|
189 | fi |
---|
190 | |
---|
191 | dumpfile="/usr/lib/analytics-server/analytics2.sql" |
---|
192 | |
---|
193 | #keys=$(mysqldump $rootuser $rootpass analytics 2>/dev/null|egrep '^[[:space:]]*KEY'|wc -l) |
---|
194 | #dumpfile="/usr/lib/analytics-server/analytics-0_1_3-to-0_1_4.sql" |
---|
195 | |
---|
196 | #UPDATE FROM 0.1.3 to 0.1.4 |
---|
197 | |
---|
198 | #older_version="$2" |
---|
199 | #target_update="0.1.4" |
---|
200 | |
---|
201 | #need_update_to_0_1_4=no |
---|
202 | #dpkg --compare-versions "$older_version" lt "$target_update" && need_update_to_0_1_4=yes |
---|
203 | #if [ -z "$tables" -o $keys -ne 12 ]; then |
---|
204 | # need_update_to_0_1_4=yes |
---|
205 | # echo "failed database test for table historico_clients or keys" |
---|
206 | #fi |
---|
207 | #if [ "x$need_update_to_0_1_4" = "xyes" ]; then |
---|
208 | # echo Updating to database version upper than 0.1.4 |
---|
209 | # if [ "x$dump_old_data" = "xyes" ]; then |
---|
210 | # mysqldump $rootuser $rootpass --no-create-info analytics > /tmp/__analytics_tmp__ 2>/dev/null |
---|
211 | # fi |
---|
212 | # mysql $rootuser $rootpass -s -N -e "drop database if exists analytics" |
---|
213 | # mysql $rootuser $rootpass < $dumpfile |
---|
214 | # mysql $rootuser $rootpass analytics < /tmp/__analytics_tmp__ |
---|
215 | #else |
---|
216 | # echo "Seems that database is newer than 0.1.4, skipping database migration" |
---|
217 | #fi |
---|
218 | |
---|
219 | |
---|
220 | #INITIALIZE DB |
---|
221 | |
---|
222 | if [ "x$create_db" = "xyes" ]; then |
---|
223 | if [ "x$create_user" = "xyes" ]; then |
---|
224 | mysql $rootuser $rootpass -s -N -e "create user '$user'@'%' identified by '$pass';" || true |
---|
225 | mysql $rootuser $rootpass -s -N -e "grant all privileges on analytics.* to '$user'@'%';" || true |
---|
226 | mysql $rootuser $rootpass -s -N -e "flush privileges;" |
---|
227 | echo "user=$user" > /etc/lliurex-analytics-server/config_db |
---|
228 | echo "pass=$pass" >> /etc/lliurex-analytics-server/config_db |
---|
229 | chmod 750 /etc/lliurex-analytics-server/config_db |
---|
230 | fi |
---|
231 | mysql $rootuser $rootpass < $dumpfile |
---|
232 | else |
---|
233 | if [ "x$create_user" != "xyes" -a "${ask_user}" != "no" ]; then |
---|
234 | do_dialog 'Analytics configuration' 'Usuario con permiso en bd analytics?' |
---|
235 | user=$input |
---|
236 | do_dialog 'Analytics configuration' 'Password del usuario con permiso en bd analytics?' |
---|
237 | pass=$input |
---|
238 | echo "user=$user" > /etc/lliurex-analytics-server/config_db |
---|
239 | echo "pass=$pass" >> /etc/lliurex-analytics-server/config_db |
---|
240 | chmod 640 /etc/lliurex-analytics-server/config_db |
---|
241 | fi |
---|
242 | fi |
---|
243 | |
---|
244 | RES1=$(grep -o '@phpuser@' /usr/lib/analytics-server/analytics/config.php) |
---|
245 | RES2=$(grep -o '@phppass@' /usr/lib/analytics-server/analytics/config.php) |
---|
246 | |
---|
247 | if [ -f "/usr/lib/analytics-server/analytics/config.php" -a -n "${RES1}" -a -n "${RES2}" ]; then |
---|
248 | sed -i.old -e "s/@phpuser@/$user/g" -e "s/@phppass@/$pass/g" /usr/lib/analytics-server/analytics/config.php |
---|
249 | rm /usr/lib/analytics-server/analytics/config.php.old |
---|
250 | chmod 640 /usr/lib/analytics-server/analytics/config.php |
---|
251 | apachectl_cmd=$(which apachectl) |
---|
252 | apache_group=$(${apachectl_cmd} -t -D DUMP_RUN_CFG 2>/dev/null|perl -ne '/group:[ ]+name=\"(.*)\"/i && print $1') |
---|
253 | if [ -z "${apache_group}" ]; then |
---|
254 | echo "Warning: apache group not detected, falling back to www-data" |
---|
255 | apache_group="www-data" |
---|
256 | fi |
---|
257 | chgrp ${apache_group} /usr/lib/analytics-server/analytics/config.php |
---|
258 | else |
---|
259 | echo "BUG !! WARNING: replace tags @phpuser@ & @phppass@ not found in /usr/lib/analytics-server/analytics/config.php" |
---|
260 | echo "Installation may not work succesfully, report bug to developers" |
---|
261 | fi |
---|
262 | |
---|
263 | need_update_1_5=$(mysql $rootuser $rootpass -s -N -Danalytics -e "describe tmp_clients;" |grep ncpu |wc -l 2>/dev/null || true) |
---|
264 | if [ "$need_update_1_5" = "0" ]; then |
---|
265 | echo "Updating database to 1.5 format" |
---|
266 | mysql $rootuser $rootpass < /usr/lib/analytics-server/update_allow_platformdata.sql || true |
---|
267 | fi |
---|
268 | |
---|
269 | |
---|
270 | # CONFIG APACHE |
---|
271 | |
---|
272 | if [ -e /usr/share/apache2/apache2-maintscript-helper ] ; then |
---|
273 | . /usr/share/apache2/apache2-maintscript-helper |
---|
274 | apache2_invoke enmod rewrite || exit $? |
---|
275 | apache2_invoke ensite analytics || exit $? |
---|
276 | fi |
---|
277 | |
---|
278 | if [ "${print_info}" = "yes" ]; then |
---|
279 | echo |
---|
280 | echo "To import database schema use:" |
---|
281 | echo "" |
---|
282 | echo "echo \"create user '<user>'@'%' identified by '<password>';\" | mysql -u root -p<root_password>" |
---|
283 | echo "echo \"grant all privileges on analytics.* to '<user>'@'%';\" | mysql -u root -p<root_password>" |
---|
284 | echo "echo \"flush privileges; \" | mysql -u root -p<root_password>" |
---|
285 | echo "mysql -u <user> -p<root_password> < /usr/lib/analytics-server/analytics.sql" |
---|
286 | echo "After that, edit /usr/lib/analytics-server/analytics/config.php" |
---|
287 | echo |
---|
288 | echo |
---|
289 | fi |
---|
290 | |
---|
291 | |
---|
292 | ;; |
---|
293 | |
---|
294 | abort-upgrade|abort-remove|abort-deconfigure) |
---|
295 | ;; |
---|
296 | |
---|
297 | *) |
---|
298 | echo "postinst called with unknown argument \`$1'" >&2 |
---|
299 | exit 1 |
---|
300 | ;; |
---|
301 | esac |
---|
302 | |
---|
303 | # dh_installdeb will replace this with shell code automatically |
---|
304 | # generated by other debhelper scripts. |
---|
305 | |
---|
306 | #DEBHELPER# |
---|
307 | |
---|
308 | exit 0 |
---|
309 | |
---|
310 | |
---|