forked from domogik/domogik
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·555 lines (518 loc) · 19.4 KB
/
install.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
#!/bin/bash
#This file is part of B{Domogik} project (U{http://www.domogik.org}).
#
#License
#=======
#
#B{Domogik} is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#B{Domogik} is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with Domogik. If not, see U{http://www.gnu.org/licenses}.
#
#Module purpose
#==============
#
#Help to manage Domogik installation
#
#Implements
#==========
#
#
#@author: Maxence Dunnewind <maxence@dunnewind.net>
#@copyright: (C) 2007-2012 Domogik project
#@license: GPL(v3)
#@organization: Domogik
# Manage options
function display_usage {
echo "Usage : "
echo " Full installation : ./install.sh"
echo " Installation on secondary host : ./install.sh --secondary"
echo " Use the C xPL hub instead of the python one : ./install.sh --hub_c"
echo " Help : ./install.sh -h"
echo " ./install.sh --help"
}
MAIN_INSTALL=y
DOMOGIK_XPL_HUB=python
while [ "$1" ] ; do
arg=$1
case $arg in
--hub_c)
DOMOGIK_XPL_HUB="c"
echo "The C version of the xPL hub will be used instead of the python one."
;;
--secondary)
MAIN_INSTALL="n"
echo "This installation will be done as a secondary host installation : only manager (and plugins) will be launched on this host."
;;
-h)
display_usage
exit 0
;;
--help)
display_usage
exit 0
;;
*)
display_usage
exit 1
;;
esac
shift
done
DMG_HOME=
DMG_ETC=/etc/domogik
DMG_CACHE=/var/cache/domogik
DMG_LIB=/var/lib/domogik
DMG_LOCK=/var/lock/domogik
function stop_domogik {
if [ -f "/etc/init.d/domogik" -o -f "/etc/rc.d/domogik" ];then
if [ -d "/var/run/domogik" ];then
[ -f /etc/conf.d/domogik ] && . /etc/conf.d/domogik
[ -f /etc/default/domogik ] && . /etc/default/domogik
if [ -f "/etc/domogik/domogik.cfg" ];then
echo "There is already a Domogik on this system. Try to stop it before installation..."
/etc/init.d/domogik stop
fi
fi
fi
}
function run_setup_py {
MODE=$1
case $MODE in
develop|install)
if [ -f "setup.py" ];then
python ./ez_setup.py
python ./setup.py $MODE
if [ "x$?" != "x0" ];then
echo "setup.py script exists with a non 0 return value : $?"
exit 13
fi
else
echo "Can't find setup.py, did you download the sources correctly? "
exit 1
fi
;;
*)
echo "Only develop and install modes are available"
;;
esac
}
function test_sources {
FILENAME=$1
[ -d "$PWD/src" ] ||( echo "Can't find src/ directory, are you running this script from the sources main directory? (with ./$FILENAME" && exit 2 )
[ -f "src/domogik/examples/config/domogik.cfg" ] ||( echo "Can't find src/domogik/examples/config/domogik.cfg file!" && exit 3 )
[ -f "src/domogik/examples/default/domogik" ] ||( echo "Can't find src/domogik/examples/default/domogik file!" && exit 4 )
[ -f "src/domogik/examples/init/domogik" ] ||( echo "Can't find src/domogik/examples/init/domogik!" && exit 5 )
}
function copy_sample_files {
read -p "Which user will run domogik, it will be created if it does not exist yet? (default : domogik) " d_user
d_user=${d_user:-domogik}
if ! getent passwd $d_user >/dev/null;then
echo "I can't find informations about this user !"
read -p "Do you want to create it ? (y/n) " create
if [ "$create" = "y" ];then
adduser $d_user
else
echo "Please restart this script when the user $d_user will exist."
exit 9
fi
fi
# For upgrade with 0.1.0
d_home=$(getent passwd $d_user |cut -d ':' -f 6)
keep="n"
already_cfg=
# create /etc/domogik entry
if [ ! -d $DMG_ETC ];then
mkdir $DMG_ETC
chown $d_user:root $DMG_ETC
chmod 755 $DMG_ETC
fi
# create /var/cache/domogik
if [ ! -d $DMG_CACHE ];then
mkdir $DMG_CACHE
chown $d_user:root $DMG_CACHE
fi
# create /var/lib/domogik
if [ ! -d $DMG_LIB ];then
mkdir $DMG_LIB
chown $d_user:root $DMG_LIB
fi
# create /var/lock/domogik
if [ ! -d $DMG_LOCK ];then
mkdir $DMG_LOCK
chown $d_user:root $DMG_LOCK
fi
# create folders for packages management
for pkg_rep in pkg-cache cache
do
if [ ! -d $DMG_CACHE/$pkg_rep ];then
mkdir $DMG_CACHE/$pkg_rep
chown $d_user:root $DMG_CACHE/$pkg_rep
fi
done
# create folders for packages management
for pkg_rep in domogik_packages domogik_packages/plugins domogik_packages/externals domogik_packages/stats domogik_packages/url2xpl domogik_packages/design domogik_packages/data
do
if [ ! -d $DMG_LIB/$pkg_rep ];then
mkdir $DMG_LIB/$pkg_rep
chown $d_user:root $DMG_LIB/$pkg_rep
fi
done
# For upgrade with 0.1
if [ -f $d_home/.domogik/domogik.cfg ];then
mv $d_home/.domogik/domogik.cfg $DMG_ETC/domogik.cfg
chown $d_user:root $DMG_ETC/domogik.cfg
chmod 640 $DMG_ETC/domogik.cfg
if [ $MAIN_INSTALL = "y" ] ; then
cp -f src/domogik/examples/packages/sources.list $DMG_ETC/sources.list
chown $d_user:root $DMG_ETC/sources.list
chmod 640 $DMG_ETC/sources.list
fi
fi
if [ ! -f $DMG_ETC/domogik.cfg ];then
cp -f src/domogik/examples/config/domogik.cfg $DMG_ETC/domogik.cfg
cp -f src/xplhub/examples/config/xplhub.cfg $DMG_ETC/xplhub.cfg
chown $d_user:root $DMG_ETC/*.cfg
chmod 640 $DMG_ETC/*.cfg
if [ $MAIN_INSTALL = "y" ] ; then
cp -f src/domogik/examples/packages/sources.list $DMG_ETC/sources.list
chown $d_user:root $DMG_ETC/sources.list
chmod 640 $DMG_ETC/sources.list
fi
else
keep="y"
already_cfg=1
read -p "You already have Domogik configuration files. Do you want to keep them ? [Y/n]" keep
if [ "x$keep" = "x" ];then
keep="y"
fi
if [ "$keep" = "n" -o "$keep" = "N" ];then
cp -f src/domogik/examples/config/domogik.cfg $DMG_ETC/domogik.cfg
cp -f src/xplhub/examples/config/xplhub.cfg $DMG_ETC/xplhub.cfg
chown $d_user:root $DMG_ETC/*.cfg
chmod 640 $DMG_ETC/*.cfg
if [ $MAIN_INSTALL = "y" ] ; then
cp -f src/domogik/examples/packages/sources.list $DMG_ETC/sources.list
chown $d_user:root $DMG_ETC/sources.list
chmod 640 $DMG_ETC/sources.list
fi
fi
fi
# Add the xplhub.cfg config file in an existing configuration it it does not exists
if [ ! -f $DMG_ETC/xplhub.cfg ] ; then
echo "No existing xplhub.cfg file : creating it"
cp -f src/xplhub/examples/config/xplhub.cfg $DMG_ETC/xplhub.cfg
chown $d_user:root $DMG_ETC/xplhub.cfg
chmod 640 $DMG_ETC/xplhub.cfg
fi
if [ -d "/etc/default/" ];then
if [ "$keep" = "n" -o "$keep" = "N" ];then
cp src/domogik/examples/default/domogik /etc/default/
fi
else
echo "Can't find the directory where I can copy system-wide config. Usually it is /etc/default/"
exit 6
fi
if [ -d "/etc/logrotate.d/" ];then
cp src/domogik/examples/logrotate/domogik /etc/logrotate.d/
cp src/xplhub/examples/logrotate/xplhub /etc/logrotate.d/
chmod 644 /etc/logrotate.d/domogik
chmod 644 /etc/logrotate.d/xplhub
fi
if [ -d "/etc/init.d/" ];then
cp src/domogik/examples/init/domogik /etc/init.d/
chmod +x /etc/init.d/domogik
elif [ -d "/etc/rc.d/" ];then
cp src/domogik/examples/init/domogik /etc/rc.d/
chmod +x /etc/rc.d/domogik
else
echo "Init directory does not exist (/etc/init.d or /etc/rc.d)"
exit 7
fi
}
function update_default_config {
if [ ! -f /etc/default/domogik ];then
echo "Can't find /etc/default/domogik!"
exit 8
fi
[ -f /etc/default/domogik ] && sed -i "s;^DOMOGIK_USER.*$;DOMOGIK_USER=$d_user;" /etc/default/domogik
[ -f /etc/default/domogik ] && sed -i "s;^DOMOGIK_XPL_HUB.*$;DOMOGIK_XPL_HUB=${DOMOGIK_XPL_HUB};" /etc/default/domogik
if [ "$MODE" = "develop" ];then
arch_path=$(python install/get_arch.py)
d_custom_path=$PWD/$arch_path
[ -f /etc/default/domogik ] && sed -i "s;^CUSTOM_PATH.*$;CUSTOM_PATH=$d_custom_path;" /etc/default/domogik
fi
}
function update_default_config_other_hosts {
if [ ! -f /etc/default/domogik ];then
echo "Can't find /etc/default/domogik!"
exit 8
fi
[ -f /etc/default/domogik ] && sed -i "s;^MANAGER_PARAMS.*$;MANAGER_PARAMS=\"-p\";" /etc/default/domogik
}
function update_user_config {
if [ "$keep" = "n" -o "$keep" = "N" ];then
if [ "$MODE" = "install" ];then
prefix=""
sed -i "s;^package_path.*$;package_path = $DMG_LIB;" $DMG_ETC/domogik.cfg
sed -i "s;^#src_prefix.*$;#src_prefix = $prefix;" $DMG_ETC/domogik.cfg
else
prefix=$PWD/src
sed -i "s;^package_path.*$;#package_path = $DMG_LIB;" $DMG_ETC/domogik.cfg
sed -i "s;^#src_prefix.*$;src_prefix = $prefix;" $DMG_ETC/domogik.cfg
fi
read -p "Which interface do you want to bind to? (default : lo) : " bind_iface
bind_iface=${bind_iface:-lo}
bind_addr=$($CMD_NET $bind_iface|egrep -o "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"|head -1)
if [ "x$bind_addr" = "x" ];then
echo "Can't find the address associated to the interface!"
exit 20
fi
sed -i "s/^bind_interface.*$/bind_interface = $bind_addr/" $DMG_ETC/domogik.cfg
sed -i "s/^HUB_IFACE.*$/HUB_IFACE=$bind_iface/" /etc/default/domogik
# will be overide on secondary host
sed -i "s/^rest_server_ip.*$/rest_server_ip = $bind_addr/" $DMG_ETC/domogik.cfg
fi
}
function update_rest_config_for_secondary_host {
if [ "$keep" = "n" -o "$keep" = "N" ];then
read -p "Ip of the main Domogik installation (ip of Rest server) : " rest_server_ip
read -p "Port of the main Domogik installation (port of Rest server) : " rest_server_port
sed -i "s/^rest_server_ip.*$/rest_server_ip = $rest_server_ip/" $DMG_ETC/domogik.cfg
sed -i "s/^rest_server_port.*$/rest_server_port = $rest_server_port/" $DMG_ETC/domogik.cfg
fi
}
function update_user_config_db {
# if keep == n
# => select the db type (mysql or postgresql
# => display auth depenting on the DB type
# db_ok = false
# if keep === y
# => test the connection based on the config
# => set db_ok to result
# while db_ok == false
# => request info comman to all dbs
# => test the connection based on the config
# => set db_ok to result
# write out to the file
if [ "$keep" = "n" -o "$keep" = "N" ];then
# request what db to use
read -p "Db Type (mysql or postgresql) [mysql] :" db_type
if [ "$db_type" = "" ];then
db_type="mysql"
fi
#Display config config
if [ "$db_type" = "mysql" ];then
echo "You need to have a working Mysql server with a domogik user and database."
echo "You can create it using these commands (as mysql admin user) :"
echo " > CREATE DATABASE domogik;"
echo " > GRANT ALL PRIVILEGES ON domogik.* to domogik@127.0.0.1 IDENTIFIED BY 'randompassword';"
else
echo "You need to have a working Postgresql server with a domogik user and database."
echo "You can create it using these commands (as postgresql admin user) :"
echo " > su - postgres"
echo " > createdb domogik"
echo " > createuser --no-createdb --no-adduser --no-createrole -P domogik"
echo " > exit"
fi
read -p "Press Enter to continue the installation when your setup is ok. "
fi
db_ok=
if [ "$keep" = "y" -o "$keep" = "Y" ];then
db_user=$(grep "^db_user" $DMG_ETC/domogik.cfg|cut -d'=' -f2|tr -d ' ')
db_type=$(grep "^db_type" $DMG_ETC/domogik.cfg|cut -d'=' -f2|tr -d ' ')
db_password=$(grep "^db_password" $DMG_ETC/domogik.cfg|cut -d'=' -f2|tr -d ' ')
db_port=$(grep "^db_port" $DMG_ETC/domogik.cfg|cut -d'=' -f2|tr -d ' ')
db_name=$(grep "^db_name" $DMG_ETC/domogik.cfg|cut -d'=' -f2|tr -d ' ')
db_host=$(grep "^db_host" $DMG_ETC/domogik.cfg|cut -d'=' -f2|tr -d ' ')
if [ "$db_type" = "mysql" ];then
echo "SELECT 1;"|mysql -h$db_host -P$db_port -u$db_user -p$db_password $db_name > /dev/null
else
psql -h $db_host -p $db_port -U $db_user -d $db_name -c "SELECT 1;" > /dev/null
fi
if [ $? -ne 0 ];then
read -p "Something was wrong, can't access to the database, please check your setup and press Enter to retry."
else
db_ok=true
fi
fi
while [ ! $db_ok ];do
echo "Please set your database parameters."
read -p "Username : " db_user
stty -echo echonl
read -p "Password : " db_password
stty echo
if [ "$db_type" = "mysql" ];then
read -p "Port [3306] : " db_port
if [ "$db_port" = "" ];then
db_port=3306
fi
else
read -p "Port [5432] : " db_port
if [ "$db_port" = "" ];then
db_port=5432
fi
fi
read -p "Host [127.0.0.1] :" db_host
if [ "$db_host" = "" ];then
db_host="127.0.0.1"
fi
read -p "Database name [domogik]: " db_name
if [ "$db_name" = "" ];then
db_name="domogik"
fi
if [ "$db_type" = "mysql" ];then
echo "SELECT 1;"|mysql -h$db_host -P$db_port -u$db_user -p$db_password $db_name > /dev/null
else
psql -h $db_host -p $db_port -U $db_user -d $db_name -c "SELECT 1;" > /dev/null
fi
if [ $? -ne 0 ];then
read -p "Something was wrong, can't access to the database, please check your setup and press Enter to retry."
else
db_ok=true
echo "Connection test OK"
sed -i "s;^db_type.*$;db_type = $db_type;" $DMG_ETC/domogik.cfg
sed -i "s;^db_user.*$;db_user = $db_user;" $DMG_ETC/domogik.cfg
sed -i "s;^db_password.*$;db_password = $db_password;" $DMG_ETC/domogik.cfg
sed -i "s;^db_port.*$;db_port = $db_port;" $DMG_ETC/domogik.cfg
sed -i "s;^db_name.*$;db_name = $db_name;" $DMG_ETC/domogik.cfg
sed -i "s;^db_host.*$;db_host = $db_host;" $DMG_ETC/domogik.cfg
fi
done
}
function call_app_installer {
echo "** Calling Application Installer"
/bin/su -c "python ./install/installer.py" $d_user
if [ $? -ne 0 ];then
echo "ERROR : An error occured during app_installer execution, read the previous lines for detail."
exit 1
fi
}
function check_tools {
if [ -x "$(which ip)" ];then
CMD_NET='ip addr show'
else
if [ -x "$(which ifconfig)" ];then
CMD_NET='ifconfig'
else
echo "no network command found"
exit 21
fi
fi
}
function check_python {
if [ ! -x "$(which python)" ];then
echo "No python binary found, please install at least python2.6";
exit 11
else
if python -V 2>&1|grep -qs "Python 2.[345]";then
echo "Bad python version used, please install at least 2.6, and check /usr/bin/python starts the good version."
exit 12
fi
fi
}
function copy_tools {
[ -d "/usr/sbin" ] && (cp -f src/tools/dmg* /usr/sbin;chmod +x /usr/sbin/dmg*)
}
function modify_hosts {
[ -f "/etc/hosts" ] || touch /etc/hosts
if ! grep localhost /etc/hosts|grep -qs 127.0.0.1;then
sed -i 's/^\(.*localhost.*\)$/#\1/' /etc/hosts
echo "127.0.0.1 localhost" >> /etc/hosts
fi
}
function create_log_dir {
mkdir -p /var/log/domogik
chown -R $d_user: /var/log/domogik
mkdir -p /var/log/xplhub
chown -R $d_user: /var/log/xplhub
}
function install_plugins {
if [ "$MODE" = "develop" ];then
chmod +x src/tools/packages/insert_data.py
for file in src/share/domogik/plugins/*.json;do
if [[ $file != "src/share/domogik/plugins/*.json" ]] ; then
echo "** Parse $file"
su -c "src/tools/packages/insert_data.py $file" $d_user
echo "** File $file parsed"
fi
done
for file in src/share/domogik/externals/*.json;do
if [[ $file != "src/share/domogik/externals/*.json" ]] ; then
echo "** Parse $file"
su -c "src/tools/packages/insert_data.py $file" $d_user
echo "** File $file parsed"
fi
done
fi
}
#Main part
if [ $UID -ne 0 ];then
echo "Please restart this script as root!"
exit 10
fi
if [ "$(dirname $0)" != "." ];then
echo "Please run this script from main source directory (as ./install.sh"
exit 15
fi
if [ "$(pwd | cut -d"/" -f2)" == "root" ];then
echo "Please use the Domogik package outside of the /root/ folder"
exit 20
fi
#stop_domogik
check_tools
check_python
test_sources $0
read -p "Which install mode do you want (choose develop if you don't know)? [install/develop] : " MODE
while [ "$MODE" != "develop" -a "$MODE" != "install" ];do
read -p "Which install mode do you want? [install/develop] : " MODE
done
read -p "If you want to use a proxy, please set it now. It will only be used during installation. (ex: http://1.2.3.4:8080)" http_proxy
if [ "x$http_proxy" != "x" ];then
export http_proxy
fi
#this is needed now only because of old bug (#792) and should be useless for new install
if [ "$SUDO_USER" ];then
trap "[ -d \"$HOME/.python-eggs\" ] && chown -R $SUDO_USER: $HOME/.python-eggs" EXIT
[ -d "$HOME/.python-eggs" ] && chown -R $SUDO_USER: $HOME/.python-eggs/
else
trap "[ -d \"$HOME/.python-eggs\" ] && chown -R $USER: $HOME/.python-eggs" EXIT
[ -d "$HOME/.python-eggs" ] && chown -R $USER: $HOME/.python-eggs/
fi
run_setup_py $MODE
copy_sample_files
update_default_config
if [ $MAIN_INSTALL = "n" ] ; then
update_default_config_other_hosts
fi
update_user_config
if [ $MAIN_INSTALL = "y" ] ; then
update_user_config_db
else
update_rest_config_for_secondary_host
fi
copy_tools
create_log_dir
if [ $MAIN_INSTALL = "y" ] ; then
call_app_installer
install_plugins
fi
modify_hosts
echo "Everything seems to be good, Domogik should be installed correctly."
echo "I will start the test_config.py script to check it."
read -p "Please press Enter when ready."
chmod +x ./test_config.py && ./test_config.py
if [ "$SUDO_USER" ];then
[ -d "$HOME/.python-eggs" ] && chown -R $SUDO_USER: $HOME/.python-eggs/
else
[ -d "$HOME/.python-eggs" ] && chown -R $USER: $HOME/.python-eggs/
fi
trap - EXIT