This repository has been archived by the owner on Nov 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathinstall.sh
executable file
·479 lines (445 loc) · 15.3 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
#!/usr/bin/env bash
HELP="
First argument : dev or prod
Second argument : path to the install destination (Only with prod argument)
dev : For local devlopement.
prod : For use in production.
Example :
./install.sh dev
./install.sh prod /usr/local/share
"
# Variables
SERVER_USER="www-data"
CURRENT_USER=$(whoami)
LOG_PATH="/var/log/"
export SERVER_USER
export CURRENT_USER
export LOG_PATH
# Get args
if [ -z $1 ] || [[ "$1" = 'dev' ]]; then
arg="dev"
dest=$( pwd )
elif [[ "$1" = 'prod' ]]; then
arg=$1
if [ -z $2 ]; then
dest='/usr/local/share'
elif [[ "$2" = "." ]]; then
dest=$( pwd )
else
dest=$2
fi
elif [[ "$1" = 'help' ]]; then
echo "$HELP"
exit
else
echo 'Bad argument'
exit 1
fi
if [[ "$dest" = $( pwd ) ]]; then
destfull="$dest"/
else
destfull="$dest"/ProbeManager/
fi
install_modules(){
echo '## Install the modules ##'
for f in probemanager/*; do
if [[ -d $f ]]; then
if test -f "$f"/install.sh ; then
./"$f"/install.sh "$arg" "$destfull"
result="$?"
if [ "$result" -ne 0 ]; then
echo "Install failed of "$f
exit "$result"
fi
fi
fi
done
}
clean() {
echo '## Clean Files ##'
if [ -f conf.ini ]; then
rm conf.ini
fi
for f in probemanager/*; do
if [[ -d $f ]]; then
if test -d "$f"/migrations ; then
rm "$f"/migrations/00*.py
fi
fi
done
if [ -f probemanager/celerybeat-schedule.db ]; then
rm probemanager/celerybeat-schedule.db
fi
if [ -f probemanager/probemanager.log ]; then
rm probemanager/probemanager.log
fi
if [ -f probemanager/celery.log ]; then
rm probemanager/celery.log
fi
if [ -f .coverage ]; then
rm .coverage
fi
}
copy_files(){
if [[ "$arg" = 'prod' ]]; then
echo '## Copy files in install dir ##'
if [[ "$destfull" != $( pwd )/ ]]; then # Don't copy in the same directory
if [ -d $destfull ]; then
# copy the project
cp -r probemanager $destfull
# copy the doc
cp -r docs $destfull
# copy the start script for test
cp start.sh $destfull
cp README.rst $destfull
cp CHANGELOG.rst $destfull
fi
fi
fi
}
install_os_dependencies() {
echo '## Install Dependencies ##'
# Debian
if [ -f /etc/debian_version ]; then
sudo apt update
grep -v "#" requirements/os/debian.txt | grep -v "^$" | xargs sudo apt install -y
if [[ "$arg" = 'prod' ]]; then
grep -v "#" requirements/os/debian_prod.txt | grep -v "^$" | xargs sudo apt install -y
fi
fi
# OSX with brew
if [[ $OSTYPE == *"darwin"* ]]; then
if brew --version | grep -qw Homebrew ; then
grep -v "#" requirements/os/osx.txt | grep -v "^$" | xargs brew install
if [[ "$arg" = 'prod' ]]; then
grep -v "#" requirements/os/osx_prod.txt | grep -v "^$" | xargs brew install
fi
fi
fi
}
select_apps(){
if [[ "$arg" = 'prod' ]]; then
echo '## Choose Apps ##'
i=0
for f in probemanager/*; do
if [[ -d $f ]]; then
if [[ -f "$f"/install.sh ]] || [[ -f "$f"/init_db.sh ]] ; then
apps[i]=$( echo $f | cut -f2 -d"/" )
i=$((i+1))
fi
fi
done
for i in ${apps[@]}; do
if [[ -z "${text// }" ]];then
text=$text"'$i'"
else
text=$text", '$i'"
fi
done
echo "Module(s) available : "${apps[*]}
echo "[APPS]" >> "$destfull"conf.ini
echo "PROD_APPS = [$text]" >> "$destfull"conf.ini
fi
}
set_git(){
echo '## Set Git ##'
git_bin=$( which git )
echo "[GIT]" >> "$destfull"conf.ini
echo "GIT_BINARY = $git_bin" >> "$destfull"conf.ini
}
install_virtualenv() {
python_major_version=$( python -c 'import platform; print(platform.python_version())' | cut -d. -f1 )
python_minor_version=$( python -c 'import platform; print(platform.python_version())' | cut -d. -f2 )
if [[ $python_major_version -ne 3 || $python_minor_version -lt 5 || $python_minor_version -gt 6 ]]; then
echo "Python version not allowed : "$( python -c 'import platform; print(platform.python_version())')
exit 1
fi
if [[ "$VIRTUAL_ENV" = "" ]]; then
if [ ! -d "$destfull"venv ]; then
echo '## Create Virtualenv ##'
python3 -m venv "$destfull"venv
fi
source "$destfull"venv/bin/activate
fi
echo '## Install Pip package ##'
pip install wheel
if [[ "$arg" = 'prod' ]] && [[ "$TRAVIS" != true ]]; then
pip install -r requirements/prod.txt
elif [[ "$TRAVIS" = true ]]; then
pip install -r requirements/prod.txt
pip install -r requirements/test.txt
else
pip install -r requirements/dev.txt
pip install -r requirements/test.txt
fi
export DJANGO_SETTINGS_MODULE="probemanager.settings.$arg"
export PYTHONPATH=$PYTHONPATH:"$destfull"/probemanager
}
generate_keys(){
if [[ "$arg" = 'prod' ]]; then
echo '## Generate secret_key and fernet_key ##'
python "$destfull"probemanager/scripts/secrets.py -d $destfull
fi
}
set_host(){
if [[ "$arg" = 'prod' ]]; then
echo '## Set Host file ##'
if [[ "$TRAVIS" = true ]]; then
host='probemanager.test.com'
else
echo "Give the host, followed by [ENTER]:"
read host
fi
echo "[DEFAULT]" > "$destfull"conf.ini
echo "HOST = $host" >> "$destfull"conf.ini
fi
}
set_timezone(){
if [[ "$arg" = 'prod' ]]; then
echo '## Set Timezone ##'
if [[ "$TRAVIS" = true ]]; then
timezone='Europe/Paris'
else
echo "Give the timezone, followed by [ENTER], example: Europe/Paris, default: UTC:"
read timezone
fi
if [ "$timezone" == "" ]; then
$timezone='UTC'
fi
if [[ "$arg" = 'prod' ]]; then
echo "TIME_ZONE = $timezone" >> "$destfull"conf.ini
fi
fi
}
set_smtp(){
if [[ "$arg" = 'prod' ]] && [[ "$TRAVIS" != true ]]; then
echo '## Set SMTP settings ##'
python "$destfull"probemanager/scripts/setup_smtp.py -d $destfull
fi
}
set_logs(){
if [[ "$arg" = 'prod' ]]; then
echo '## Set logs ##'
echo "[LOG]" >> "$destfull"conf.ini
echo "FILE_PATH = ""$LOG_PATH""probemanager.log" >> "$destfull"conf.ini
echo "FILE_ERROR_PATH = ""$LOG_PATH""probemanager-error.log" >> "$destfull"conf.ini
sudo touch "$LOG_PATH"probemanager.log
sudo touch "$LOG_PATH"probemanager-error.log
sudo chown "$SERVER_USER":"$CURRENT_USER" "$LOG_PATH"probemanager.log
sudo chown "$SERVER_USER":"$CURRENT_USER" "$LOG_PATH"probemanager-error.log
sudo chmod 770 "$LOG_PATH"probemanager.log
sudo chmod 770 "$LOG_PATH"probemanager-error.log
fi
}
generate_version(){
echo '## Generate version ##'
python "$destfull"probemanager/manage.py runscript version --settings=probemanager.settings.$arg --script-args $(pwd) $destfull
}
create_db() {
echo '## Create DB ##'
if [ -f /etc/debian_version ]; then
sudo service postgresql restart
sleep 5
sudo su postgres -c 'dropdb --if-exists probemanager'
sudo su postgres -c 'dropuser --if-exists probemanager'
if [[ "$arg" = 'prod' ]] && [[ "$TRAVIS" != true ]]; then
password=$(python probemanager/scripts/db_password.py -d $destfull 2>&1)
sudo su postgres -c "psql -c \"CREATE USER probemanager WITH LOGIN CREATEDB ENCRYPTED PASSWORD '$password';\""
else
sudo su postgres -c "psql -c \"CREATE USER probemanager WITH LOGIN CREATEDB ENCRYPTED PASSWORD 'probemanager';\""
fi
sudo su postgres -c 'createdb -T template0 -O probemanager probemanager'
elif [[ $OSTYPE == *"darwin"* ]]; then
brew services restart postgresql
sleep 5
dropdb --if-exists probemanager
dropuser --if-exists probemanager
if [[ "$arg" = 'prod' ]]; then
password=$(python probemanager/scripts/db_password.py -d $destfull 2>&1)
psql -d postgres -c "CREATE USER probemanager WITH LOGIN CREATEDB ENCRYPTED PASSWORD '$password';"
else
psql -d postgres -c "CREATE USER probemanager WITH LOGIN CREATEDB ENCRYPTED PASSWORD 'probemanager';"
fi
createdb -T template0 -O probemanager probemanager
fi
python "$destfull"probemanager/manage.py makemigrations --settings=probemanager.settings.$arg
python "$destfull"probemanager/manage.py migrate --settings=probemanager.settings.$arg
python "$destfull"probemanager/manage.py loaddata init.json --settings=probemanager.settings.$arg
python "$destfull"probemanager/manage.py loaddata crontab.json --settings=probemanager.settings.$arg
for f in probemanager/*; do
if [[ -d $f ]]; then
if test -f "$f"/init_db.sh ; then
./"$f"/init_db.sh $arg $destfull
fi
fi
done
}
update_db(){
echo '## Update DB ##'
python "$destfull"probemanager/manage.py makemigrations --settings=probemanager.settings.$arg
python "$destfull"probemanager/manage.py migrate --settings=probemanager.settings.$arg
}
create_superuser(){
if [[ "$TRAVIS" != true ]]; then
echo '## Create Super user ##'
python "$destfull"probemanager/manage.py createsuperuser --settings=probemanager.settings.$arg
fi
}
collect_static(){
if [[ "$arg" = 'prod' ]]; then
echo '## Collect static ##'
python "$destfull"probemanager/manage.py collectstatic --noinput --settings=probemanager.settings.$arg
fi
}
check_deployement(){
if [[ "$arg" = 'prod' ]]; then
echo '## Check deployment ##'
python "$destfull"probemanager/manage.py check --deploy --settings=probemanager.settings.$arg
python "$destfull"probemanager/manage.py validate_templates --settings=probemanager.settings.$arg
fi
}
generate_doc(){
echo '## Generate doc ##'
python "$destfull"probemanager/manage.py runscript generate_doc --settings=probemanager.settings.$arg
sphinx-build -b html "$destfull"docs "$destfull"docs/_build/html
touch "$destfull"docs/_build/html/.nojekyll
}
setup_tests(){
if [[ "$arg" = 'dev' ]]; then
echo '## Setup tests ##'
python "$destfull"probemanager/manage.py runscript setup_tests --settings=probemanager.settings.$arg
fi
}
apache_conf(){
if [[ "$arg" = 'prod' ]]; then
echo '## Create Apache configuration ##'
sudo touch /etc/apache2/sites-enabled/probemanager.conf
sudo chown "$CURRENT_USER" /etc/apache2/sites-enabled/probemanager.conf
python "$destfull"probemanager/manage.py runscript apache --script-args $destfull
fi
}
update_repo(){
if [[ "$TRAVIS" != true ]]; then
echo '## Update Git repository ##'
branch=$( git branch | grep \* | cut -d ' ' -f2 )
git pull origin $branch
git submodule update --remote
fi
}
launch_celery(){
if [[ "$arg" = 'prod' ]]; then
if [ -f "$LOG_PATH"probemanager-celery.log ]; then
sudo chown "$CURRENT_USER":"$SERVER_USER" "$LOG_PATH"probemanager-celery.log
else
sudo touch "$LOG_PATH"probemanager-celery.log
sudo chown "$CURRENT_USER":"$SERVER_USER" "$LOG_PATH"probemanager-celery.log
fi
if [ ! -f "$destfull"probemanager/celery.pid ]; then
echo '## Start Celery ##'
(cd "$destfull"probemanager/ && celery -A probemanager worker -D --pidfile celery.pid -B -l info -f "$LOG_PATH"probemanager-celery.log --scheduler django_celery_beat.schedulers:DatabaseScheduler)
else
echo '## Restart Celery ##'
sudo kill $( cat "$destfull"probemanager/celery.pid)
sudo pkill -f celery
if [ -f "$destfull"probemanager/celery.pid ]; then
sudo rm "$destfull"probemanager/celery.pid
fi
sleep 8
(cd "$destfull"probemanager/ && celery -A probemanager worker -D --pidfile celery.pid -B -l info -f "$LOG_PATH"probemanager-celery.log --scheduler django_celery_beat.schedulers:DatabaseScheduler)
fi
fi
}
post_install() {
if [[ "$arg" = 'prod' ]]; then
echo '## Post Install ##'
sudo usermod -a -G "$SERVER_USER" "$CURRENT_USER"
sudo chown -R "$SERVER_USER":"$CURRENT_USER" "$destfull"
sudo chmod -R 774 "$destfull"
if [ -f /etc/apache2/sites-enabled/probemanager.conf ]; then
sudo chown "$SERVER_USER":"$SERVER_USER" /etc/apache2/sites-enabled/probemanager.conf
fi
sudo chmod 440 "$destfull"fernet_key.txt
sudo chmod 440 "$destfull"secret_key.txt
sudo chmod 440 "$destfull"conf.ini
if [ -f "$destfull"password_db.txt ]; then
sudo chmod 440 "$destfull"password_db.txt
fi
if [ -f "$destfull"password_email.txt ]; then
sudo chmod 440 "$destfull"password_email.txt
fi
if [ -f "$LOG_PATH"probemanager.log ]; then
sudo chown "$SERVER_USER":"$CURRENT_USER" "$LOG_PATH"probemanager.log
sudo chmod 774 "$LOG_PATH"probemanager.log
else
sudo touch "$LOG_PATH"probemanager.log
sudo chown "$SERVER_USER":"$CURRENT_USER" "$LOG_PATH"probemanager.log
sudo chmod 774 "$LOG_PATH"probemanager.log
fi
if [ -f "$LOG_PATH"probemanager-error.log ]; then
sudo chown "$SERVER_USER":"$CURRENT_USER" "$LOG_PATH"probemanager-error.log
sudo chmod 774 "$LOG_PATH"probemanager-error.log
else
sudo touch "$LOG_PATH"probemanager-error.log
sudo chown "$SERVER_USER":"$CURRENT_USER" "$LOG_PATH"probemanager-error.log
sudo chmod 774 "$LOG_PATH"probemanager-error.log
fi
sudo a2dissite 000-default.conf
sudo a2dismod deflate -f
sudo service apache2 restart
fi
}
first=false
if [ ! -d "$destfull" ]; then
sudo mkdir $destfull
if [[ "$arg" = 'prod' ]]; then
sudo chown "$SERVER_USER":"$CURRENT_USER" $destfull
else
sudo chown "$CURRENT_USER":"$CURRENT_USER" $destfull
fi
sudo chmod 774 "$destfull"
first=true
elif [ ! -f "$destfull"probemanager/version.txt ]; then
first=true
fi
export DJANGO_SETTINGS_MODULE="probemanager.settings.$arg"
export PYTHONPATH=$PYTHONPATH:"$destfull"/probemanager
if [ "$first" = true ]; then
echo 'First install'
echo 'Install in dir : '$destfull
update_repo
clean
copy_files
install_os_dependencies
install_virtualenv
set_host
set_timezone
set_logs
set_git
generate_keys
select_apps
set_smtp
install_modules
create_db
generate_version
create_superuser
collect_static
check_deployement
generate_doc
setup_tests
apache_conf
launch_celery
post_install
else
echo 'Update install'
update_repo
copy_files
install_virtualenv
generate_version
update_db
collect_static
check_deployement
generate_doc
launch_celery
post_install
fi
exit