aboutsummaryrefslogtreecommitdiff
path: root/docker/run.sh
blob: 1162f17ad9b006a364bee8221b49acc4b8c6a918 (plain) (blame)
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/bash
set -e

source "${EJABBERD_HOME}/scripts/lib/base_config.sh"
source "${EJABBERD_HOME}/scripts/lib/config.sh"
source "${EJABBERD_HOME}/scripts/lib/base_functions.sh"
source "${EJABBERD_HOME}/scripts/lib/functions.sh"

# discover hostname
readonly nodename=$(get_nodename)

is_zero ${ERLANG_NODE} \
    && export ERLANG_NODE="ejabberd@localhost"

## backward compatibility
# if ERLANG_NODE is true reset it to "ejabberd" and add
# hostname to the nodename.
# else: export ${ERLANG_NODE} with nodename
if (is_true ${ERLANG_NODE}); then
    export ERLANG_NODE="ejabberd@${nodename}"
fi


run_scripts() {
    local run_script_dir="${EJABBERD_HOME}/scripts/${1}"
    for script in ${run_script_dir}/*.sh ; do
        if [ -f ${script} -a -x ${script} ] ; then
            ${script}
        fi
    done
}


pre_scripts() {
    run_scripts "pre"
}


post_scripts() {
    run_scripts "post"
}

stop_scripts() {
    run_scripts "stop"
}


ctl() {
    local action="$1"
    ${EJABBERDCTL} ${action} >/dev/null
}


_trap() {
    echo "Stopping ejabberd..."
    stop_scripts
    if ctl stop ; then
        local cnt=0
        sleep 1
        while ctl status || test $? = 1 ; do
            cnt=`expr $cnt + 1`
            if [ $cnt -ge 60 ] ; then
                break
            fi
            sleep 1
        done
    fi
}


# Catch signals and shutdown ejabberd
trap _trap SIGTERM SIGINT

## run ejabberd
case "$@" in
    start)
        pre_scripts
        tail -n 0 -F ${LOGDIR}/crash.log \
                ${LOGDIR}/error.log \
                ${LOGDIR}/erlang.log &
        echo "Starting ejabberd..."
        exec ${EJABBERDCTL} "foreground" &
        child=$!
        ${EJABBERDCTL} "started"
        post_scripts
        wait $child
    ;;
    live)
        pre_scripts
        echo "Starting ejabberd in 'live' mode..."
        exec ${EJABBERDCTL} "live"
    ;;
    shell)
        exec "/bin/bash"
    ;;
    *)
        exec $@
    ;;
esac