forked from GeotrekCE/Geotrek-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·567 lines (470 loc) · 16.7 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
562
563
564
565
566
567
#!/usr/bin/env bash
if [ "$(id -u)" == "0" ]; then
echo -e "\e[91m\e[1mThis script should NOT be run as root\e[0m" >&2
fi
# Go to folder of install.sh
cd "$(dirname "$0")"
#------------------------------------------------------------------------------
# Redirect whole output to log file
rm -f install.log
touch install.log
chmod 600 install.log
exec 3>&1 4>&2
exec 1> install.log 2>&1
#------------------------------------------------------------------------------
STABLE_VERSION=${STABLE_VERSION:-2.11.3}
dev=false
tests=false
prod=false
standalone=true
interactive=true
settingsfile=etc/settings.ini
usage () {
exec 2>&4
cat >&2 <<- _EOF_
Usage: Install project [OPTIONS]
-d, --dev minimum dependencies for development
-t, --tests install testing environment
-p, --prod deploy a production instance
--noinput do not prompt user
-s, --standalone deploy a single-server production instance (Default)
-h, --help show this help
_EOF_
exec 2>&1
return
}
while [[ -n $1 ]]; do
case $1 in
-d | --dev ) dev=true
standalone=false
;;
-t | --tests ) tests=true
standalone=false
;;
-p | --prod ) prod=true
standalone=false
;;
-s | --standalone ) ;;
--noinput ) interactive=false
;;
-h | --help ) usage
exit
;;
*) usage
exit
;;
esac
shift
done
#------------------------------------------------------------------------------
#
# Helpers
#
#------------------------------------------------------------------------------
function echo_step () {
set +x
exec 2>&4
echo -e "\n\e[92m\e[1m$1\e[0m" >&2
exec 2>&1
set -x
}
function echo_warn () {
set +x
exec 2>&4
echo -e "\e[93m\e[1m$1\e[0m" >&2
exec 2>&1
set -x
}
function echo_error () {
set +x
exec 2>&4
echo -e "\e[91m\e[1m$1\e[0m" >&2
exec 2>&1
set -x
}
function echo_progress () {
set +x
exec 2>&4
echo -e ".\c" >&2
exec 2>&1
set -x
}
function exit_error () {
code=$1
shift
echo_error "$@"
echo "(More details in install.log)" >&2
exit $code
}
function echo_header () {
set +x
exec 2>&4
cat docs/logo.ans >&2
exec 2>&1
set -x
version=$(cat VERSION)
echo_step "... install $version" >&2
if [ ! -z $1 ] ; then
echo_warn "... upgrade $1" >&2
fi
echo_step "(details in install.log)" >&2
echo_step >&2
}
function existing_version {
existing=`cat /etc/nginx/sites-available/* | grep gunicorn-geotrek.sock | sed "s/^.*unix://" | sed "s/var\\/run.*$//"`
version=`cat $existing/VERSION`
echo $version
}
function database_exists () {
# /!\ Will return false if psql can't list database. Edit your pg_hba.conf
# as appropriate.
if [ -z $1 ]
then
# Argument is null
return 0
else
# Grep db name in the list of database
sudo -n -u postgres -s -- psql -tAl | grep -q "^$1|"
return $?
fi
}
function user_does_not_exists () {
# /!\ Will return false if psql can't list database. Edit your pg_hba.conf
# as appropriate.
if [ -z $1 ]
then
# Argument is null
return 0
else
exists=`sudo -n -u postgres -s -- psql -tAc "SELECT 1 FROM pg_roles WHERE rolname='$1'" | wc -l`
return $exists
fi
}
function ini_value () {
echo $(sed -n "s/^\s*$2 *= *\([^ ]*.*\)/\1/p" < $1)
}
function check_postgres_connection {
echo_step "Check postgres connection settings..."
# Check that database connection is correct
dbname=$(ini_value $settingsfile dbname)
dbhost=$(ini_value $settingsfile dbhost)
dbport=$(ini_value $settingsfile dbport)
dbuser=$(ini_value $settingsfile dbuser)
dbpassword=$(ini_value $settingsfile dbpassword)
export PGPASSWORD=$dbpassword
psql $dbname -h $dbhost -p $dbport -U $dbuser -c "SELECT PostGIS_full_version();"
result=$?
export PGPASSWORD=
if [ ! $result -eq 0 ]
then
echo_error "Failed to connect to database with settings provided in '$settingsfile'."
exit_error 4 "Check your postgres configuration (``pg_hba.conf``) : it should allow md5 identification for user '${dbuser}' on database '${dbname}'"
fi
}
function minimum_system_dependencies {
sudo apt-get update -qq
echo_progress
sudo apt-get install -y -qq python unzip wget python-software-properties
echo_progress
if [ $precise -eq 1 ]; then
sudo apt-add-repository -y ppa:git-core/ppa
sudo apt-add-repository -y ppa:ubuntugis/ppa
sudo apt-get update -qq
echo_progress
fi
sudo apt-get install -y -qq git gettext python-virtualenv build-essential python-dev
echo_progress
}
function geotrek_system_dependencies {
sudo apt-get install -y -q --no-upgrade libjson0 gdal-bin libgdal-dev
if [ $xenial -eq 1 ]; then
sudo apt-get install libgeos-c1v5 libproj9
else
sudo apt-get install libgeos-c1 libproj0
fi
echo_progress
# PostgreSQL client and headers
sudo apt-get install -y -q --no-upgrade postgresql-client-$psql_version postgresql-server-dev-$psql_version
echo_progress
sudo apt-get install -y -qq libxml2-dev libxslt-dev # pygal lxml
echo_progress
# Necessary for MapEntity Weasyprint
sudo apt-get install -y -qq python-dev python-lxml libcairo2 libpango1.0-0 libgdk-pixbuf2.0-dev libffi-dev shared-mime-info
echo_progress
# Redis for async imports and tasks management
sudo apt-get install -y -qq redis-server
echo_progress
if $prod || $standalone ; then
sudo apt-get install -y -qq ntp fail2ban
echo_progress
sudo apt-get install -y -qq nginx memcached supervisor
echo_progress
fi
}
function convertit_system_dependencies {
if $standalone ; then
echo_step "Conversion server dependencies..."
sudo apt-get install -y -qq libreoffice unoconv inkscape
echo_progress
fi
}
function screamshotter_system_dependencies {
if $dev || $tests || $standalone ; then
# Note: because tests require casper and phantomjs
echo_step "Capture server dependencies..."
arch=`uname -m`
libpath=`pwd`/lib
binpath=`pwd`/bin
mkdir -p $libpath
mkdir -p $binpath
wget --quiet https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-1.9.7-linux-$arch.tar.bz2 -O phantomjs.tar.bz2
rm -rf $libpath/*phantomjs*/
tar -jxvf phantomjs.tar.bz2 -C $libpath/ > /dev/null
rm phantomjs.tar.bz2
ln -sf $libpath/*phantomjs*/bin/phantomjs $binpath/phantomjs
echo_progress
wget --quiet https://github.com/n1k0/casperjs/archive/1.1-beta3.zip -O casperjs.zip
rm -rf $libpath/*casperjs*/
unzip -o casperjs.zip -d $libpath/ > /dev/null
rm casperjs.zip
ln -sf $libpath/*casperjs*/bin/casperjs $binpath/casperjs
echo_progress
if ! $dev ; then
# Install system-wide binaries
sudo ln -sf $binpath/phantomjs /usr/local/bin/phantomjs
sudo ln -sf $binpath/casperjs /usr/local/bin/casperjs
fi
fi
}
function install_postgres_local {
echo_step "Installing postgresql server locally..."
sudo apt-get install -y -q postgresql-$psql_version postgresql-$psql_version-postgis-$pgis_version
sudo /etc/init.d/postgresql restart
echo_progress
dbname=$(ini_value $settingsfile dbname)
dbuser=$(ini_value $settingsfile dbuser)
dbpassword=$(ini_value $settingsfile dbpassword)
# Activate PostGIS in database
if ! database_exists ${dbname}
then
echo_step "Create database ${dbname}..."
sudo -n -u postgres -s -- psql -c "CREATE DATABASE ${dbname} ENCODING 'UTF8' TEMPLATE template0;"
sudo -n -u postgres -s -- psql -d ${dbname} -c "CREATE EXTENSION postgis;"
fi
# Create user if missing
if user_does_not_exists ${dbuser}
then
echo_step "Create user ${dbuser} and configure database access rights..."
sudo -n -u postgres -s -- psql -c "CREATE USER ${dbuser} WITH PASSWORD '${dbpassword}';"
sudo -n -u postgres -s -- psql -c "GRANT ALL PRIVILEGES ON DATABASE ${dbname} TO ${dbuser};"
sudo -n -u postgres -s -- psql -d ${dbname} -c "GRANT ALL ON spatial_ref_sys, geometry_columns, raster_columns TO ${dbuser};"
echo_progress
# Open local and host connection for this user as md5
sudo sed -i "/DISABLE/a \
# Automatically added by Geotrek installation :\
local ${dbname} ${dbuser} md5" /etc/postgresql/*/main/pg_hba.conf
cat << _EOF_ | sudo tee -a /etc/postgresql/*/main/pg_hba.conf
# Automatically added by Geotrek installation :
local ${dbname} ${dbuser} md5
host ${dbname} ${dbuser} 0.0.0.0/0 md5
_EOF_
sudo /etc/init.d/postgresql restart
echo_progress
fi
if $dev || $tests ; then
echo_step "Give all priviliges to user ${dbuser}..."
# In development give full rights to db user
sudo -n -u postgres -s -- psql -c "ALTER ROLE ${dbuser} SUPERUSER CREATEDB CREATEROLE INHERIT LOGIN;"
# A postgis template is required for django tests
if ! database_exists template_postgis
then
echo_step "Create template_postgis..."
sudo -n -u postgres -s -- createdb template_postgis
sudo -n -u postgres -s -- psql -d template_postgis -c "CREATE EXTENSION postgis"
sudo -n -u postgres -s -- psql -d template_postgis -c "VACUUM FREEZE"
sudo -n -u postgres -s -- psql -c "UPDATE pg_database SET datistemplate = TRUE WHERE datname = 'template_postgis'"
sudo -n -u postgres -s -- psql -c "UPDATE pg_database SET datallowconn = FALSE WHERE datname = 'template_postgis'"
# Listen to all network interfaces (useful for VM etc.)
listen="'*'"
sudo sed -i "s/^#listen_addresses.*$/listen_addresses = $listen/" /etc/postgresql/*/main/postgresql.conf
sudo sed -i "s/^client_min_messages.*$/client_min_messages = log/" /etc/postgresql/*/main/postgresql.conf
sudo /etc/init.d/postgresql restart
fi
fi
}
function backup_existing_database {
set +x
if $interactive ; then
exec 2>&4
read -p "Backup existing database ? [yN] " -n 1 -r
echo # new line
exec 2>&1
else
REPLY=N;
fi
set -x
if [[ $REPLY =~ ^[Yy]$ ]]
then
dbname=$(ini_value $settingsfile dbname)
echo_step "Backup existing database $name..."
sudo -n -u postgres -s -- pg_dump --format=custom $dbname > `date +%Y%m%d%H%M`-$dbname.backup
fi
}
#------------------------------------------------------------------------------
#
# Install scenario
#
#------------------------------------------------------------------------------
function geotrek_setup {
set -x
existing=$(existing_version)
freshinstall=true
if [ ! -z $existing ] ; then
freshinstall=false
if [ $existing \< "0.22" ]; then
echo_warn "Geotrek $existing was detected."
echo_error "Geotrek 0.22+ is required."
exit 7
fi
fi
echo_header $existing
echo_step "Install system minimum components..."
minimum_system_dependencies
if [ ! -f Makefile ]; then
echo_step "Downloading Geotrek latest stable version..."
echo "wget --quiet https://github.com/makinacorpus/Geotrek/archive/$STABLE_VERSION.zip"
wget --quiet https://github.com/makinacorpus/Geotrek/archive/$STABLE_VERSION.zip
unzip $STABLE_VERSION.zip -d /tmp > /dev/null
rm -f /tmp/Geotrek-$STABLE_VERSION/install.sh
shopt -s dotglob nullglob
mv /tmp/Geotrek-$STABLE_VERSION/* .
fi
if ! $freshinstall ; then
backup_existing_database
# Python should be fresh
make clean
fi
# Python bootstrap
make install
echo_progress
if $freshinstall && $interactive && ($prod || $standalone) ; then
# Prompt user to edit/review settings
exec 1>&3
editor $settingsfile
exec 1> install.log 2>&1
fi
echo_step "Configure Unicode and French locales..."
#sudo apt-get update > /dev/null
echo_progress
sudo apt-get install -y -qq language-pack-en-base language-pack-fr-base
sudo locale-gen fr_FR.UTF-8
echo_progress
echo_step "Install Geotrek system dependencies..."
geotrek_system_dependencies
convertit_system_dependencies
screamshotter_system_dependencies
# If database is local, install it !
dbhost=$(ini_value $settingsfile dbhost)
if [ "${dbhost}" == "localhost" ] ; then
install_postgres_local
fi
check_postgres_connection
echo_step "Install Geotrek python dependencies..."
if $dev ; then
make env_dev
elif $tests ; then
make env_test
elif $prod ; then
make env_prod
elif $standalone ; then
make env_standalone
fi
success=$?
if [ $success -ne 0 ]; then
exit_error 3 "Could not setup python environment !"
fi
if $dev ; then
echo_step "Initializing data..."
make update
echo_progress
fi
if $tests ; then
# XXX: Why Django tests require the main database :( ?
bin/django syncdb --noinput
bin/django collectstatic --clear --noinput --verbosity=0
fi
if $prod || $standalone ; then
echo_step "Generate services configuration files..."
# restart supervisor in case of xenial before 'make deploy'
/etc/init.d/supervisor force-stop && /etc/init.d/supervisor stop && /etc/init.d/supervisor start
make deploy
echo_progress
# If buildout was successful, deploy really !
if [ -f /etc/supervisor/supervisord.conf ]; then
sudo rm /etc/nginx/sites-enabled/default
sudo cp etc/nginx.conf /etc/nginx/sites-available/geotrek
sudo ln -sf /etc/nginx/sites-available/geotrek /etc/nginx/sites-enabled/geotrek
# Nginx does not create log files !
# touch var/log/nginx-access.log
# touch var/log/nginx-error.log
# if 15.04 or higher
if [ $vivid -eq 1 -o $xenial -eq 1 ]; then
sudo systemctl restart nginx
else
sudo /etc/init.d/nginx restart
fi
if [ -f /etc/init/supervisor.conf ]; then
# Previous Geotrek naming
sudo stop supervisor
sudo rm -f /etc/init/supervisor.conf
fi
sudo cp etc/logrotate.conf /etc/logrotate.d/geotrek
echo_step "Enable Geotrek services and start..."
if [ -f /etc/init/geotrek.conf ]; then
# Previous Geotrek naming
sudo stop geotrek
sudo rm -f /etc/init/geotrek.conf
fi
sudo chgrp www-data -R ./var/static
sudo chmod g+r -R ./var/static
sudo chgrp www-data -R ./var/media/upload
sudo cp etc/supervisor-geotrek.conf /etc/supervisor/conf.d/
sudo cp etc/supervisor-geotrek-api.conf /etc/supervisor/conf.d/
sudo cp etc/supervisor-geotrek-celery.conf /etc/supervisor/conf.d/
sudo cp etc/supervisor-tilecache.conf /etc/supervisor/conf.d/
if $standalone ; then
sudo cp etc/supervisor-convertit.conf /etc/supervisor/conf.d/
sudo cp etc/supervisor-screamshotter.conf /etc/supervisor/conf.d/
fi
sudo supervisorctl reread
sudo supervisorctl reload
echo_progress
else
exit_error 6 "Geotrek package could not be installed."
fi
fi
set +x
echo_step "Done."
}
precise=$(grep "Ubuntu 12.04" /etc/issue | wc -l)
trusty=$(grep "Ubuntu 14.04" /etc/issue | wc -l)
vivid=$(grep "Ubuntu 15.04" /etc/issue | wc -l)
xenial=$(grep "Ubuntu 16.04" /etc/issue | wc -l)
if [ $precise -eq 1 ]; then
psql_version=9.1
pgis_version=2.0
elif [ $trusty -eq 1 ]; then
psql_version=9.3
pgis_version=2.1
elif [ $vivid -eq 1 ]; then
psql_version=9.4
pgis_version=2.1
elif [ $xenial -eq 1 ]; then
psql_version=9.5
pgis_version=2.2
fi
if [ $precise -eq 1 -o $trusty -eq 1 -o $vivid -eq 1 -o $xenial -eq 1 ] ; then
geotrek_setup
else
exit_error 5 "Unsupported operating system. Aborted."
fi