-
Notifications
You must be signed in to change notification settings - Fork 0
/
install-publik.bash
executable file
·614 lines (535 loc) · 16.9 KB
/
install-publik.bash
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
#!/bin/bash
# _ _ _ _ _ _ _ _
# (_)_ __ ___| |_ __ _| | | _ __ _ _| |__ | (_) | __
# | | '_ \/ __| __/ _` | | |_____| '_ \| | | | '_ \| | | |/ /
# | | | | \__ \ || (_| | | |_____| |_) | |_| | |_) | | | <
# |_|_| |_|___/\__\__,_|_|_| | .__/ \__,_|_.__/|_|_|_|\_\
# |_|
# Global variables definition
APPLICATION="Publik (Entrouvert) installation"
STDOUT="stdout.log"
STDERR="stderr.log"
SUDO_PASSWORD=""
PUBLIK_DIRECTORY="publik-env"
SUP_USER="admin"
SUP_EMAIL="admin@example.com"
SUP_PASSWORD="admin"
declare -a DIALOG_VALUES
declare -a PROXY_SETTINGS
# Check requirements for this script, exits if any requirement is not met.
function check_requirements() {
# Checks the presence of the sudo utility
which "sudo" > /dev/null
if [ $? -ne 0 ]
then
echo "This script requires the sudo command."
exit 10
fi
# Checks the presence of the dialog utility
which "dialog" > /dev/null
if [ $? -ne 0 ]
then
echo "This script requires the dialog command."
exit 10
fi
# Ensures we are on Debian or Ubuntu
which "apt-get" > /dev/null
if [ $? -ne 0 ]
then
echo "This script works only with Debian or Ubuntu."
exit 10
fi
}
# Run a dialog command. If the command has a return code not set to 0, it
# directly exits the current script.
#
# Global variables:
# - APPLICATION: the application title, will be written in background of screen
# - DIALOG_VALUES: this array will contain the value the user has input
function run_dialog() {
local temporary_file=$(mktemp)
local rc=0
dialog --backtitle "$APPLICATION" "$@" 2> "$temporary_file"
rc=$?
readarray -t DIALOG_VALUES < "$temporary_file"
rm -- "$temporary_file"
if [ $rc -ne 0 ]
then
dialog --backtitle "$APPLICATION" --msgbox "Script aborted" 5 50
exit 1
fi
}
# Run a command with the current user.
# Arguments:
# - 1: title
# - 2: progression function which converts command output to percentage
# - 3-*: command to execute
#
# Global variables:
# - STDERR: the file receiving the standard error stream
function run() {
local title="$1"
local progression="$2"
shift 2
LANG= "$@" 2>> $STDERR | $progression | run_dialog --gauge "$title" 0 80 0
if [ "${PIPESTATUS[1]}" -ne 0 ]
then
run_dialog --tailbox "$STDERR" 25 80
exit 1
fi
}
# Run a command with the root user.
# Arguments:
# - 1: title
# - 2: progression function which converts command output to percentage
# - 3-*: command to execute
#
# Global variables:
# - SUDO_PASSWORD: the sudo password
# - STDERR: the file receiving the standard error stream
function run_sudo() {
local title="$1"
local progression="$2"
shift 2
printf "%s\n" "$SUDO_PASSWORD" \
| LANG= sudo -S "$@" 2>> $STDERR \
| $progression \
| run_dialog --gauge "$title" 0 80 0
if [ "${PIPESTATUS[1]}" -ne 0 ]
then
run_dialog --tailbox "$STDERR" 25 80
exit 1
fi
}
# Ask the user for the sudo password.
#
# Global variables:
# - SUDO_PASSWORD: this variable is set with the password
function ask_password() {
run_dialog --insecure --passwordbox "Type in the SUDO password" 8 50
SUDO_PASSWORD="${DIALOG_VALUES[0]}"
unset DIALOG_VALUES
}
# Ask the user for the Publik directory.
#
# Global variables:
# - PUBLIK_DIRECTORY: this variable is set with the Publik directory
function ask_publik_directory() {
run_dialog --inputbox "Directory to create" 8 50 "$PUBLIK_DIRECTORY"
PUBLIK_DIRECTORY="${DIALOG_VALUES[0]}"
unset DIALOG_VALUES
if [ -d "$PUBLIK_DIRECTORY" ]
then
run_dialog --msgbox "$PUBLIK_DIRECTORY already exists!" 5 50
exit 3
fi
}
# Ask the user for the super user settings.
#
# Global variables:
# - DIALOG_VALUES: contains the 3 default values (user, email, password).
# On return, it will contain the values set by the user.
function ask_superuser() {
run_dialog \
--form "Combo Superuser settings" 0 0 4 \
"User" 1 1 "${DIALOG_VALUES[0]}" 1 10 20 0 \
"Email" 2 1 "${DIALOG_VALUES[1]}" 2 10 50 0 \
"Password" 3 1 "${DIALOG_VALUES[2]}" 3 10 20 0
}
# Ask the user for the proxy settings.
#
# Global variables:
# - DIALOG_VALUES: contains the 4 default values (user, password, URL, port).
# On return, it will contain the values set by the user.
function ask_proxy() {
run_dialog \
--form "Proxy settings" 0 0 4 \
"User" 1 1 "${DIALOG_VALUES[0]}" 1 10 20 0 \
"Password" 2 1 "${DIALOG_VALUES[1]}" 2 10 20 0 \
"URL" 3 1 "${DIALOG_VALUES[2]}" 3 10 50 0 \
"Port" 4 1 "${DIALOG_VALUES[3]}" 4 10 6 0
}
# A progression filter for apt-get.
# This functions is meant to be used by the run and run_sudo functions.
function apt_progression() {
sed --quiet --unbuffered '
/Reading package lists/a15
/Reading state information/a30
/upgraded/a45
/Reading database/a60
/Preparing to unpack/a75
/Setting up/a90
'
echo 100
}
# A progression filter for virtualenv.
# This functions is meant to be used by the run and run_sudo functions.
function virtualenv_progression() {
sed --quiet --unbuffered '
/Running virtualenv with/a20
/New python executable in/a40
/Also creating executable in/a60
/Installing setuptools/a80
'
echo 100
}
# A progression filter for git.
# This functions is meant to be used by the run and run_sudo functions.
function git_progression() {
sed --quiet --unbuffered '
/Cloning into/a20
/Checking connectivity/a80
'
echo 100
}
# A progression filter for pip install.
# This functions is meant to be used by the run and run_sudo functions.
function get_pip_progression() {
sed --quiet --unbuffered '
/Resolving/a10
/Connecting to/a20
/HTTP request sent/a30
/Length:/a40
/Saving to:/a50
/written to stdout/a60
/Collecting/a70
/Installing collected packages/a80
/Successfully installed/a90
'
echo 100
}
# A progression filter for gem install sass.
# This functions is meant to be used by the run and run_sudo functions.
function gem_sass_progression() {
sed --quiet --unbuffered '
/Building native extensions/a8
/Successfully installed ffi/a16
/Successfully installed rb-inotify/a24
/Successfully installed rb-fsevent/a32
/Successfully installed sass-listen/a40
/Successfully installed sass-[^l]/a48
/Installing ri documentation for ffi/a56
/Installing ri documentation for rb-fsevent/a64
/Installing ri documentation for rb-inotify/a72
/Installing ri documentation for sass-[^l]/a80
/Installing ri documentation for sass-listen/a88
/gems installed/a96
'
echo 100
}
# A progression filter for ./manage.py createsuperuser.
# This functions is meant to be used by the run and run_sudo functions.
function manage_createsuperuser_progression() {
sed --quiet --unbuffered '
/^Username/a20
/^Email address/a40
/^Password:/a60
/^Password (again):/a80
'
echo 100
}
# A progression filter for pip install -e .
# This functions is meant to be used by the run and run_sudo functions.
function pip_requirements_progression() {
local requirements
local counter=0
local req_count=0
local found=""
local line=""
local re_start='^(Requirement already satisfied:|Collecting) '
local re_end='[ ,<>=]'
readarray -t requirements < <(get_python_requirements "setup.py")
req_count="${#requirements[@]}"
while read line
do
found=$(
for requirement in "${requirements[@]}"
do
if [[ $line =~ $re_start$requirement$re_end ]]
then
printf "x"
break
fi
done
)
if [ "$found" = "x" ]
then
counter=$((counter + 1))
printf "%d\n" $((100 * counter / (req_count + 1) ))
fi
done
echo 100
}
# A progression filter for ./manage.py collectstatic.
# This functions is meant to be used by the run and run_sudo functions.
function manage_collectstatic_progression() {
local file_count=$(find ../lib -type f -regex '.*/x?static/.*' | wc --lines)
local counter=0
local line=""
while read line
do
if [[ $line =~ ^Copying ]]
then
counter=$((counter + 1))
printf "%d\n" $((100 * counter / (file_count + 1) ))
fi
done
echo 100
}
# A progression filter for ./manage.py migrate.
# This functions is meant to be used by the run and run_sudo functions.
function manage_migrate_progression() {
local re_start='^ *Applying '
local re_end='\.\.\. OK$'
local counter=0
local found=""
local step_count=0
local step=""
local line=""
readarray -t steps < <(manage_migrate_dependencies | sort | uniq)
step_count="${#steps[@]}"
while read line
do
found=$(
for step in "${steps[@]}"
do
if [[ $line =~ $re_start$step$re_end ]]
then
printf "x"
break
fi
done
)
if [ "$found" = "x" ]
then
counter=$((counter + 1))
printf "%d\n" $((100 * counter / (step_count + 1) ))
fi
done
echo 100
}
# Find all migration steps that ./manage.py migrate will go through
function manage_migrate_dependencies() {
local filter_files='.*/migrations/[0-9][0-9][0-9][0-9]_.*'
local path_to_id="s/^.*\/\([^/]*\)\/migrations\/\([^/]*\)\.py$/\1.\2/gp"
local dep_to_id="
/^ *dependencies *= *\\[/,/^ *\\]$/{
s/^ *('\([^']*\)'.*'\([^']*\)').*$/\1.\2/gp
}
"
# Retrieve IDs from dependencies inside migration files
find . -name "*.py" -regex "$filter_files" \
| xargs cat -- \
| sed --quiet "$dep_to_id"
# Retrieve IDs from migration files of the current application
find . -name "*.py" -regex "$filter_files" | sed --quiet "$path_to_id"
# Retrieve IDs from frameworks and other base packages
find ../lib -name "*.py" -regex "$filter_files" | sed --quiet "$path_to_id"
}
# Retrieve requirements from a setup.py file
#
# It outputs the list of requirements in reverse order to help differentiate,
# for example, django from django-ckeditor.
#
# Arguments:
# - 1: the path to a setup.py file
function get_python_requirements() {
local setuppy="$1"
cat "$setuppy" \
| sed --quiet '/^ *install_requires/,/^ *\],/p' \
| grep --perl-regexp --only-matching "(?<=')[^ ,'<>=]+" \
| sort --reverse
}
# Retrieve proxy settings from the http_proxy environment variable.
#
# This function only considers the following cases:
# - user + password + URL
# - user + password + URL + port
# - URL
# - URL + port
#
# It outputs 4 lines in the following order:
# - user name
# - user password
# - URL of proxy server
# - port of the proxy server
function get_proxy_settings() {
local re='^http://(([^@:/]+):([^@:/]+)@)?([^@:/]+)(:([0-9]+))?'
if [[ $http_proxy =~ $re ]]
then
printf "%s\n%s\n%s\n%s\n" \
"${BASH_REMATCH[2]}" \
"${BASH_REMATCH[3]}" \
"${BASH_REMATCH[4]}" \
"${BASH_REMATCH[6]}"
fi
}
# Set proxy settings.
#
# This function sets and exports http_proxy, https_proxy and all_proxy
# environment variables.
#
# Arguments:
# - 1: user name (optional)
# - 2: password (optional if user name is empty)
# - 3: url
# - 4: port (optional)
function set_proxy_settings() {
local user="$1"
local password="$2"
local url="$3"
local port="$4"
local proxy_url=""
if [ "$user" ]
then
proxy_url="$user:$password@"
fi
if [ "$port" ]
then
proxy_url="$proxy_url$url:$port/"
else
proxy_url="$proxy_url$url/"
fi
export http_proxy="http://$proxy_url"
export https_proxy="https://$proxy_url"
export all_proxy="https://$proxy_url"
}
# Show introduction with a yes/no dialog allowing the user to abort the script.
function show_introduction() {
run_dialog --yesno \
"This script will install Publik (Entr'ouvert).
The following packages will be installed, if needed, in your system:
- Git
- Python VirtualEnv
- Ruby + Ruby dev
- Sass
Do you wish to continue?" \
0 0
}
# Since this command contains a pipe, it can not be used directly, by the
# run or run_sudo function, that’s why it is written in a function.
function install_new_pip() {
wget -O - https://bootstrap.pypa.io/get-pip.py | python
}
# Create the Combo super user
#
# Global variables:
# - SUP_USER: the super user name
# - SUP_EMAIL: the super user email
# - SUP_PASSWORD: the super user password
function create_superuser() {
local create_superuser="./manage.py createsuperuser"
for input in "$SUP_USER" "$SUP_EMAIL" "$SUP_PASSWORD" "$SUP_PASSWORD"
do
sleep 1
printf -- "%s\n" "$input"
done | script --quiet --return --command "$create_superuser" /dev/null
}
# __ __ _
# | \/ | __ _(_)_ __ _ __ _ __ ___ __ _ _ __ __ _ _ __ ___
# | |\/| |/ _` | | '_ \ | '_ \| '__/ _ \ / _` | '__/ _` | '_ ` _ \
# | | | | (_| | | | | | | |_) | | | (_) | (_| | | | (_| | | | | | |
# |_| |_|\__,_|_|_| |_| | .__/|_| \___/ \__, |_| \__,_|_| |_| |_|
# |_| |___/
check_requirements
show_introduction
ask_password
# Ask for superuser settings
DIALOG_VALUES=("$SUP_USER" "$SUP_EMAIL" "$SUP_PASSWORD")
ask_superuser
SUP_USER="${DIALOG_VALUES[0]}"
SUP_EMAIL="${DIALOG_VALUES[1]}"
SUP_PASSWORD="${DIALOG_VALUES[2]}"
# Ask for proxy settings if the http_proxy variable has been set
if [ "$http_proxy" ]
then
readarray -t DIALOG_VALUES < <(get_proxy_settings)
ask_proxy
set_proxy_settings \
"${DIALOG_VALUES[0]}" \
"${DIALOG_VALUES[1]}" \
"${DIALOG_VALUES[2]}" \
"${DIALOG_VALUES[3]}"
fi
ask_publik_directory
run_sudo "Installing Git" \
apt_progression \
apt-get -y install git
run_sudo "Installing Python VirtualEnv" \
apt_progression \
apt-get -y install python-virtualenv
run_sudo "Installing Python C header files (needed for Gadjo)" \
apt_progression \
apt-get -y install python-dev
run_sudo "Installing Ruby (needed for Sass)" \
apt_progression \
apt-get -y install ruby ruby-dev
run_sudo "Installing Sass" \
gem_sass_progression \
gem install sass
run "Creating virtual environment in $PUBLIK_DIRECTORY" \
virtualenv_progression \
virtualenv "$PUBLIK_DIRECTORY"
# We are now running in a Python virtual environment!
cd "$PUBLIK_DIRECTORY"
source bin/activate
# Debian/Ubuntu has a very old version of pip, we need a more recent version!
run "Retrieving a current version of pip" \
get_pip_progression \
install_new_pip
run "Cloning Combo repository in $PUBLIK_DIRECTORY/combo" \
git_progression \
git clone http://repos.entrouvert.org/combo.git
# Adding STATIC_ROOT settings for ./manage.py collectstatic
printf "\nSTATIC_ROOT = '%s'\n" \
"$(readlink -e "../$PUBLIK_DIRECTORY")/static" \
>> combo/combo/settings.py
cd combo
run "Installing Combo requirements" \
pip_requirements_progression \
pip install -e .
cd ..
cd combo
run "Initializing the Combo database (manage.py migrate)" \
manage_migrate_progression \
./manage.py migrate
cd ..
cd combo
run "Creating Combo superuser (manage.py createsuperuser)" \
manage_createsuperuser_progression \
create_superuser
cd ..
cd combo
run "Collecting Combo static files (manage.py collectstatic)" \
manage_collectstatic_progression \
./manage.py collectstatic
cd ..
run "Cloning WCS repository in $PUBLIK_DIRECTORY/wcs" \
git_progression \
git clone http://repos.entrouvert.org/wcs.git
# Adding APP_DIR and DATA_DIR settings to Defaults.py
# TODO: rewrite with a search and replace
printf "\nAPP_DIR = '%s'\nDATA_DIR = '%s'\n" \
"$(readlink -e "../$PUBLIK_DIRECTORY")/wcs/wcs" \
"$(readlink -e "../$PUBLIK_DIRECTORY")/share/wcs" \
>> wcs/wcs/Defaults.py
# Adding STATIC_ROOT settings for ./manage.py collectstatic
# TODO: rewrite with a search and replace
printf "\nSTATIC_ROOT = '%s'\nADMINS = (('%s', '%s'))\nMANAGERS = ADMINS\n" \
"$(readlink -e "../$PUBLIK_DIRECTORY")/static" \
"$SUP_USER" \
"$SUP_EMAIL" \
>> wcs/wcs/settings.py
# Force Quixote version since latest version only supports Python 3
run "Installing Quixote 2.9.1" \
pip_requirements_progression \
pip install quixote==2.9.1
# Install vobject
run "Installing vobject" \
pip_requirements_progression \
pip install vobject
cd wcs
run "Installing WCS requirements" \
pip_requirements_progression \
pip install -e .
cd ..