forked from spaun299/profireader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·562 lines (473 loc) · 18.2 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
556
557
558
559
560
561
#!/bin/bash
rand=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 16 | head -n 1)
PWD=$(pwd)
dialog_installed=$(dpkg-query -W --showformat='${Status}\n' dialog|grep "install ok installed")
if [[ "$dialog_installed" == "" ]]; then
sudo apt-get install dialog
fi
function e {
echo "$*"
}
function menu_ {
echo ''
}
function ret {
e
read -p "Press [enter] to continue"
e
}
rr() {
if [[ "$2" == "" ]]; then
read -p "$1" retvalue
else
read -p "$1[$2]" retvalue
if [[ "$retvalue" == "" ]]; then
retvalue="$2"
fi
fi
echo "$retvalue"
}
function menu_exit {
exit
}
function conf {
# call with a prompt string or use a default
read -r -p "${1:-Are you sure? [Y/n]} " response
if [[ "$response" == "" ]]; then
true
else
case $response in
[yY][eE][sS]|[yY])
true
;;
*)
false
;;
esac
fi
}
function get_main_domain {
echo "from main_domain import MAIN_DOMAIN
print(MAIN_DOMAIN)" | python
}
function down {
filetoget=$1
if [[ "$2" == "" ]]; then
filetoput=${1##*/}
else
filetoput=$2
fi
filetobak="$3"
ntaxauser=$(rr 'Enter ntaxa username:')
ntaxapass=$(rr "Enter ntaxa password:")
if [[ "$3" != '' ]]; then
echo " mv $filetoget $filetobak"
fi
command="wget --user='$ntaxauser' --password='$ntaxapass' -O /tmp/"$rand"tmpfile http://x.m.ntaxa.com/profireader/$filetoget
if [[ \"\$?\" == \"0\" ]]; then"
if [[ "$3" != '' ]]; then
command="$command
mv $filetoput $filetobak"
fi
command="$command
mv /tmp/"$rand"tmpfile $filetoput
else
echo 'wget failed!'
fi"
conf_comm "$command" nosudo $4
}
conf_comm() {
rd=`tput setaf 1`
rst=`tput sgr0`
if [[ "$2" == "sudo" ]]; then
echo "${rd}"
echo "Command we're going to execute (with sudo):"
else
echo "Command we're going to execute:"
fi
e
echo "$1" | sed -e 's/^/ /g'
echo "${rst}"
if conf; then
e
echo "$1" > /tmp/"$rand"menu_command_run_confirmed.sh
if [[ "$2" == "sudo" ]]; then
sudo bash /tmp/"$rand"menu_command_run_confirmed.sh
else
bash /tmp/"$rand"menu_command_run_confirmed.sh
fi
# eval `echo "$1" | sed -e 's/"/\"/g' -e 's/^/sudo bash -c "/g' -e 's/$/";/g'`
if [[ "$4" == "" ]]; then
ret
fi
if [[ "$3" != "" ]]; then
next="$3"
fi
true
else
false
fi
}
function warn_about_rm {
if [[ -e $1 ]]; then
echo "warning: $1 exists and will be removed"
fi
}
function error_if_exists {
if [[ -e $1 ]]; then
echo "warning: $1 exists and will be removed"
fi
}
function get_profidb {
echo `cat scrt/secret_data.py | grep 'DB_NAME\s*=' | sed -e 's/^\s*DB_NAME\s*=\s*['"'"'"]\([^'"'"'"]*\).*$/\1/g' `
}
function runsql {
conf_comm "systemctl restart postgresql.service
su postgres -c \"echo \\\"$1\\\" | psql\"" sudo "$2"
}
function runsql_dump {
profidb=$(get_profidb)
filenam=$(rr "$1" "$2")
conf_comm "systemctl restart postgresql.service
su postgres -c 'cat $filenam | psql $profidb'" sudo "$3"
}
function menu_origin {
destination=`git remote -v | grep 'fetch' | sed -e 's/^.*github.com:\([^\/]*\)\/.*$/\1/g'`
conf_comm "git remote rename origin $destination
git remote add origin [email protected]:kakabomba/profireader.git" nosudo postgres_9_4
}
function menu_postgres_9_4 {
conf_comm "echo 'deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main' >> /etc/apt/sources.list.d/pgdg.list
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | \
sudo apt-key add -
sudo apt-get update
apt-get install postgresql-9.4" sudo elastic
}
function menu_elastic {
elastic_version=$(rr 'elasticsearch version' 2.3.3)
conf_comm "apt-get install openjdk-7-jre
wget https://download.elastic.co/elasticsearch/release/org/elasticsearch/distribution/deb/elasticsearch/"$elastic_version"/elasticsearch-"$elastic_version".deb
dpkg -i ./elasticsearch-"$elastic_version".deb
rm ./elasticsearch-"$elastic_version".deb" sudo fluent
}
function menu_fluent {
# elastic_version=$(rr 'elasticsearch version' 2.3.3)
conf_comm "curl -L https://toolbelt.treasuredata.com/sh/install-debian-jessie-td-agent2.sh | sh" sudo deb
}
function menu_deb {
conf_comm "apt-get update
apt-get install libpq-dev python-dev libapache2-mod-wsgi-py3 libjpeg-dev memcached build-essential libssl-dev libffi-dev openjdk-8-jre haproxy" sudo npm
}
function menu_npm {
conf_comm "apt-get install nodejs
apt-get install npm
ln -s /usr/bin/nodejs /usr/bin/node
npm install -g bower
npm install -g gulp" sudo bower
}
function menu_bower {
conf_comm "cd ./profapp/static
bower install" nosudo bower_components_dev
}
function menu_bower_components_dev {
conf_comm "cd ./profapp/static/bower_components_dev
mkdir ./angular-db-filemanager
cd ./angular-db-filemanager
git clone [email protected]:kakabomba/angular-server-driven-filemanager.git .
cd ..
mkdir ./angular-crop
cd ./angular-crop
git clone [email protected]:kakabomba/ng-crop.git .
" nosudo hosts
}
function menu_gulp {
conf_comm "cd ./profapp/static
npm install gulp del gulp-less-sourcemap gulp-watch run-sequence gulp-task-listing
gulp" nosudo gulp_install_all
}
function menu_gulp_install_all {
conf_comm "cd ./profapp/static
gulp all" nosudo hosts
}
function menu_hosts {
conf_comm "sed -i '/\(db\|web\|mail\|memcached\|elastic\).profi/d' /etc/hosts
sed -i '/\\.profi/d' /etc/hosts
echo '' >> /etc/hosts
echo '127.0.0.1 db.profi mail.profi memcached.profi elastic.profi fluent.profi' >> /etc/hosts
echo '127.0.0.1 web.profi static.web.profi file001.web.profi socket.web.profi portal.web.profi' >> /etc/hosts
cat /etc/hosts" sudo cron_files
}
function menu_cron_files {
files=$(/bin/ls ./conf/cron | tr '\n' ' ')
conf_comm "mkdir /var/log/profi
mkdir /run/profi
rm /etc/cron.d/profi_*
for file in $files
do
cat conf/cron/\$file | sed 's#/var/www/#$PWD/#g' > /etc/cron.d/profi_\$file
done
systemctl restart cron.service" sudo haproxy_config
}
# function menu_haproxy_compile {
# conf_comm "apt-get purge haproxy
# sed -i '/haproxy-1.5/d' /etc/apt/sources.list
# echo '' >> /etc/apt/sources.list
# echo 'deb http://ppa.launchpad.net/vbernat/haproxy-1.5/ubuntu trusty main' >> /etc/apt/sources.list
# echo 'deb-src http://ppa.launchpad.net/vbernat/haproxy-1.5/ubuntu trusty main' >> /etc/apt/sources.list
# apt-get update
# apt-get install haproxy" sudo haproxy_config
# }
function menu_haproxy_config {
conf_comm "cp ./conf/haproxy.cfg /etc/haproxy/
systemctl restart haproxy.service" sudo letsencrypt
}
function menu_letsencrypt {
conf_comm "echo 'deb http://ftp.debian.org/debian jessie-backports main' > /etc/apt/sources.list.d/jessie-backports.list
apt-get update
apt-get install certbot -t jessie-backports" sudo apache2_config
}
function menu_apache2_config {
wwwdir=$(rr 'Enter http dir' $PWD)
maindomain=$(rr 'Enter main domain' `get_main_domain`)
conf_comm "cat ./conf/apache2/directory-access.conf | sed -e 's#----directory----#$wwwdir#g' | sed -e 's#----maindomain----#$maindomain#g' > /etc/apache2/conf-enabled/directory-access.conf
cat ./conf/apache2/lets-encrypt-requests.conf | sed -e 's#----directory----#$wwwdir#g' | sed -e 's#----maindomain----#$maindomain#g' > /etc/apache2/conf-enabled/lets-encrypt-requests.conf
cp ./conf/apache2/ports.conf /etc/apache2/
rm /etc/apache2/sites-enabled/000-default.conf
mkdir /var/log/profi
a2enmod wsgi
a2enmod ssl
systemctl restart postgresql.service
systemctl restart apache2.service" sudo apache2_profi_vh_ssl
}
function menu_apache2_profi_vh_ssl {
wwwdir=$(rr 'Enter http dir' $PWD)
maindomain=$(rr 'Enter main domain' `get_main_domain`)
conf_comm "cat ./conf/apache2/main-domain.conf | sed -e 's#----directory----#$wwwdir#g' | sed -e 's#----maindomain----#$maindomain#g' > /etc/apache2/conf-enabled/main-domain.conf
cd `pwd`/tools
./get_ssl_for_domain.sh `pwd`/letsencryptrequests $maindomain www.$maindomain static.$maindomain file001.$maindomain
systemctl restart apache2.service" sudo apache2_fronts_vh_ssl
}
function menu_apache2_fronts_vh_ssl {
venvdir=$(rr 'venv directory' .venv)
conf_comm "cd `pwd`
source $venvdir/bin/activate
cd tools
python check_ssl.py
systemctl restart apache2.service" sudo secret_data
}
function menu_apache2_check_ssls {
venvdir=$(rr 'venv directory' .venv)
conf_comm "cd `pwd`
source $venvdir/bin/activate
cd tools
python check_ssl.py
systemctl restart apache2.service" sudo secret_data
}
function menu_secret_data {
down secret_data.txt scrt/secret_data.py scrt/secret_data.`$gitv`_`$datev`.bak secret_client
}
function menu_secret_client {
down client_secret.json scrt/client_secret.json scrt/client_secret.json.`$gitv`_`$datev`.bak download_key_pem
}
function menu_download_key_pem {
down profireader_haproxy.key.pem scrt/profireader_haproxy.key.pem profireader_haproxy.key.pem scrt/profireader_haproxy.key.pem.`$gitv`_`$datev`.bak python_3
}
function menu_python_3 {
pversion=$(rr 'Enter python version' 3.4.2)
destdir=$(rr 'destination dir' /usr/local/opt/python-$pversion)
if [[ -e $destdir ]]; then
echo "error: $destdir exists"
else
# warn_about_rm '/usr/bin/python3'
# warn_about_rm '/usr/bin/pyvenv'
conf_comm "cd /tmp/
rm -rf 'Python-$pversion/*'
rm 'Python-$pversion.tgz'
wget 'https://www.python.org/ftp/python/$pversion/Python-$pversion.tgz'
tar -zxf 'Python-$pversion.tgz'
cd 'Python-$pversion'
./configure --prefix='$destdir'
make
make install
cd /tmp
rm -rf 'Python-$pversion'" nosudo venv
fi
}
function menu_venv {
destdir=$(rr 'destination dir for virtual directory' .venv)
pythondir=$(rr 'python dir' /usr/local/opt/python-3.4.2)
if [[ -e $destdir ]]; then
echo "error: $destdir exists"
else
conf_comm "$pythondir/bin/pyvenv $destdir
cp ./activate_this.py $destdir/bin
source $destdir/bin/activate
pip install --upgrade pip==9.0.1" nosudo modules
fi
}
function menu_modules {
req=$(rr 'file with modules' requirements.txt)
destdir=$(rr 'venv directory' .venv)
conf_comm "
cd `pwd`
source $destdir/bin/activate
pip3 install -I -r $req" nosudo bower_components_dev
}
function menu_port {
toport=$(rr 'redirect port 80 to port' 8080)
conf_comm "iptables -t nat -A OUTPUT -d 127.0.0.1 -p tcp --dport 80 -j REDIRECT --to-port $toport" sudo db_user_bass
}
function menu_db_user_pass {
echo "Going to create user/pass from secret data and create such user/pass using postgres user"
echo "If user exists, only password will be changed"
profiuser=`cat scrt/secret_data.py | grep 'DB_USER' | sed -e 's/^\s*DB_USER\s*=\s*['"'"'"]\([^'"'"'"]*\).*$/\1/g' `
psqluser=$(rr 'Enter postgresql user' $profiuser)
profipass=`cat scrt/secret_data.py | grep 'DB_PASSWORD' | sed -e 's/^\s*DB_PASSWORD\s*=\s*['"'"'"]\([^'"'"'"]*\).*$/\1/g' `
psqlpass=$(rr 'Enter postgresql password' $profipass)
runsql "CREATE USER $psqluser;
ALTER USER $psqluser WITH PASSWORD '$psqlpass';" compare_local_makarony
}
makaronyaddress='m.ntaxa.com/profireader/54322'
localaddress='localhost/profireader/5432'
kupytyaddress='a.ntaxa.com/profireader/54143'
artekaddress='a.ntaxa.com/profireader/54141'
function menu_compare_local_makarony {
conf_comm "cd ./db
./postgres.dump_and_compare_structure.sh $makaronyaddress $localaddress" nosudo compare_local_kupyty
}
function menu_compare_local_kupyty {
conf_comm "cd ./db
./postgres.dump_and_compare_structure.sh $localaddress $kupytyaddress" nosudo compare_makarony_artek
}
function menu_compare_local_artek {
conf_comm "cd ./db
./postgres.dump_and_compare_structure.sh $localaddress $artekaddress" nosudo compare_makarony_artek
}
function menu_compare_makarony_artek {
conf_comm "cd ./db
./postgres.dump_and_compare_structure.sh $makaronyaddress $artekaddress" nosudo db_rename
}
function menu_db_rename {
profidb=$(get_profidb)
runsql "ALTER DATABASE $profidb RENAME TO bak_$profidb""_"`$gitv`"_"`$datev` 'db_create'
}
function menu_db_create {
profidb=$(get_profidb)
psqldb=$(rr 'Enter postgresql database name' $profidb)
profiuser=`cat scrt/secret_data.py | grep 'DB_USER' | sed -e 's/^\s*DB_USER\s*=\s*['"'"'"]\([^'"'"'"]*\).*$/\1/g' `
runsql "CREATE DATABASE $psqldb WITH ENCODING 'UTF8' LC_COLLATE='C.UTF-8' LC_CTYPE='C.UTF-8' OWNER = $profiuser TEMPLATE=template0" db_download_minimal
}
function menu_db_download_minimal {
down database.structure db/database.structure db/database.structure.`$gitv`_`$datev`.bak db_load_minimal
}
function menu_db_load_minimal {
runsql_dump 'Enter sql structure filename' db/database.structure db_save_minimal
}
function menu_db_save_minimal {
profidb=$(get_profidb)
conf_comm "su postgres -c 'pg_dump -s $profidb' > db/database.structure
tables=\$(su postgres -c \"echo 'SELECT RelName FROM pg_Description JOIN pg_Class ON pg_Description.ObjOID = pg_Class.OID WHERE ObjSubID = 0 AND Description LIKE '\\\"'\\\"%persistent%\\\"'\\\" | psql -t $profidb\" | sed '/^\\s*\$/d' | sed -e 's/^/-t /g' | tr \"\\n\" \" \" )
su postgres -c \"pg_dump --inserts -a \$tables $profidb\" >> db/database.initial
git diff db/database.structure
git diff db/database.initial" sudo 'db_download_full'
}
function menu_db_download_full {
down database_full.sql db/database_full.sql db/database_full.sql.`$gitv`_`$datev`.bak db_load_full
}
function menu_db_load_full {
runsql_dump 'Enter sql full dump filename' db/database_full.sql menu_db_reassign_ownership
}
function menu_db_reassign_ownership {
profidb=$(get_profidb)
profiuser=`cat scrt/secret_data.py | grep 'DB_USER' | sed -e 's/^\s*DB_USER\s*=\s*['"'"'"]\([^'"'"'"]*\).*$/\1/g' `
conf_comm "
su postgres -c \"for tbl in \\\$(psql -qAt -c 'SELECT tablename FROM pg_tables WHERE schemaname = '\\\"'\\\"public\\\"'\\\"';' $profidb); do echo \\\$tbl; psql -c 'ALTER table \\\"'\\\$tbl'\\\" owner to $profiuser' $profidb ; done\"
su postgres -c \"for tbl in \\\$(psql -qAt -c 'SELECT sequence_name FROM information_schema.sequences WHERE sequence_schema = '\\\"'\\\"public\\\"'\\\"';' $profidb); do echo \\\$tbl; psql -c 'ALTER table \\\"'\\\$tbl'\\\" owner to $profiuser' $profidb ; done\"
su postgres -c \"for tbl in \\\$(psql -qAt -c 'SELECT table_name FROM information_schema.views WHERE table_schema = '\\\"'\\\"public\\\"'\\\"';' $profidb); do echo \\\$tbl; psql -c 'ALTER table \\\"'\\\$tbl'\\\" owner to $profiuser' $profidb ; done\"
" sudo 'db_localize'
}
function menu_db_localize {
profidb=$(get_profidb)
maindomain=$(rr 'Enter main domain' `get_main_domain`)
conf_comm "
su postgres -c \"psql -c 'SELECT __localize_emails()' $profidb;\"
su postgres -c \"psql -c 'SELECT __localize_hosts('\\\"'\\\"'$maindomain'\\\"'\\\"')' $profidb;\"
" sudo 'elastic_reindex_all'
}
function menu_db_save_full {
profidb=$(get_profidb)
conf_comm "
mv db/database_full.sql db/database_full.sql."`$gitv`"_"`$datev`".bak
su postgres -c 'pg_dump $profidb' > db/database_full.sql
ls -l1sh db/database_full.*
" sudo 'exit'
}
function menu_elastic_reindex_all {
destdir=$(rr 'venv directory' .venv)
conf_comm "
source $destdir/bin/activate
cd ./tools
python ./update_elastic_search.py delete_elastic_indexes
python ./update_elastic_search.py recreate_all_elastic_documents
" nosudo 'exit'
}
next='_'
#a="/bin/ls;
#/bin/ls"
#eval $a
#exit
if [[ "$1" == "" ]]; then
while :
do
#next='exit'
#"haproxy_compile" "compile and install haproxy" \
#
dialog --title "profireader" --nocancel --default-item $next --menu "Choose an option" 22 78 17 \
"origin" "change git origin and add new remote repo" \
"postgres_9_4" "install postgres 9.4" \
"elastic" "install elastic search" \
"fluent" "install fluent" \
"deb" "install deb packages" \
"cron_files" "update cron files" \
"haproxy_config" "copy haproxy config to /etc/haproxy" \
"npm" "install nodejs, npm, bower and gulp globally" \
"bower" "download bower components in ./profapp/static/bower_components" \
"bower_components_dev" "download bower development components in ./profapp/static/bower_components_dev" \
"gulp" "install gulp in ./profapp/static" \
"gulp_install_all" "install gulp modules from ./profapp/static/bower/ to project working directories" \
"hosts" "create virtual domain zone in /etc/hosts" \
"letsencrypt" "install letsencrypt" \
"apache2_config" "copy apache config to /etc/apache2 and allow currend dir" \
"apache2_profi_vh_ssl" "create vh conf file and create/update ssl for main domain (with www., static. and file001. aliases)" \
"apache2_fronts_vh_ssl" "create vh conf file for fronts and create/update ssl via letsencrypt" \
"secret_data" "download secret data" \
"secret_client" "download secret client data" \
"download_key_pem" "download https key and pem file" \
"python_3" "install python 3" \
"venv" "create virtual environment" \
"modules" "install required python modules (via pip)" \
"db_user_pass" "create postgres user/password" \
"db_rename" "rename database (create backup)" \
"db_create" "create empty database" \
"db_save_minimal" "save initial database to file" \
"db_download_minimal" "get minimal database from x.m.ntaxa.com" \
"db_load_minimal" "load minimal database from file" \
"db_save_full" "save full database to file" \
"db_download_full" "get full database from x.m.ntaxa.com" \
"db_load_full" "load full database from file" \
"db_reassign_ownership" "reassign ownership" \
"db_localize" "localize project (change emails and portal hosts)" \
"elastic_reindex_all" "recreate all documents in elasticsearch" \
"compare_local_makarony" "compare local database and dev version" \
"compare_local_kupyty" "compare local database and testing version" \
"compare_local_artek" "compare local database and production version" \
"compare_makarony_artek" "compare dev database and production version" \
"exit" "Exit" 2> /tmp/"$rand"selected_menu_
reset
datev="date +%y_%m_%d___%H_%M_%S"
gitv='git rev-parse --short HEAD'
menu_`cat /tmp/"$rand"selected_menu_`
done
else
menu_$1
fi