-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile_mmsgate2
More file actions
223 lines (194 loc) · 8.62 KB
/
Dockerfile_mmsgate2
File metadata and controls
223 lines (194 loc) · 8.62 KB
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
# syntax=docker/dockerfile:1
# registry to pull opensips is the default
ARG REG=""
# this builds the mmsgate2 and lego go execs. we'll run native and cross compile
FROM --platform=$BUILDPLATFORM golang:latest AS gobuild
LABEL org.opencontainers.image.authors="https://github.com/RVgo4it"
# need for cross compiling targets
ARG BUILDPLATFORM
ARG TARGETPLATFORM
ARG TARGETOS
ARG TARGETARCH
# we'll put output under /root
WORKDIR /root
RUN apt-get -y update; apt-get -y install \
# Important: required for go-sqlite3
gcc \
# Required for Alpine
musl-dev
# use bash for all shells w/ ~/.bashstart
SHELL ["bash", "-c"]
RUN ln -s bash /bin/sh.bash && \
mv /bin/sh.bash /bin/sh
ENV BASH_ENV=/root/.bashstart
RUN echo BASH_ENV=/root/.bashstart >> ~/.bashstart
# check host system memory and set GOMAXPROCS and other gcc options for memory use
RUN read X KELO Y < /proc/meminfo && \
echo from /proc/meminfo = ${KELO}kb && \
MEGS=$((KELO/1024)) && \
NPROC=$(nproc) && \
echo export MEGS=$MEGS >> ~/.bashstart && \
echo Memory: ${MEGS}mb && \
if [ $MEGS -le 512 ] ; then \
[ $NPROC -gt 1 ] && echo export GOMAXPROCS=1 >> ~/.bashstart ; \
echo "export CGO_CPPFLAGS='--param=ggc-min-expand=10 --param=ggc-min-heapsize=8192 -Wa,--reduce-memory-overheads'" >> ~/.bashstart ; \
elif [ $MEGS -le 1024 ] ; then \
[ $NPROC -gt 2 ] && echo export GOMAXPROCS=2 >> ~/.bashstart ; \
echo "export CGO_CPPFLAGS='--param=ggc-min-expand=20 --param=ggc-min-heapsize=16384 -Wa,--reduce-memory-overheads'" >> ~/.bashstart ; \
elif [ $MEGS -le 2048 ] ; then \
[ $NPROC -gt 3 ] && echo export GOMAXPROCS=3 >> ~/.bashstart ; \
fi
RUN echo $CPU Limit: $GOMAXPROCS && \
echo Go Cpp Flags: $CGO_CPPFLAGS
# compile mmsgate2
RUN mkdir ~/mmsgate2
COPY --chown=root --chmod=755 mmsgate2/scripts/*.go mmsgate2/scripts/go.* /root/mmsgate2/
# if doing cross compile... then add the needed GCC compiler. then start compile...
RUN if [ ${TARGETPLATFORM} != ${BUILDPLATFORM} ] ; then \
if [ ${TARGETARCH} == arm ] ; then \
apt-get -y install gcc-arm-linux-gnueabihf && \
export CC=arm-linux-gnueabihf-gcc; \
fi && \
if [ ${TARGETARCH} == arm64 ] ; then \
apt-get -y install gcc-aarch64-linux-gnu && \
export CC=aarch64-linux-gnu-gcc; \
fi && \
if [ ${TARGETARCH} == amd64 ] ; then \
apt-get -y install gcc-x86-64-linux-gnu && \
export CC=x86_64-linux-gnu-gcc; \
fi ; \
fi && \
cd ~/mmsgate2 && \
go get -u && go mod tidy && \
CGO_ENABLED=1 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -o ./mmsgate2
# download the LEGO_VER image (if host <1g mem) or compile latest lego
ARG LEGO_VER=4.32.0
RUN mkdir ~/lego && cd ~/lego && \
if [ $MEGS -le 1024 ] ; then \
echo Downloading lego && \
LEGOVER=$LEGO_VER && \
echo LEGOVER: $LEGO_VER && \
curl -L -o lego_${LEGOVER}_checksums.txt https://github.com/go-acme/lego/releases/download/v$LEGOVER/lego_${LEGOVER}_checksums.txt && \
if [ ${TARGETARCH} == arm ] ; then \
LEGOARCH=armv7; \
fi && \
if [ ${TARGETARCH} == arm64 ] ; then \
LEGOARCH=arm64; \
fi && \
if [ ${TARGETARCH} == amd64 ] ; then \
LEGOARCH=amd64; \
fi && \
echo LEGOARCH: $LEGOARCH && \
curl -L -o lego_v${LEGOVER}_linux_$LEGOARCH.tar.gz https://github.com/go-acme/lego/releases/download/v$LEGOVER/lego_v${LEGOVER}_linux_$LEGOARCH.tar.gz && \
SUM=$(sha256sum lego_v${LEGOVER}_linux_$LEGOARCH.tar.gz) && \
grep "$SUM" lego_${LEGOVER}_checksums.txt && \
tar -xvzf lego_v${LEGOVER}_linux_$LEGOARCH.tar.gz ; \
else \
echo Installing lego && \
cd ~ && \
git clone https://github.com/go-acme/lego && \
cd lego && \
go get -u ./... && \
go mod tidy && \
GOOS=${TARGETOS} GOARCH=${TARGETARCH} make build; \
fi
# compile sipexer
RUN git clone https://github.com/miconda/sipexer && \
cd sipexer && \
go get ./... && \
go get -u && go mod tidy && \
GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build .
# This builds a MMSgate2 image from the OpenSIPS installed image
FROM ${REG}rvgo4it/opensips
LABEL org.opencontainers.image.authors="https://github.com/RVgo4it"
USER root
WORKDIR /root
# install needed apps
RUN apt -y update && \
apt -y upgrade && \
apt install -y curl \
netcat-openbsd \
net-tools \
tor \
nano \
cron \
jq \
dumb-init \
nginx \
lynx \
traceroute \
rsyslog \
iputils-tracepath \
htop \
dnsutils
# Configure user mmsgate, with a little sudo
RUN useradd -ms /bin/bash mmsgate && \
echo 'mmsgate:notnot' | chpasswd && \
echo 'mmsgate ALL=(ALL) NOPASSWD:SETENV:/etc/opensips/globalcfg.sh, /scripts/stopopensips.sh, /scripts/fwtestviator.sh, /scripts/fwtestviaproxy.sh, /scripts/certs.sh, /scripts/restart.sh, /scripts/displaylog.sh' >> /etc/sudoers
# Configure user opensips, no sudo
RUN useradd -ms /bin/bash opensips && \
echo 'opensips:notnot' | chpasswd
# Configure user mmsgateadm, with sudo
RUN useradd -ms /bin/bash mmsgateadm && \
echo 'mmsgateadm:notnot' | chpasswd && \
echo 'mmsgateadm ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
# bring over the scripts and other files
COPY --chown=root mmsgate2/scripts/ /scripts/
COPY --chown=root mmsgate2/globalcfg.txt mmsgate2/globalcfg.sh mmsgate2/opensips.cfg /etc/opensips/
# have a backup opensips config file
RUN md5sum /etc/opensips/opensips.cfg >/etc/opensips/opensips.cfg.md5sum && \
mkdir /etc/opensips-bak && \
cp /etc/opensips/opensips.cfg* /etc/opensips-bak
# cron new config location
# have cron run preprocchk.sh every 10 minutes, dynudnschk.sh 5 minutes after that
# also, clean up media files (not xml, vcard or qrcode files) over 90 days old once a day, 2 am
# and clean up messages from over 30 days ago, sundays at 2:30 am
# finally compact the databases sundays at 2:45 am and 2:55 am
RUN mv /var/spool/cron/crontabs /etc/opensips/ && \
ln -s /etc/opensips/crontabs /var/spool/cron && \
echo 'SHELL=/bin/bash' | crontab - && \
{ crontab -l; echo '*/10 * * * * . /etc/profile; /scripts/preprocchk.sh'; } | crontab - && \
{ crontab -l; echo '5,15,25,35,45,55 * * * * . /etc/profile; /scripts/dynudnschk.sh'; } | crontab - && \
{ crontab -l; echo '0 2 * * * . /etc/opensips/globalcfg.sh; find $LOCALMEDIA -mtime +90 ! -name '"'"'contacts.vcard'"'"' ! -name '"'"'*.xml'"'"' ! -name '"'"'*-cfg.png'"'"' -delete'; } | crontab - && \
{ crontab -l; echo '30 2 * * 0 . /etc/opensips/globalcfg.sh; sqlite3 $DBPATHM '"'"'DELETE FROM send_msgs WHERE rcvd_ts < CAST(strftime("\%s",date("now","-30 days")) AS INTEGER);'"'"; } | crontab - && \
{ crontab -l; echo '45 2 * * 0 . /etc/opensips/globalcfg.sh; sqlite3 $DBPATHM "VACUUM;"'; } | crontab - && \
{ crontab -l; echo '55 2 * * 0 . /etc/opensips/globalcfg.sh; sqlite3 $DBPATH "VACUUM;"'; } | crontab -
# setup log files for opensips and mmsgate
RUN echo 'local7.* /var/log/opensips.log' > /etc/rsyslog.d/40-opensips.conf && \
echo 'local7.* stop' >> /etc/rsyslog.d/40-opensips.conf && \
echo 'local6.* /var/log/mmsgate.log' > /etc/rsyslog.d/41-mmsgate.conf && \
echo 'local6.* stop' >> /etc/rsyslog.d/41-mmsgate.conf
# setup log rotates. includes opensips.log, mmsgate.log and nginx files and HUB/USR1 signal to reopen
RUN sed --in-place 's_rsyslog-rotate_rsyslog-rotate\n\t\t/bin/kill -HUP $(cat /var/run/rsyslogd.pid)_' /etc/logrotate.d/rsyslog && \
sed --in-place 's_invoke-rc.d nginx rotate_invoke-rc.d nginx rotate >/dev/null 2>&1\n\t\t/bin/kill -USR1 $(cat /var/run/nginx.pid)_' /etc/logrotate.d/nginx
# need some data folders
RUN mkdir -m=777 /data /data/mmsmedia /data/mmsgate /data/opensips
# setup Nginx, new config location, no default site and bump up hash bucket
ARG ADMPW="Apple99"
RUN sed 's/\/etc\/nginx/\/etc\/opensips\/nginx/' -i /etc/nginx/nginx.conf && \
sed 's/# server_names_hash_bucket_size/server_names_hash_bucket_size/' -i /etc/nginx/nginx.conf && \
mv /etc/nginx /etc/opensips/ && \
rm /etc/opensips/nginx/sites-enabled/default && \
ln -s /etc/opensips/nginx /etc && \
echo -n 'admin:' >> /etc/opensips/nginx/.htpasswd && \
openssl passwd -apr1 ${ADMPW} >> /etc/opensips/nginx/.htpasswd && \
chown mmsgate:mmsgate /etc/opensips/nginx/.htpasswd
COPY --chown=root --chmod=755 mmsgate2/admin /etc/opensips/nginx/sites-available/
RUN ln -fs /etc/opensips/nginx/sites-available/admin /etc/opensips/nginx/sites-enabled/admin
# setup group for shared db access
RUN addgroup db && \
adduser mmsgateadm db && \
adduser mmsgate db && \
adduser opensips db && \
chown root:db /data/mmsgate && \
chmod g+s /data/mmsgate
# get go exec
COPY --from=gobuild /root/mmsgate2/mmsgate2 /scripts/
COPY --from=gobuild /root/lego/dist/lego /usr/bin/lego
COPY --from=gobuild /root/sipexer/sipexer /scripts/
USER mmsgateadm
WORKDIR /home/mmsgateadm
# Startup config
ENTRYPOINT ["/usr/bin/dumb-init", "--"]
CMD ["/scripts/init.sh"]
EXPOSE 5061/tcp 38443/tcp 38000/tcp