File: tryton-server-nginx.postinst

package info (click to toggle)
tryton-server 7.0.30-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 7,756 kB
  • sloc: python: 53,218; xml: 5,194; sh: 802; sql: 217; makefile: 28
file content (99 lines) | stat: -rw-r--r-- 2,572 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
#!/bin/sh

set -e

. /usr/share/debconf/confmodule

TRYTON_USER="tryton"
TRYTON_CONFDIR="/etc/tryton"
NGINX_CONF_AVAILABLE="/etc/nginx/sites-available"
NGINX_CONF_ENABLED="/etc/nginx/sites-enabled"
TRYTON_SHAREDIR="/usr/share/tryton-server"
TRYTON_CONFFILE="${TRYTON_CONFDIR}/trytond.conf"
TRYTON_NGINX_CONF="${NGINX_CONF_AVAILABLE}/tryton-uwsgi.conf"
TRYTON_NGINX_CONF_TEMPLATE="${TRYTON_SHAREDIR}/default/tryton-uwsgi.conf"
TRYTON_NGINX_CONF_NEW=

# POSIX-compliant shell function to check for the existence of a command
# s. developers-reference 6.4
pathfind() {
    OLDIFS="$IFS"
    IFS=:
    for p in $PATH; do
        if [ -x "$p/$*" ]; then
            IFS="$OLDIFS"
            return 0
        fi
    done
    IFS="$OLDIFS"
    return 1
}

cleanup () {
    [ "$TRYTON_NGINX_CONF_NEW" ] && rm -f "$TRYTON_NGINX_CONF_NEW"
}

# For now we depend on tryton-server-uwsgi, i.e. the uwsgi app will run
# on the same machine on port 8001
create_config () {
    trap cleanup EXIT
    TRYTON_NGINX_CONF_NEW=$(mktemp)
    cp -a "$TRYTON_NGINX_CONF_TEMPLATE" "$TRYTON_NGINX_CONF_NEW"

    uwsgi_uri=$(hostname):8001
    website_uri=$(hostname -f)
    run_certbot="false"

    db_get tryton-server-nginx/website-uri
    if [ ! -z "$RET" ]; then
        website_uri="$RET"
    fi
    db_get tryton-server-nginx/run-certbot && run_certbot="$RET"
    
    # Insert the uwsgi uri into the nginx conf
    sed -i "s|TRYTON_URI|$uwsgi_uri|g" "$TRYTON_NGINX_CONF_NEW"
    sed -i "s|TRYTON_DOMAIN|$website_uri|g" "$TRYTON_NGINX_CONF_NEW"
    
    # Manage/register the configuration files
    if pathfind ucfr; then
        ucf --debconf-ok --src-dir "$TRYTON_SHAREDIR/default/" "$TRYTON_NGINX_CONF_NEW" "$TRYTON_NGINX_CONF"
        ucfr tryton-server-nginx "${TRYTON_NGINX_CONF}"
    fi
    
    cleanup
    
    ln -sf "$TRYTON_NGINX_CONF" "$NGINX_CONF_ENABLED"

    service_name=nginx.service
    if [ -d /run/systemd/system ]; then
        deb-systemd-invoke reload "$service_name" >/dev/null || true
    fi
    
    # Run the certbot configuration if agreed and an email was configured
    if [ "$run_certbot" = "true" ]; then
        db_get tryton-server-nginx/certbot-email
        if [ ! -z "$RET" ]; then
            certbot_email="$RET"
            certbot --agree-tos --nginx -n -m "$certbot_email" -d "$website_uri"
        fi
    fi
}

case "$1" in
    configure)
        create_config
    ;;

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

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

#DEBHELPER#

exit 0