forked from sameersbn/docker-bind
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentrypoint.sh
executable file
·192 lines (157 loc) · 4.43 KB
/
entrypoint.sh
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
#!/bin/bash
set -e
# usage: file_env VAR [DEFAULT]
# ie: file_env 'XYZ_DB_PASSWORD' 'example'
# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
# "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature)
file_env() {
local var="$1"
local fileVar="${var}_FILE"
local def="${2:-}"
if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
echo >&2 "error: both $var and $fileVar are set (but are exclusive)"
exit 1
fi
local val="$def"
if [ "${!var:-}" ]; then
val="${!var}"
elif [ "${!fileVar:-}" ]; then
val="$(< "${!fileVar}")"
fi
export "$var"="$val"
unset "$fileVar"
}
file_env 'ROOT_PASSWORD'
file_env 'FTPUSER_PASSWORD'
ROOT_PASSWORD=${ROOT_PASSWORD:-password}
WEBMIN_ENABLED=${WEBMIN_ENABLED:-true}
BIND_DATA_DIR=${DATA_DIR}/bind
WEBMIN_DATA_DIR=${DATA_DIR}/webmin
PROFTP_DATA_DIR=${DATA_DIR}/proftpd
FTPUSER_PASSWORD=${FTPUSER_PASSWORD:-ftpuser}
NGINX_DATA_DIR=${DATA_DIR}/nginx
# ProFTPD files
FTPD_BIN=/usr/sbin/proftpd
FTPD_CONF=/etc/proftpd/proftpd.conf
PIDFILE=/var/run/proftpd.pid
create_proftpd_data_dir() {
# ftp root dir
mkdir -p ${PROFTP_DATA_DIR}
# populate default proftpd configuration if it does not exist
if [ ! -d ${PROFTP_DATA_DIR}/etc ]; then
mv /etc/proftpd ${PROFTP_DATA_DIR}/etc
fi
rm -rf /etc/proftpd
ln -sf ${PROFTP_DATA_DIR}/etc /etc/proftpd
chmod -R 0775 ${PROFTP_DATA_DIR}
chown -R ${PROFTP_USER}:${PROFTP_USER} ${PROFTP_DATA_DIR}
# make data dir
if [ ! -d ${PROFTP_DATA_DIR}/data ]; then
mkdir -p ${PROFTP_DATA_DIR}/data
usermod -m -d ${PROFTP_DATA_DIR}/data ${PROFTP_USER}
#chown ${PROFTP_USER}:${PROFTP_USER} ${PROFTP_DATA_DIR}/data
fi
usermod -m -d ${PROFTP_DATA_DIR}/data ${PROFTP_USER} || echo "usermod ftpuser"
}
create_bind_data_dir() {
mkdir -p ${BIND_DATA_DIR}
# populate default bind configuration if it does not exist
if [ ! -d ${BIND_DATA_DIR}/etc ]; then
mv /etc/bind ${BIND_DATA_DIR}/etc
fi
rm -rf /etc/bind
ln -sf ${BIND_DATA_DIR}/etc /etc/bind
chmod -R 0775 ${BIND_DATA_DIR}
chown -R ${BIND_USER}:${BIND_USER} ${BIND_DATA_DIR}
if [ ! -d ${BIND_DATA_DIR}/lib ]; then
mkdir -p ${BIND_DATA_DIR}/lib
chown ${BIND_USER}:${BIND_USER} ${BIND_DATA_DIR}/lib
fi
rm -rf /var/lib/bind
ln -sf ${BIND_DATA_DIR}/lib /var/lib/bind
}
create_nginx_data_dir() {
mkdir -p ${NGINX_DATA_DIR}
chmod -R 0755 ${NGINX_DATA_DIR}
chown -R root:root ${NGINX_DATA_DIR}
# populate the default webmin configuration if it does not exist
if [ ! -d ${NGINX_DATA_DIR}/etc ]; then
mv /etc/nginx ${NGINX_DATA_DIR}/etc
fi
rm -rf /etc/nginx
ln -sf ${NGINX_DATA_DIR}/etc /etc/nginx
}
create_webmin_data_dir() {
mkdir -p ${WEBMIN_DATA_DIR}
chmod -R 0755 ${WEBMIN_DATA_DIR}
chown -R root:root ${WEBMIN_DATA_DIR}
# populate the default webmin configuration if it does not exist
if [ ! -d ${WEBMIN_DATA_DIR}/etc ]; then
mv /etc/webmin ${WEBMIN_DATA_DIR}/etc
fi
rm -rf /etc/webmin
ln -sf ${WEBMIN_DATA_DIR}/etc /etc/webmin
}
set_root_passwd() {
echo "root:$ROOT_PASSWORD" | chpasswd
}
create_proftpd_user() {
useradd ${PROFTP_USER} || echo "$PROFTP_USER already exists."
echo "$PROFTP_USER:$FTPUSER_PASSWORD" | chpasswd
}
create_named_pid_dir() {
mkdir -m 0775 -p /var/run/named
chown root:${BIND_USER} /var/run/named
}
create_bind_cache_dir() {
mkdir -m 0775 -p /var/cache/bind
chown root:${BIND_USER} /var/cache/bind
}
create_named_pid_dir
create_bind_data_dir
create_bind_cache_dir
create_proftpd_user
create_proftpd_data_dir
create_nginx_data_dir
start_proftpd() {
if [ -f $PIDFILE ]; then
pid=`cat $PIDFILE`
fi
if [ ! -x $FTPD_BIN ]; then
echo "$0: $FTPD_BIN: cannot execute"
exit 1
fi
if [ -n "$pid" ]; then
echo "$0: proftpd [PID $pid] already running"
fi
if [ -r $FTPD_CONF ]; then
echo "Starting proftpd..."
$FTPD_BIN -c $FTPD_CONF
else
echo "$0: cannot start proftpd -- $FTPD_CONF missing"
fi
}
# allow arguments to be passed to named
if [[ ${1:0:1} = '-' ]]; then
EXTRA_ARGS="$@"
set --
elif [[ ${1} == named || ${1} == $(which named) ]]; then
EXTRA_ARGS="${@:2}"
set --
fi
# default behaviour is to launch named
if [[ -z ${1} ]]; then
if [ "${WEBMIN_ENABLED}" == "true" ]; then
create_webmin_data_dir
set_root_passwd
echo "Starting webmin..."
/etc/init.d/webmin start
fi
start_proftpd
echo "Starting nginx..."
/etc/init.d/nginx start
echo "Starting named..."
exec $(which named) -u ${BIND_USER} -g ${EXTRA_ARGS}
else
exec "$@"
fi