Skip to content

Commit c3c7328

Browse files
authored
Address DockerHub feedback on 2.0 (#457)
* Use same entrypoint in "normal" and alpine 2.0 images. * Use DOCKER_INFLUXDB_ as prefix for new env vars.
1 parent 8f48c09 commit c3c7328

12 files changed

+170
-179
lines changed

influxdb/2.0/alpine/entrypoint.sh

Lines changed: 79 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,17 @@
1-
#!/bin/sh
1+
#!/bin/bash
22
set -eo pipefail
33

4-
function log_level () {
5-
if [ "$1" = debug ]; then
6-
echo 3
7-
elif [ "$1" = info ]; then
8-
echo 2
9-
elif [ "$1" = warn ]; then
10-
echo 1
11-
elif [ "$1" = error ]; then
12-
echo 0
13-
fi
14-
}
15-
LOG_LEVEL=error
4+
# Do our best to match the logging requested by the user running the container.
5+
declare -rA LOG_LEVELS=([error]=0 [warn]=1 [info]=2 [debug]=3)
6+
declare LOG_LEVEL=error
167

178
# Mimic the structured logging used by InfluxDB.
189
# Usage: log <level> <msg> [<key> <val>]...
1910
function log () {
20-
local level=$1 msg=$2
11+
local -r level=$1 msg=$2
2112
shift 2
2213

23-
if [ "$(log_level ${level})" -gt "$(log_level ${LOG_LEVEL})" ]; then
14+
if [ "${LOG_LEVELS[${level}]}" -gt "${LOG_LEVELS[${LOG_LEVEL}]}" ]; then
2415
return
2516
fi
2617

@@ -30,31 +21,31 @@ function log () {
3021
shift 2
3122
done
3223

33-
local logtime="$(date --utc +'%FT%T.%NZ')"
24+
local -r logtime="$(date --utc +'%FT%T.%NZ')"
3425
1>&2 echo -e "${logtime}\t${level}\t${msg}\t{${attrs}}"
3526
}
3627

3728
# Set the global log-level for the entry-point to match the config passed to influxd.
3829
function set_global_log_level () {
3930
local level="$(influxd print-config --key-name log-level "${@}")"
40-
if [ -z "${level}" ] || [ -z "$(log_level ${level})" ]; then
31+
if [ -z "${level}" ] || [ -z "${LOG_LEVELS[${level}]}" ]; then
4132
return 1
4233
fi
43-
LOG_LEVEL="${level}"
34+
LOG_LEVEL=${level}
4435
}
4536

4637
# Look for standard config names in the volume configured in our Dockerfile.
47-
readonly CONFIG_VOLUME=/etc/influxdb2
48-
readonly CONFIG_NAMES="config.json config.toml config.yaml config.yml"
38+
declare -r CONFIG_VOLUME=/etc/influxdb2
39+
declare -ra CONFIG_NAMES=(config.json config.toml config.yaml config.yml)
4940

5041
# Search for a V2 config file, and export its path into the env for influxd to use.
5142
function set_config_path () {
5243
local config_path=/etc/defaults/influxdb2/config.yml
5344

54-
if [ -n "${INFLUXD_CONFIG_PATH}" ]; then
45+
if [ -n "$INFLUXD_CONFIG_PATH" ]; then
5546
config_path="${INFLUXD_CONFIG_PATH}"
5647
else
57-
for name in ${CONFIG_NAMES}; do
48+
for name in "${CONFIG_NAMES[@]}"; do
5849
if [ -f "${CONFIG_VOLUME}/${name}" ]; then
5950
config_path="${CONFIG_VOLUME}/${name}"
6051
break
@@ -72,31 +63,31 @@ function set_data_paths () {
7263

7364
# Ensure all the data directories needed by influxd exist with the right permissions.
7465
function create_directories () {
75-
local bolt_dir="$(dirname "${BOLT_PATH}")"
76-
local user="$(id -u)"
66+
local -r bolt_dir="$(dirname "${BOLT_PATH}")"
67+
local user=$(id -u)
7768

7869
mkdir -p "${bolt_dir}" "${ENGINE_PATH}"
7970
chmod 700 "${bolt_dir}" "${ENGINE_PATH}" || :
8071

8172
mkdir -p "${CONFIG_VOLUME}" || :
8273
chmod 775 "${CONFIG_VOLUME}" || :
8374

84-
if [ "${user}" = 0 ]; then
75+
if [ ${user} = 0 ]; then
8576
find "${bolt_dir}" \! -user influxdb -exec chown influxdb '{}' +
8677
find "${ENGINE_PATH}" \! -user influxdb -exec chown influxdb '{}' +
8778
find "${CONFIG_VOLUME}" \! -user influxdb -exec chown influxdb '{}' +
8879
fi
8980
}
9081

9182
# List of env vars required to auto-run setup or upgrade processes.
92-
readonly REQUIRED_INIT_VARS="INFLUXDB_INIT_USERNAME INFLUXDB_INIT_PASSWORD INFLUXDB_INIT_ORG INFLUXDB_INIT_BUCKET"
83+
declare -ra REQUIRED_INIT_VARS=(DOCKER_INFLUXDB_INIT_USERNAME DOCKER_INFLUXDB_INIT_PASSWORD DOCKER_INFLUXDB_INIT_ORG DOCKER_INFLUXDB_INIT_BUCKET)
9384

9485
# Ensure all env vars required to run influx setup or influxd upgrade are set in the env.
9586
function ensure_init_vars_set () {
9687
local missing_some=0
97-
for var in ${REQUIRED_INIT_VARS}; do
98-
if [ -z "$(printenv ${var})" ]; then
99-
log error "missing parameter, cannot init InfluxDB" parameter "${var}"
88+
for var in "${REQUIRED_INIT_VARS[@]}"; do
89+
if [ -z "${!var}" ]; then
90+
log error "missing parameter, cannot init InfluxDB" parameter ${var}
10091
missing_some=1
10192
fi
10293
done
@@ -116,55 +107,55 @@ function cleanup_influxd () {
116107
# Upgrade V1 data into the V2 format using influxd upgrade.
117108
# The process will use either a V1 config file or a V1 data dir to drive
118109
# the upgrade, with precedence order:
119-
# 1. Config file pointed to by INFLUXDB_INIT_UPGRADE_V1_CONFIG env var
120-
# 2. Data dir pointed to by INFLUXDB_INIT_UPGRADE_V1_DIR env var
110+
# 1. Config file pointed to by DOCKER_INFLUXDB_INIT_UPGRADE_V1_CONFIG env var
111+
# 2. Data dir pointed to by DOCKER_INFLUXDB_INIT_UPGRADE_V1_DIR env var
121112
# 3. Config file at /etc/influxdb/influxdb.conf
122113
# 4. Data dir at /var/lib/influxdb
123114
function upgrade_influxd () {
124-
set -- \
125-
--force \
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}" \
115+
local -a upgrade_args=(
116+
--force
117+
--username "${DOCKER_INFLUXDB_INIT_USERNAME}"
118+
--password "${DOCKER_INFLUXDB_INIT_PASSWORD}"
119+
--org "${DOCKER_INFLUXDB_INIT_ORG}"
120+
--bucket "${DOCKER_INFLUXDB_INIT_BUCKET}"
121+
--v2-config-path "${CONFIG_VOLUME}/config.toml"
122+
--influx-configs-path "${INFLUX_CONFIGS_PATH}"
123+
--continuous-query-export-path "${CONFIG_VOLUME}/v1-cq-export.txt"
124+
--log-path "${CONFIG_VOLUME}/upgrade.log"
125+
--log-level "${LOG_LEVEL}"
126+
--bolt-path "${BOLT_PATH}"
127+
--engine-path "${ENGINE_PATH}"
137128
--overwrite-existing-v2
138-
139-
if [ -n "${INFLUXDB_INIT_RETENTION}" ]; then
140-
set -- "${@}" --retention "${INFLUXDB_INIT_RETENTION}"
129+
)
130+
if [ -n "${DOCKER_INFLUXDB_INIT_RETENTION}" ]; then
131+
upgrade_args=("${upgrade_args[@]}" --retention "${DOCKER_INFLUXDB_INIT_RETENTION}")
141132
fi
142-
if [ -n "${INFLUXDB_INIT_ADMIN_TOKEN}" ]; then
143-
set -- "${@}" --token "${INFLUXDB_INIT_ADMIN_TOKEN}"
133+
if [ -n "${DOCKER_INFLUXDB_INIT_ADMIN_TOKEN}" ]; then
134+
upgrade_args=("${upgrade_args[@]}" --token "${DOCKER_INFLUXDB_INIT_ADMIN_TOKEN}")
144135
fi
145136

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}"
137+
if [[ -n "${DOCKER_INFLUXDB_INIT_UPGRADE_V1_CONFIG}" && -f "${DOCKER_INFLUXDB_INIT_UPGRADE_V1_CONFIG}" ]]; then
138+
upgrade_args=("${upgrade_args[@]}" --config-file "${DOCKER_INFLUXDB_INIT_UPGRADE_V1_CONFIG}")
139+
elif [[ -n "${DOCKER_INFLUXDB_INIT_UPGRADE_V1_DIR}" && -d "${DOCKER_INFLUXDB_INIT_UPGRADE_V1_DIR}" ]]; then
140+
upgrade_args=("${upgrade_args[@]}" --v1-dir "${DOCKER_INFLUXDB_INIT_UPGRADE_V1_DIR}")
150141
elif [ -f /etc/influxdb/influxdb.conf ]; then
151-
set -- "${@}" --config-file /etc/influxdb/influxdb.conf
142+
upgrade_args=("${upgrade_args[@]}" --config-file /etc/influxdb/influxdb.conf)
152143
elif [ -d /var/lib/influxdb ]; then
153-
set -- "${@}" --v1-dir /var/lib/influxdb
144+
upgrade_args=("${upgrade_args[@]}" --v1-dir /var/lib/influxdb)
154145
else
155146
log error "failed to autodetect usable V1 config or data dir, aborting upgrade"
156147
exit 1
157148
fi
158149

159-
influxd upgrade "${@}"
150+
influxd upgrade "${upgrade_args[@]}"
160151

161152
# Reset global influxd config to pick up new file written by the upgrade process.
162153
set_config_path
163154
}
164155

165156
# Wait up to a minute for the DB to boot
166-
readonly STARTUP_PING_WAIT_SECONDS=2
167-
readonly STARTUP_PING_ATTEMPTS=30
157+
declare -r STARTUP_PING_WAIT_SECONDS=2
158+
declare -r STARTUP_PING_ATTEMPTS=30
168159

169160
# Ping influxd until it responds.
170161
# Used to block execution until the server is ready to process setup requests.
@@ -184,34 +175,34 @@ function wait_for_influxd () {
184175

185176
# Create an initial user/org/bucket in the DB using the influx CLI.
186177
function setup_influxd () {
187-
set -- \
188-
--force \
189-
--username "${INFLUXDB_INIT_USERNAME}" \
190-
--password "${INFLUXDB_INIT_PASSWORD}" \
191-
--org "${INFLUXDB_INIT_ORG}" \
192-
--bucket "${INFLUXDB_INIT_BUCKET}"
193-
194-
if [ -n "${INFLUXDB_INIT_RETENTION}" ]; then
195-
set -- "${@}" --retention "${INFLUXDB_INIT_RETENTION}"
178+
local -a setup_args=(
179+
--force
180+
--username "${DOCKER_INFLUXDB_INIT_USERNAME}"
181+
--password "${DOCKER_INFLUXDB_INIT_PASSWORD}"
182+
--org "${DOCKER_INFLUXDB_INIT_ORG}"
183+
--bucket "${DOCKER_INFLUXDB_INIT_BUCKET}"
184+
)
185+
if [ -n "${DOCKER_INFLUXDB_INIT_RETENTION}" ]; then
186+
setup_args=("${setup_args[@]}" --retention "${DOCKER_INFLUXDB_INIT_RETENTION}")
196187
fi
197-
if [ -n "${INFLUXDB_INIT_ADMIN_TOKEN}" ]; then
198-
set -- "${@}" --token "${INFLUXDB_INIT_ADMIN_TOKEN}"
188+
if [ -n "${DOCKER_INFLUXDB_INIT_ADMIN_TOKEN}" ]; then
189+
setup_args=("${setup_args[@]}" --token "${DOCKER_INFLUXDB_INIT_ADMIN_TOKEN}")
199190
fi
200191

201-
influx setup "${@}"
192+
influx setup "${setup_args[@]}"
202193
}
203194

204195
# Get the IDs of the initial user/org/bucket created during setup, and export them into the env.
205196
# We do this to help with arbitrary user scripts, since many influx CLI commands only take IDs.
206197
function set_init_resource_ids () {
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)"
198+
export DOCKER_INFLUXDB_INIT_USER_ID="$(influx user list -n "${DOCKER_INFLUXDB_INIT_USER}" --hide-headers | cut -f 1)"
199+
export DOCKER_INFLUXDB_INIT_ORG_ID="$(influx org list -n "${DOCKER_INFLUXDB_INIT_ORG}" --hide-headers | cut -f 1)"
200+
export DOCKER_INFLUXDB_INIT_BUCKET_ID="$(influx bucket list -n "${DOCKER_INFLUXDB_INIT_BUCKET}" --hide-headers | cut -f 1)"
210201
}
211202

212203
# Allow users to mount arbitrary startup scripts into the container,
213204
# for execution after initial setup/upgrade.
214-
readonly USER_SCRIPT_DIR=/docker-entrypoint-initdb.d
205+
declare -r USER_SCRIPT_DIR=/docker-entrypoint-initdb.d
215206

216207
# Execute all shell files mounted into the expected path for user-defined startup scripts.
217208
function run_user_scripts () {
@@ -231,23 +222,23 @@ function handle_signal () {
231222
# Perform initial setup on the InfluxDB instance, either by setting up fresh metadata
232223
# or by upgrading existing V1 data.
233224
function init_influxd () {
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}"
225+
if [[ "${DOCKER_INFLUXDB_INIT_MODE}" != setup && "${DOCKER_INFLUXDB_INIT_MODE}" != upgrade ]]; then
226+
log error "found invalid DOCKER_INFLUXDB_INIT_MODE, valid values are 'setup' and 'upgrade'" DOCKER_INFLUXDB_INIT_MODE "${DOCKER_INFLUXDB_INIT_MODE}"
236227
exit 1
237228
fi
238229
ensure_init_vars_set
239230
trap "cleanup_influxd" EXIT
240231

241232
# The upgrade process needs to run before we boot the server, otherwise the
242233
# boltdb file will be generated and cause conflicts.
243-
if [ "${INFLUXDB_INIT_MODE}" = upgrade ]; then
234+
if [ "${DOCKER_INFLUXDB_INIT_MODE}" = upgrade ]; then
244235
upgrade_influxd
245236
fi
246237

247238
# Generate a config file with a known HTTP port
248-
local init_config=/tmp/config.yml
249-
local final_bind_addr="$(influxd print-config --key-name http-bind-address "${@}")"
250-
local init_bind_addr=":${INFLUXD_INIT_PORT}"
239+
local -r init_config=/tmp/config.yml
240+
local -r final_bind_addr="$(influxd print-config --key-name http-bind-address "${@}")"
241+
local -r init_bind_addr=":${INFLUXD_INIT_PORT}"
251242
if [ "${init_bind_addr}" = "${final_bind_addr}" ]; then
252243
log warn "influxd setup binding to same addr as final config, server will be exposed before ready" addr "${init_bind_addr}"
253244
fi
@@ -256,15 +247,15 @@ function init_influxd () {
256247
# Start influxd in the background.
257248
log info "booting influxd server in the background"
258249
INFLUXD_CONFIG_PATH=${init_config} influxd "${@}" &
259-
local influxd_init_pid="$!"
250+
local -r influxd_init_pid="$!"
260251
trap "handle_signal TERM ${influxd_init_pid}" TERM
261252
trap "handle_signal INT ${influxd_init_pid}" INT
262253

263254
export INFLUX_HOST="http://localhost:${INFLUXD_INIT_PORT}"
264255
wait_for_influxd
265256

266257
# Use the influx CLI to create an initial user/org/bucket.
267-
if [ "${INFLUXDB_INIT_MODE}" = setup ]; then
258+
if [ "${DOCKER_INFLUXDB_INIT_MODE}" = setup ]; then
268259
setup_influxd
269260
fi
270261

@@ -277,16 +268,16 @@ function init_influxd () {
277268
trap - EXIT INT TERM
278269

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

284275
# Run influxd, with optional setup logic.
285276
function influxd_main () {
286277
if [ -f "${BOLT_PATH}" ]; then
287278
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}"
279+
elif [ -z "${DOCKER_INFLUXDB_INIT_MODE}" ]; then
280+
log warn "boltdb not found at configured path, but DOCKER_INFLUXDB_INIT_MODE not specified, skipping setup wrapper" bolt_path "${bolt_path}"
290281
else
291282
init_influxd "${@}"
292283
fi
@@ -301,9 +292,9 @@ function main () {
301292
set_config_path
302293

303294
local run_influxd=false
304-
if [ $# -eq 0 -o "$1" = run -o "$(echo "$1" | cut -c1-1)" = '-' ]; then
295+
if [[ $# = 0 || "$1" = run || "${1:0:1}" = '-' ]]; then
305296
run_influxd=true
306-
elif [ "$1" = influxd ] && [ $# -eq 1 -o "$2" = run -o "$(echo "$2" | cut -c1-1)" = '-' ]; then
297+
elif [[ "$1" = influxd && ($# = 1 || "$2" = run || "${2:0:1}" = '-') ]]; then
307298
run_influxd=true
308299
shift 1
309300
fi
@@ -324,7 +315,7 @@ function main () {
324315
create_directories
325316

326317
if [ "$(id -u)" = 0 ]; then
327-
exec gosu influxdb "$0" "${@}"
318+
exec gosu influxdb "$BASH_SOURCE" "${@}"
328319
return
329320
fi
330321

0 commit comments

Comments
 (0)