File: slm.postinst

package info (click to toggle)
slm 2.12-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,520 kB
  • sloc: python: 15,419; javascript: 5,061; makefile: 184; sh: 182; xml: 57
file content (126 lines) | stat: -rw-r--r-- 4,078 bytes parent folder | download | duplicates (2)
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
#!/bin/sh
# postinst script for slm.
#
# See: dh_installdeb(1).

set -e


if [ -e /usr/share/debconf/confmodule ]; then
    . /usr/share/debconf/confmodule
    db_get slm/debug || true
    DEBUG="$RET"
    db_get slm/servername || true
    SERVERNAME="$RET"
    CSRF_TRUSTED_ORIGINS="https://$SERVERNAME"
    ALLOWED_HOSTS="$SERVERNAME"
    db_get slm/adminmail  || true
    ADMIN_E_MAIL="$RET"
    db_get slm/adminpass  || true
    ADMIN_PASS="$RET"
    db_get slm/lang  || true
    SLM_LANG="$RET"
fi
   


case "$1" in
    configure)
	######## prepare a subdirectory to speed up matplotlib ############
	for d in /var/www/.config/matplotlib /var/www/.cache/matplotlib; do
	    mkdir -p $d;
	    find $d | xargs chown www-data
	done
	################# configuration of settings.py ####################
	# choose between debug or production settings
	if [ "$DEBUG" = "false" ]; then
	    SETTINGS=/var/lib/slm/manuels/settings-production.py
	else
	    SETTINGS=/var/lib/slm/manuels/settings-debug.py
	fi
	cat $SETTINGS | sed -e "s/__ALLOWED_HOSTS__/$ALLOWED_HOSTS/" \
			    -e "s%__CSRF_TRUSTED_ORIGINS__%$CSRF_TRUSTED_ORIGINS%" \
			    -e "s/^LANGUAGE_CODE.*/LANGUAGE_CODE = '$SLM_LANG'/" \
			    > /var/lib/slm/manuels/settings.py
	# modify the secret key to have an original key (to keep it secret)
	echo "Changing the secret key in settings.py"
	sed -i "s/SECRET_KEY = .*/SECRET_KEY = '$(pwgen 64 1)'/" \
	    /var/lib/slm/manuels/settings.py

	a2enmod rewrite
	a2enmod ssl

	# in case of a missing package node-svgdotjs-svg.js,
	# create a fake directory
	if [ ! -e /usr/share/nodejs/@svgdotjs/svg.js ]; then
	    mkdir -p /usr/share/nodejs/@svgdotjs/svg.js
	fi
	# and the same for package node-svgdotjs-svg.draggable.js
	if [ ! -e /usr/share/javascript/svg.draggable.js ]; then
	    mkdir -p /usr/share/javascript/svg.draggable.js
	fi
	
	cd /var/lib/slm; echo 'yes' | python3 manage.py collectstatic
	cd /var/lib/slm; python3 manage.py makemigrations
	cd /var/lib/slm; python3 manage.py makemigrations gestion
	cd /var/lib/slm; python3 manage.py migrate
	# create a superuser if it does not yet exist
	cd /var/lib/slm; cat createSuperUserIfNone | \
	    sed -e "s/__ADMIN_E_MAIL__/$ADMIN_E_MAIL/" \
		-e "s/__ADMIN_PASS__/$ADMIN_PASS/" | \
	    python3 manage.py shell
	# let www-data write to the database
	setfacl -m u:www-data:rwx /var/lib/slm
	setfacl -m u:www-data:rw /var/lib/slm/db.sqlite3
	# let www-data write to media
	mkdir -p /var/lib/slm/media/sauvegarde
	find /var/lib/slm/media -type d | xargs setfacl -m u:www-data:rwx
	find /var/lib/slm/media -type f | xargs setfacl -m u:www-data:rw
	# symlink /srv/$SERVERNAME => /var/lib/slm
	# when necessary
	[ -e /srv/$SERVERNAME ] || ln -s /var/lib/slm /srv/$SERVERNAME
	############### prepare apache2's configuration ##################
	############### when it does not yet exist      ##################
	if [ ! -e /etc/apache2/sites-available/slm.conf ]; then
	    cat /usr/share/doc/slm/apache.conf | \
		sed "s/__SERVERNAME__/$SERVERNAME/g" > \
		    /etc/apache2/sites-available/slm.conf;
	    cat <<EOF
A configuration is available at /etc/apache2/sites-available/slm.conf
in order to serve SLM with the help of Apache2, at $CSRF_TRUSTED_ORIGINS

to make use of the file /etc/apache2/sites-available/slm.conf:
--------------------------------------------------------------

1 - declare $SERVERNAME to your registrar, ...
    so it will be resolved as this machine's IP address

2 - stop apache2 server: "systemctl stop apache2"

3 - request a certificate: "certbot certonly --standalone -d $SERVERNAME"

4 - activate the configuration file: "a2ensite slm.conf"

5 - start Apache2: "systemctl start apache2"

EOF
	# restart apache (as rewrite or ssl may be recently installed)
	deb-systemd-invoke restart apache2
	fi
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument '$1'" >&2
        exit 1
    ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0