Skip to content

Commit 2c1aa43

Browse files
authored
Address points from DockerHub review (#453)
* Un-set INT and TERM traps in alpine entrypoint before exec * Use `apk --print-arch` instead of `arch` in alpine Dockerfile * Quote all the things * Fixup setting global log-level
1 parent 21b42bc commit 2c1aa43

File tree

3 files changed

+175
-174
lines changed

3 files changed

+175
-174
lines changed

influxdb/2.0/alpine/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ RUN addgroup -S -g 1000 influxdb && \
1313
# https://github.com/tianon/gosu/releases
1414
ENV GOSU_VER 1.12
1515
RUN set -eux; \
16-
ARCH=$(arch) && \
16+
ARCH="$(apk --print-arch)" && \
1717
if [ ${ARCH} = x86_64 ]; then \
1818
ARCH=amd64; \
1919
elif [ ${ARCH} = aarch64 ]; then \

influxdb/2.0/alpine/entrypoint.sh

Lines changed: 91 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function log_level () {
88
echo 2
99
elif [ "$1" = warn ]; then
1010
echo 1
11-
else
11+
elif [ "$1" = error ]; then
1212
echo 0
1313
fi
1414
}
@@ -19,7 +19,8 @@ LOG_LEVEL=error
1919
function log () {
2020
local level=$1 msg=$2
2121
shift 2
22-
if [ $(log_level ${level}) -gt $(log_level ${LOG_LEVEL}) ]; then
22+
23+
if [ "$(log_level ${level})" -gt "$(log_level ${LOG_LEVEL})" ]; then
2324
return
2425
fi
2526

@@ -29,17 +30,17 @@ function log () {
2930
shift 2
3031
done
3132

32-
local logtime=$(date --utc +'%FT%T.%NZ')
33+
local logtime="$(date --utc +'%FT%T.%NZ')"
3334
1>&2 echo -e "${logtime}\t${level}\t${msg}\t{${attrs}}"
3435
}
3536

3637
# Set the global log-level for the entry-point to match the config passed to influxd.
3738
function set_global_log_level () {
38-
local level="$(influxd print-config --key-name log-level ${@})"
39-
if [ -z "$(log_level ${level})" ]; then
40-
log error "Invalid log-level specified, using 'error'" level ${level}
39+
local level="$(influxd print-config --key-name log-level "${@}")"
40+
if [ -z "${level}" ] || [ -z "$(log_level ${level})" ]; then
41+
return 1
4142
fi
42-
LOG_LEVEL=${level}
43+
LOG_LEVEL="${level}"
4344
}
4445

4546
# Look for standard config names in the volume configured in our Dockerfile.
@@ -50,40 +51,40 @@ readonly CONFIG_NAMES="config.json config.toml config.yaml config.yml"
5051
function set_config_path () {
5152
local config_path=/etc/defaults/influxdb2/config.yml
5253

53-
if [ -n "$INFLUXD_CONFIG_PATH" ]; then
54-
config_path=${INFLUXD_CONFIG_PATH}
54+
if [ -n "${INFLUXD_CONFIG_PATH}" ]; then
55+
config_path="${INFLUXD_CONFIG_PATH}"
5556
else
56-
for name in $CONFIG_NAMES; do
57-
if [ -f ${CONFIG_VOLUME}/${name} ]; then
58-
config_path=${CONFIG_VOLUME}/${name}
57+
for name in ${CONFIG_NAMES}; do
58+
if [ -f "${CONFIG_VOLUME}/${name}" ]; then
59+
config_path="${CONFIG_VOLUME}/${name}"
5960
break
6061
fi
6162
done
6263
fi
6364

64-
export INFLUXD_CONFIG_PATH=${config_path}
65+
export INFLUXD_CONFIG_PATH="${config_path}"
6566
}
6667

6768
function set_data_paths () {
68-
export BOLT_PATH=$(influxd print-config --key-name bolt-path ${@})
69-
export ENGINE_PATH=$(influxd print-config --key-name engine-path ${@})
69+
export BOLT_PATH="$(influxd print-config --key-name bolt-path "${@}")"
70+
export ENGINE_PATH="$(influxd print-config --key-name engine-path "${@}")"
7071
}
7172

7273
# Ensure all the data directories needed by influxd exist with the right permissions.
7374
function create_directories () {
74-
local bolt_dir=$(dirname ${BOLT_PATH})
75-
local user=$(id -u)
75+
local bolt_dir="$(dirname "${BOLT_PATH}")"
76+
local user="$(id -u)"
7677

77-
mkdir -p ${bolt_dir} ${ENGINE_PATH}
78-
chmod 700 ${bolt_dir} ${ENGINE_PATH} || :
78+
mkdir -p "${bolt_dir}" "${ENGINE_PATH}"
79+
chmod 700 "${bolt_dir}" "${ENGINE_PATH}" || :
7980

80-
mkdir -p ${CONFIG_VOLUME} || :
81-
chmod 775 ${CONFIG_VOLUME} || :
81+
mkdir -p "${CONFIG_VOLUME}" || :
82+
chmod 775 "${CONFIG_VOLUME}" || :
8283

83-
if [ ${user} = 0 ]; then
84-
find ${bolt_dir} \! -user influxdb -exec chown influxdb '{}' +
85-
find ${ENGINE_PATH} \! -user influxdb -exec chown influxdb '{}' +
86-
find ${CONFIG_VOLUME} \! -user influxdb -exec chown influxdb '{}' +
84+
if [ "${user}" = 0 ]; then
85+
find "${bolt_dir}" \! -user influxdb -exec chown influxdb '{}' +
86+
find "${ENGINE_PATH}" \! -user influxdb -exec chown influxdb '{}' +
87+
find "${CONFIG_VOLUME}" \! -user influxdb -exec chown influxdb '{}' +
8788
fi
8889
}
8990

@@ -93,9 +94,9 @@ readonly REQUIRED_INIT_VARS="INFLUXDB_INIT_USERNAME INFLUXDB_INIT_PASSWORD INFLU
9394
# Ensure all env vars required to run influx setup or influxd upgrade are set in the env.
9495
function ensure_init_vars_set () {
9596
local missing_some=0
96-
for var in $REQUIRED_INIT_VARS; do
97+
for var in ${REQUIRED_INIT_VARS}; do
9798
if [ -z "$(printenv ${var})" ]; then
98-
log error "missing parameter, cannot init InfluxDB" parameter ${var}
99+
log error "missing parameter, cannot init InfluxDB" parameter "${var}"
99100
missing_some=1
100101
fi
101102
done
@@ -108,8 +109,8 @@ function ensure_init_vars_set () {
108109
# If we didn't do this, the container would see the boltdb file on reboot and assume
109110
# the DB is already full set up.
110111
function cleanup_influxd () {
111-
log warn "cleaning bolt and engine files to prevent conflicts on retry" bolt_path ${BOLT_PATH} engine_path ${ENGINE_PATH}
112-
rm -rf ${BOLT_PATH} ${ENGINE_PATH}
112+
log warn "cleaning bolt and engine files to prevent conflicts on retry" bolt_path "${BOLT_PATH}" engine_path "${ENGINE_PATH}"
113+
rm -rf "${BOLT_PATH}" "${ENGINE_PATH}"
113114
}
114115

115116
# Upgrade V1 data into the V2 format using influxd upgrade.
@@ -122,40 +123,40 @@ function cleanup_influxd () {
122123
function upgrade_influxd () {
123124
set -- \
124125
--force \
125-
--username ${INFLUXDB_INIT_USERNAME} \
126-
--password ${INFLUXDB_INIT_PASSWORD} \
127-
--org ${INFLUXDB_INIT_ORG} \
128-
--bucket ${INFLUXDB_INIT_BUCKET} \
129-
--v2-config-path ${CONFIG_VOLUME}/config.toml \
130-
--influx-configs-path ${INFLUX_CONFIGS_PATH} \
131-
--continuous-query-export-path ${CONFIG_VOLUME}/v1-cq-export.txt \
132-
--log-path ${CONFIG_VOLUME}/upgrade.log \
133-
--log-level ${LOG_LEVEL} \
134-
--bolt-path ${BOLT_PATH} \
135-
--engine-path ${ENGINE_PATH} \
126+
--username "${INFLUXDB_INIT_USERNAME}" \
127+
--password "${INFLUXDB_INIT_PASSWORD}" \
128+
--org "${INFLUXDB_INIT_ORG}" \
129+
--bucket "${INFLUXDB_INIT_BUCKET}" \
130+
--v2-config-path "${CONFIG_VOLUME}/config.toml" \
131+
--influx-configs-path "${INFLUX_CONFIGS_PATH}" \
132+
--continuous-query-export-path "${CONFIG_VOLUME}/v1-cq-export.txt" \
133+
--log-path "${CONFIG_VOLUME}/upgrade.log" \
134+
--log-level "${LOG_LEVEL}" \
135+
--bolt-path "${BOLT_PATH}" \
136+
--engine-path "${ENGINE_PATH}" \
136137
--overwrite-existing-v2
137138

138-
if [ -n "$INFLUXDB_INIT_RETENTION" ]; then
139-
set -- ${@} --retention ${INFLUXDB_INIT_RETENTION}
139+
if [ -n "${INFLUXDB_INIT_RETENTION}" ]; then
140+
set -- "${@}" --retention "${INFLUXDB_INIT_RETENTION}"
140141
fi
141-
if [ -n "$INFLUXDB_INIT_ADMIN_TOKEN" ]; then
142-
set -- ${@} --token ${INFLUXDB_INIT_ADMIN_TOKEN}
142+
if [ -n "${INFLUXDB_INIT_ADMIN_TOKEN}" ]; then
143+
set -- "${@}" --token "${INFLUXDB_INIT_ADMIN_TOKEN}"
143144
fi
144145

145-
if [[ -n "$INFLUXDB_INIT_UPGRADE_V1_CONFIG" && -f ${INFLUXDB_INIT_UPGRADE_V1_CONFIG} ]]; then
146-
set -- ${@} --config-file ${INFLUXDB_INIT_UPGRADE_V1_CONFIG}
147-
elif [[ -n "$INFLUXDB_INIT_UPGRADE_V1_DIR" && -d ${INFLUXDB_INIT_UPGRADE_V1_DIR} ]]; then
148-
set -- ${@} --v1-dir ${INFLUXDB_INIT_UPGRADE_V1_DIR}
146+
if [[ -n "${INFLUXDB_INIT_UPGRADE_V1_CONFIG}" && -f "${INFLUXDB_INIT_UPGRADE_V1_CONFIG}" ]]; then
147+
set -- "${@}" --config-file "${INFLUXDB_INIT_UPGRADE_V1_CONFIG}"
148+
elif [[ -n "${INFLUXDB_INIT_UPGRADE_V1_DIR}" && -d "${INFLUXDB_INIT_UPGRADE_V1_DIR}" ]]; then
149+
set -- "${@}" --v1-dir "${INFLUXDB_INIT_UPGRADE_V1_DIR}"
149150
elif [ -f /etc/influxdb/influxdb.conf ]; then
150-
set -- ${@} --config-file /etc/influxdb/influxdb.conf
151+
set -- "${@}" --config-file /etc/influxdb/influxdb.conf
151152
elif [ -d /var/lib/influxdb ]; then
152-
set -- ${@} --v1-dir /var/lib/influxdb
153+
set -- "${@}" --v1-dir /var/lib/influxdb
153154
else
154155
log error "failed to autodetect usable V1 config or data dir, aborting upgrade"
155156
exit 1
156157
fi
157158

158-
influxd upgrade ${@}
159+
influxd upgrade "${@}"
159160

160161
# Reset global influxd config to pick up new file written by the upgrade process.
161162
set_config_path
@@ -185,27 +186,27 @@ function wait_for_influxd () {
185186
function setup_influxd () {
186187
set -- \
187188
--force \
188-
--username ${INFLUXDB_INIT_USERNAME} \
189-
--password ${INFLUXDB_INIT_PASSWORD} \
190-
--org ${INFLUXDB_INIT_ORG} \
191-
--bucket ${INFLUXDB_INIT_BUCKET}
189+
--username "${INFLUXDB_INIT_USERNAME}" \
190+
--password "${INFLUXDB_INIT_PASSWORD}" \
191+
--org "${INFLUXDB_INIT_ORG}" \
192+
--bucket "${INFLUXDB_INIT_BUCKET}"
192193

193-
if [ -n "$INFLUXDB_INIT_RETENTION" ]; then
194-
set -- ${@} --retention ${INFLUXDB_INIT_RETENTION}
194+
if [ -n "${INFLUXDB_INIT_RETENTION}" ]; then
195+
set -- "${@}" --retention "${INFLUXDB_INIT_RETENTION}"
195196
fi
196-
if [ -n "$INFLUXDB_INIT_ADMIN_TOKEN" ]; then
197-
set -- ${@} --token ${INFLUXDB_INIT_ADMIN_TOKEN}
197+
if [ -n "${INFLUXDB_INIT_ADMIN_TOKEN}" ]; then
198+
set -- "${@}" --token "${INFLUXDB_INIT_ADMIN_TOKEN}"
198199
fi
199200

200-
influx setup ${@}
201+
influx setup "${@}"
201202
}
202203

203204
# Get the IDs of the initial user/org/bucket created during setup, and export them into the env.
204205
# We do this to help with arbitrary user scripts, since many influx CLI commands only take IDs.
205206
function set_init_resource_ids () {
206-
export INFLUXDB_INIT_USER_ID=$(influx user list -n ${INFLUXDB_INIT_USER} --hide-headers | cut -f 1)
207-
export INFLUXDB_INIT_ORG_ID=$(influx org list -n ${INFLUXDB_INIT_ORG} --hide-headers | cut -f 1)
208-
export INFLUXDB_INIT_BUCKET_ID=$(influx bucket list -n ${INFLUXDB_INIT_BUCKET} --hide-headers | cut -f 1)
207+
export INFLUXDB_INIT_USER_ID="$(influx user list -n "${INFLUXDB_INIT_USER}" --hide-headers | cut -f 1)"
208+
export INFLUXDB_INIT_ORG_ID="$(influx org list -n "${INFLUXDB_INIT_ORG}" --hide-headers | cut -f 1)"
209+
export INFLUXDB_INIT_BUCKET_ID="$(influx bucket list -n "${INFLUXDB_INIT_BUCKET}" --hide-headers | cut -f 1)"
209210
}
210211

211212
# Allow users to mount arbitrary startup scripts into the container,
@@ -230,31 +231,31 @@ function handle_signal () {
230231
# Perform initial setup on the InfluxDB instance, either by setting up fresh metadata
231232
# or by upgrading existing V1 data.
232233
function init_influxd () {
233-
if [[ ${INFLUXDB_INIT_MODE} != setup && ${INFLUXDB_INIT_MODE} != upgrade ]]; then
234-
log error "found invalid INFLUXDB_INIT_MODE, valid values are 'setup' and 'upgrade'" INFLUXDB_INIT_MODE ${INFLUXDB_INIT_MODE}
234+
if [[ "${INFLUXDB_INIT_MODE}" != setup && "${INFLUXDB_INIT_MODE}" != upgrade ]]; then
235+
log error "found invalid INFLUXDB_INIT_MODE, valid values are 'setup' and 'upgrade'" INFLUXDB_INIT_MODE "${INFLUXDB_INIT_MODE}"
235236
exit 1
236237
fi
237238
ensure_init_vars_set
238239
trap "cleanup_influxd" EXIT
239240

240241
# The upgrade process needs to run before we boot the server, otherwise the
241242
# boltdb file will be generated and cause conflicts.
242-
if [ ${INFLUXDB_INIT_MODE} = upgrade ]; then
243+
if [ "${INFLUXDB_INIT_MODE}" = upgrade ]; then
243244
upgrade_influxd
244245
fi
245246

246247
# Generate a config file with a known HTTP port
247248
local init_config=/tmp/config.yml
248-
local final_bind_addr=$(influxd print-config --key-name http-bind-address ${@})
249+
local final_bind_addr="$(influxd print-config --key-name http-bind-address "${@}")"
249250
local init_bind_addr=":${INFLUXD_INIT_PORT}"
250-
if [ ${init_bind_addr} = ${final_bind_addr} ]; then
251-
log warn "influxd setup binding to same addr as final config, server will be exposed before ready" addr ${init_bind_addr}
251+
if [ "${init_bind_addr}" = "${final_bind_addr}" ]; then
252+
log warn "influxd setup binding to same addr as final config, server will be exposed before ready" addr "${init_bind_addr}"
252253
fi
253-
influxd print-config ${@} | sed "s#${final_bind_addr}#${init_bind_addr}#" > ${init_config}
254+
influxd print-config "${@}" | sed "s#${final_bind_addr}#${init_bind_addr}#" > ${init_config}
254255

255256
# Start influxd in the background.
256257
log info "booting influxd server in the background"
257-
INFLUXD_CONFIG_PATH=${init_config} influxd ${@} &
258+
INFLUXD_CONFIG_PATH=${init_config} influxd "${@}" &
258259
local influxd_init_pid="$!"
259260
trap "handle_signal TERM ${influxd_init_pid}" TERM
260261
trap "handle_signal INT ${influxd_init_pid}" INT
@@ -263,34 +264,34 @@ function init_influxd () {
263264
wait_for_influxd
264265

265266
# Use the influx CLI to create an initial user/org/bucket.
266-
if [ ${INFLUXDB_INIT_MODE} = setup ]; then
267+
if [ "${INFLUXDB_INIT_MODE}" = setup ]; then
267268
setup_influxd
268269
fi
269270

270271
set_init_resource_ids
271272
run_user_scripts
272273

273274
log info "initialization complete, shutting down background influxd"
274-
kill -TERM ${influxd_init_pid}
275-
wait ${influxd_init_pid} || true
276-
trap - EXIT
275+
kill -TERM "${influxd_init_pid}"
276+
wait "${influxd_init_pid}" || true
277+
trap - EXIT INT TERM
277278

278279
# Rewrite the ClI configs to point at the server's final HTTP address.
279-
local final_port=$(echo ${final_bind_addr} | sed -E 's#[^:]*:(.*)#\1#')
280-
sed -i "s#http://localhost:${INFLUXD_INIT_PORT}#http://localhost:${final_port}#g" ${INFLUX_CONFIGS_PATH}
280+
local final_port="$(echo ${final_bind_addr} | sed -E 's#[^:]*:(.*)#\1#')"
281+
sed -i "s#http://localhost:${INFLUXD_INIT_PORT}#http://localhost:${final_port}#g" "${INFLUX_CONFIGS_PATH}"
281282
}
282283

283284
# Run influxd, with optional setup logic.
284285
function influxd_main () {
285-
if test -f ${BOLT_PATH}; then
286-
log info "found existing boltdb file, skipping setup wrapper" bolt_path ${BOLT_PATH}
287-
elif [ -z "$INFLUXDB_INIT_MODE" ]; then
288-
log warn "boltdb not found at configured path, but INFLUXDB_INIT_MODE not specified, skipping setup wrapper" bolt_path ${bolt_path}
286+
if test -f "${BOLT_PATH}"; then
287+
log info "found existing boltdb file, skipping setup wrapper" bolt_path "${BOLT_PATH}"
288+
elif [ -z "${INFLUXDB_INIT_MODE}" ]; then
289+
log warn "boltdb not found at configured path, but INFLUXDB_INIT_MODE not specified, skipping setup wrapper" bolt_path "${bolt_path}"
289290
else
290-
init_influxd ${@}
291+
init_influxd "${@}"
291292
fi
292293

293-
exec influxd ${@}
294+
exec influxd "${@}"
294295
}
295296

296297
function main () {
@@ -313,23 +314,23 @@ function main () {
313314
fi
314315

315316
# Configure logging for our wrapper.
316-
set_global_log_level ${@}
317+
set_global_log_level "${@}"
317318
# Configure data paths used across functions.
318-
set_data_paths ${@}
319+
set_data_paths "${@}"
319320
# Ensure volume directories exist w/ correct permissions.
320321
create_directories
321322
fi
322323

323-
if [ $(id -u) = 0 ]; then
324-
exec gosu influxdb "$0" ${@}
324+
if [ "$(id -u)" = 0 ]; then
325+
exec gosu influxdb "$0" "${@}"
325326
return
326327
fi
327328

328329
if ${run_influxd}; then
329-
influxd_main ${@}
330+
influxd_main "${@}"
330331
else
331-
exec ${@}
332+
exec "${@}"
332333
fi
333334
}
334335

335-
main ${@}
336+
main "${@}"

0 commit comments

Comments
 (0)