This repository has been archived by the owner on Aug 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
run
executable file
·182 lines (139 loc) · 4.01 KB
/
run
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
#!/bin/bash
set -e
readonly HOSTNAME=$(hostname -f)
readonly ERLANGCOOKIEFILE="${EJABBERD_ROOT}/.erlang.cookie"
readonly EJABBERDCTL="${EJABBERD_ROOT}/bin/ejabberdctl"
readonly CONFIGFILE="${EJABBERD_ROOT}/conf/ejabberd.yml"
readonly CONFIGTEMPLATE="${EJABBERD_ROOT}/conf/ejabberd.yml.tpl"
readonly CTLCONFIGFILE="${EJABBERD_ROOT}/conf/ejabberdctl.cfg"
readonly CTLCONFIGTEMPLATE="${EJABBERD_ROOT}/conf/ejabberdctl.cfg.tpl"
readonly SSLCERTHOST="${EJABBERD_ROOT}/ssl/host.pem"
readonly SSLCERTDOMAIN="${EJABBERD_ROOT}/ssl/xmpp_domain.pem"
readonly LOGDIR="${EJABBERD_ROOT}/logs"
readonly PYTHON_JINJA2="import os;
import sys;
import jinja2;
sys.stdout.write(
jinja2.Template
(sys.stdin.read()
).render(env=os.environ))"
is_set() {
local var=$1
[[ -n $var ]]
}
file_exist() {
local file=$1
[[ -e $file ]]
}
is_true() {
local var=${1,,}
local choices=("yes" "1" "y" "true")
for ((i=0;i < ${#choices[@]};i++)) {
[[ "${choices[i]}" == $var ]] && return 0
}
return 1
}
make_snakeoil_certificate() {
local domain=$1
local certfile=$2
openssl req -subj "/CN=${domain}" \
-new \
-newkey rsa:2048 \
-days 365 \
-nodes \
-x509 \
-keyout /tmp/selfsigned.key \
-out /tmp/selfsigned.crt
echo "Writing ssl cert and private key to '${certfile}'..."
cat /tmp/selfsigned.crt /tmp/selfsigned.key > ${certfile}
rm /tmp/selfsigned.crt /tmp/selfsigned.key
}
make_host_snakeoil_certificate() {
local IFS=@
local domain='localhost'
local erlang_node=${ERLANG_NODE}
if is_true ${erlang_node} ; then
domain=${HOSTNAME}
elif is_set ${erlang_node} ; then
set ${erlang_node}
domain=$2
fi
echo -n "Missing ssl cert for your host. "
echo "Generating snakeoil ssl cert for ${domain}..."
make_snakeoil_certificate ${domain} ${SSLCERTHOST}
}
make_domain_snakeoil_certificate() {
local domain='localhost'
is_set ${XMPP_DOMAIN} \
&& domain=${XMPP_DOMAIN}
echo -n "Missing ssl cert for your xmpp domain. "
echo "Generating snakeoil ssl cert for ${domain}..."
make_snakeoil_certificate ${domain} ${SSLCERTDOMAIN}
}
make_config() {
echo "Generating ejabberd config file..."
cat ${CONFIGTEMPLATE} \
| python -c "${PYTHON_JINJA2}" \
> ${CONFIGFILE}
echo "Generating ejabberdctl config file..."
cat ${CTLCONFIGTEMPLATE} \
| python -c "${PYTHON_JINJA2}" \
> ${CTLCONFIGFILE}
}
set_erlang_cookie() {
echo "Set erlang cookie to ${ERLANG_COOKIE}..."
echo ${ERLANG_COOKIE} > ${ERLANGCOOKIEFILE}
chmod 400 ${ERLANGCOOKIEFILE}
}
ctl() {
local action="$1"
${EJABBERDCTL} ${action} >/dev/null
}
_trap() {
echo "Stopping ejabberd..."
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
}
## setup
# if ERLANG_NODE is true reset it to "ejabberd" and add the
# hostname to the node. This is for backward compatibility.
is_true ${ERLANG_NODE} \
&& export ERLANG_NODE="ejabberd@${HOSTNAME}"
# generate config file
make_config
# set erlang cookie if ERLANG_COOKIE is set
is_set ${ERLANG_COOKIE} \
&& set_erlang_cookie
# generate host ssl cert if missing
file_exist ${SSLCERTHOST} \
|| make_host_snakeoil_certificate
# generate xmmp domain ssl cert if missing
file_exist ${SSLCERTDOMAIN} \
|| make_domain_snakeoil_certificate
# Catch signals and shutdown ejabberd
trap _trap SIGTERM SIGINT
## run ejabberd
case "$@" in
start)
tail -F ${LOGDIR}/crash.log \
${LOGDIR}/error.log \
${LOGDIR}/erlang.log &
echo "Starting ejabberd..."
exec ${EJABBERDCTL} "live" &
child=$!
wait $child
;;
live)
echo "Starting ejabberd in 'live' mode..."
exec ${EJABBERDCTL} "live"
;;
esac